source: branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/PortRectangle.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: 4.3 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.Linq;
25using HeuristicLab.Core.Networks;
26using HeuristicLab.Visualization;
27
28namespace HeuristicLab.Networks.Views.NetworkVisualization {
29  public class PortRectangle : FixedSizeRectangle, INetworkItemPrimitive<IPort> {
30    private const int size = 10;
31
32    public override Size Size {
33      get {
34        return new Size((int)Math.Round(size * Chart.WorldToPixelRatio.Width),
35                        (int)Math.Round(size * Chart.WorldToPixelRatio.Height));
36      }
37    }
38
39    protected IPort networkItem;
40    public IPort NetworkItem {
41      get { return networkItem; }
42      set {
43        if (networkItem == value) return;
44        if (networkItem != null) DeregisterNetworkItemEvents();
45        networkItem = value;
46        if (networkItem != null) RegisterNetworkItemEvents();
47        OnNetworkItemChanged();
48      }
49    }
50
51    public PortRectangle(NodeRectangle nodeRectangle, IPort port)
52      : base(nodeRectangle.Chart, PointD.Empty, Size.Empty, Pens.Black, Brushes.Red) {
53      NetworkItem = port;
54    }
55
56    protected virtual void RegisterNetworkItemEvents() {
57      networkItem.NameChanged += NetworkItemNameChanged;
58      var connectablePort = networkItem as IConnectablePort;
59      if (connectablePort != null) {
60        connectablePort.ConnectedPortChanged += Port_ConnectedPortChanged;
61      }
62    }
63
64    protected virtual void DeregisterNetworkItemEvents() {
65      networkItem.NameChanged -= NetworkItemNameChanged;
66      var connectablePort = networkItem as IConnectablePort;
67      if (connectablePort != null) {
68        connectablePort.ConnectedPortChanged -= Port_ConnectedPortChanged;
69      }
70    }
71
72    #region Events
73    public event EventHandler NetworkItemChanged;
74    protected virtual void OnNetworkItemChanged() {
75      UpdateToolTip();
76      var handler = NetworkItemChanged;
77      if (handler != null) handler(this, EventArgs.Empty);
78    }
79
80    public event EventHandler NodeRectangleChanged;
81    protected virtual void OnNodeRectangleChanged() {
82      var handler = NodeRectangleChanged;
83      if (handler != null) handler(this, EventArgs.Empty);
84    }
85    #endregion
86
87    #region Event Handlers
88    private void NetworkItemNameChanged(object sender, EventArgs e) {
89      UpdateToolTip();
90    }
91
92    private void Port_ConnectedPortChanged(object sender, EventArgs e) {
93      var connectablePort = (IConnectablePort)networkItem;
94
95      var connectionLines = Chart.Group.GetAllPrimitives(Point).OfType<ConnectionLine>();
96      var outgoingLines = connectionLines.Where(x => x.StartPortRectangle == this);
97
98      foreach (var l in outgoingLines)
99        Chart.Group.Remove(l);
100
101      var connectedPort = connectablePort.ConnectedPort as IConnectablePort;
102      if (connectedPort == null) return;
103
104      var connectedNode = connectedPort.Parent;
105      if (connectedNode == null) return;
106
107      var nodeRectangles = Chart.Group.Primitives.OfType<NodeRectangle>();
108      PortRectangle endPortRectangle = null;
109      foreach (var nodeRectangle in nodeRectangles) {
110        endPortRectangle = nodeRectangle.GetPortRectangle(connectedPort);
111        if (endPortRectangle != null) break;
112      }
113
114      if (endPortRectangle == null) return;
115
116      var connectionShape = new ConnectionLine(Chart, this, endPortRectangle);
117      Chart.Group.Add(connectionShape);
118
119    }
120    #endregion
121
122    #region Helpers
123    private void UpdateToolTip() {
124      ToolTipText = networkItem.Name;
125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.