Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/MatrixTSPDataView.cs @ 17248

Last change on this file since 17248 was 17248, checked in by abeham, 4 years ago

#2521: working on TSP refactoring

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Problems.TravelingSalesman.Views {
33  [View("Matrix TSP Data View")]
34  [Content(typeof(MatrixTSPData), IsDefaultView = true)]
35  public partial class MatrixTSPDataView : ItemView {
36
37    public new MatrixTSPData Content {
38      get { return (MatrixTSPData)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public MatrixTSPDataView() {
43      InitializeComponent();
44      setMatrixButton.Text = string.Empty;
45      setMatrixButton.Image = VSImageLibrary.Edit;
46      setCoordinatesButton.Text = string.Empty;
47      setCoordinatesButton.Image = VSImageLibrary.Edit;
48    }
49
50    #region Content Events
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.PropertyChanged += ContentOnPropertyChanged;
54      if (Content.DisplayCoordinates != null) {
55        Content.DisplayCoordinates.Reset += ContentDisplayCoordinatesOnChanged;
56        Content.DisplayCoordinates.ItemChanged += ContentDisplayCoordinatesOnChanged;
57      }
58    }
59
60    protected override void DeregisterContentEvents() {
61      base.DeregisterContentEvents();
62      Content.PropertyChanged -= ContentOnPropertyChanged;
63      if (Content.DisplayCoordinates != null) {
64        Content.DisplayCoordinates.Reset -= ContentDisplayCoordinatesOnChanged;
65        Content.DisplayCoordinates.ItemChanged -= ContentDisplayCoordinatesOnChanged;
66      }
67    }
68
69    private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
70      switch (e.PropertyName) {
71        case nameof(Content.Matrix):
72          distanceMatrixView.Content = Content.Matrix;
73          break;
74        case nameof(Content.DisplayCoordinates):
75          if (coordinatesMatrixView.Content != null) {
76            coordinatesMatrixView.Content.Reset -= ContentDisplayCoordinatesOnChanged;
77            coordinatesMatrixView.Content.ItemChanged -= ContentDisplayCoordinatesOnChanged;
78          }
79          if (Content.DisplayCoordinates != null) {
80            Content.DisplayCoordinates.Reset += ContentDisplayCoordinatesOnChanged;
81            Content.DisplayCoordinates.ItemChanged += ContentDisplayCoordinatesOnChanged;
82          }
83          coordinatesMatrixView.Content = Content.DisplayCoordinates;
84          GenerateImage();
85          break;
86      }
87    }
88
89    private void ContentDisplayCoordinatesOnChanged(object sender, EventArgs e) {
90      GenerateImage();
91    }
92    #endregion
93
94    protected override void OnContentChanged() {
95      base.OnContentChanged();
96      if (Content == null) {
97        distanceMatrixView.Content = null;
98        coordinatesMatrixView.Content = null;
99        coordinatesPictureBox.Image = null;
100      } else {
101        distanceMatrixView.Content = Content.Matrix;
102        coordinatesMatrixView.Content = Content.DisplayCoordinates;
103        GenerateImage();
104      }
105    }
106
107    protected override void SetEnabledStateOfControls() {
108      base.SetEnabledStateOfControls();
109      setMatrixButton.Enabled = !ReadOnly && !Locked && Content != null;
110      setCoordinatesButton.Enabled = !ReadOnly && !Locked && Content != null;
111      distanceMatrixView.Enabled = !ReadOnly && !Locked && Content != null;
112      coordinatesMatrixView.Enabled = !ReadOnly && !Locked && Content != null;
113    }
114
115    private void GenerateImage() {
116      if (coordinatesPictureBox.Width > 0 && coordinatesPictureBox.Height > 0) {
117        DoubleMatrix coordinates = Content?.DisplayCoordinates;
118        var bitmap = new Bitmap(coordinatesPictureBox.Width, coordinatesPictureBox.Height);
119
120        if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
121          double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
122          for (int i = 0; i < coordinates.Rows; i++) {
123            if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
124            if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
125            if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
126            if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
127          }
128
129          int border = 20;
130          double xStep = xMax != xMin ? (coordinatesPictureBox.Width - 2 * border) / (xMax - xMin) : 1;
131          double yStep = yMax != yMin ? (coordinatesPictureBox.Height - 2 * border) / (yMax - yMin) : 1;
132
133          Point[] points = new Point[coordinates.Rows];
134          for (int i = 0; i < coordinates.Rows; i++)
135            points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
136                                  bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
137
138          using (Graphics graphics = Graphics.FromImage(bitmap)) {
139            for (int i = 0; i < points.Length; i++)
140              graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
141          }
142        } else {
143          using (Graphics graphics = Graphics.FromImage(bitmap)) {
144            graphics.Clear(Color.White);
145            Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
146            string text = "No coordinates defined or in wrong format.";
147            SizeF strSize = graphics.MeasureString(text, font);
148            graphics.DrawString(text, font, Brushes.Black, (float)(coordinatesPictureBox.Width - strSize.Width) / 2.0f, (float)(coordinatesPictureBox.Height - strSize.Height) / 2.0f);
149          }
150        }
151        coordinatesPictureBox.Image = bitmap;
152      }
153    }
154
155    private void SetCoordinatesButton_Click(object sender, EventArgs e) {
156      if (Content.DisplayCoordinates != null) {
157        Content.DisplayCoordinates.Reset -= ContentDisplayCoordinatesOnChanged;
158        Content.DisplayCoordinates.ItemChanged -= ContentDisplayCoordinatesOnChanged;
159      }
160      var items = Math.Max(Content.Matrix.Rows, 1);
161      Content.DisplayCoordinates = new DoubleMatrix(items, 2);
162    }
163
164    private void SetMatrixButton_Click(object sender, EventArgs e) {
165      MessageBox.Show("Not yet implemented");
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.