Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

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