Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Operators.Views.GraphVisualization.Views/3.3/OperatorShape.cs @ 9793

Last change on this file since 9793 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 11.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
25using System.Drawing.Drawing2D;
26using System.Linq;
27using HeuristicLab.Netron;
28using Netron.Diagramming.Core;
29
30namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
31  public class OperatorShape : ComplexShapeBase {
32    private static int LABEL_HEIGHT = 16;
33    private static int LABEL_WIDTH = 180;
34    private static int LABEL_SPACING = 3;
35    private int headerHeight = 30;
36
37    private ExpandableIconMaterial expandIconMaterial;
38    public OperatorShape()
39      : base() {
40      this.Resizable = false;
41      this.additionalConnectors = new List<IConnector>();
42      this.labels = new List<string>();
43    }
44
45    public override string EntityName {
46      get { return "Operator Shape"; }
47    }
48
49    public bool Collapsed {
50      get { return this.expandIconMaterial.Collapsed; }
51      set {
52        if (this.expandIconMaterial.Collapsed != value)
53          this.expandIconMaterial.Collapsed = value;
54      }
55    }
56
57    private Color lineColor;
58    public Color LineColor {
59      get { return this.lineColor; }
60      set { this.lineColor = value; }
61    }
62
63    private float lineWidth;
64    public float LineWidth {
65      get { return this.lineWidth; }
66      set { this.lineWidth = value; }
67    }
68
69    private Color color;
70    public Color Color {
71      get { return this.color; }
72      set { this.color = value; }
73    }
74
75    private string title;
76    public string Title {
77      get { return title; }
78      set { title = value; }
79    }
80
81    public string Subtitle { get; set; }
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.VSImageLibrary.Expand);
199      Bitmap collapseBitmap = new Bitmap(HeuristicLab.Common.Resources.VSImageLibrary.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      using (Pen pen = new Pen(lineColor, lineWidth)) {
221
222        SizeF titleSize = g.MeasureString(this.Title, ArtPalette.DefaultBoldFont, Rectangle.Width - 45);
223        titleSize.Height += 10; //add spacing
224        SizeF subtitleSize = g.MeasureString(this.Subtitle, ArtPalette.DefaultFont, Rectangle.Width - 45);
225        subtitleSize.Height += 5; //add spacing
226        if (this.Title == this.Subtitle || string.IsNullOrEmpty(this.Subtitle)) subtitleSize = new SizeF(0, 0);
227
228        if ((int)titleSize.Height + (int)subtitleSize.Height != Rectangle.Height) {
229          headerHeight = (int)titleSize.Height + (int)subtitleSize.Height;
230          this.UpdateLabels();
231        }
232
233        GraphicsPath path = new GraphicsPath();
234        path.AddArc(Rectangle.X, Rectangle.Y, 20, 20, -180, 90);
235        path.AddLine(Rectangle.X + 10, Rectangle.Y, Rectangle.X + Rectangle.Width - 10, Rectangle.Y);
236        path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y, 20, 20, -90, 90);
237        path.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + 10, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height - 10);
238        path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y + Rectangle.Height - 20, 20, 20, 0, 90);
239        path.AddLine(Rectangle.X + Rectangle.Width - 10, Rectangle.Y + Rectangle.Height, Rectangle.X + 10, Rectangle.Y + Rectangle.Height);
240        path.AddArc(Rectangle.X, Rectangle.Y + Rectangle.Height - 20, 20, 20, 90, 90);
241        path.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height - 10, Rectangle.X, Rectangle.Y + 10);
242        //shadow
243        if (ArtPalette.EnableShadows) {
244          Region darkRegion = new Region(path);
245          darkRegion.Translate(5, 5);
246          g.FillRegion(ArtPalette.ShadowBrush, darkRegion);
247        }
248        //background
249        g.FillPath(Brush, path);
250
251        using (LinearGradientBrush gradientBrush = new LinearGradientBrush(Rectangle.Location, new Point(Rectangle.X + Rectangle.Width, Rectangle.Y), this.Color, Color.White)) {
252          Region gradientRegion = new Region(path);
253          g.FillRegion(gradientBrush, gradientRegion);
254        }
255
256        if (!this.Collapsed) {
257          TextStyle textStyle = new TextStyle(Color.Black, new Font("Arial", 7), StringAlignment.Near, StringAlignment.Near);
258          StringFormat stringFormat = textStyle.StringFormat;
259          stringFormat.Trimming = StringTrimming.EllipsisWord;
260          stringFormat.FormatFlags = StringFormatFlags.LineLimit;
261          Rectangle rect;
262
263          const int verticalHeaderSpacing = 5;
264          Point separationLineStart = new Point(Rectangle.X + 25, Rectangle.Y + headerHeight - verticalHeaderSpacing);
265          Point separationLineEnd = new Point(Rectangle.X + Rectangle.Width - 25, Rectangle.Y + headerHeight - verticalHeaderSpacing);
266          using (LinearGradientBrush brush = new LinearGradientBrush(separationLineStart, separationLineEnd, Color.Black, Color.White)) {
267            using (Pen separationLinePen = new Pen(brush)) {
268              g.DrawLine(separationLinePen, separationLineStart, separationLineEnd);
269            }
270          }
271
272
273          for (int i = 0; i < this.labels.Count; i++) {
274            rect = new Rectangle(Rectangle.X + 25, Rectangle.Y + headerHeight + i * (LABEL_HEIGHT + LABEL_SPACING), LABEL_WIDTH, LABEL_HEIGHT);
275            g.DrawString(textStyle.GetFormattedText(this.labels[i]), textStyle.Font, textStyle.GetBrush(), rect, stringFormat);
276          }
277        }
278
279        //the border
280        g.DrawPath(pen, path);
281
282        //the title
283        g.DrawString(this.Title, ArtPalette.DefaultBoldFont, Brushes.Black,
284                     new Rectangle(Rectangle.X + 25, Rectangle.Y + 5,
285                                   Rectangle.Width - 45, Rectangle.Height - 5 - (int)subtitleSize.Height));
286
287        //the subtitle
288        if (this.Title != this.Subtitle || string.IsNullOrEmpty(this.Subtitle)) {
289          g.DrawString(this.Subtitle, ArtPalette.DefaultFont, Brushes.Black,
290                       new Rectangle(Rectangle.X + 25, Rectangle.Y + (int)titleSize.Height -5 ,
291                                     Rectangle.Width - 45, Rectangle.Height - 5));
292        }
293
294
295        //the material
296        foreach (IShapeMaterial material in Children)
297          material.Paint(g);
298
299        //the connectors
300        if (this.ShowConnectors) {
301          foreach (IConnector t in Connectors)
302            t.Paint(g);
303        }
304      }
305    }
306  }
307}
Note: See TracBrowser for help on using the repository browser.