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

Last change on this file since 11268 was 11268, checked in by pfleck, 6 years ago

#2208 added additional missing value changed event handlers

File size: 8.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.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Problems.Orienteering.Views {
29
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 -= new EventHandler(Content_QualityChanged);
43      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
44      Content.StartingPointChanged -= new EventHandler(Content_StartingPointChanged);
45      Content.TerminusPointChanged -= new EventHandler(Content_TerminusPointChanged);
46      Content.ScoresChanged -= new EventHandler(Content_ScoresChanged);
47      Content.IntegerVectorChanged -= new EventHandler(Content_IntegerVectorChanged);
48      base.DeregisterContentEvents();
49    }
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      Content.QualityChanged += new EventHandler(Content_QualityChanged);
53      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
54      Content.StartingPointChanged += new EventHandler(Content_StartingPointChanged);
55      Content.TerminusPointChanged += new EventHandler(Content_TerminusPointChanged);
56      Content.ScoresChanged += new EventHandler(Content_ScoresChanged);
57      Content.IntegerVectorChanged += new EventHandler(Content_IntegerVectorChanged);
58    }
59
60    protected override void OnContentChanged() {
61      base.OnContentChanged();
62      if (Content == null) {
63        qualityViewHost.Content = null;
64        pictureBox.Image = null;
65        tourViewHost.Content = null;
66      } else {
67        qualityViewHost.Content = Content.Quality;
68        GenerateImage();
69        tourViewHost.Content = Content.IntegerVector;
70      }
71    }
72
73    protected override void SetEnabledStateOfControls() {
74      base.SetEnabledStateOfControls();
75      qualityGroupBox.Enabled = Content != null;
76      pictureBox.Enabled = Content != null;
77      tourGroupBox.Enabled = Content != null;
78    }
79
80    private void GenerateImage() {
81      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
82        if (Content == null) {
83          pictureBox.Image = null;
84        } else {
85          var coordinates = Content.Coordinates;
86          var scores = Content.Scores;
87          var integerVector = Content.IntegerVector;
88          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
89
90          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
91            && (scores != null) && (coordinates.Rows == scores.Length)) {
92            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
93            for (int i = 0; i < coordinates.Rows; i++) {
94              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
95              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
96              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
97              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
98            }
99
100            int border = 20;
101            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
102            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
103
104            Point[] points = new Point[coordinates.Rows];
105            for (int i = 0; i < coordinates.Rows; i++)
106              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
107                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
108
109            using (Graphics graphics = Graphics.FromImage(bitmap)) {
110              if (integerVector != null && integerVector.Length > 1) {
111                Point[] tour = new Point[integerVector.Length];
112                for (int i = 0; i < integerVector.Length; i++) {
113                  tour[i] = points[integerVector[i]];
114                }
115                graphics.DrawLines(Pens.Black, tour);
116              }
117
118              double scoreMin = scores.Min();
119              double scoreMax = scores.Max();
120              double scoreRange = scoreMax - scoreMin;
121              for (int i = 0; i < points.Length; i++) {
122                double score = scores[i];
123                int size = (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
124                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
125              }
126
127              int startingPoint = Content.StartingPoint.Value;
128              int terminusPoint = Content.TerminusPoint.Value;
129              Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
130              graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X, points[startingPoint].Y);
131              graphics.DrawString("End", font, Brushes.Black, points[terminusPoint].X, points[terminusPoint].Y);
132            }
133          } else {
134            using (Graphics graphics = Graphics.FromImage(bitmap)) {
135              graphics.Clear(Color.White);
136              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
137              string text = "No coordinates defined or in wrong format.";
138              SizeF strSize = graphics.MeasureString(text, font);
139              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
140            }
141          }
142          pictureBox.Image = bitmap;
143        }
144      }
145    }
146
147    private void Content_QualityChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_QualityChanged), sender, e);
150      else
151        qualityViewHost.Content = Content.Quality;
152    }
153    private void Content_CoordinatesChanged(object sender, EventArgs e) {
154      if (InvokeRequired)
155        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
156      else
157        GenerateImage();
158    }
159    private void Content_StartingPointChanged(object sender, EventArgs e) {
160      if (InvokeRequired)
161        Invoke(new EventHandler(Content_StartingPointChanged), sender, e);
162      else
163        GenerateImage();
164    }
165    private void Content_TerminusPointChanged(object sender, EventArgs e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler(Content_TerminusPointChanged), sender, e);
168      else
169        GenerateImage();
170    }
171    private void Content_ScoresChanged(object sender, EventArgs e) {
172      if (InvokeRequired)
173        Invoke(new EventHandler(Content_ScoresChanged), sender, e);
174      else
175        GenerateImage();
176    }
177    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
178      if (InvokeRequired)
179        Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
180      else {
181        GenerateImage();
182        tourViewHost.Content = Content.IntegerVector;
183      }
184    }
185
186    private void pictureBox_SizeChanged(object sender, EventArgs e) {
187      GenerateImage();
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.