Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/TSPSolutionView.cs @ 17335

Last change on this file since 17335 was 17335, checked in by abeham, 5 years ago

#2521: Refactored pTSP to use compositional pattern

File size: 4.3 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.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Problems.TravelingSalesman.Views {
29  /// <summary>
30  /// The base class for visual representations of a path tour for a TSP.
31  /// </summary>
32  [View("TSP Solution View")]
33  [Content(typeof(ITSPSolution), true)]
34  public partial class TSPSolutionView : ItemView, ITSPVisualizerView {
35    public TSPVisualizer Visualizer { get; set; }
36
37    public new ITSPSolution Content {
38      get { return (ITSPSolution)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="TSPSolutionView"/>.
44    /// </summary>
45    public TSPSolutionView() {
46      InitializeComponent();
47      Visualizer = new TSPVisualizer();
48    }
49
50    protected override void DeregisterContentEvents() {
51      Content.PropertyChanged -= ContentOnPropertyChanged;
52      base.DeregisterContentEvents();
53    }
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.PropertyChanged += ContentOnPropertyChanged;
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) {
62        qualityViewHost.Content = null;
63        pictureBox.Image = null;
64        tourViewHost.Content = null;
65        Visualizer.Coordinates = null;
66        Visualizer.Tour = null;
67      } else {
68        qualityViewHost.Content = Content.TourLength;
69        if (Content.TSPData is CoordinatesTSPData coordTsp)
70          Visualizer.Coordinates = coordTsp.Coordinates;
71        else if (Content.TSPData is MatrixTSPData matrixTsp)
72          Visualizer.Coordinates = matrixTsp.DisplayCoordinates;
73        Visualizer.Tour = Content.Tour;
74        GenerateImage();
75        tourViewHost.Content = Content.Tour;
76      }
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      qualityGroupBox.Enabled = Content != null;
82      pictureBox.Enabled = Content != null;
83      tourGroupBox.Enabled = Content != null;
84    }
85
86    protected virtual void GenerateImage() {
87      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
88        if (Content == null) {
89          pictureBox.Image = null;
90        } else {
91          pictureBox.Image = Visualizer.Draw(pictureBox.Width, pictureBox.Height);
92        }
93      }
94    }
95
96    protected virtual void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
97      if (InvokeRequired)
98        Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e);
99      else {
100        switch (e.PropertyName) {
101          case nameof(Content.TSPData):
102            if (Content.TSPData is CoordinatesTSPData coordTsp)
103              Visualizer.Coordinates = coordTsp.Coordinates;
104            else if (Content.TSPData is MatrixTSPData matrixTsp)
105              Visualizer.Coordinates = matrixTsp.DisplayCoordinates;
106            GenerateImage();
107            break;
108          case nameof(Content.Tour):
109            Visualizer.Tour = Content.Tour;
110            GenerateImage();
111            tourViewHost.Content = Content.Tour;
112            break;
113          case nameof(Content.TourLength):
114            qualityViewHost.Content = Content.TourLength;
115            break;
116        }
117      }
118    }
119
120    private void pictureBox_SizeChanged(object sender, EventArgs e) {
121      GenerateImage();
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.