Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringSolutionView.cs @ 15580

Last change on this file since 15580 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.8 KB
RevLine 
[11235]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11235]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;
[11240]24using System.Linq;
[11276]25using HeuristicLab.Common;
[11235]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() {
[12721]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;
[11235]50      base.DeregisterContentEvents();
51    }
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
[12721]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;
[11235]62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == null) {
[12721]67        qualityValueView.Content = null;
68        penaltyValueView.Content = null;
69        distanceValueView.Content = null;
[11329]70        splitContainer.Panel1Collapsed = true;
[11235]71        pictureBox.Image = null;
72        tourViewHost.Content = null;
73      } else {
[12721]74        qualityValueView.Content = Content.Quality;
75        penaltyValueView.Content = Content.Penalty;
76        distanceValueView.Content = Content.Distance;
[11329]77        SetPanelCollapsing();
[11235]78        GenerateImage();
79        tourViewHost.Content = Content.IntegerVector;
80      }
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
[12721]85      qualityValueView.Enabled = Content != null;
86      penaltyValueView.Enabled = Content != null;
87      distanceValueView.Enabled = Content != null;
[11235]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;
[11240]98          var scores = Content.Scores;
[11235]99          var integerVector = Content.IntegerVector;
100          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
101
[11245]102          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
103            && (scores != null) && (coordinates.Rows == scores.Length)) {
[11235]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                }
[11311]127                bool visualizePenalty = Content.Penalty != null && Content.Penalty.Value > 0;
128                graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
[11235]129              }
[11240]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];
[11276]136                int size = scoreRange.IsAlmost(0.0)
137                  ? 6
138                  : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
[11240]139                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
140              }
[11265]141              int startingPoint = Content.StartingPoint.Value;
[11319]142              int terminalPoint = Content.TerminalPoint.Value;
[11265]143              Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
[11314]144              var beginSize = graphics.MeasureString("Begin", font);
[11325]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);
[11235]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
[11329]164    private void SetPanelCollapsing() {
[12721]165      splitContainer.Panel1Collapsed = qualityValueView.Content == null && penaltyValueView.Content == null && distanceValueView.Content == null;
[11329]166    }
167
[11235]168    private void Content_QualityChanged(object sender, EventArgs e) {
[12721]169      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_QualityChanged, sender, e);
[11329]170      else {
[12721]171        qualityValueView.Content = Content.Quality;
[11329]172        SetPanelCollapsing();
173      }
[11235]174    }
[11311]175    private void Content_PenaltyChanged(object sender, EventArgs e) {
[12721]176      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_PenaltyChanged, sender, e);
[11327]177      else {
[12721]178        penaltyValueView.Content = Content.Penalty;
[11311]179        GenerateImage();
[11329]180        SetPanelCollapsing();
[11327]181      }
[11311]182    }
[11327]183
184    private void Content_DistanceChanged(object sender, EventArgs e) {
[12721]185      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_DistanceChanged, sender, e);
[11329]186      else {
[12721]187        distanceValueView.Content = Content.Distance;
[11329]188        SetPanelCollapsing();
189      }
[11327]190    }
[12721]191
[11235]192    private void Content_CoordinatesChanged(object sender, EventArgs e) {
[12721]193      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_CoordinatesChanged, sender, e);
194      else GenerateImage();
[11235]195    }
[12721]196
[11268]197    private void Content_StartingPointChanged(object sender, EventArgs e) {
[12721]198      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_StartingPointChanged, sender, e);
199      else GenerateImage();
[11268]200    }
[12721]201
[11319]202    private void Content_TerminalPointChanged(object sender, EventArgs e) {
[12721]203      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_TerminalPointChanged, sender, e);
204      else GenerateImage();
[11268]205    }
[12721]206
[11240]207    private void Content_ScoresChanged(object sender, EventArgs e) {
[12721]208      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ScoresChanged, sender, e);
209      else GenerateImage();
[11240]210    }
[12721]211
[11235]212    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
[12721]213      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_IntegerVectorChanged, sender, e);
[11235]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.