Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Routing.TSP/3.3/TSPTourView.cs @ 2830

Last change on this file since 2830 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Permutation;
32using HeuristicLab.Charting;
33using HeuristicLab.Core.Views;
34using HeuristicLab.MainForm;
35
36namespace HeuristicLab.Routing.TSP {
37  /// <summary>
38  /// Class for the visual representation of a <see cref="TSPTour"/>.
39  /// </summary>
40  [Content(typeof(TSPTour), true)]
41  public partial class TSPTourView : ItemViewBase {
42    /// <summary>
43    /// Gets or sets the <see cref="TSPTour"/> to represent visually.
44    /// </summary>
45    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
46    /// No own data storage present.</remarks>
47    public TSPTour TSPTour {
48      get { return (TSPTour)base.Item; }
49      set { base.Item = value; }
50    }
51
52
53    /// <summary>
54    /// Initializes a new instance of <see cref="TSPTourView"/> with caption "TSP Tour View".
55    /// </summary>
56    public TSPTourView() {
57      InitializeComponent();
58      Caption = "TSP Tour View";
59    }
60    /// <summary>
61    /// Initializes a new instance of <see cref="TSPTourView"/> with the given <paramref name="tspTour"/>.
62    /// </summary>
63    /// <param name="tspTour">The tour to display.</param>
64    public TSPTourView(TSPTour tspTour)
65      : this() {
66      TSPTour = tspTour;
67    }
68
69
70    /// <summary>
71    /// Removes all event handlers from the underlying <see cref="TSPTour"/>.
72    /// </summary>
73    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
74    protected override void RemoveItemEvents() {
75      TSPTour.CoordinatesChanged -= new EventHandler(TSPTour_CoordinatesChanged);
76      TSPTour.TourChanged -= new EventHandler(TSPTour_TourChanged);
77      base.RemoveItemEvents();
78    }
79    /// <summary>
80    /// Adds event handlers to the underlying <see cref="TSPTour"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
83    protected override void AddItemEvents() {
84      base.AddItemEvents();
85      TSPTour.CoordinatesChanged += new EventHandler(TSPTour_CoordinatesChanged);
86      TSPTour.TourChanged += new EventHandler(TSPTour_TourChanged);
87    }
88
89    /// <summary>
90    /// Updates all controls with the latest data of the model.
91    /// </summary>
92    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
93    protected override void UpdateControls() {
94      base.UpdateControls();
95      Chart chart = new Chart(0, 0, 10, 10);
96      chartControl.ScaleOnResize = false;
97
98      if ((TSPTour.Coordinates != null) && (TSPTour.Tour != null)) {
99        chart.UpdateEnabled = false;
100
101        double[,] coords = TSPTour.Coordinates.Data;
102        int[] tour = TSPTour.Tour.Data;
103
104        for (int i = 0; i < tour.Length - 1; i++)
105          chart.Group.Add(new Line(chart, coords[tour[i], 0], coords[tour[i], 1],
106                                          coords[tour[i + 1], 0], coords[tour[i + 1], 1], Pens.Black));
107        chart.Group.Add(new Line(chart, coords[tour[tour.Length - 1], 0], coords[tour[tour.Length - 1], 1],
108                                        coords[tour[0], 0], coords[tour[0], 1], Pens.Black));
109
110        for (int i = 0; i < coords.GetLength(0); i++) {
111          FixedSizeCircle circle = new FixedSizeCircle(chart, coords[i, 0], coords[i, 1], 8, Pens.Black, Brushes.Blue);
112          circle.ToolTipText = "(" + coords[i, 0].ToString() + " ; " + coords[i, 1].ToString() + ")";
113          chart.Group.Add(circle);
114        }
115
116        if (coords.GetLength(0) > 0) {
117          PointD min = new PointD(coords[0, 0], coords[0, 1]);
118          PointD max = new PointD(coords[0, 0], coords[0, 1]);
119          for (int i = 0; i < coords.GetLength(0); i++) {
120            if (coords[i, 0] < min.X) min.X = coords[i, 0];
121            if (coords[i, 1] < min.Y) min.Y = coords[i, 1];
122            if (coords[i, 0] > max.X) max.X = coords[i, 0];
123            if (coords[i, 1] > max.Y) max.Y = coords[i, 1];
124          }
125          Offset offset = new Offset((max.X - min.X) / 20, (max.Y - min.Y) / 20);
126          min = min - offset;
127          max = max + offset;
128          chart.SetPosition(min, max);
129        }
130        chart.UpdateEnabled = true;
131      }
132      chartControl.Chart = chart;
133    }
134
135    #region TSPTour Events
136    private void TSPTour_TourChanged(object sender, EventArgs e) {
137      Refresh();
138    }
139    private void TSPTour_CoordinatesChanged(object sender, EventArgs e) {
140      Refresh();
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.