Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13180 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 11.8 KB
RevLine 
[2853]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2853]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;
[2934]25using System.Drawing.Drawing2D;
[4068]26using System.Linq;
[2934]27using HeuristicLab.Netron;
[4068]28using Netron.Diagramming.Core;
[2853]29
[6036]30namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
[2934]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;
[3169]35    private int headerHeight = 30;
[2853]36
[2934]37    private ExpandableIconMaterial expandIconMaterial;
[2853]38    public OperatorShape()
39      : base() {
[2934]40      this.Resizable = false;
[2861]41      this.additionalConnectors = new List<IConnector>();
[2934]42      this.labels = new List<string>();
[2853]43    }
44
[2934]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
[2935]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
[2934]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
[7226]81    public string Subtitle { get; set; }
[7199]82
[2934]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
[2861]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 {
[2853]100      get { return this.predecessor; }
101    }
102
[2861]103    private IConnector successor;
104    public IConnector Successor {
[2853]105      get { return this.successor; }
106    }
107
[2861]108    private IConnector CreateConnector(string connectorName, Point location) {
[2868]109      Connector connector = new Connector(location, this.Model);
[2853]110      connector.ConnectorStyle = ConnectorStyle.Square;
111      connector.Parent = this;
[2861]112      connector.Name = connectorName;
113      return connector;
[2853]114    }
115
[2861]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);
[2853]121      this.UpdateConnectorLocation();
122    }
123
[2861]124    public void RemoveConnector(string connectorName) {
[2868]125      IConnector connector = this.additionalConnectors.Where(c => c.Name == connectorName).FirstOrDefault();
[2861]126      if (connector != null) {
127        this.additionalConnectors.Remove(connector);
128        this.Connectors.Remove(connector);
129        this.UpdateConnectorLocation();
130      }
[2853]131    }
132
[2861]133    private void UpdateConnectorLocation() {
[2868]134      if (this.additionalConnectors.Count == 0)
135        return;
136
137      int spacing = this.Rectangle.Width / this.additionalConnectors.Count;
[2861]138      int margin = spacing / 2;
139      int posX = margin + this.Rectangle.X;
140      for (int i = 0; i < this.additionalConnectors.Count; i++) {
[2872]141        this.additionalConnectors[i].MoveBy(new Point(posX - this.additionalConnectors[i].Point.X, 0));
[2861]142        posX += spacing;
143      }
144    }
[2934]145    #endregion
[2853]146
[2934]147    #region label material
148    private List<string> labels;
149    public IEnumerable<string> Labels {
150      get { return this.labels; }
151    }
[2861]152
[2934]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;
[3169]170      int height = headerHeight;
[2934]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
[2853]187    protected override void Initialize() {
188      base.Initialize();
189
[2934]190      //the initial size
[3169]191      this.Transform(0, 0, 200, headerHeight);
[2934]192      this.color = Color.LightBlue;
[2853]193
[2934]194      this.iconMaterial = new IconMaterial();
195      this.iconMaterial.Gliding = false;
196      this.Children.Add(iconMaterial);
[2853]197
[5287]198      Bitmap expandBitmap = new Bitmap(HeuristicLab.Common.Resources.VSImageLibrary.Expand);
199      Bitmap collapseBitmap = new Bitmap(HeuristicLab.Common.Resources.VSImageLibrary.Collapse);
[2934]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
[3422]208      this.predecessor = this.CreateConnector(OperatorShapeInfoFactory.PredecessorConnector, new Point(Rectangle.Left, Center.Y));
[2934]209      this.Connectors.Add(predecessor);
210
[3422]211      this.successor = this.CreateConnector(OperatorShapeInfoFactory.SuccessorConnector, (new Point(Rectangle.Right, Center.Y)));
[2934]212      this.Connectors.Add(successor);
[2853]213    }
[2934]214
215    public override void Paint(Graphics g) {
216      base.Paint(g);
217
218      g.SmoothingMode = SmoothingMode.HighQuality;
219
[7226]220      using (Pen pen = new Pen(lineColor, lineWidth)) {
[2934]221
[7226]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);
[7199]227
[7226]228        if ((int)titleSize.Height + (int)subtitleSize.Height != Rectangle.Height) {
229          headerHeight = (int)titleSize.Height + (int)subtitleSize.Height;
230          this.UpdateLabels();
231        }
[3169]232
[7226]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);
[2934]250
[7226]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        }
[2934]255
[7226]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;
[2934]262
[7226]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          }
[2934]277        }
278
[7226]279        //the border
280        g.DrawPath(pen, path);
[2934]281
[7226]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));
[2934]286
[7226]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        }
[3169]293
[7199]294
[7226]295        //the material
296        foreach (IShapeMaterial material in Children)
297          material.Paint(g);
[2934]298
[7226]299        //the connectors
300        if (this.ShowConnectors) {
301          foreach (IConnector t in Connectors)
302            t.Paint(g);
303        }
[2934]304      }
305    }
[2853]306  }
307}
Note: See TracBrowser for help on using the repository browser.