source: branches/2906_Transformations/HeuristicLab.DataPreprocessing.Views/3.4/TransformationListView.cs @ 15865

Last change on this file since 15865 was 15865, checked in by pfleck, 3 years ago

#2906 Added PreprocessingTransformation as a custom view-model for transformations in preprocessing.

File size: 4.2 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33  [View("TransformationList View")]
34  [Content(typeof(IItemList<PreprocessingTransformation>), false)]
35  public partial class TransformationListView : ItemListView<PreprocessingTransformation> {
36
37    internal IFilteredPreprocessingData PreprocessingData { get; set; }
38
39    public TransformationListView() {
40      InitializeComponent();
41      itemsGroupBox.Text = "Transformations";
42    }
43
44    protected override PreprocessingTransformation CreateItem() {
45      var variableNames = PreprocessingData.VariableNames.Select(x => new StringValue(x))
46        .Concat(Content.Select(x => new StringValue(x.TransformedVariable))).Distinct();
47
48      var newTransformation = new PreprocessingTransformation(variableNames);
49      newTransformation.TransformedVariableParameter.ValidValues.Add(new StringValue("<New Variable>"));
50      return newTransformation;
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55
56      if (Content == null) return;
57
58      int i = 0;
59      foreach (ListViewItem item in ItemsListView.Items) {
60        var transformation = PreprocessingData.Transformations[i];
61
62        item.ForeColor = transformation.IsApplied ? Color.Black : Color.Gray;
63        i++;
64      }
65      UpdateButtonsState();
66    }
67
68    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<PreprocessingTransformation>> e) {
69      base.Content_ItemsAdded(sender, e);
70
71      foreach (var transformation in e.Items) {
72        UpdateListViewItemsColor(transformation.Value);
73      }
74    }
75
76    protected override void RegisterItemEvents(PreprocessingTransformation item) {
77      base.RegisterItemEvents(item);
78      item.PropertyChanged += Item_PropertyChanged;
79    }
80    protected override void DeregisterItemEvents(PreprocessingTransformation item) {
81      item.PropertyChanged -= Item_PropertyChanged;
82      base.DeregisterItemEvents(item);
83    }
84
85    private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
86      if (sender is PreprocessingTransformation transformation)
87        UpdateListViewItemsColor(transformation);
88    }
89
90    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
91      base.itemsListView_SelectedIndexChanged(sender, e);
92
93      UpdateButtonsState();
94    }
95
96    private void UpdateListViewItemsColor(PreprocessingTransformation transformation) {
97      foreach (ListViewItem item in itemListViewItemMapping[transformation]) {
98        item.ForeColor = transformation.IsApplied ? Color.Black : Color.Gray;
99      }
100    }
101
102    private void UpdateButtonsState() {
103      if (itemsListView.SelectedItems.Count != 1) return;
104      int selectedIndex = itemsListView.SelectedIndices[0];
105
106      // TODO: do not move/remove already applied transformations
107      // TODO: general disalow of movement?
108      if (Content[selectedIndex].IsApplied) {
109        moveUpButton.Enabled = selectedIndex > 0 && Content[selectedIndex - 1].IsApplied;
110        moveDownButton.Enabled = false;
111        removeButton.Enabled = false;
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.