Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5304 was 5287, checked in by abeham, 14 years ago

#1337

  • Renamed VS2008ImageLibrary resource to VSImageLibrary
  • Added Filter icon to the VS2010ImageLibrary folder and the resource manager
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.Drawing;
25using System.Drawing.Drawing2D;
26using System.Linq;
27using HeuristicLab.Netron;
28using Netron.Diagramming.Core;
29
30
31namespace HeuristicLab.Operators.Views.GraphVisualization {
32  public class OperatorShape : ComplexShapeBase {
33    private static int LABEL_HEIGHT = 16;
34    private static int LABEL_WIDTH = 180;
35    private static int LABEL_SPACING = 3;
36    private int headerHeight = 30;
37
38    private ExpandableIconMaterial expandIconMaterial;
39    public OperatorShape()
40      : base() {
41      this.Resizable = false;
42      this.additionalConnectors = new List<IConnector>();
43      this.labels = new List<string>();
44    }
45
46    public override string EntityName {
47      get { return "Operator Shape"; }
48    }
49
50    public bool Collapsed {
51      get { return this.expandIconMaterial.Collapsed; }
52      set {
53        if (this.expandIconMaterial.Collapsed != value)
54          this.expandIconMaterial.Collapsed = value;
55      }
56    }
57
58    private Color lineColor;
59    public Color LineColor {
60      get { return this.lineColor; }
61      set { this.lineColor = value; }
62    }
63
64    private float lineWidth;
65    public float LineWidth {
66      get { return this.lineWidth; }
67      set { this.lineWidth = value; }
68    }
69
70    private Color color;
71    public Color Color {
72      get { return this.color; }
73      set { this.color = value; }
74    }
75
76    private string title;
77    public string Title {
78      get { return title; }
79      set { title = value; }
80    }
81
82    private IconMaterial iconMaterial;
83    public Bitmap Icon {
84      get { return this.iconMaterial.Icon; }
85      set {
86        this.iconMaterial.Icon = value;
87        this.iconMaterial.Transform(new Rectangle(new Point(Rectangle.X + 5, Rectangle.Y + 5), this.iconMaterial.Icon.Size));
88      }
89    }
90
91    #region additional connectors
92    private List<IConnector> additionalConnectors;
93    public IEnumerable<string> AdditionalConnectorNames {
94      get { return this.additionalConnectors.Select(c => c.Name); }
95    }
96
97    private IConnector predecessor;
98    public IConnector Predecessor {
99      get { return this.predecessor; }
100    }
101
102    private IConnector successor;
103    public IConnector Successor {
104      get { return this.successor; }
105    }
106
107    private IConnector CreateConnector(string connectorName, Point location) {
108      Connector connector = new Connector(location, this.Model);
109      connector.ConnectorStyle = ConnectorStyle.Square;
110      connector.Parent = this;
111      connector.Name = connectorName;
112      return connector;
113    }
114
115    public void AddConnector(string connectorName) {
116      IConnector connector = this.CreateConnector(connectorName, this.BottomRightCorner);
117
118      this.additionalConnectors.Add(connector);
119      this.Connectors.Add(connector);
120      this.UpdateConnectorLocation();
121    }
122
123    public void RemoveConnector(string connectorName) {
124      IConnector connector = this.additionalConnectors.Where(c => c.Name == connectorName).FirstOrDefault();
125      if (connector != null) {
126        this.additionalConnectors.Remove(connector);
127        this.Connectors.Remove(connector);
128        this.UpdateConnectorLocation();
129      }
130    }
131
132    private void UpdateConnectorLocation() {
133      if (this.additionalConnectors.Count == 0)
134        return;
135
136      int spacing = this.Rectangle.Width / this.additionalConnectors.Count;
137      int margin = spacing / 2;
138      int posX = margin + this.Rectangle.X;
139      for (int i = 0; i < this.additionalConnectors.Count; i++) {
140        this.additionalConnectors[i].MoveBy(new Point(posX - this.additionalConnectors[i].Point.X, 0));
141        posX += spacing;
142      }
143    }
144    #endregion
145
146    #region label material
147    private List<string> labels;
148    public IEnumerable<string> Labels {
149      get { return this.labels; }
150    }
151
152    public void UpdateLabels(IEnumerable<string> labels) {
153      this.labels = new List<string>(labels);
154      this.expandIconMaterial.Visible = this.labels.Count != 0;
155      this.UpdateLabels();
156    }
157    #endregion
158
159    private void expandIconMaterial_OnExpand(object sender, EventArgs e) {
160      this.UpdateLabels();
161    }
162
163    private void expandIconMaterial_OnCollapse(object sender, EventArgs e) {
164      this.UpdateLabels();
165    }
166
167    private Size CalculateSize() {
168      int width = this.Rectangle.Width;
169      int height = headerHeight;
170      if (!Collapsed)
171        height += this.labels.Count * (LABEL_HEIGHT + LABEL_SPACING);
172      return new Size(width, height);
173    }
174
175    private void UpdateLabels() {
176      Size newSize = CalculateSize();
177      if (this.Rectangle.Size != newSize) {
178        foreach (IConnector connector in this.additionalConnectors)
179          connector.MoveBy(new Point(0, newSize.Height - this.Rectangle.Height));
180        this.mRectangle = new Rectangle(this.Rectangle.Location, newSize);
181        this.Invalidate();
182        this.RaiseOnChange(this, new EntityEventArgs(this));
183      }
184    }
185
186    protected override void Initialize() {
187      base.Initialize();
188
189      //the initial size
190      this.Transform(0, 0, 200, headerHeight);
191      this.color = Color.LightBlue;
192
193      this.iconMaterial = new IconMaterial();
194      this.iconMaterial.Gliding = false;
195      this.Children.Add(iconMaterial);
196
197      Bitmap expandBitmap = new Bitmap(HeuristicLab.Common.Resources.VSImageLibrary.Expand);
198      Bitmap collapseBitmap = new Bitmap(HeuristicLab.Common.Resources.VSImageLibrary.Collapse);
199      this.expandIconMaterial = new ExpandableIconMaterial(expandBitmap, collapseBitmap);
200      this.expandIconMaterial.Gliding = false;
201      this.expandIconMaterial.Transform(new Rectangle(new Point(Rectangle.Right - 20, Rectangle.Y + 7), expandIconMaterial.Icon.Size));
202      this.expandIconMaterial.Visible = false;
203      this.expandIconMaterial.OnExpand += new EventHandler(expandIconMaterial_OnExpand);
204      this.expandIconMaterial.OnCollapse += new EventHandler(expandIconMaterial_OnCollapse);
205      this.Children.Add(expandIconMaterial);
206
207      this.predecessor = this.CreateConnector(OperatorShapeInfoFactory.PredecessorConnector, new Point(Rectangle.Left, Center.Y));
208      this.Connectors.Add(predecessor);
209
210      this.successor = this.CreateConnector(OperatorShapeInfoFactory.SuccessorConnector, (new Point(Rectangle.Right, Center.Y)));
211      this.Connectors.Add(successor);
212    }
213
214    public override void Paint(Graphics g) {
215      base.Paint(g);
216
217      g.SmoothingMode = SmoothingMode.HighQuality;
218
219      Pen pen = new Pen(lineColor, lineWidth);
220
221      SizeF titleSize = g.MeasureString(this.Title, ArtPalette.DefaultBoldFont, Rectangle.Width - 45);
222      if (titleSize.Height + 10 > Rectangle.Height) {
223        headerHeight = (int)titleSize.Height + 10;
224        this.UpdateLabels();
225      }
226
227      GraphicsPath path = new GraphicsPath();
228      path.AddArc(Rectangle.X, Rectangle.Y, 20, 20, -180, 90);
229      path.AddLine(Rectangle.X + 10, Rectangle.Y, Rectangle.X + Rectangle.Width - 10, Rectangle.Y);
230      path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y, 20, 20, -90, 90);
231      path.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + 10, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height - 10);
232      path.AddArc(Rectangle.X + Rectangle.Width - 20, Rectangle.Y + Rectangle.Height - 20, 20, 20, 0, 90);
233      path.AddLine(Rectangle.X + Rectangle.Width - 10, Rectangle.Y + Rectangle.Height, Rectangle.X + 10, Rectangle.Y + Rectangle.Height);
234      path.AddArc(Rectangle.X, Rectangle.Y + Rectangle.Height - 20, 20, 20, 90, 90);
235      path.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height - 10, Rectangle.X, Rectangle.Y + 10);
236      //shadow
237      if (ArtPalette.EnableShadows) {
238        Region darkRegion = new Region(path);
239        darkRegion.Translate(5, 5);
240        g.FillRegion(ArtPalette.ShadowBrush, darkRegion);
241      }
242      //background
243      g.FillPath(Brush, path);
244
245      using (LinearGradientBrush gradientBrush = new LinearGradientBrush(Rectangle.Location, new Point(Rectangle.X + Rectangle.Width, Rectangle.Y), this.Color, Color.White)) {
246        Region gradientRegion = new Region(path);
247        g.FillRegion(gradientBrush, gradientRegion);
248      }
249
250      if (!this.Collapsed) {
251        TextStyle textStyle = new TextStyle(Color.Black, new Font("Arial", 7), StringAlignment.Near, StringAlignment.Near);
252        StringFormat stringFormat = textStyle.StringFormat;
253        stringFormat.Trimming = StringTrimming.EllipsisWord;
254        stringFormat.FormatFlags = StringFormatFlags.LineLimit;
255        Rectangle rect;
256
257        for (int i = 0; i < this.labels.Count; i++) {
258          rect = new Rectangle(Rectangle.X + 25, Rectangle.Y + headerHeight + i * (LABEL_HEIGHT + LABEL_SPACING), LABEL_WIDTH, LABEL_HEIGHT);
259          g.DrawString(textStyle.GetFormattedText(this.labels[i]), textStyle.Font, textStyle.GetBrush(), rect, stringFormat);
260        }
261      }
262
263      //the border
264      g.DrawPath(pen, path);
265
266      //the title
267      g.DrawString(this.Title, ArtPalette.DefaultBoldFont, Brushes.Black, new Rectangle(Rectangle.X + 25, Rectangle.Y + 5, Rectangle.Width - 45, Rectangle.Height - 5));
268
269
270      //the material
271      foreach (IPaintable material in Children)
272        material.Paint(g);
273
274      //the connectors
275      if (this.ShowConnectors) {
276        for (int k = 0; k < Connectors.Count; k++)
277          Connectors[k].Paint(g);
278      }
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.