Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization.Views/3.3/NetworkVisualizationView.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: 5.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.Linq;
23using HeuristicLab.Collections;
24using HeuristicLab.Core;
25using HeuristicLab.Core.Networks;
26using HeuristicLab.Core.Networks.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Networks.Views.NetworkVisualization.Views.ChartModes;
29using HeuristicLab.Visualization;
30
31namespace HeuristicLab.Networks.Views.NetworkVisualization.Views {
32  [View("Network Visualization View")]
33  [Content(typeof(Network), false)]
34  [Content(typeof(INetwork), false)]
35  public partial class NetworkVisualizationView : NetworkItemView {
36    public new INetwork Content {
37      get { return (INetwork)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public NetworkVisualizationView() {
42      InitializeComponent();
43
44      var initialMode = new NetworkSelectChartMode(chartControl);
45      chartControl.AddChartModes(
46        initialMode,
47        new MoveChartMode(chartControl),
48        new ZoomInChartMode(chartControl),
49        new ZoomOutChartMode(chartControl),
50        new AddNodesChartMode(chartControl),
51        new AddPortsChartMode(chartControl),
52        new ConnectPortsChartMode(chartControl)
53      );
54      chartControl.Mode = initialMode;
55    }
56
57    protected override void OnContentChanged() {
58      base.OnContentChanged();
59      UpdateContent();
60      chartControl.Tag = Content;
61    }
62
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.Nodes.ItemsAdded += Nodes_ItemsAdded;
66      Content.Nodes.ItemsRemoved += Nodes_ItemsRemoved;
67      Content.Nodes.ItemsReplaced += Nodes_ItemsReplaced;
68      Content.Ports.ItemsAdded += Ports_ItemsAdded;
69      Content.Ports.ItemsRemoved += Ports_ItemsRemoved;
70      Content.Ports.ItemsReplaced += Ports_ItemsReplaced;
71    }
72
73    protected override void DeregisterContentEvents() {
74      Content.Nodes.ItemsAdded -= Nodes_ItemsAdded;
75      Content.Nodes.ItemsRemoved -= Nodes_ItemsRemoved;
76      Content.Nodes.ItemsReplaced -= Nodes_ItemsReplaced;
77      Content.Ports.ItemsAdded -= Ports_ItemsAdded;
78      Content.Ports.ItemsRemoved -= Ports_ItemsRemoved;
79      Content.Ports.ItemsReplaced -= Ports_ItemsReplaced;
80      base.DeregisterContentEvents();
81    }
82
83    private void UpdateContent() {
84      if (Content == null) return;
85
86      chartControl.Chart.Group.Clear();
87      DrawNodes(Content.Nodes);
88      ConnectNodes(Content.Nodes);
89    }
90
91    private void DrawNodes(IKeyedItemCollection<string, INode> nodes) {
92      var chart = chartControl.Chart;
93
94      foreach (var node in nodes.OfType<IVisualizableNode>()) {
95        var nodeShape = new NodeRectangle(chart, node);
96        chart.Group.Add(nodeShape);
97      }
98    }
99
100    private void ConnectNodes(IKeyedItemCollection<string, INode> nodes) {
101      var chart = chartControl.Chart;
102      var nodeRectangles = chart.Group.Primitives.OfType<NodeRectangle>();
103      var node2NodeShape = nodeRectangles.ToDictionary(k => k.NetworkItem.Name, v => v);
104
105      foreach (var node in nodes) {
106        var nodeShape = node2NodeShape[node.Name];
107        foreach (var port in node.Ports.OfType<IConnectablePort>()) {
108          var connectedPort = port.ConnectedPort as IConnectablePort;
109          if (connectedPort == null) continue;
110
111          var connectedNode = connectedPort.Parent;
112          if (connectedNode == null) continue;
113
114          var connectedShape = node2NodeShape[connectedNode.Name];
115          var leftPortRectangle = nodeShape.GetPortRectangle(port);
116          var rightPortRectangle = connectedShape.GetPortRectangle(connectedPort);
117
118          var connectionShape = new ConnectionLine(chart, leftPortRectangle, rightPortRectangle);
119
120          chart.Group.Add(connectionShape);
121        }
122      }
123    }
124
125    #region Event Handlers
126    #region Nodes Event Handlers
127    private void Nodes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<INode> e) {
128      // TODO: don't update everything
129      UpdateContent();
130    }
131
132    private void Nodes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<INode> e) {
133      // TODO: don't update everything
134      UpdateContent();
135    }
136
137    private void Nodes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<INode> e) {
138      // TODO: don't update everything
139      UpdateContent();
140    }
141    #endregion
142
143    #region Ports Event Handlers
144    private void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
145
146    private void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
147
148    private void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
149    #endregion
150    #endregion
151  }
152}
Note: See TracBrowser for help on using the repository browser.