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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
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 initialMode = new SelectChartMode(chartControl);
|
---|
49 | chartControl.AddChartModes(
|
---|
50 | initialMode,
|
---|
51 | new PanChartMode(chartControl),
|
---|
52 | new RulerChartMode(chartControl),
|
---|
53 | new ZoomInChartMode(chartControl),
|
---|
54 | new ZoomOutChartMode(chartControl),
|
---|
55 | new AddNodesChartMode(chartControl),
|
---|
56 | new AddPortsChartMode(chartControl),
|
---|
57 | new ConnectPortsChartMode(chartControl)
|
---|
58 | );
|
---|
59 | chartControl.Mode = initialMode;
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected override void OnContentChanged() {
|
---|
63 | base.OnContentChanged();
|
---|
64 | UpdateContent();
|
---|
65 | chartControl.Tag = Content;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void RegisterContentEvents() {
|
---|
69 | base.RegisterContentEvents();
|
---|
70 | Content.Nodes.ItemsAdded += Nodes_ItemsAdded;
|
---|
71 | Content.Nodes.ItemsRemoved += Nodes_ItemsRemoved;
|
---|
72 | Content.Nodes.ItemsReplaced += Nodes_ItemsReplaced;
|
---|
73 | Content.Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
74 | Content.Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
75 | Content.Ports.ItemsReplaced += Ports_ItemsReplaced;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override void DeregisterContentEvents() {
|
---|
79 | Content.Nodes.ItemsAdded -= Nodes_ItemsAdded;
|
---|
80 | Content.Nodes.ItemsRemoved -= Nodes_ItemsRemoved;
|
---|
81 | Content.Nodes.ItemsReplaced -= Nodes_ItemsReplaced;
|
---|
82 | Content.Ports.ItemsAdded -= Ports_ItemsAdded;
|
---|
83 | Content.Ports.ItemsRemoved -= Ports_ItemsRemoved;
|
---|
84 | Content.Ports.ItemsReplaced -= Ports_ItemsReplaced;
|
---|
85 | base.DeregisterContentEvents();
|
---|
86 | }
|
---|
87 |
|
---|
88 | #region Event Handlers
|
---|
89 | #region Nodes Event Handlers
|
---|
90 | private void Nodes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<INode> e) {
|
---|
91 | // TODO: don't update everything
|
---|
92 | UpdateContent();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void Nodes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<INode> e) {
|
---|
96 | // TODO: don't update everything
|
---|
97 | UpdateContent();
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void Nodes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<INode> e) {
|
---|
101 | // TODO: don't update everything
|
---|
102 | UpdateContent();
|
---|
103 | }
|
---|
104 | #endregion
|
---|
105 |
|
---|
106 | #region Ports Event Handlers
|
---|
107 | private void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
|
---|
108 |
|
---|
109 | private void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
|
---|
110 |
|
---|
111 | private void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
|
---|
112 | #endregion
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 | #region Helpers
|
---|
116 | private void UpdateContent() {
|
---|
117 | if (Content == null) return;
|
---|
118 |
|
---|
119 | chartControl.SuspendRendering();
|
---|
120 | try {
|
---|
121 | chartControl.Chart.Group.Clear();
|
---|
122 | DrawNodes(Content.Nodes);
|
---|
123 | ConnectNodes(Content.Nodes);
|
---|
124 | } finally {
|
---|
125 | chartControl.ResumeRendering();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void DrawNodes(IKeyedItemCollection<string, INode> nodes) {
|
---|
130 | foreach (var node in nodes) {
|
---|
131 | var defaultPrimitive = PrimitiveAttribute.CreateDefaultPrimitive(node.GetType(), chartControl.Chart, node);
|
---|
132 | if (defaultPrimitive == null) continue;
|
---|
133 | chartControl.Chart.Group.Add(defaultPrimitive);
|
---|
134 | primitiveDict[node.Name] = defaultPrimitive;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void ConnectNodes(IKeyedItemCollection<string, INode> nodes) {
|
---|
139 | var chart = chartControl.Chart;
|
---|
140 | foreach (var node in nodes) {
|
---|
141 | var nodeRectangle = primitiveDict[node.Name] as NodeRectangle;
|
---|
142 | if (nodeRectangle == null) continue;
|
---|
143 |
|
---|
144 | foreach (var port in node.Ports.OfType<IConnectablePort>()) {
|
---|
145 | var connectedPort = port.ConnectedPort as IConnectablePort;
|
---|
146 | if (connectedPort == null) continue;
|
---|
147 |
|
---|
148 | var connectedNode = connectedPort.Parent;
|
---|
149 | if (connectedNode == null) continue;
|
---|
150 |
|
---|
151 | var connectedRectangle = primitiveDict[connectedNode.Name] as NodeRectangle;
|
---|
152 | if (connectedRectangle == null) continue;
|
---|
153 |
|
---|
154 | var leftPortRectangle = (PortRectangle)nodeRectangle.GetPortPrimitive(port);
|
---|
155 | var rightPortRectangle = (PortRectangle)connectedRectangle.GetPortPrimitive(connectedPort);
|
---|
156 |
|
---|
157 | var connectionShape = new ConnectionLine(chart, leftPortRectangle, rightPortRectangle);
|
---|
158 |
|
---|
159 | chart.Group.Add(connectionShape);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | #endregion
|
---|
164 | }
|
---|
165 | }
|
---|