Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Problems.TravelingSalesman.Views/3.3/PathTSPTourView.cs @ 3580

Last change on this file since 3580 was 3566, checked in by mkommend, 15 years ago

removed ctors with contents in all views (ticket #972)

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Problems.TravelingSalesman.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("PathTSPTour View")]
35  [Content(typeof(PathTSPTour), true)]
36  public sealed partial class PathTSPTourView : ItemView {
37    public new PathTSPTour Content {
38      get { return (PathTSPTour)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ItemBaseView"/>.
44    /// </summary>
45    public PathTSPTourView() {
46      InitializeComponent();
47    }
48
49    protected override void DeregisterContentEvents() {
50      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
51      Content.PermutationChanged -= new EventHandler(Content_PermutationChanged);
52      base.DeregisterContentEvents();
53    }
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
57      Content.PermutationChanged += new EventHandler(Content_PermutationChanged);
58    }
59
60    protected override void OnContentChanged() {
61      base.OnContentChanged();
62      if (Content == null) {
63        pictureBox.Image = null;
64      } else {
65        GenerateImage();
66      }
67      SetEnabledStateOfControls();
68    }
69
70    protected override void OnReadOnlyChanged() {
71      base.OnReadOnlyChanged();
72      SetEnabledStateOfControls();
73    }
74
75    private void SetEnabledStateOfControls() {
76      pictureBox.Enabled = Content != null;
77    }
78
79    private void GenerateImage() {
80      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
81        if (Content == null) {
82          pictureBox.Image = null;
83        } else {
84          DoubleMatrix coordinates = Content.Coordinates;
85          Permutation permutation = Content.Permutation;
86          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
87
88          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
89            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
90            for (int i = 0; i < coordinates.Rows; i++) {
91              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
92              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
93              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
94              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
95            }
96
97            int border = 20;
98            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
99            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
100
101            Point[] points = new Point[coordinates.Rows];
102            for (int i = 0; i < coordinates.Rows; i++)
103              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
104                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
105
106            using (Graphics graphics = Graphics.FromImage(bitmap)) {
107              if ((permutation != null) && (permutation.Length == coordinates.Rows) && (permutation.Validate())) {
108                Point[] tour = new Point[permutation.Length];
109                for (int i = 0; i < permutation.Length; i++) {
110                  tour[i] = points[permutation[i]];
111                }
112                graphics.DrawPolygon(Pens.Black, tour);
113              }
114              for (int i = 0; i < points.Length; i++)
115                graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
116            }
117          }
118          pictureBox.Image = bitmap;
119        }
120      }
121    }
122
123    private void Content_CoordinatesChanged(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
126      else
127        GenerateImage();
128    }
129    private void Content_PermutationChanged(object sender, EventArgs e) {
130      if (InvokeRequired)
131        Invoke(new EventHandler(Content_PermutationChanged), sender, e);
132      else
133        GenerateImage();
134    }
135
136    private void pictureBox_SizeChanged(object sender, EventArgs e) {
137      GenerateImage();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.