Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingCheckedVariablesView.cs @ 17631

Last change on this file since 17631 was 15884, checked in by pfleck, 7 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: 6.6 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.DataPreprocessing.Views {
32  [View("Preprocessing Checked Variables View")]
33  [Content(typeof(PreprocessingChartContent), false)]
34  public abstract partial class PreprocessingCheckedVariablesView : ItemView {
35    protected bool SuppressCheckedChangedUpdate = false;
36
37    public new PreprocessingChartContent Content {
38      get { return (PreprocessingChartContent)base.Content; }
39      set { base.Content = value; }
40    }
41
42    protected PreprocessingCheckedVariablesView() {
43      InitializeComponent();
44
45      // TODO: fix auto-size of columns
46      //for (int i = 0; i < variablesListView.Columns.Count; i++) {
47      //  variablesListView.Columns[i].Width = -1;
48      //  variablesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
49      //}
50    }
51
52    protected bool IsVariableChecked(string name) {
53      return Content.VariableItemList.CheckedItems.Any(x => x.Value.Value == name);
54    }
55    protected IList<string> GetCheckedVariables() {
56      return Content.VariableItemList.CheckedItems.Select(i => i.Value.Value).ToList();
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) return;
62
63      variablesListView.ItemChecked -= variablesListView_ItemChecked;
64      variablesListView.Items.Clear();
65      foreach (var variable in Content.VariableItemList) {
66        bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable.Value)
67          || Content.PreprocessingData.TargetVariable == variable.Value;
68        variablesListView.Items.Add(new ListViewItem(variable.Value) {
69          Tag = variable,
70          Checked = IsVariableChecked(variable.Value),
71          ForeColor = isInputTarget ? Color.Black : Color.Gray
72        });
73      }
74      variablesListView.ItemChecked += variablesListView_ItemChecked;
75    }
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.PreprocessingData.Changed += PreprocessingData_Changed;
79      Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
80    }
81
82    protected override void DeregisterContentEvents() {
83      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
84      Content.VariableItemList.CheckedItemsChanged -= CheckedItemsChanged;
85      base.DeregisterContentEvents();
86    }
87
88    protected virtual void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
89      // sync listview
90      foreach (var item in checkedItems.Items)
91        variablesListView.Items[item.Index].Checked = Content.VariableItemList.ItemChecked(item.Value);
92    }
93    private void variablesListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
94      // sync checked item list
95      var variable = (StringValue)e.Item.Tag;
96      Content.VariableItemList.SetItemCheckedState(variable, e.Item.Checked);
97    }
98
99    private void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
100      switch (e.Type) {
101        case DataPreprocessingChangedEventType.DeleteColumn:
102          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
103          break;
104        case DataPreprocessingChangedEventType.AddColumn:
105          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
106          break;
107        case DataPreprocessingChangedEventType.ChangeColumn:
108        case DataPreprocessingChangedEventType.ChangeItem:
109          UpdateVariable(Content.PreprocessingData.GetVariableName(e.Column));
110          break;
111        default:
112          ResetAllVariables();
113          break;
114      }
115    }
116
117    protected virtual void AddVariable(string name) {
118      Content.VariableItemList.Add(new StringValue(name));
119      if (!Content.PreprocessingData.InputVariables.Contains(name) && Content.PreprocessingData.TargetVariable != name) {
120        var listViewItem = variablesListView.FindItemWithText(name, false, 0, false);
121        listViewItem.ForeColor = Color.LightGray;
122      }
123    }
124    protected virtual void RemoveVariable(string name) {
125      var stringValue = Content.VariableItemList.SingleOrDefault(n => n.Value == name);
126      if (stringValue != null)
127        Content.VariableItemList.Remove(stringValue);
128    }
129    protected virtual void UpdateVariable(string name) { }
130    protected virtual void ResetAllVariables() { }
131    protected virtual void CheckedChangedUpdate() { }
132
133    private void checkInputsTargetButton_Click(object sender, System.EventArgs e) {
134      SuppressCheckedChangedUpdate = true;
135      foreach (var name in Content.VariableItemList) {
136        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
137        Content.VariableItemList.SetItemCheckedState(name, isInputTarget);
138      }
139      SuppressCheckedChangedUpdate = false;
140      CheckedChangedUpdate();
141    }
142
143    private void checkAllButton_Click(object sender, System.EventArgs e) {
144      SuppressCheckedChangedUpdate = true;
145      foreach (var name in Content.VariableItemList) {
146        Content.VariableItemList.SetItemCheckedState(name, true);
147      }
148      SuppressCheckedChangedUpdate = false;
149      CheckedChangedUpdate();
150    }
151
152    private void uncheckAllButton_Click(object sender, System.EventArgs e) {
153      SuppressCheckedChangedUpdate = true;
154      foreach (var name in Content.VariableItemList) {
155        Content.VariableItemList.SetItemCheckedState(name, false);
156      }
157      SuppressCheckedChangedUpdate = false;
158      CheckedChangedUpdate();
159    }
160  }
161}
162
163
Note: See TracBrowser for help on using the repository browser.