Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11329 was 11329, checked in by pfleck, 10 years ago

#2208 Hide Quality Penalty and Distance values in OrienteeringSolutionView when no solution is present.

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