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

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

#2205: worked on optimization networks

  • refactored network visualization
File size: 4.5 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    }
62
63    protected virtual void DeregisterPortRectangleEvents(PortRectangle portRectangle) {
64      portRectangle.RedrawRequired -= PortRectangle_RedrawRequired;
65    }
66
67    #region Overrides
68    public override void Move(Offset delta) { }
69    public override void Move(PointD point, Offset delta) { }
70
71    public override void Draw(Graphics graphics) {
72      base.Draw(graphics);
73
74      var startP = Chart.TransformWorldToPixel(Start);
75      var endP = Chart.TransformWorldToPixel(End);
76
77      if (startP == endP) return;
78
79      double length = Math.Sqrt(Math.Pow(startP.X - endP.X, 2.0) + Math.Pow(startP.Y - endP.Y, 2));
80      double ax = (endP.X - startP.X) / length, ay = (endP.Y - startP.Y) / length;
81      double bx = -2.5 * ax - ay, by = -2.5 * ay + ax;
82      double cx = -2.5 * ax + ay, cy = -2.5 * ay - ax;
83      double mul = 4 * Chart.WorldToPixelRatio.Width;
84      bx *= mul; by *= mul;
85      cx *= mul; cy *= mul;
86      var p1 = new Point((int)Math.Round(endP.X + bx), (int)Math.Round(endP.Y + by));
87      var p2 = new Point((int)Math.Round(endP.X + cx), (int)Math.Round(endP.Y + cy));
88      graphics.FillPolygon(Pen.Brush, new[] { p1, endP, p2 });
89    }
90
91    public override void PostDraw(Graphics graphics) {
92      if (Selected) {
93        using (var pen = new Pen(Color.LightGray, 3) { DashStyle = DashStyle.Dash })
94          graphics.DrawLine(pen, Chart.TransformWorldToPixel(Start), Chart.TransformWorldToPixel(End));
95      }
96    }
97    #endregion
98
99    #region Events
100    public event EventHandler StartPortRectangleChanged;
101    protected virtual void OnStartPortRectangleChanged() {
102      var handler = StartPortRectangleChanged;
103      if (handler != null) handler(this, EventArgs.Empty);
104    }
105
106    public event EventHandler EndPortRectangleChanged;
107    protected virtual void OnEndPortRectangleChanged() {
108      var handler = EndPortRectangleChanged;
109      if (handler != null) handler(this, EventArgs.Empty);
110    }
111    #endregion
112
113    #region Event Handlers
114    private void PortRectangle_RedrawRequired(object sender, EventArgs e) {
115      SetPosition(startPortRectangle.Point, endPortRectangle.Point);
116    }
117    #endregion
118  }
119}
Note: See TracBrowser for help on using the repository browser.