Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization.Views/3.3/ChartModes/AddNodesChartMode.cs @ 13077

Last change on this file since 13077 was 13077, checked in by jkarder, 9 years ago

#2205: worked on optimization networks

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