Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DataAnalysisSolutionView.cs @ 15884

Last change on this file since 15884 was 15884, checked in by pfleck, 6 years ago

#2906 Refactoring

  • Moved transformation-specific parts out of existing interfaces.
  • Moved all Transformation logic to DataAnalysisTransformation.
  • Simplified (Inverse)Transformation of Dataset/ProblemData/Model/Solution.
File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.Xml;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.DataAnalysis.Views {
36
37  [View("DataAnalysisSolution View")]
38  [Content(typeof(DataAnalysisSolution), false)]
39  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
40
41    private const string BacktransformButtonText = "Integrate Transformations";
42    private const string ReapplyTransformationsButtonText = "Restore Trained Model";
43
44    public DataAnalysisSolutionView() {
45      InitializeComponent();
46      viewHost.ViewsLabelVisible = false;
47      transformButton.Image = VSImageLibrary.Event;
48    }
49
50    public new DataAnalysisSolution Content {
51      get { return (DataAnalysisSolution)base.Content; }
52      set { base.Content = value; }
53    }
54
55    protected override void SetEnabledStateOfControls() {
56      base.SetEnabledStateOfControls();
57      addButton.Enabled = false;
58      removeButton.Enabled = false;
59      loadProblemDataButton.Enabled = Content != null && !Locked;
60      transformButton.Enabled = Content != null && !Locked;
61      transformButton.Visible = Content != null && Content.ProblemData.Transformations.Any();
62    }
63
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
67    }
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
71    }
72    private void Content_ProblemDataChanged(object sender, EventArgs e) {
73      OnContentChanged();
74    }
75
76    protected override void OnContentChanged() {
77      string selectedName = null;
78      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null && itemsListView.SelectedItems[0].Tag is Type))
79        selectedName = itemsListView.SelectedItems[0].Text;
80
81      base.OnContentChanged();
82      AddEvaluationViewTypes();
83
84      //recover selection
85      if (selectedName != null) {
86        foreach (ListViewItem item in itemsListView.Items) {
87          if (item.Tag != null && item.Tag is Type && item.Text == selectedName)
88            item.Selected = true;
89        }
90      }
91
92      if (Content == null) return;
93
94      transformButton.Text = Content.Model is IDataAnalysisTransformationModel
95        ? ReapplyTransformationsButtonText
96        : BacktransformButtonText;
97    }
98
99    protected override IResult CreateItem() {
100      return null;
101    }
102
103    protected virtual void AddEvaluationViewTypes() {
104      if (Content != null && !Content.ProblemData.IsEmpty) {
105        var viewTypes = MainFormManager.GetViewTypes(Content.GetType(), true)
106          .Where(t => typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(t));
107        foreach (var viewType in viewTypes)
108          AddViewListViewItem(viewType, ((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
109      }
110    }
111
112    protected sealed override void itemsListView_DoubleClick(object sender, EventArgs e) {
113      if (itemsListView.SelectedItems.Count != 1) return;
114
115      IResult result = itemsListView.SelectedItems[0].Tag as IResult;
116      Type viewType = itemsListView.SelectedItems[0].Tag as Type;
117      if (result != null) {
118        IContentView view = MainFormManager.MainForm.ShowContent(result.Value);
119        if (view != null) {
120          view.Caption = result.Name;
121          view.ReadOnly = ReadOnly;
122          view.Locked = Locked;
123        }
124      } else if (viewType != null) {
125        MainFormManager.MainForm.ShowContent(Content, viewType);
126      }
127    }
128
129    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
130      if (itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
131        detailsGroupBox.Enabled = true;
132        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
133        viewHost.ViewType = viewType;
134        viewHost.Content = Content;
135      } else
136        base.itemsListView_SelectedIndexChanged(sender, e);
137    }
138
139    protected virtual void loadProblemDataButton_Click(object sender, EventArgs e) {
140      if (loadProblemDataFileDialog.ShowDialog(this) != DialogResult.OK) return;
141      try {
142        object hlFile = XmlParser.Deserialize(loadProblemDataFileDialog.FileName);
143
144        IDataAnalysisProblemData problemData = null;
145        if (hlFile is IDataAnalysisProblemData) {
146          problemData = (IDataAnalysisProblemData)hlFile;
147        } else if (hlFile is IDataAnalysisProblem) {
148          problemData = ((IDataAnalysisProblem)hlFile).ProblemData;
149        } else if (hlFile is IDataAnalysisSolution) {
150          problemData = ((IDataAnalysisSolution)hlFile).ProblemData;
151        }
152
153        if (problemData == null)
154          throw new InvalidOperationException("The chosen HeuristicLab file does not contain a ProblemData, Problem, or DataAnalysisSolution.");
155
156        var solution = (IDataAnalysisSolution)Content.Clone();
157        problemData.AdjustProblemDataProperties(solution.ProblemData);
158
159        solution.ProblemData = problemData;
160        if (!solution.Name.EndsWith(" with loaded problemData"))
161          solution.Name += " with loaded problemData";
162        MainFormManager.MainForm.ShowContent(solution);
163      } catch (InvalidOperationException invalidOperationException) {
164        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
165      } catch (ArgumentException argumentException) {
166        ErrorHandling.ShowErrorDialog(this, argumentException);
167      }
168    }
169
170    protected void AddViewListViewItem(Type viewType, Image image) {
171      ListViewItem listViewItem = new ListViewItem();
172      listViewItem.Text = ViewAttribute.GetViewName(viewType);
173      itemsListView.SmallImageList.Images.Add(image);
174      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
175      listViewItem.Tag = viewType;
176      itemsListView.Items.Add(listViewItem);
177
178      AdjustListViewColumnSizes();
179    }
180
181    protected void RemoveViewListViewItem(Type viewType) {
182      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
183
184      foreach (ListViewItem item in itemsToRemove)
185        itemsListView.Items.Remove(item);
186    }
187
188    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
189      if (showDetailsCheckBox.Checked && itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
190        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
191        viewHost.ViewType = viewType;
192        viewHost.Content = Content;
193        splitContainer.Panel2Collapsed = false;
194        detailsGroupBox.Enabled = true;
195      } else base.showDetailsCheckBox_CheckedChanged(sender, e);
196    }
197
198    protected override void RebuildImageList() {
199      itemsListView.SmallImageList.Images.Clear();
200      foreach (ListViewItem listViewItem in itemsListView.Items) {
201        IResult result = listViewItem.Tag as IResult;
202        Type viewType = listViewItem.Tag as Type;
203        if (result != null) itemsListView.SmallImageList.Images.Add(result.ItemImage);
204        else if (viewType != null && typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
205          itemsListView.SmallImageList.Images.Add(((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
206        else itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
207
208        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
209      }
210    }
211
212    #region drag and drop
213    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
214      validDragOperation = false;
215      if (ReadOnly) return;
216      if (e.Effect != DragDropEffects.Copy) return;
217
218      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
219      if (dropData is IDataAnalysisProblemData) validDragOperation = true;
220      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
221      else if (dropData is IValueParameter) {
222        var param = (IValueParameter)dropData;
223        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
224      }
225    }
226
227    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
228      if (e.Effect != DragDropEffects.Copy) return;
229
230      IDataAnalysisProblemData problemData = null;
231      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
232      if (dropData is IDataAnalysisProblemData)
233        problemData = (IDataAnalysisProblemData)dropData;
234      else if (dropData is IDataAnalysisProblem)
235        problemData = ((IDataAnalysisProblem)dropData).ProblemData;
236      else if (dropData is IValueParameter) {
237        var param = (IValueParameter)dropData;
238        problemData = param.Value as DataAnalysisProblemData;
239      }
240
241      if (problemData == null) return;
242
243      problemData = (IDataAnalysisProblemData)problemData.Clone();
244
245      try {
246        problemData.AdjustProblemDataProperties(Content.ProblemData);
247        Content.ProblemData = problemData;
248
249        if (!Content.Name.EndsWith(" with changed problemData"))
250          Content.Name += " with changed problemData";
251        Content.Filename = string.Empty;
252        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
253      } catch (InvalidOperationException invalidOperationException) {
254        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
255      } catch (ArgumentException argumentException) {
256        ErrorHandling.ShowErrorDialog(this, argumentException);
257      }
258    }
259    #endregion
260
261    private void transformButton_Click(object sender, EventArgs e) {
262      var transformedSolution = DataAnalysisTransformation.TransformSolution(Content);
263      MainFormManager.MainForm.ShowContent(transformedSolution);
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.