#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Drawing; using System.Linq; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.Problems.Orienteering.Views { [View("OrienteeringSolution View")] [Content(typeof(OrienteeringSolution), true)] public partial class OrienteeringSolutionView : ItemView { public new OrienteeringSolution Content { get { return (OrienteeringSolution)base.Content; } set { base.Content = value; } } public OrienteeringSolutionView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.QualityChanged -= new EventHandler(Content_QualityChanged); Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged); Content.ScoresChanged -= new EventHandler(Content_ScoresChanged); Content.IntegerVectorChanged -= new EventHandler(Content_IntegerVectorChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.QualityChanged += new EventHandler(Content_QualityChanged); Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged); Content.ScoresChanged += new EventHandler(Content_ScoresChanged); Content.IntegerVectorChanged += new EventHandler(Content_IntegerVectorChanged); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { qualityViewHost.Content = null; pictureBox.Image = null; tourViewHost.Content = null; } else { qualityViewHost.Content = Content.Quality; GenerateImage(); tourViewHost.Content = Content.IntegerVector; } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); qualityGroupBox.Enabled = Content != null; pictureBox.Enabled = Content != null; tourGroupBox.Enabled = Content != null; } private void GenerateImage() { if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) { if (Content == null) { pictureBox.Image = null; } else { var coordinates = Content.Coordinates; var scores = Content.Scores; var integerVector = Content.IntegerVector; var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2) && (scores != null) && (coordinates.Rows == scores.Length)) { double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue; for (int i = 0; i < coordinates.Rows; i++) { if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0]; if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1]; if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0]; if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1]; } int border = 20; double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1; double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1; Point[] points = new Point[coordinates.Rows]; for (int i = 0; i < coordinates.Rows; i++) points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)), bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep)))); using (Graphics graphics = Graphics.FromImage(bitmap)) { if (integerVector != null && integerVector.Length > 1) { Point[] tour = new Point[integerVector.Length]; for (int i = 0; i < integerVector.Length; i++) { tour[i] = points[integerVector[i]]; } graphics.DrawPolygon(Pens.Black, tour); } double scoreMin = scores.Min(); double scoreMax = scores.Max(); double scoreRange = scoreMax - scoreMin; for (int i = 0; i < points.Length; i++) { double score = scores[i]; int size = (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2); graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size); } } } else { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.White); Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular); string text = "No coordinates defined or in wrong format."; SizeF strSize = graphics.MeasureString(text, font); graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f); } } pictureBox.Image = bitmap; } } } private void Content_QualityChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_QualityChanged), sender, e); else qualityViewHost.Content = Content.Quality; } private void Content_CoordinatesChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_CoordinatesChanged), sender, e); else GenerateImage(); } private void Content_ScoresChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ScoresChanged), sender, e); else GenerateImage(); } private void Content_IntegerVectorChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e); else { GenerateImage(); tourViewHost.Content = Content.IntegerVector; } } private void pictureBox_SizeChanged(object sender, EventArgs e) { GenerateImage(); } } }