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.Windows.Forms;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Common.Resources;
|
---|
28 | using HeuristicLab.Core.Networks;
|
---|
29 | using HeuristicLab.Core.Views;
|
---|
30 | using HeuristicLab.Networks.Programmable;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 | using HeuristicLab.Visualization;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Networks.Views.NetworkVisualization.Views {
|
---|
35 | public class AddNodesChartMode : ChartMode {
|
---|
36 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
37 |
|
---|
38 | public override Image Image { get { return VSImageLibrary.RadialChart; } }
|
---|
39 | public override string Text { get { return "Add &Nodes"; } }
|
---|
40 | public override string ToolTipText { get { return "Add Nodes"; } }
|
---|
41 |
|
---|
42 | public AddNodesChartMode(ChartControl control) : base(control) { }
|
---|
43 |
|
---|
44 | public override void HandleOnMouseMove(object sender, MouseEventArgs e) {
|
---|
45 | try {
|
---|
46 | if (!(chartControl.Tag is IUserDefinedNetwork)) return;
|
---|
47 |
|
---|
48 | chartControl.RefreshPicture();
|
---|
49 |
|
---|
50 | var size = new Size(200, 100);
|
---|
51 |
|
---|
52 | using (var graphics = chartControl.CreatePictureGraphics())
|
---|
53 | using (var pen = new Pen(Color.Gray) { DashStyle = DashStyle.Dash }) {
|
---|
54 | graphics.DrawRectangle(pen,
|
---|
55 | (int)Math.Round(e.X - size.Width * 0.5),
|
---|
56 | (int)Math.Round(e.Y - size.Height * 0.5),
|
---|
57 | size.Width,
|
---|
58 | size.Height);
|
---|
59 | }
|
---|
60 |
|
---|
61 | } finally {
|
---|
62 | chartControl.UpdatePicture();
|
---|
63 | base.HandleOnMouseMove(sender, e);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override void HandleOnMouseDown(object sender, MouseEventArgs e) {
|
---|
68 | try {
|
---|
69 | switch (e.Button) {
|
---|
70 | case MouseButtons.Left:
|
---|
71 | var network = chartControl.Tag as IUserDefinedNetwork;
|
---|
72 | if (network == null) return;
|
---|
73 |
|
---|
74 | var node = CreateNode(network);
|
---|
75 | if (node == null) return;
|
---|
76 |
|
---|
77 | var center = chartControl.Chart.TransformPixelToWorld(e.Location);
|
---|
78 | var size = chartControl.Chart.TransformPixelToWorld(new Size(100, 50));
|
---|
79 | var lowerLeft = center - new Offset(size);
|
---|
80 | var upperRight = center + new Offset(size);
|
---|
81 | node.VisualProperties = new NodeVisualProperties(new Point2D<double>(lowerLeft.X, lowerLeft.Y),
|
---|
82 | new Point2D<double>(upperRight.X, upperRight.Y));
|
---|
83 | network.Nodes.Add(node);
|
---|
84 | break;
|
---|
85 | }
|
---|
86 | } finally {
|
---|
87 | base.HandleOnMouseDown(sender, e);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public override void HandleOnMouseLeave(object sender, EventArgs e) {
|
---|
92 | chartControl.RefreshPicture();
|
---|
93 | base.HandleOnMouseLeave(sender, e);
|
---|
94 | }
|
---|
95 |
|
---|
96 | #region Helpers
|
---|
97 | protected virtual INode CreateNode(INetwork network) {
|
---|
98 | if (typeSelectorDialog == null) {
|
---|
99 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
100 | typeSelectorDialog.Caption = "Select Node";
|
---|
101 | typeSelectorDialog.TypeSelector.Caption = "Available Nodes";
|
---|
102 | typeSelectorDialog.TypeSelector.Configure(typeof(INode), false, true);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (typeSelectorDialog.ShowDialog(chartControl) == DialogResult.OK) {
|
---|
106 | try {
|
---|
107 | var n = (INode)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
108 | n.Name = GetUniqueName(n.Name, network);
|
---|
109 | return n;
|
---|
110 | } catch (Exception ex) {
|
---|
111 | ErrorHandling.ShowErrorDialog(chartControl, ex);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return null;
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected virtual string GetUniqueName(string originalName, INetwork network) {
|
---|
118 | if (!network.Nodes.ContainsKey(originalName))
|
---|
119 | return originalName;
|
---|
120 |
|
---|
121 | string name;
|
---|
122 | int index = 0;
|
---|
123 | do {
|
---|
124 | index++;
|
---|
125 | name = originalName + index;
|
---|
126 | } while (network.Nodes.ContainsKey(name));
|
---|
127 | return name;
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 | }
|
---|
131 | }
|
---|