Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • improved network visualization
File size: 4.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.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core.Networks;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Networks.Programmable;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Visualization;
33
34namespace 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 {
82              LowerLeft = new Point2D<double>(lowerLeft.X, lowerLeft.Y),
83              UpperRight = new Point2D<double>(upperRight.X, upperRight.Y)
84            };
85            network.Nodes.Add(node);
86            break;
87        }
88      } finally {
89        base.HandleOnMouseDown(sender, e);
90      }
91    }
92
93    public override void HandleOnMouseLeave(object sender, EventArgs e) {
94      chartControl.RefreshPicture();
95      base.HandleOnMouseLeave(sender, e);
96    }
97
98    #region Helpers
99    protected virtual INode CreateNode(INetwork network) {
100      if (typeSelectorDialog == null) {
101        typeSelectorDialog = new TypeSelectorDialog();
102        typeSelectorDialog.Caption = "Select Node";
103        typeSelectorDialog.TypeSelector.Caption = "Available Nodes";
104        typeSelectorDialog.TypeSelector.Configure(typeof(INode), false, true);
105      }
106
107      if (typeSelectorDialog.ShowDialog(chartControl) == DialogResult.OK) {
108        try {
109          var n = (INode)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
110          n.Name = GetUniqueName(n.Name, network);
111          return n;
112        } catch (Exception ex) {
113          ErrorHandling.ShowErrorDialog(chartControl, ex);
114        }
115      }
116      return null;
117    }
118
119    protected virtual string GetUniqueName(string originalName, INetwork network) {
120      if (!network.Nodes.ContainsKey(originalName))
121        return originalName;
122
123      string name;
124      int index = 0;
125      do {
126        index++;
127        name = originalName + index;
128      } while (network.Nodes.ContainsKey(name));
129      return name;
130    }
131    #endregion
132  }
133}
Note: See TracBrowser for help on using the repository browser.