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