Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringSolutionView.cs @ 15211

Last change on this file since 15211 was 12721, checked in by abeham, 9 years ago

#2208:

  • Added missing license headers
  • Updates copyright year
  • Renamed analyzer (us spelling)
  • Removed script
  • Implemented samples unit test
  • Changed solution view to use horizontal splitting, removed viewhosts
  • Updated instance provider to use .NET45 zip compression
  • Restructuring and reformatting
File size: 9.8 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.Orienteering.Views {
30  [View("OrienteeringSolution View")]
31  [Content(typeof(OrienteeringSolution), true)]
32  public partial class OrienteeringSolutionView : ItemView {
33    public new OrienteeringSolution Content {
34      get { return (OrienteeringSolution)base.Content; }
35      set { base.Content = value; }
36    }
37    public OrienteeringSolutionView() {
38      InitializeComponent();
39    }
40
41    protected override void DeregisterContentEvents() {
42      Content.QualityChanged -= Content_QualityChanged;
43      Content.PenaltyChanged -= Content_PenaltyChanged;
44      Content.DistanceChanged -= Content_DistanceChanged;
45      Content.CoordinatesChanged -= Content_CoordinatesChanged;
46      Content.StartingPointChanged -= Content_StartingPointChanged;
47      Content.TerminalPointChanged -= Content_TerminalPointChanged;
48      Content.ScoresChanged -= Content_ScoresChanged;
49      Content.IntegerVectorChanged -= Content_IntegerVectorChanged;
50      base.DeregisterContentEvents();
51    }
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
54      Content.QualityChanged += Content_QualityChanged;
55      Content.PenaltyChanged += Content_PenaltyChanged;
56      Content.DistanceChanged += Content_DistanceChanged;
57      Content.CoordinatesChanged += Content_CoordinatesChanged;
58      Content.StartingPointChanged += Content_StartingPointChanged;
59      Content.TerminalPointChanged += Content_TerminalPointChanged;
60      Content.ScoresChanged += Content_ScoresChanged;
61      Content.IntegerVectorChanged += Content_IntegerVectorChanged;
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == null) {
67        qualityValueView.Content = null;
68        penaltyValueView.Content = null;
69        distanceValueView.Content = null;
70        splitContainer.Panel1Collapsed = true;
71        pictureBox.Image = null;
72        tourViewHost.Content = null;
73      } else {
74        qualityValueView.Content = Content.Quality;
75        penaltyValueView.Content = Content.Penalty;
76        distanceValueView.Content = Content.Distance;
77        SetPanelCollapsing();
78        GenerateImage();
79        tourViewHost.Content = Content.IntegerVector;
80      }
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      qualityValueView.Enabled = Content != null;
86      penaltyValueView.Enabled = Content != null;
87      distanceValueView.Enabled = Content != null;
88      pictureBox.Enabled = Content != null;
89      tourGroupBox.Enabled = Content != null;
90    }
91
92    private void GenerateImage() {
93      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
94        if (Content == null) {
95          pictureBox.Image = null;
96        } else {
97          var coordinates = Content.Coordinates;
98          var scores = Content.Scores;
99          var integerVector = Content.IntegerVector;
100          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
101
102          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
103            && (scores != null) && (coordinates.Rows == scores.Length)) {
104            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
105            for (int i = 0; i < coordinates.Rows; i++) {
106              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
107              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
108              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
109              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
110            }
111
112            int border = 20;
113            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
114            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
115
116            Point[] points = new Point[coordinates.Rows];
117            for (int i = 0; i < coordinates.Rows; i++)
118              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
119                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
120
121            using (Graphics graphics = Graphics.FromImage(bitmap)) {
122              if (integerVector != null && integerVector.Length > 1) {
123                Point[] tour = new Point[integerVector.Length];
124                for (int i = 0; i < integerVector.Length; i++) {
125                  tour[i] = points[integerVector[i]];
126                }
127                bool visualizePenalty = Content.Penalty != null && Content.Penalty.Value > 0;
128                graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
129              }
130
131              double scoreMin = scores.Min();
132              double scoreMax = scores.Max();
133              double scoreRange = scoreMax - scoreMin;
134              for (int i = 0; i < points.Length; i++) {
135                double score = scores[i];
136                int size = scoreRange.IsAlmost(0.0)
137                  ? 6
138                  : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
139                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
140              }
141              int startingPoint = Content.StartingPoint.Value;
142              int terminalPoint = Content.TerminalPoint.Value;
143              Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
144              var beginSize = graphics.MeasureString("Begin", font);
145              if (startingPoint >= 0 && startingPoint < points.Length)
146                graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X - beginSize.Width, points[startingPoint].Y - beginSize.Height);
147              if (terminalPoint >= 0 && terminalPoint < points.Length)
148                graphics.DrawString("End", font, Brushes.Black, points[terminalPoint].X, points[terminalPoint].Y);
149            }
150          } else {
151            using (Graphics graphics = Graphics.FromImage(bitmap)) {
152              graphics.Clear(Color.White);
153              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
154              string text = "No coordinates defined or in wrong format.";
155              SizeF strSize = graphics.MeasureString(text, font);
156              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
157            }
158          }
159          pictureBox.Image = bitmap;
160        }
161      }
162    }
163
164    private void SetPanelCollapsing() {
165      splitContainer.Panel1Collapsed = qualityValueView.Content == null && penaltyValueView.Content == null && distanceValueView.Content == null;
166    }
167
168    private void Content_QualityChanged(object sender, EventArgs e) {
169      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_QualityChanged, sender, e);
170      else {
171        qualityValueView.Content = Content.Quality;
172        SetPanelCollapsing();
173      }
174    }
175    private void Content_PenaltyChanged(object sender, EventArgs e) {
176      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_PenaltyChanged, sender, e);
177      else {
178        penaltyValueView.Content = Content.Penalty;
179        GenerateImage();
180        SetPanelCollapsing();
181      }
182    }
183
184    private void Content_DistanceChanged(object sender, EventArgs e) {
185      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_DistanceChanged, sender, e);
186      else {
187        distanceValueView.Content = Content.Distance;
188        SetPanelCollapsing();
189      }
190    }
191
192    private void Content_CoordinatesChanged(object sender, EventArgs e) {
193      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_CoordinatesChanged, sender, e);
194      else GenerateImage();
195    }
196
197    private void Content_StartingPointChanged(object sender, EventArgs e) {
198      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_StartingPointChanged, sender, e);
199      else GenerateImage();
200    }
201
202    private void Content_TerminalPointChanged(object sender, EventArgs e) {
203      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_TerminalPointChanged, sender, e);
204      else GenerateImage();
205    }
206
207    private void Content_ScoresChanged(object sender, EventArgs e) {
208      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ScoresChanged, sender, e);
209      else GenerateImage();
210    }
211
212    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
213      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_IntegerVectorChanged, sender, e);
214      else {
215        GenerateImage();
216        tourViewHost.Content = Content.IntegerVector;
217      }
218    }
219
220    private void pictureBox_SizeChanged(object sender, EventArgs e) {
221      GenerateImage();
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.