Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • refactored 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.Drawing;
24using System.Drawing.Drawing2D;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core.Networks;
29using HeuristicLab.Networks.Programmable;
30using HeuristicLab.Visualization;
31
32namespace HeuristicLab.Networks.Views.NetworkVisualization.Views.ChartModes {
33  public class ConnectPortsChartMode : ChartMode {
34    protected PortRectangle startPortRectangle, endPortRectangle;
35
36    public override Image Image { get { return VSImageLibrary.OrgChart; } }
37    public override string Text { get { return "&Connect Ports"; } }
38    public override string ToolTipText { get { return "Connect Ports"; } }
39
40    public ConnectPortsChartMode(ChartControl control) : base(control) { }
41
42    public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
43      try {
44        if (!(chartControl.Tag is IUserDefinedNetwork)) return;
45
46        chartControl.RefreshPicture();
47
48        var worldLocation = chartControl.Chart.TransformPixelToWorld(e.Location);
49        var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>();
50        var portRectangles = nodeRectangles.SelectMany(x => x.NetworkItem.Ports.Select(x.GetPortPrimitive));
51        var currentPortRectangle = (PortRectangle)portRectangles.FirstOrDefault(x => x.ContainsPoint(worldLocation));
52
53        // TODO: highlight allowed port connections only
54
55        using (var graphics = chartControl.CreatePictureGraphics()) {
56          Point startLocation = Point.Empty;
57          if (startPortRectangle != null) {
58            DrawCircleAroundPortRectangle(graphics, startPortRectangle, startPortRectangle.NetworkItem is IConnectablePort);
59            startLocation = chartControl.Chart.TransformWorldToPixel(startPortRectangle.Point);
60          }
61
62          Point endLocation = e.Location;
63          if (currentPortRectangle != null && (startPortRectangle != null || e.Button == MouseButtons.None)) {
64            DrawCircleAroundPortRectangle(graphics, currentPortRectangle, currentPortRectangle.NetworkItem is IConnectablePort);
65            endLocation = chartControl.Chart.TransformWorldToPixel(currentPortRectangle.Point);
66          }
67
68          bool connectionValid = startPortRectangle != null && startPortRectangle.NetworkItem is IConnectablePort;
69          connectionValid &= currentPortRectangle == null || currentPortRectangle.NetworkItem is IConnectablePort;
70
71          if (startPortRectangle != null)
72            DrawLineBetweenLocations(graphics, startLocation, endLocation, connectionValid);
73        }
74      } finally {
75        chartControl.UpdatePicture();
76        base.HandleOnMouseMove(sender, e);
77      }
78    }
79
80    public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
81      try {
82        var network = chartControl.Tag as IUserDefinedNetwork;
83        if (network == null) return;
84
85        switch (e.Button) {
86          case MouseButtons.Left:
87            var worldLocation = chartControl.Chart.TransformPixelToWorld(e.Location);
88            var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>();
89            var portRectangles = nodeRectangles.SelectMany(x => x.NetworkItem.Ports.Select(x.GetPortPrimitive));
90            startPortRectangle = (PortRectangle)portRectangles.FirstOrDefault(x => x.ContainsPoint(worldLocation));
91            break;
92        }
93      } finally {
94        base.HandleOnMouseDown(sender, e);
95      }
96    }
97
98    public override void HandleOnMouseUp(object sender, MouseEventArgs e) {
99      try {
100        var network = chartControl.Tag as IUserDefinedNetwork;
101        if (network == null) return;
102
103        switch (e.Button) {
104          case MouseButtons.Left:
105            var worldLocation = chartControl.Chart.TransformPixelToWorld(e.Location);
106            var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>();
107            var portRectangles = nodeRectangles.SelectMany(x => x.NetworkItem.Ports.Select(x.GetPortPrimitive));
108            endPortRectangle = (PortRectangle)portRectangles.FirstOrDefault(x => x.ContainsPoint(worldLocation));
109
110            if (startPortRectangle == endPortRectangle || startPortRectangle == null || endPortRectangle == null) {
111              startPortRectangle = null;
112              endPortRectangle = null;
113              return;
114            }
115
116            var startPort = (IConnectablePort)startPortRectangle.NetworkItem;
117            var endPort = (IConnectablePort)endPortRectangle.NetworkItem;
118
119            // TODO: check if port connection is allowed
120
121            startPort.ConnectedPort = endPort;
122
123            startPortRectangle = null;
124            endPortRectangle = null;
125            break;
126        }
127      } finally {
128        base.HandleOnMouseUp(sender, e);
129      }
130    }
131
132    #region Helpers
133    protected virtual void DrawCircleAroundPortRectangle(Graphics graphics, PortRectangle portRectangle, bool valid) {
134      var centerP = chartControl.Chart.TransformWorldToPixel(portRectangle.Point);
135      int a = (int)Math.Round(portRectangle.Size.Width / 2.0 * Math.Sqrt(2));
136      int b = (int)Math.Round(portRectangle.Size.Height / 2.0 * Math.Sqrt(2));
137
138      using (var pen = new Pen(valid ? Color.Green : Color.Red) { Width = 2f }) {
139        graphics.DrawEllipse(pen,
140          centerP.X - a,
141          centerP.Y - b,
142          a * 2,
143          b * 2);
144      }
145    }
146
147    protected virtual void DrawLineBetweenLocations(Graphics graphics, Point startLocation, Point endLocation, bool valid) {
148      using (var pen = new Pen(valid ? Color.Green : Color.Red) { DashStyle = DashStyle.Dash, Width = 2f })
149        graphics.DrawLine(pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y);
150    }
151    #endregion
152  }
153}
Note: See TracBrowser for help on using the repository browser.