[13077] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Drawing;
|
---|
| 24 | using System.Drawing.Drawing2D;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common.Resources;
|
---|
| 28 | using HeuristicLab.Core.Networks;
|
---|
| 29 | using HeuristicLab.Networks.Programmable;
|
---|
| 30 | using HeuristicLab.Visualization;
|
---|
| 31 |
|
---|
| 32 | namespace 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 ToolTip { get { return "Connect Ports"; } }
|
---|
| 38 |
|
---|
| 39 | public ConnectPortsChartMode(ChartControl control) : base(control) { }
|
---|
| 40 |
|
---|
| 41 | public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
|
---|
| 42 | base.HandleOnMouseDown(sender, e);
|
---|
| 43 |
|
---|
| 44 | if (!(chartControl.Tag is IUserDefinedNetwork)) return;
|
---|
| 45 |
|
---|
| 46 | switch (e.Button) {
|
---|
| 47 | case MouseButtons.Left:
|
---|
| 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.GetPortRectangle));
|
---|
| 51 | startPortRectangle = portRectangles.FirstOrDefault(x => x.ContainsPoint(worldLocation));
|
---|
| 52 | break;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
|
---|
| 57 | base.HandleOnMouseMove(sender, e);
|
---|
| 58 |
|
---|
| 59 | if (!(chartControl.Tag is IUserDefinedNetwork)) return;
|
---|
| 60 |
|
---|
| 61 | try {
|
---|
| 62 | chartControl.PictureBox.Refresh();
|
---|
| 63 |
|
---|
| 64 | var worldLocation = chartControl.Chart.TransformPixelToWorld(e.Location);
|
---|
| 65 | var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>();
|
---|
| 66 | var portRectangles = nodeRectangles.SelectMany(x => x.NetworkItem.Ports.Select(x.GetPortRectangle)).ToList();
|
---|
| 67 | var currentPortRectangle = portRectangles.FirstOrDefault(x => x.ContainsPoint(worldLocation));
|
---|
| 68 |
|
---|
| 69 | // TODO: highlight allowed port connections only
|
---|
| 70 |
|
---|
| 71 | Point startLocation = Point.Empty;
|
---|
| 72 | if (startPortRectangle != null) {
|
---|
| 73 | DrawCircleAroundPortRectangle(startPortRectangle);
|
---|
| 74 | startLocation = chartControl.Chart.TransformWorldToPixel(startPortRectangle.Point);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | Point endLocation = e.Location;
|
---|
| 78 | if (currentPortRectangle != null) {
|
---|
| 79 | DrawCircleAroundPortRectangle(currentPortRectangle);
|
---|
| 80 | endLocation = chartControl.Chart.TransformWorldToPixel(currentPortRectangle.Point);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | if (startPortRectangle != null)
|
---|
| 84 | DrawLineBetweenLocations(startLocation, endLocation);
|
---|
| 85 | } finally {
|
---|
| 86 | chartControl.PictureBox.Update();
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public override void HandleOnMouseUp(object sender, MouseEventArgs e) {
|
---|
| 91 | base.HandleOnMouseUp(sender, e);
|
---|
| 92 |
|
---|
| 93 | if (!(chartControl.Tag is IUserDefinedNetwork)) return;
|
---|
| 94 |
|
---|
| 95 | switch (e.Button) {
|
---|
| 96 | case MouseButtons.Left:
|
---|
| 97 | var worldLocation = chartControl.Chart.TransformPixelToWorld(e.Location);
|
---|
| 98 | var nodeRectangles = chartControl.Chart.Group.Primitives.OfType<NodeRectangle>();
|
---|
| 99 | var portRectangles = nodeRectangles.SelectMany(x => x.NetworkItem.Ports.Select(x.GetPortRectangle)).ToList();
|
---|
| 100 | endPortRectangle = portRectangles.FirstOrDefault(x => x.ContainsPoint(worldLocation));
|
---|
| 101 |
|
---|
| 102 | if (startPortRectangle == endPortRectangle && startPortRectangle == null || endPortRectangle == null) {
|
---|
| 103 | startPortRectangle = null;
|
---|
| 104 | endPortRectangle = null;
|
---|
| 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | var startPort = (IConnectablePort)startPortRectangle.NetworkItem;
|
---|
| 109 | var endPort = (IConnectablePort)endPortRectangle.NetworkItem;
|
---|
| 110 |
|
---|
| 111 | // TODO: check if port connection is allowed
|
---|
| 112 |
|
---|
| 113 | startPort.ConnectedPort = endPort;
|
---|
| 114 |
|
---|
| 115 | startPortRectangle = null;
|
---|
| 116 | endPortRectangle = null;
|
---|
| 117 | break;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | protected virtual void DrawCircleAroundPortRectangle(PortRectangle portRectangle) {
|
---|
| 122 | var centerP = chartControl.Chart.TransformWorldToPixel(portRectangle.Point);
|
---|
| 123 | int a = (int)Math.Round(portRectangle.Size.Width / 2.0 * Math.Sqrt(2));
|
---|
| 124 | int b = (int)Math.Round(portRectangle.Size.Height / 2.0 * Math.Sqrt(2));
|
---|
| 125 |
|
---|
| 126 | using (var graphics = chartControl.PictureBox.CreateGraphics())
|
---|
| 127 | using (var pen = new Pen(Color.Green) { Width = 2f }) {
|
---|
| 128 | graphics.DrawEllipse(pen,
|
---|
| 129 | centerP.X - a,
|
---|
| 130 | centerP.Y - b,
|
---|
| 131 | a * 2,
|
---|
| 132 | b * 2);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | protected virtual void DrawLineBetweenLocations(Point startLocation, Point endLocation) {
|
---|
| 137 | using (var graphics = chartControl.PictureBox.CreateGraphics())
|
---|
| 138 | using (var pen = new Pen(Color.Green) { DashStyle = DashStyle.Dash, Width = 2f })
|
---|
| 139 | graphics.DrawLine(pen, startLocation.X, startLocation.Y, endLocation.X, endLocation.Y);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|