Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/OperatorShape.cs @ 3001

Last change on this file since 3001 was 2935, checked in by mkommend, 15 years ago

added visual indication for operator breakpoints (ticket #867)

File size: 9.9 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 static int HEADER_HEIGHT = 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 = HEADER_HEIGHT;
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, HEADER_HEIGHT);
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.Redo);
199      Bitmap collapseBitmap = new Bitmap(HeuristicLab.Common.Resources.VS2008ImageLibrary.Undo);
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("Predecessor", new Point(Rectangle.Left, Center.Y));
209      this.Connectors.Add(predecessor);
210
211      this.successor = this.CreateConnector("Successor", (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      GraphicsPath path = new GraphicsPath();
223      path.AddArc(Rectangle.X, Rectangle.Y, 20, 20, -180, 90);
224      path.AddLine(Rectangle.X + 10, Rectangle.Y, Rectangle.X + Rectangle.Width - 10, Rectangle.Y);
225      path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y, 20, 20, -90, 90);
226      path.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + 10, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height - 10);
227      path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y + Rectangle.Height - 20, 20, 20, 0, 90);
228      path.AddLine(Rectangle.X + Rectangle.Width - 10, Rectangle.Y + Rectangle.Height, Rectangle.X + 10, Rectangle.Y + Rectangle.Height);
229      path.AddArc(Rectangle.X, Rectangle.Y + Rectangle.Height - 20, 20, 20, 90, 90);
230      path.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height - 10, Rectangle.X, Rectangle.Y + 10);
231      //shadow
232      if (ArtPalette.EnableShadows) {
233        Region darkRegion = new Region(path);
234        darkRegion.Translate(5, 5);
235        g.FillRegion(ArtPalette.ShadowBrush, darkRegion);
236      }
237      //background
238      g.FillPath(Brush, path);
239
240      using (LinearGradientBrush gradientBrush = new LinearGradientBrush(Rectangle.Location, new Point(Rectangle.X + Rectangle.Width, Rectangle.Y), this.Color, Color.White)) {
241        Region gradientRegion = new Region(path);
242        g.FillRegion(gradientBrush, gradientRegion);
243      }
244
245      if (!this.Collapsed) {
246        TextStyle textStyle = new TextStyle(Color.Black, new Font("Arial", 7), StringAlignment.Near, StringAlignment.Near);
247        StringFormat stringFormat = textStyle.StringFormat;
248        stringFormat.Trimming = StringTrimming.EllipsisWord;
249        stringFormat.FormatFlags = StringFormatFlags.LineLimit;
250        Rectangle rect;
251
252        for (int i = 0; i < this.labels.Count; i++) {
253          rect = new Rectangle(Rectangle.X + 25, Rectangle.Y + HEADER_HEIGHT + i * (LABEL_HEIGHT + LABEL_SPACING), LABEL_WIDTH, LABEL_HEIGHT);
254          g.DrawString(textStyle.GetFormattedText(this.labels[i]), textStyle.Font, textStyle.GetBrush(), rect, stringFormat);
255        }
256      }
257
258      //the border
259      g.DrawPath(pen, path);
260
261      //the title
262      g.DrawString(this.Title, ArtPalette.DefaultBoldFont, Brushes.Black, new Rectangle(Rectangle.X + 25, Rectangle.Y + 5, Rectangle.Width - 45, 27));
263
264      //the material
265      foreach (IPaintable material in Children)
266        material.Paint(g);
267
268      //the connectors
269      if (this.ShowConnectors) {
270        for (int k = 0; k < Connectors.Count; k++)
271          Connectors[k].Paint(g);
272      }
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.