Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/ConnectionLine.cs @ 13077

Last change on this file since 13077 was 13077, checked in by jkarder, 7 years ago

#2205: worked on optimization networks

  • added first version of network visualization
  • updated frame files
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
24using System.Drawing.Drawing2D;
25using HeuristicLab.Visualization;
26
27namespace HeuristicLab.Networks.Views.NetworkVisualization {
28  public class ConnectionLine : Line {
29    protected PortRectangle startPortRectangle;
30    public PortRectangle StartPortRectangle {
31      get { return startPortRectangle; }
32      set {
33        if (startPortRectangle == value) return;
34        if (startPortRectangle != null) DeregisterPortRectangleEvents(startPortRectangle);
35        startPortRectangle = value;
36        if (startPortRectangle != null) RegisterPortRectangleEvents(startPortRectangle);
37        OnStartPortRectangleChanged();
38      }
39    }
40
41    protected PortRectangle endPortRectangle;
42    public PortRectangle EndPortRectangle {
43      get { return endPortRectangle; }
44      set {
45        if (endPortRectangle == value) return;
46        if (endPortRectangle != null) DeregisterPortRectangleEvents(endPortRectangle);
47        endPortRectangle = value;
48        if (endPortRectangle != null) RegisterPortRectangleEvents(endPortRectangle);
49        OnEndPortRectangleChanged();
50      }
51    }
52
53    public ConnectionLine(IChart chart, PortRectangle start, PortRectangle end)
54      : base(chart, start.Point, end.Point) {
55      StartPortRectangle = start;
56      EndPortRectangle = end;
57    }
58
59    protected virtual void RegisterPortRectangleEvents(PortRectangle portRectangle) {
60      portRectangle.RedrawRequired += PortRectangle_RedrawRequired;
61      portRectangle.NetworkItemChanged += PortRectangleNetworkItemChanged;
62      if (portRectangle.NetworkItem != null) {
63        var port = portRectangle.NetworkItem;
64        port.NameChanged += Port_NameChanged;
65      }
66    }
67
68    protected virtual void DeregisterPortRectangleEvents(PortRectangle portRectangle) {
69      portRectangle.RedrawRequired -= PortRectangle_RedrawRequired;
70      portRectangle.NetworkItemChanged -= PortRectangleNetworkItemChanged;
71      if (portRectangle.NetworkItem != null) {
72        var port = portRectangle.NetworkItem;
73        port.NameChanged -= Port_NameChanged;
74      }
75    }
76
77    #region Overrides
78    public override void Move(Offset delta) { }
79    public override void Move(PointD point, Offset delta) { }
80
81    public override void Draw(Graphics graphics) {
82      base.Draw(graphics);
83
84      var startP = Chart.TransformWorldToPixel(Start);
85      var endP = Chart.TransformWorldToPixel(End);
86
87      if (startP == endP) return;
88
89      double length = Math.Sqrt(Math.Pow(startP.X - endP.X, 2.0) + Math.Pow(startP.Y - endP.Y, 2));
90      double ax = (endP.X - startP.X) / length, ay = (endP.Y - startP.Y) / length;
91      double bx = -2.5 * ax - ay, by = -2.5 * ay + ax;
92      double cx = -2.5 * ax + ay, cy = -2.5 * ay - ax;
93      double mul = 4 * Chart.WorldToPixelRatio.Width;
94      bx *= mul; by *= mul;
95      cx *= mul; cy *= mul;
96      var p1 = new Point((int)Math.Round(endP.X + bx), (int)Math.Round(endP.Y + by));
97      var p2 = new Point((int)Math.Round(endP.X + cx), (int)Math.Round(endP.Y + cy));
98      graphics.FillPolygon(Pen.Brush, new[] { p1, endP, p2 });
99    }
100
101    public override void PostDraw(Graphics graphics) {
102      if (Selected) {
103        using (var pen = new Pen(Color.LightGray, 3) { DashStyle = DashStyle.Dash })
104          graphics.DrawLine(pen, Chart.TransformWorldToPixel(Start), Chart.TransformWorldToPixel(End));
105      }
106    }
107
108    #endregion
109
110    #region Events
111    public event EventHandler StartPortRectangleChanged;
112    protected virtual void OnStartPortRectangleChanged() {
113      UpdateToolTip();
114      var handler = StartPortRectangleChanged;
115      if (handler != null) handler(this, EventArgs.Empty);
116    }
117
118    public event EventHandler EndPortRectangleChanged;
119    protected virtual void OnEndPortRectangleChanged() {
120      UpdateToolTip();
121      var handler = EndPortRectangleChanged;
122      if (handler != null) handler(this, EventArgs.Empty);
123    }
124    #endregion
125
126    #region Event Handlers
127    private void PortRectangle_RedrawRequired(object sender, EventArgs e) {
128      SetPosition(startPortRectangle.Point, endPortRectangle.Point);
129    }
130
131    private void PortRectangleNetworkItemChanged(object sender, EventArgs e) {
132      UpdateToolTip();
133    }
134
135    private void Port_NameChanged(object sender, EventArgs e) {
136      UpdateToolTip();
137    }
138    #endregion
139
140    #region Helpers
141    private void UpdateToolTip() {
142      string startText = "null", endText = "null";
143
144      if (startPortRectangle != null) {
145        var startPort = startPortRectangle.NetworkItem;
146        if (startPort != null) {
147          startText = startPort.Name;
148          var startNode = startPort.Parent;
149          startText = (startNode != null ? startNode.Name : "null") + "." + startText;
150        }
151      }
152
153      if (endPortRectangle != null) {
154        var endPort = endPortRectangle.NetworkItem;
155        if (endPort != null) {
156          endText = endPort.Name;
157          var endNode = endPort.Parent;
158          endText = (endNode != null ? endNode.Name : "null") + "." + endText;
159        }
160      }
161
162      ToolTipText = string.Format("[{0}] --> [{1}]", startText, endText);
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.