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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Core.Networks;
|
---|
27 | using HeuristicLab.Core.Networks.Views;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.Networks.Views.NetworkVisualization.Views.ChartModes;
|
---|
30 | using HeuristicLab.Visualization;
|
---|
31 | using HeuristicLab.Visualization.ChartModes;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Networks.Views.NetworkVisualization.Views {
|
---|
34 | [View("Network Visualization View")]
|
---|
35 | [Content(typeof(Network), false)]
|
---|
36 | [Content(typeof(INetwork), false)]
|
---|
37 | public partial class NetworkVisualizationView : NetworkItemView {
|
---|
38 | private Dictionary<string, IPrimitive> primitiveDict = new Dictionary<string, IPrimitive>();
|
---|
39 |
|
---|
40 | public new INetwork Content {
|
---|
41 | get { return (INetwork)base.Content; }
|
---|
42 | set { base.Content = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public NetworkVisualizationView() {
|
---|
46 | InitializeComponent();
|
---|
47 |
|
---|
48 | var selectMode = new SelectChartMode(chartControl);
|
---|
49 | selectMode.SelectedPrimitivesChanged += SelectMode_SelectedPrimitivesChanged;
|
---|
50 | chartControl.AddChartModes(
|
---|
51 | selectMode,
|
---|
52 | new PanChartMode(chartControl),
|
---|
53 | new RulerChartMode(chartControl),
|
---|
54 | new ZoomInChartMode(chartControl),
|
---|
55 | new ZoomOutChartMode(chartControl),
|
---|
56 | new AddNodesChartMode(chartControl),
|
---|
57 | new AddPortsChartMode(chartControl),
|
---|
58 | new ConnectPortsChartMode(chartControl)
|
---|
59 | );
|
---|
60 | chartControl.Mode = selectMode;
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override void OnContentChanged() {
|
---|
64 | base.OnContentChanged();
|
---|
65 | bool doLayout = Content != null && Content.Nodes.Any(x => x.VisualProperties == null);
|
---|
66 | UpdateContent();
|
---|
67 | if (doLayout) {
|
---|
68 | LayoutNodes();
|
---|
69 | LayoutPorts();
|
---|
70 | }
|
---|
71 | chartControl.Tag = Content;
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void RegisterContentEvents() {
|
---|
75 | base.RegisterContentEvents();
|
---|
76 | Content.Nodes.ItemsAdded += Nodes_ItemsAdded;
|
---|
77 | Content.Nodes.ItemsRemoved += Nodes_ItemsRemoved;
|
---|
78 | Content.Nodes.ItemsReplaced += Nodes_ItemsReplaced;
|
---|
79 | Content.Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
80 | Content.Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
81 | Content.Ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void DeregisterContentEvents() {
|
---|
85 | Content.Nodes.ItemsAdded -= Nodes_ItemsAdded;
|
---|
86 | Content.Nodes.ItemsRemoved -= Nodes_ItemsRemoved;
|
---|
87 | Content.Nodes.ItemsReplaced -= Nodes_ItemsReplaced;
|
---|
88 | Content.Ports.ItemsAdded -= Ports_ItemsAdded;
|
---|
89 | Content.Ports.ItemsRemoved -= Ports_ItemsRemoved;
|
---|
90 | Content.Ports.ItemsReplaced -= Ports_ItemsReplaced;
|
---|
91 | base.DeregisterContentEvents();
|
---|
92 | }
|
---|
93 |
|
---|
94 | #region Event Handlers
|
---|
95 | private void SelectMode_SelectedPrimitivesChanged(object sender, EventArgs e) {
|
---|
96 | var selectedPrimitives = chartControl.Chart.Group.SelectedPrimitives;
|
---|
97 | var nodePrimitive = selectedPrimitives.OfType<NodeRectangle>().SingleOrDefault();
|
---|
98 | viewHost.Content = nodePrimitive != null ? nodePrimitive.NetworkItem : null;
|
---|
99 | }
|
---|
100 |
|
---|
101 | #region Nodes Event Handlers
|
---|
102 | private void Nodes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<INode> e) {
|
---|
103 | // TODO: don't update everything
|
---|
104 | UpdateContent();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void Nodes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<INode> e) {
|
---|
108 | // TODO: don't update everything
|
---|
109 | UpdateContent();
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void Nodes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<INode> e) {
|
---|
113 | // TODO: don't update everything
|
---|
114 | UpdateContent();
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 |
|
---|
118 | #region Ports Event Handlers
|
---|
119 | private void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
|
---|
120 |
|
---|
121 | private void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
|
---|
122 |
|
---|
123 | private void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | private void detailsToggleButton_Click(object sender, EventArgs e) {
|
---|
127 | splitContainer.Panel2Collapsed = !splitContainer.Panel2Collapsed;
|
---|
128 | detailsToggleButton.Text = splitContainer.Panel2Collapsed ? "<" : ">";
|
---|
129 | }
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | #region Helpers
|
---|
133 | private void UpdateContent() {
|
---|
134 | if (Content == null) return;
|
---|
135 |
|
---|
136 | chartControl.SuspendRendering();
|
---|
137 | try {
|
---|
138 | foreach (var r in primitiveDict.Values.OfType<NodeRectangle>())
|
---|
139 | r.NetworkItem = null;
|
---|
140 | primitiveDict.Clear();
|
---|
141 | chartControl.Chart.Group.Clear();
|
---|
142 | DrawNodes();
|
---|
143 | ConnectNodes();
|
---|
144 | } finally {
|
---|
145 | chartControl.ResumeRendering();
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void DrawNodes() {
|
---|
150 | foreach (var node in Content.Nodes) {
|
---|
151 | var defaultPrimitive = PrimitiveAttribute.CreateDefaultPrimitive(node.GetType(), chartControl.Chart, node);
|
---|
152 | if (defaultPrimitive == null) continue;
|
---|
153 | chartControl.Chart.Group.Add(defaultPrimitive);
|
---|
154 | primitiveDict[node.Name] = defaultPrimitive;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void ConnectNodes() {
|
---|
159 | foreach (var node in Content.Nodes) {
|
---|
160 | var nodeRectangle = primitiveDict[node.Name] as NodeRectangle;
|
---|
161 | if (nodeRectangle == null) continue;
|
---|
162 |
|
---|
163 | foreach (var port in node.Ports.OfType<IConnectablePort>()) {
|
---|
164 | var connectedPort = port.ConnectedPort as IConnectablePort;
|
---|
165 | if (connectedPort == null) continue;
|
---|
166 |
|
---|
167 | var connectedNode = connectedPort.Parent;
|
---|
168 | if (connectedNode == null) continue;
|
---|
169 |
|
---|
170 | var connectedRectangle = primitiveDict[connectedNode.Name] as NodeRectangle;
|
---|
171 | if (connectedRectangle == null) continue;
|
---|
172 |
|
---|
173 | var leftPortRectangle = (PortRectangle)nodeRectangle.GetPortPrimitive(port);
|
---|
174 | var rightPortRectangle = (PortRectangle)connectedRectangle.GetPortPrimitive(connectedPort);
|
---|
175 |
|
---|
176 | var connectionShape = new ConnectionLine(chartControl.Chart, leftPortRectangle, rightPortRectangle);
|
---|
177 |
|
---|
178 | chartControl.Chart.Group.Add(connectionShape);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void LayoutNodes() {
|
---|
184 | var layout = new Layout();
|
---|
185 | var layoutNodes = new Dictionary<string, Layout.LayoutNode>();
|
---|
186 |
|
---|
187 | foreach (var node in Content.Nodes) {
|
---|
188 | var nodeRectangle = primitiveDict[node.Name] as NodeRectangle;
|
---|
189 | if (nodeRectangle == null) continue;
|
---|
190 |
|
---|
191 | var nodeSize = chartControl.Chart.TransformWorldToPixel(nodeRectangle.Size);
|
---|
192 | Layout.LayoutNode layoutNode;
|
---|
193 | if (!layoutNodes.TryGetValue(node.Name, out layoutNode)) {
|
---|
194 | layoutNodes.Add(node.Name, layoutNode = new Layout.LayoutNode(nodeSize));
|
---|
195 | layout.AddNode(layoutNode);
|
---|
196 | }
|
---|
197 |
|
---|
198 | foreach (var port in node.Ports.OfType<IConnectablePort>()) {
|
---|
199 | var connectedPort = port.ConnectedPort as IConnectablePort;
|
---|
200 | if (connectedPort == null) continue;
|
---|
201 |
|
---|
202 | var connectedNode = connectedPort.Parent;
|
---|
203 | if (connectedNode == null) continue;
|
---|
204 |
|
---|
205 | var connectedRectangle = primitiveDict[connectedNode.Name] as NodeRectangle;
|
---|
206 | if (connectedRectangle == null) continue;
|
---|
207 |
|
---|
208 | nodeSize = chartControl.Chart.TransformWorldToPixel(connectedRectangle.Size);
|
---|
209 | if (!layoutNodes.ContainsKey(connectedNode.Name)) {
|
---|
210 | layoutNodes.Add(connectedNode.Name, new Layout.LayoutNode(nodeSize));
|
---|
211 | layout.AddNode(layoutNodes[connectedNode.Name]);
|
---|
212 | }
|
---|
213 |
|
---|
214 | layoutNode.AddNode(layoutNodes[connectedNode.Name]);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | var bounds = chartControl.GetPictureBounds();
|
---|
219 | bounds.Offset((int)chartControl.Chart.LowerLeft.X, -(int)chartControl.Chart.UpperRight.Y);
|
---|
220 |
|
---|
221 | layout.Arrange(bounds);
|
---|
222 |
|
---|
223 | foreach (var entry in layoutNodes) {
|
---|
224 | var nodeRectangle = primitiveDict[entry.Key] as NodeRectangle;
|
---|
225 | if (nodeRectangle == null) continue;
|
---|
226 | var rectangleNode = entry.Value;
|
---|
227 | nodeRectangle.Move(rectangleNode.Location - nodeRectangle.Center());
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void LayoutPorts() {
|
---|
232 | foreach (var node in Content.Nodes) {
|
---|
233 | var nodeRectangle = primitiveDict[node.Name] as NodeRectangle;
|
---|
234 | if (nodeRectangle == null) continue;
|
---|
235 |
|
---|
236 | foreach (var port in node.Ports.OfType<IConnectablePort>()) {
|
---|
237 | var connectedPort = port.ConnectedPort as IConnectablePort;
|
---|
238 | if (connectedPort == null) continue;
|
---|
239 |
|
---|
240 | var connectedNode = connectedPort.Parent;
|
---|
241 | if (connectedNode == null) continue;
|
---|
242 |
|
---|
243 | var connectedRectangle = primitiveDict[connectedNode.Name] as NodeRectangle;
|
---|
244 | if (connectedRectangle == null) continue;
|
---|
245 |
|
---|
246 | var leftCenter = nodeRectangle.Center();
|
---|
247 | var rightCenter = connectedRectangle.Center();
|
---|
248 | var line = new Line(null, rightCenter, leftCenter);
|
---|
249 |
|
---|
250 | var leftIntersection = nodeRectangle.ComputeIntersect(line).Single();
|
---|
251 | var rightIntersection = connectedRectangle.ComputeIntersect(line).Single();
|
---|
252 |
|
---|
253 | var leftPortRectangle = (PortRectangle)nodeRectangle.GetPortPrimitive(port);
|
---|
254 | leftPortRectangle.SetPosition(leftIntersection);
|
---|
255 | var rightPortRectangle = (PortRectangle)connectedRectangle.GetPortPrimitive(connectedPort);
|
---|
256 | rightPortRectangle.SetPosition(rightIntersection);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | #endregion
|
---|
261 | }
|
---|
262 | }
|
---|