1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Optimization.Views {
|
---|
34 | /// <summary>
|
---|
35 | /// The visual representation of a <see cref="Variable"/>.
|
---|
36 | /// </summary>
|
---|
37 | [View("Run View")]
|
---|
38 | [Content(typeof(IRun), true)]
|
---|
39 | public sealed partial class RunView : NamedItemView {
|
---|
40 | private readonly Dictionary<string, ListViewItem> parametersItemToListViewItem;
|
---|
41 | private readonly Dictionary<string, ListViewItem> resultsItemToListViewItem;
|
---|
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>
|
---|
47 | public new IRun Content {
|
---|
48 | get { return (IRun)base.Content; }
|
---|
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();
|
---|
57 | parametersItemToListViewItem = new Dictionary<string, ListViewItem>();
|
---|
58 | resultsItemToListViewItem = new Dictionary<string, ListViewItem>();
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override void RegisterContentEvents() {
|
---|
62 | base.RegisterContentEvents();
|
---|
63 | Content.PropertyChanged += Content_PropertyChanged;
|
---|
64 | RegisterContentParametersEvents();
|
---|
65 | RegisterContentResultsEents();
|
---|
66 | }
|
---|
67 | private void RegisterContentParametersEvents() {
|
---|
68 | Content.Parameters.ItemsAdded += ParametersOnItemsChanged;
|
---|
69 | Content.Parameters.ItemsRemoved += ParametersOnItemsRemoved;
|
---|
70 | Content.Parameters.ItemsReplaced += ParametersOnItemsChanged;
|
---|
71 | Content.Parameters.CollectionReset += ParametersOnItemsChanged;
|
---|
72 | }
|
---|
73 | private void RegisterContentResultsEents() {
|
---|
74 | Content.Results.ItemsAdded += ResultsOnItemsChanged;
|
---|
75 | Content.Results.ItemsRemoved += ResultsOnItemsRemoved;
|
---|
76 | Content.Results.ItemsReplaced += ResultsOnItemsChanged;
|
---|
77 | Content.Results.CollectionReset += ResultsOnItemsChanged;
|
---|
78 | }
|
---|
79 | protected override void DeregisterContentEvents() {
|
---|
80 | base.DeregisterContentEvents();
|
---|
81 | Content.PropertyChanged -= Content_PropertyChanged;
|
---|
82 | DeregisterContentParametersEvents();
|
---|
83 | DeregisterContentResultsEvents();
|
---|
84 | }
|
---|
85 | private void DeregisterContentParametersEvents() {
|
---|
86 | Content.Parameters.ItemsAdded -= ParametersOnItemsChanged;
|
---|
87 | Content.Parameters.ItemsRemoved -= ParametersOnItemsRemoved;
|
---|
88 | Content.Parameters.ItemsReplaced -= ParametersOnItemsChanged;
|
---|
89 | Content.Parameters.CollectionReset -= ParametersOnItemsChanged;
|
---|
90 | }
|
---|
91 | private void DeregisterContentResultsEvents() {
|
---|
92 | Content.Results.ItemsAdded -= ResultsOnItemsChanged;
|
---|
93 | Content.Results.ItemsRemoved -= ResultsOnItemsRemoved;
|
---|
94 | Content.Results.ItemsReplaced -= ResultsOnItemsChanged;
|
---|
95 | Content.Results.CollectionReset -= ResultsOnItemsChanged;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
99 | if (InvokeRequired) {
|
---|
100 | Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
|
---|
101 | return;
|
---|
102 | }
|
---|
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) {
|
---|
118 | if (InvokeRequired) {
|
---|
119 | Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ParametersOnItemsChanged, sender, e);
|
---|
120 | return;
|
---|
121 | }
|
---|
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 |
|
---|
133 | private void ParametersOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
|
---|
134 | if (InvokeRequired) {
|
---|
135 | Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ParametersOnItemsRemoved, sender, e);
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | foreach (var item in e.Items) {
|
---|
139 | listView.Items.Remove(parametersItemToListViewItem[item.Key]);
|
---|
140 | parametersItemToListViewItem.Remove(item.Key);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void ResultsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
|
---|
145 | if (InvokeRequired) {
|
---|
146 | Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ResultsOnItemsChanged, sender, e);
|
---|
147 | return;
|
---|
148 | }
|
---|
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 |
|
---|
160 | private void ResultsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>> e) {
|
---|
161 | if (InvokeRequired) {
|
---|
162 | Invoke((Action<object, CollectionItemsChangedEventArgs<KeyValuePair<string, IItem>>>)ResultsOnItemsRemoved, sender, e);
|
---|
163 | return;
|
---|
164 | }
|
---|
165 | foreach (var item in e.Items) {
|
---|
166 | listView.Items.Remove(resultsItemToListViewItem[item.Key]);
|
---|
167 | resultsItemToListViewItem.Remove(item.Key);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | protected override void OnContentChanged() {
|
---|
172 | base.OnContentChanged();
|
---|
173 | viewHost.Content = null;
|
---|
174 | if (Content != null)
|
---|
175 | UpdateColor();
|
---|
176 | FillListView();
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected override void SetEnabledStateOfControls() {
|
---|
180 | base.SetEnabledStateOfControls();
|
---|
181 | listView.Enabled = Content != null;
|
---|
182 | detailsGroupBox.Enabled = (Content != null) && (listView.SelectedItems.Count == 1);
|
---|
183 | changeColorButton.Enabled = Content != null;
|
---|
184 | showAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !Locked;
|
---|
185 | }
|
---|
186 |
|
---|
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 |
|
---|
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 | }
|
---|
197 | private void UpdateColor() {
|
---|
198 | this.colorDialog.Color = this.Content.Color;
|
---|
199 | this.colorArea.BackColor = this.Content.Color;
|
---|
200 | }
|
---|
201 |
|
---|
202 | private string selectedName;
|
---|
203 | private void FillListView() {
|
---|
204 | if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
|
---|
205 |
|
---|
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 |
|
---|
228 | if (Content != null) {
|
---|
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) {
|
---|
236 | for (int i = 0; i < listView.Columns.Count; i++)
|
---|
237 | listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
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() : "-" });
|
---|
271 | item.Tag = value;
|
---|
272 | item.Group = group;
|
---|
273 | listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
|
---|
274 | item.ImageIndex = listView.SmallImageList.Images.Count - 1;
|
---|
275 | return item;
|
---|
276 | }
|
---|
277 |
|
---|
278 | private void listView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
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 | }
|
---|
288 | }
|
---|
289 | private void listView_DoubleClick(object sender, EventArgs e) {
|
---|
290 | if (listView.SelectedItems.Count == 1) {
|
---|
291 | IItem item = (IItem)listView.SelectedItems[0].Tag;
|
---|
292 | IContentView view = MainFormManager.MainForm.ShowContent(item);
|
---|
293 | if (view != null) {
|
---|
294 | view.ReadOnly = true;
|
---|
295 | view.Locked = Locked;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 | private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
300 | if (!Locked) {
|
---|
301 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
302 | IItem item = (IItem)listViewItem.Tag;
|
---|
303 | if (item != null) {
|
---|
304 | DataObject data = new DataObject();
|
---|
305 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
|
---|
306 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
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 | }
|
---|
320 | private void showAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
321 | if (!Locked) {
|
---|
322 | MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|