source: branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization.Views/3.3/NetworkVisualizationView.cs @ 13799

Last change on this file since 13799 was 13799, checked in by jkarder, 3 years ago

#2205: worked on optimization networks

  • improved network visualization
File size: 6.6 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Networks;
28using HeuristicLab.Core.Networks.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Networks.Views.NetworkVisualization.Views.ChartModes;
31using HeuristicLab.Visualization;
32using HeuristicLab.Visualization.ChartModes;
33
34namespace HeuristicLab.Networks.Views.NetworkVisualization.Views {
35  [View("Network Visualization View")]
36  [Content(typeof(Network), false)]
37  [Content(typeof(INetwork), false)]
38  public partial class NetworkVisualizationView : NetworkItemView {
39    private Dictionary<string, IPrimitive> primitiveDict = new Dictionary<string, IPrimitive>();
40
41    public new INetwork Content {
42      get { return (INetwork)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public NetworkVisualizationView() {
47      InitializeComponent();
48
49      var selectMode = new SelectChartMode(chartControl);
50      selectMode.SelectedPrimitivesChanged += SelectMode_SelectedPrimitivesChanged;
51      chartControl.AddChartModes(
52        selectMode,
53        new PanChartMode(chartControl),
54        new RulerChartMode(chartControl),
55        new ZoomInChartMode(chartControl),
56        new ZoomOutChartMode(chartControl),
57        new AddNodesChartMode(chartControl),
58        new AddPortsChartMode(chartControl),
59        new ConnectPortsChartMode(chartControl)
60      );
61      chartControl.Mode = selectMode;
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      UpdateContent();
67      chartControl.Tag = Content;
68    }
69
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72      Content.Nodes.ItemsAdded += Nodes_ItemsAdded;
73      Content.Nodes.ItemsRemoved += Nodes_ItemsRemoved;
74      Content.Nodes.ItemsReplaced += Nodes_ItemsReplaced;
75      Content.Ports.ItemsAdded += Ports_ItemsAdded;
76      Content.Ports.ItemsRemoved += Ports_ItemsRemoved;
77      Content.Ports.ItemsReplaced += Ports_ItemsReplaced;
78    }
79
80    protected override void DeregisterContentEvents() {
81      Content.Nodes.ItemsAdded -= Nodes_ItemsAdded;
82      Content.Nodes.ItemsRemoved -= Nodes_ItemsRemoved;
83      Content.Nodes.ItemsReplaced -= Nodes_ItemsReplaced;
84      Content.Ports.ItemsAdded -= Ports_ItemsAdded;
85      Content.Ports.ItemsRemoved -= Ports_ItemsRemoved;
86      Content.Ports.ItemsReplaced -= Ports_ItemsReplaced;
87      base.DeregisterContentEvents();
88    }
89
90    #region Event Handlers
91    private void SelectMode_SelectedPrimitivesChanged(object sender, EventArgs e) {
92      var selectedPrimitives = chartControl.Chart.Group.SelectedPrimitives;
93      var nodePrimitive = selectedPrimitives.OfType<NodeRectangle>().SingleOrDefault();
94      viewHost.Content = nodePrimitive != null ? nodePrimitive.NetworkItem : null;
95    }
96
97    #region Nodes Event Handlers
98    private void Nodes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<INode> e) {
99      // TODO: don't update everything
100      UpdateContent();
101    }
102
103    private void Nodes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<INode> e) {
104      // TODO: don't update everything
105      UpdateContent();
106    }
107
108    private void Nodes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<INode> e) {
109      // TODO: don't update everything
110      UpdateContent();
111    }
112    #endregion
113
114    #region Ports Event Handlers
115    private void Ports_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
116
117    private void Ports_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
118
119    private void Ports_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IPort> e) { }
120    #endregion
121
122    private void detailsToggleButton_Click(object sender, EventArgs e) {
123      splitContainer.Panel2Collapsed = !splitContainer.Panel2Collapsed;
124      detailsToggleButton.Text = splitContainer.Panel2Collapsed ? "<" : ">";
125    }
126    #endregion
127
128    #region Helpers
129    private void UpdateContent() {
130      if (Content == null) return;
131
132      chartControl.SuspendRendering();
133      try {
134        chartControl.Chart.Group.Clear();
135        DrawNodes(Content.Nodes);
136        ConnectNodes(Content.Nodes);
137      } finally {
138        chartControl.ResumeRendering();
139      }
140    }
141
142    private void DrawNodes(IKeyedItemCollection<string, INode> nodes) {
143      foreach (var node in nodes) {
144        var defaultPrimitive = PrimitiveAttribute.CreateDefaultPrimitive(node.GetType(), chartControl.Chart, node);
145        if (defaultPrimitive == null) continue;
146        chartControl.Chart.Group.Add(defaultPrimitive);
147        primitiveDict[node.Name] = defaultPrimitive;
148      }
149    }
150
151    private void ConnectNodes(IKeyedItemCollection<string, INode> nodes) {
152      var chart = chartControl.Chart;
153      foreach (var node in nodes) {
154        var nodeRectangle = primitiveDict[node.Name] as NodeRectangle;
155        if (nodeRectangle == null) continue;
156
157        foreach (var port in node.Ports.OfType<IConnectablePort>()) {
158          var connectedPort = port.ConnectedPort as IConnectablePort;
159          if (connectedPort == null) continue;
160
161          var connectedNode = connectedPort.Parent;
162          if (connectedNode == null) continue;
163
164          var connectedRectangle = primitiveDict[connectedNode.Name] as NodeRectangle;
165          if (connectedRectangle == null) continue;
166
167          var leftPortRectangle = (PortRectangle)nodeRectangle.GetPortPrimitive(port);
168          var rightPortRectangle = (PortRectangle)connectedRectangle.GetPortPrimitive(connectedPort);
169
170          var connectionShape = new ConnectionLine(chart, leftPortRectangle, rightPortRectangle);
171
172          chart.Group.Add(connectionShape);
173        }
174      }
175    }
176    #endregion
177  }
178}
Note: See TracBrowser for help on using the repository browser.