Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2934 was 2934, checked in by mkommend, 14 years ago

added parameters, corrected drag and drop position (ticket #867)

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