Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • improved network visualization
File size: 7.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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Networks.Programmable;
28using HeuristicLab.Visualization;
29
30namespace HeuristicLab.Networks.Views.NetworkVisualization.Views {
31  public class SelectChartMode : Visualization.SelectChartMode {
32    protected PortRectangle selectedPortRectangle;
33    protected NodeRectangle constrainingNodeRectangle;
34
35    public SelectChartMode(ChartControl control) : base(control) { }
36
37    protected override void OnDeselected() {
38      try {
39        chartControl.SuspendRendering();
40        if (selectedPortRectangle != null) {
41          selectedPortRectangle.Selected = false;
42          selectedPortRectangle = null;
43          constrainingNodeRectangle = null;
44        }
45      } finally {
46        chartControl.ResumeRendering();
47        base.OnDeselected();
48      }
49    }
50
51    public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
52      try {
53        switch (e.Button) {
54          case MouseButtons.Left:
55            chartControl.SuspendRendering();
56
57            if (selectedPortRectangle != null) {
58              selectedPortRectangle.Selected = false;
59              selectedPortRectangle = null;
60              constrainingNodeRectangle = null;
61            }
62
63            var worldLocation = chartControl.Chart.TransformPixelToWorld(e.Location);
64            var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>();
65            foreach (var nodeRectangle in nodeRectangles) {
66              var node = nodeRectangle.NetworkItem;
67              var portRectangles = node.Ports.Select(nodeRectangle.GetPortPrimitive).OfType<PortRectangle>();
68              foreach (var portRectangle in portRectangles) {
69                portRectangle.Selected = selectedPortRectangle == null && portRectangle.ContainsPoint(worldLocation);
70                if (portRectangle.Selected) {
71                  selectedPortRectangle = portRectangle;
72                  constrainingNodeRectangle = nodeRectangle;
73                  break;
74                }
75              }
76            }
77
78            if (selectedPortRectangle != null)
79              foreach (var sp in chartControl.Chart.Group.SelectedPrimitives) {
80                sp.Selected = false;
81                OnSelectedPrimitivesChanged();
82              } else base.HandleOnMouseDown(sender, e);
83            break;
84        }
85      } finally {
86        chartControl.ResumeRendering();
87      }
88    }
89
90    public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
91      if (selectedPortRectangle != null) {
92        switch (e.Button) {
93          case MouseButtons.Left:
94            var point = chartControl.Chart.TransformPixelToWorld(e.Location);
95            var lowerLeft = constrainingNodeRectangle.LowerLeft;
96            var upperRight = constrainingNodeRectangle.UpperRight;
97
98            if (point.X < lowerLeft.X) point.X = lowerLeft.X;
99            else if (point.X > upperRight.X) point.X = upperRight.X;
100            if (point.Y < lowerLeft.Y) point.Y = lowerLeft.Y;
101            else if (point.Y > upperRight.Y) point.Y = upperRight.Y;
102
103            double dS = lowerLeft.Y - point.Y;
104            double dW = lowerLeft.X - point.X;
105
106            double dX = upperRight.X - point.X; // dE
107            if (Math.Abs(dX) > Math.Abs(dW)) dX = dW;
108            double dY = upperRight.Y - point.Y; // dN
109            if (Math.Abs(dY) > Math.Abs(dS)) dY = dS;
110
111            var offset = Offset.Empty;
112            if (Math.Abs(dX) < Math.Abs(dY)) offset.DX = dX;
113            else offset.DY = dY;
114
115            point = point + offset;
116            offset = point - selectedPortRectangle.Point;
117            try {
118              chartControl.SuspendRendering();
119              selectedPortRectangle.Move(offset);
120            } finally { chartControl.ResumeRendering(); }
121            break;
122        }
123      } else
124        base.HandleOnMouseMove(sender, e);
125      previousLocation = e.Location;
126    }
127
128    public override void HandleOnMouseDoubleClick(object sender, MouseEventArgs e) {
129      try {
130        switch (e.Button) {
131          case MouseButtons.Left:
132            var nodeRectangle = chartControl.Chart.GetPrimitive(e.Location) as NodeRectangle;
133            if (nodeRectangle != null)
134              MainForm.MainFormManager.MainForm.ShowContent(nodeRectangle.NetworkItem);
135            break;
136        }
137      } finally {
138        base.HandleOnMouseDoubleClick(sender, e);
139      }
140    }
141
142    protected override void DeleteSelectedPrimitives() {
143      if (!(chartControl.Tag is IUserDefinedNetwork)) return;
144
145      try {
146        chartControl.SuspendRendering();
147
148        var connectionLines = chartControl.Chart.Group.Primitives.OfType<ConnectionLine>().ToList();
149        var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>().ToList();
150        var portRectangles = nodeRectangles.SelectMany(x => x.NetworkItem.Ports.Select(x.GetPortPrimitive)).OfType<PortRectangle>().ToList();
151
152        var deletedNodes = new HashSet<INode>();
153        var deletedPorts = new HashSet<IMessagePort>();
154
155        foreach (var nodeRectangle in nodeRectangles) {
156          if (nodeRectangle.Selected) {
157            var node = nodeRectangle.NetworkItem;
158            if (node == null) continue;
159            deletedNodes.Add(node);
160            foreach (var port in node.Ports.OfType<IMessagePort>())
161              deletedPorts.Add(port);
162          }
163        }
164
165        foreach (var portRectangle in portRectangles) {
166          if (portRectangle.Selected) {
167            var port = portRectangle.NetworkItem as IMessagePort;
168            if (port == null) continue;
169            port.Parent.Ports.Remove(port);
170            deletedPorts.Add(port);
171          }
172        }
173
174        selectedPortRectangle = null;
175
176        foreach (var connectionLine in connectionLines) {
177          var startPortRectangle = connectionLine.StartPortRectangle;
178          var endPortRectangle = connectionLine.EndPortRectangle;
179          if (startPortRectangle == null || endPortRectangle == null) continue;
180
181          var startPort = startPortRectangle.NetworkItem as IMessagePort;
182          var endPort = endPortRectangle.NetworkItem as IMessagePort;
183          if (startPort == null || endPort == null) continue;
184
185          if (connectionLine.Selected || deletedPorts.Contains(startPort) || deletedPorts.Contains(endPort))
186            startPort.ConnectedPort = null;
187        }
188
189        foreach (var node in deletedNodes)
190          node.Parent.Nodes.Remove(node);
191      } finally { chartControl.ResumeRendering(); }
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.