Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 12865

Last change on this file since 12865 was 12804, checked in by abeham, 9 years ago

#2431:

  • worked on IRRRun (early abort still troublesome)
  • Updated RLD view to allow defining targets
  • Attempting to handle maximization/minimization
File size: 13.6 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3260]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;
[11344]23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
[3260]26using System.Windows.Forms;
[11344]27using HeuristicLab.Collections;
[3376]28using HeuristicLab.Common;
[3260]29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Optimization.Views {
34  /// <summary>
35  /// The visual representation of a <see cref="Variable"/>.
36  /// </summary>
37  [View("Run View")]
[3280]38  [Content(typeof(IRun), true)]
[3260]39  public sealed partial class RunView : NamedItemView {
[11344]40    private readonly Dictionary<string, ListViewItem> parametersItemToListViewItem;
41    private readonly Dictionary<string, ListViewItem> resultsItemToListViewItem;
[3260]42    /// <summary>
43    /// Gets or sets the variable to represent visually.
44    /// </summary>
45    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
46    /// No own data storage present.</remarks>
[3280]47    public new IRun Content {
48      get { return (IRun)base.Content; }
[3260]49      set { base.Content = value; }
50    }
51
52    /// <summary>
53    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
54    /// </summary>
55    public RunView() {
56      InitializeComponent();
[11344]57      parametersItemToListViewItem = new Dictionary<string, ListViewItem>();
58      resultsItemToListViewItem = new Dictionary<string, ListViewItem>();
[3260]59    }
60
[3638]61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
[11344]63      Content.PropertyChanged += Content_PropertyChanged;
64      RegisterContentParametersEvents();
65      RegisterContentResultsEents();
[3638]66    }
[11344]67    private void RegisterContentParametersEvents() {
68      Content.Parameters.ItemsAdded += ParametersOnItemsChanged;
[11345]69      Content.Parameters.ItemsRemoved += ParametersOnItemsRemoved;
[11344]70      Content.Parameters.ItemsReplaced += ParametersOnItemsChanged;
71      Content.Parameters.CollectionReset += ParametersOnItemsChanged;
72    }
73    private void RegisterContentResultsEents() {
74      Content.Results.ItemsAdded += ResultsOnItemsChanged;
[11345]75      Content.Results.ItemsRemoved += ResultsOnItemsRemoved;
[11344]76      Content.Results.ItemsReplaced += ResultsOnItemsChanged;
77      Content.Results.CollectionReset += ResultsOnItemsChanged;
78    }
[3638]79    protected override void DeregisterContentEvents() {
80      base.DeregisterContentEvents();
[11344]81      Content.PropertyChanged -= Content_PropertyChanged;
82      DeregisterContentParametersEvents();
83      DeregisterContentResultsEvents();
[3638]84    }
[11344]85    private void DeregisterContentParametersEvents() {
86      Content.Parameters.ItemsAdded -= ParametersOnItemsChanged;
[11345]87      Content.Parameters.ItemsRemoved -= ParametersOnItemsRemoved;
[11344]88      Content.Parameters.ItemsReplaced -= ParametersOnItemsChanged;
89      Content.Parameters.CollectionReset -= ParametersOnItemsChanged;
[3638]90    }
[11344]91    private void DeregisterContentResultsEvents() {
92      Content.Results.ItemsAdded -= ResultsOnItemsChanged;
[11345]93      Content.Results.ItemsRemoved -= ResultsOnItemsRemoved;
[11344]94      Content.Results.ItemsReplaced -= ResultsOnItemsChanged;
95      Content.Results.CollectionReset -= ResultsOnItemsChanged;
96    }
[3638]97
[11344]98    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
[12804]99      if (InvokeRequired) {
100        Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
101        return;
102      }
[11344]103      if (e.PropertyName == "Color") {
104        if (InvokeRequired) this.Invoke((Action)UpdateColor, null);
105        else UpdateColor();
106      } else if (e.PropertyName == "Parameters") {
107        if (InvokeRequired) this.Invoke((Action<bool>)FillParametersListView, true);
108        else FillParametersListView();
109        RegisterContentParametersEvents();
110      } else if (e.PropertyName == "Results") {
111        if (InvokeRequired) this.Invoke((Action<bool>)FillResultsListView, true);
112        else FillResultsListView();
113        RegisterContentResultsEents();
114      }
115    }
116
117    private void ParametersOnItemsChanged(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
[12804]118      if (InvokeRequired) {
119        Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ParametersOnItemsChanged, sender, e);
120        return;
121      }
[11344]122      foreach (var item in e.OldItems) {
123        listView.Items.Remove(parametersItemToListViewItem[item.Key]);
124        parametersItemToListViewItem.Remove(item.Key);
125      }
126      foreach (var item in e.Items) {
127        var listViewItem = CreateListViewItem(item.Key, Content.Parameters[item.Key], listView.Groups["parametersGroup"]);
128        listView.Items.Add(listViewItem);
129        parametersItemToListViewItem[item.Key] = listViewItem;
130      }
131    }
132
[11345]133    private void ParametersOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
[12804]134      if (InvokeRequired) {
135        Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ParametersOnItemsRemoved, sender, e);
136        return;
137      }
[11345]138      foreach (var item in e.Items) {
139        listView.Items.Remove(parametersItemToListViewItem[item.Key]);
140        parametersItemToListViewItem.Remove(item.Key);
141      }
142    }
143
[11344]144    private void ResultsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
[12804]145      if (InvokeRequired) {
146        Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ResultsOnItemsChanged, sender, e);
147        return;
148      }
[11344]149      foreach (var item in e.OldItems) {
150        listView.Items.Remove(resultsItemToListViewItem[item.Key]);
151        resultsItemToListViewItem.Remove(item.Key);
152      }
153      foreach (var item in e.Items) {
154        var listViewItem = CreateListViewItem(item.Key, Content.Results[item.Key], listView.Groups["resultsGroup"]);
155        listView.Items.Add(listViewItem);
156        resultsItemToListViewItem[item.Key] = listViewItem;
157      }
158    }
159
[11345]160    private void ResultsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
[12804]161      if (InvokeRequired) {
162        Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ResultsOnItemsRemoved, sender, e);
163        return;
164      }
[11345]165      foreach (var item in e.Items) {
166        listView.Items.Remove(resultsItemToListViewItem[item.Key]);
167        resultsItemToListViewItem.Remove(item.Key);
168      }
169    }
170
[3260]171    protected override void OnContentChanged() {
172      base.OnContentChanged();
173      viewHost.Content = null;
[3764]174      if (Content != null)
[4096]175        UpdateColor();
[3775]176      FillListView();
[3904]177    }
[3764]178
[3904]179    protected override void SetEnabledStateOfControls() {
180      base.SetEnabledStateOfControls();
[3454]181      listView.Enabled = Content != null;
[4099]182      detailsGroupBox.Enabled = (Content != null) && (listView.SelectedItems.Count == 1);
[3638]183      changeColorButton.Enabled = Content != null;
[4102]184      showAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !Locked;
[3260]185    }
186
[7760]187    protected override void PropagateStateChanges(Control control, Type type, System.Reflection.PropertyInfo propertyInfo) {
188      if (propertyInfo.Name == "ReadOnly") return;
189      base.PropagateStateChanges(control, type, propertyInfo);
190    }
191
[3638]192    private void changeColorButton_Click(object sender, EventArgs e) {
193      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
194        this.Content.Color = this.colorDialog.Color;
195      }
196    }
[4096]197    private void UpdateColor() {
[3638]198      this.colorDialog.Color = this.Content.Color;
[4096]199      this.colorArea.BackColor = this.Content.Color;
[3638]200    }
201
[4011]202    private string selectedName;
[3260]203    private void FillListView() {
[3775]204      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
205
[11344]206      FillParametersListView(false);
207      FillResultsListView(false);
208      if (listView.Items.Count > 0) {
209        for (int i = 0; i < listView.Columns.Count; i++)
210          listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
211        selectedName = null;
212      }
213    }
214
215    private void FillParametersListView(bool resize = true) {
216      listView.BeginUpdate();
217      foreach (var item in listView.Groups["parametersGroup"].Items.OfType<ListViewItem>().OrderByDescending(x => x.ImageIndex).ToList()) {
218        listView.SmallImageList.Images.RemoveAt(item.ImageIndex);
219        listView.Items.Remove(item);
220      }
221      parametersItemToListViewItem.Clear();
222
223      var counter = 0;
224      foreach (var item in listView.Items.OfType<ListViewItem>().OrderBy(x => x.ImageIndex).ToList())
225        item.ImageIndex = counter++;
226      listView.EndUpdate();
227
[3260]228      if (Content != null) {
[11344]229        foreach (string key in Content.Parameters.Keys) {
230          var listViewItem = CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"]);
231          listView.Items.Add(listViewItem);
232          if ((selectedName != null) && key.Equals(selectedName)) listViewItem.Selected = true;
233          parametersItemToListViewItem[key] = listViewItem;
234        }
235        if (resize && listView.Items.Count > 0) {
[3260]236          for (int i = 0; i < listView.Columns.Count; i++)
237            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
238        }
239      }
240    }
241
[11344]242    private void FillResultsListView(bool resize = true) {
243      listView.BeginUpdate();
244      foreach (var item in listView.Groups["resultsGroup"].Items.OfType<ListViewItem>().OrderByDescending(x => x.ImageIndex).ToList()) {
245        listView.SmallImageList.Images.RemoveAt(item.ImageIndex);
246        listView.Items.Remove(item);
247      }
248      resultsItemToListViewItem.Clear();
249
250      var counter = 0;
251      foreach (var item in listView.Items.OfType<ListViewItem>().OrderBy(x => x.ImageIndex).ToList())
252        item.ImageIndex = counter++;
253      listView.EndUpdate();
254
255      if (Content != null) {
256        foreach (string key in Content.Results.Keys) {
257          var listViewItem = CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"]);
258          listView.Items.Add(listViewItem);
259          if ((selectedName != null) && key.Equals(selectedName)) listViewItem.Selected = true;
260          resultsItemToListViewItem[key] = listViewItem;
261        }
262        if (resize && listView.Items.Count > 0) {
263          for (int i = 0; i < listView.Columns.Count; i++)
264            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
265        }
266      }
267    }
268
269    private ListViewItem CreateListViewItem(string name, IItem value, ListViewGroup group) {
270      var item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
[3341]271      item.Tag = value;
272      item.Group = group;
[5287]273      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
[3341]274      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
[11344]275      return item;
[3341]276    }
277
[3260]278    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
[4096]279      if (showDetailsCheckBox.Checked) {
280        if (listView.SelectedItems.Count == 1) {
281          detailsGroupBox.Enabled = true;
282          viewHost.Content = listView.SelectedItems[0].Tag as IContent;
283        } else {
284          viewHost.Content = null;
285          detailsGroupBox.Enabled = false;
286        }
287      }
[3260]288    }
[3300]289    private void listView_DoubleClick(object sender, EventArgs e) {
290      if (listView.SelectedItems.Count == 1) {
291        IItem item = (IItem)listView.SelectedItems[0].Tag;
[3557]292        IContentView view = MainFormManager.MainForm.ShowContent(item);
[3416]293        if (view != null) {
[3766]294          view.ReadOnly = true;
[3423]295          view.Locked = Locked;
[3416]296        }
[3300]297      }
298    }
299    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]300      if (!Locked) {
301        ListViewItem listViewItem = (ListViewItem)e.Item;
302        IItem item = (IItem)listViewItem.Tag;
[3731]303        if (item != null) {
304          DataObject data = new DataObject();
[5837]305          data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
[3731]306          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
307        }
[3432]308      }
[3300]309    }
[4096]310    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
311      if (showDetailsCheckBox.Checked) {
312        splitContainer.Panel2Collapsed = false;
313        detailsGroupBox.Enabled = listView.SelectedItems.Count == 1;
314        viewHost.Content = listView.SelectedItems.Count == 1 ? (IContent)listView.SelectedItems[0].Tag : null;
315      } else {
316        splitContainer.Panel2Collapsed = true;
317        viewHost.Content = null;
318      }
319    }
[3300]320    private void showAlgorithmButton_Click(object sender, EventArgs e) {
[3454]321      if (!Locked) {
[3557]322        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
[3416]323      }
[3300]324    }
[3260]325  }
326}
Note: See TracBrowser for help on using the repository browser.