Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorShape.cs @ 3951

Last change on this file since 3951 was 3422, checked in by mkommend, 15 years ago
  • corrected bugs in operator graph visualization (ticket #867)
  • changed multi call operators in graph visualization (ticket #979)
File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using Netron.Diagramming.Core;
27using System.Drawing;
28using System.Drawing.Drawing2D;
29using HeuristicLab.Netron;
30
31
32namespace HeuristicLab.Operators.Views.GraphVisualization {
33  public class OperatorShape : ComplexShapeBase {
34    private static int LABEL_HEIGHT = 16;
35    private static int LABEL_WIDTH = 180;
36    private static int LABEL_SPACING = 3;
37    private int headerHeight = 30;
38
39    private ExpandableIconMaterial expandIconMaterial;
40    public OperatorShape()
41      : base() {
42      this.Resizable = false;
43      this.additionalConnectors = new List<IConnector>();
44      this.labels = new List<string>();
45    }
46
47    public override string EntityName {
48      get { return "Operator Shape"; }
49    }
50
51    public bool Collapsed {
52      get { return this.expandIconMaterial.Collapsed; }
53      set {
54        if (this.expandIconMaterial.Collapsed != value)
55          this.expandIconMaterial.Collapsed = value;
56      }
57    }
58
59    private Color lineColor;
60    public Color LineColor {
61      get { return this.lineColor; }
62      set { this.lineColor = value; }
63    }
64
65    private float lineWidth;
66    public float LineWidth {
67      get { return this.lineWidth; }
68      set { this.lineWidth = value; }
69    }
70
71    private Color color;
72    public Color Color {
73      get { return this.color; }
74      set { this.color = value; }
75    }
76
77    private string title;
78    public string Title {
79      get { return title; }
80      set { title = value; }
81    }
82
83    private IconMaterial iconMaterial;
84    public Bitmap Icon {
85      get { return this.iconMaterial.Icon; }
86      set {
87        this.iconMaterial.Icon = value;
88        this.iconMaterial.Transform(new Rectangle(new Point(Rectangle.X + 5, Rectangle.Y + 5), this.iconMaterial.Icon.Size));
89      }
90    }
91
92    #region additional connectors
93    private List<IConnector> additionalConnectors;
94    public IEnumerable<string> AdditionalConnectorNames {
95      get { return this.additionalConnectors.Select(c => c.Name); }
96    }
97
98    private IConnector predecessor;
99    public IConnector Predecessor {
100      get { return this.predecessor; }
101    }
102
103    private IConnector successor;
104    public IConnector Successor {
105      get { return this.successor; }
106    }
107
108    private IConnector CreateConnector(string connectorName, Point location) {
109      Connector connector = new Connector(location, this.Model);
110      connector.ConnectorStyle = ConnectorStyle.Square;
111      connector.Parent = this;
112      connector.Name = connectorName;
113      return connector;
114    }
115
116    public void AddConnector(string connectorName) {
117      IConnector connector = this.CreateConnector(connectorName, this.BottomRightCorner);
118
119      this.additionalConnectors.Add(connector);
120      this.Connectors.Add(connector);
121      this.UpdateConnectorLocation();
122    }
123
124    public void RemoveConnector(string connectorName) {
125      IConnector connector = this.additionalConnectors.Where(c => c.Name == connectorName).FirstOrDefault();
126      if (connector != null) {
127        this.additionalConnectors.Remove(connector);
128        this.Connectors.Remove(connector);
129        this.UpdateConnectorLocation();
130      }
131    }
132
133    private void UpdateConnectorLocation() {
134      if (this.additionalConnectors.Count == 0)
135        return;
136
137      int spacing = this.Rectangle.Width / this.additionalConnectors.Count;
138      int margin = spacing / 2;
139      int posX = margin + this.Rectangle.X;
140      for (int i = 0; i < this.additionalConnectors.Count; i++) {
141        this.additionalConnectors[i].MoveBy(new Point(posX - this.additionalConnectors[i].Point.X, 0));
142        posX += spacing;
143      }
144    }
145    #endregion
146
147    #region label material
148    private List<string> labels;
149    public IEnumerable<string> Labels {
150      get { return this.labels; }
151    }
152
153    public void UpdateLabels(IEnumerable<string> labels) {
154      this.labels = new List<string>(labels);
155      this.expandIconMaterial.Visible = this.labels.Count != 0;
156      this.UpdateLabels();
157    }
158    #endregion
159
160    private void expandIconMaterial_OnExpand(object sender, EventArgs e) {
161      this.UpdateLabels();
162    }
163
164    private void expandIconMaterial_OnCollapse(object sender, EventArgs e) {
165      this.UpdateLabels();
166    }
167
168    private Size CalculateSize() {
169      int width = this.Rectangle.Width;
170      int height = headerHeight;
171      if (!Collapsed)
172        height += this.labels.Count * (LABEL_HEIGHT + LABEL_SPACING);
173      return new Size(width, height);
174    }
175
176    private void UpdateLabels() {
177      Size newSize = CalculateSize();
178      if (this.Rectangle.Size != newSize) {
179        foreach (IConnector connector in this.additionalConnectors)
180          connector.MoveBy(new Point(0, newSize.Height - this.Rectangle.Height));
181        this.mRectangle = new Rectangle(this.Rectangle.Location, newSize);
182        this.Invalidate();
183        this.RaiseOnChange(this, new EntityEventArgs(this));
184      }
185    }
186
187    protected override void Initialize() {
188      base.Initialize();
189
190      //the initial size
191      this.Transform(0, 0, 200, headerHeight);
192      this.color = Color.LightBlue;
193
194      this.iconMaterial = new IconMaterial();
195      this.iconMaterial.Gliding = false;
196      this.Children.Add(iconMaterial);
197
198      Bitmap expandBitmap = new Bitmap(HeuristicLab.Common.Resources.VS2008ImageLibrary.Expand);
199      Bitmap collapseBitmap = new Bitmap(HeuristicLab.Common.Resources.VS2008ImageLibrary.Collapse);
200      this.expandIconMaterial = new ExpandableIconMaterial(expandBitmap, collapseBitmap);
201      this.expandIconMaterial.Gliding = false;
202      this.expandIconMaterial.Transform(new Rectangle(new Point(Rectangle.Right - 20, Rectangle.Y + 7), expandIconMaterial.Icon.Size));
203      this.expandIconMaterial.Visible = false;
204      this.expandIconMaterial.OnExpand += new EventHandler(expandIconMaterial_OnExpand);
205      this.expandIconMaterial.OnCollapse += new EventHandler(expandIconMaterial_OnCollapse);
206      this.Children.Add(expandIconMaterial);
207
208      this.predecessor = this.CreateConnector(OperatorShapeInfoFactory.PredecessorConnector, new Point(Rectangle.Left, Center.Y));
209      this.Connectors.Add(predecessor);
210
211      this.successor = this.CreateConnector(OperatorShapeInfoFactory.SuccessorConnector, (new Point(Rectangle.Right, Center.Y)));
212      this.Connectors.Add(successor);
213    }
214
215    public override void Paint(Graphics g) {
216      base.Paint(g);
217
218      g.SmoothingMode = SmoothingMode.HighQuality;
219
220      Pen pen = new Pen(lineColor, lineWidth);
221
222      SizeF titleSize = g.MeasureString(this.Title, ArtPalette.DefaultBoldFont, Rectangle.Width - 45);
223      if (titleSize.Height + 10 > Rectangle.Height) {
224        headerHeight =  (int)titleSize.Height + 10;
225        this.UpdateLabels();
226      }
227
228      GraphicsPath path = new GraphicsPath();
229      path.AddArc(Rectangle.X, Rectangle.Y, 20, 20, -180, 90);
230      path.AddLine(Rectangle.X + 10, Rectangle.Y, Rectangle.X + Rectangle.Width - 10, Rectangle.Y);
231      path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y, 20, 20, -90, 90);
232      path.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + 10, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height - 10);
233      path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y + Rectangle.Height - 20, 20, 20, 0, 90);
234      path.AddLine(Rectangle.X + Rectangle.Width - 10, Rectangle.Y + Rectangle.Height, Rectangle.X + 10, Rectangle.Y + Rectangle.Height);
235      path.AddArc(Rectangle.X, Rectangle.Y + Rectangle.Height - 20, 20, 20, 90, 90);
236      path.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height - 10, Rectangle.X, Rectangle.Y + 10);
237      //shadow
238      if (ArtPalette.EnableShadows) {
239        Region darkRegion = new Region(path);
240        darkRegion.Translate(5, 5);
241        g.FillRegion(ArtPalette.ShadowBrush, darkRegion);
242      }
243      //background
244      g.FillPath(Brush, path);
245
246      using (LinearGradientBrush gradientBrush = new LinearGradientBrush(Rectangle.Location, new Point(Rectangle.X + Rectangle.Width, Rectangle.Y), this.Color, Color.White)) {
247        Region gradientRegion = new Region(path);
248        g.FillRegion(gradientBrush, gradientRegion);
249      }
250
251      if (!this.Collapsed) {
252        TextStyle textStyle = new TextStyle(Color.Black, new Font("Arial", 7), StringAlignment.Near, StringAlignment.Near);
253        StringFormat stringFormat = textStyle.StringFormat;
254        stringFormat.Trimming = StringTrimming.EllipsisWord;
255        stringFormat.FormatFlags = StringFormatFlags.LineLimit;
256        Rectangle rect;
257
258        for (int i = 0; i < this.labels.Count; i++) {
259          rect = new Rectangle(Rectangle.X + 25, Rectangle.Y + headerHeight + i * (LABEL_HEIGHT + LABEL_SPACING), LABEL_WIDTH, LABEL_HEIGHT);
260          g.DrawString(textStyle.GetFormattedText(this.labels[i]), textStyle.Font, textStyle.GetBrush(), rect, stringFormat);
261        }
262      }
263
264      //the border
265      g.DrawPath(pen, path);
266
267      //the title
268      g.DrawString(this.Title, ArtPalette.DefaultBoldFont, Brushes.Black, new Rectangle(Rectangle.X + 25, Rectangle.Y + 5, Rectangle.Width - 45, Rectangle.Height - 5));
269
270
271      //the material
272      foreach (IPaintable material in Children)
273        material.Paint(g);
274
275      //the connectors
276      if (this.ShowConnectors) {
277        for (int k = 0; k < Connectors.Count; k++)
278          Connectors[k].Paint(g);
279      }
280    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.