Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingCheckedVariablesView.cs @ 14579

Last change on this file since 14579 was 14511, checked in by pfleck, 8 years ago

#2709

  • Added Check Inputs/All/None buttons instead of showing disabled buttons of the ItemCollectionView.
  • Removed the PreprocessingCheckedItemListView. A standard ListView is used instead.
  • Fixed slow updating when simultaneously (un-)checking multiple variables in the chart views. (currently only works by using the new buttons)
File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Common.Resources;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33  [View("Preprocessing Checked Variables View")]
34  [Content(typeof(PreprocessingChartContent), false)]
35  public partial class PreprocessingCheckedVariablesView : ItemView {
36
37    protected bool suppressCheckedChangedUpdate = false;
38
39    public new PreprocessingChartContent Content {
40      get { return (PreprocessingChartContent)base.Content; }
41      set { base.Content = value; }
42    }
43
44    protected PreprocessingCheckedVariablesView() {
45      InitializeComponent();
46      checkInputsTargetButton.Text = string.Empty;
47      checkInputsTargetButton.Image = VSImageLibrary.Namespace;
48      checkAllButton.Text = string.Empty;
49      checkAllButton.Image = VSImageLibrary.Expand;
50      uncheckAllButton.Text = string.Empty;
51      uncheckAllButton.Image = VSImageLibrary.Collapse;
52    }
53
54    protected bool IsVariableChecked(string name) {
55      return Content.VariableItemList.CheckedItems.Any(x => x.Value.Value == name);
56    }
57    protected IList<string> GetCheckedVariables() {
58      return Content.VariableItemList.CheckedItems.Select(i => i.Value.Value).ToList();
59    }
60
61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63      if (Content == null) return;
64
65      variablesListView.ItemChecked -= variablesListView_ItemChecked;
66      variablesListView.Items.Clear();
67      foreach (var variable in Content.VariableItemList) {
68        bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable.Value)
69          || Content.PreprocessingData.TargetVariable == variable.Value;
70        variablesListView.Items.Add(new ListViewItem(variable.Value) {
71          Tag = variable,
72          Checked = IsVariableChecked(variable.Value),
73          ForeColor = isInputTarget ? Color.Black : Color.Gray
74        });
75      }
76      variablesListView.ItemChecked += variablesListView_ItemChecked;
77    }
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.PreprocessingData.Changed += PreprocessingData_Changed;
81      Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
82    }
83
84    protected override void DeregisterContentEvents() {
85      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
86      Content.VariableItemList.CheckedItemsChanged -= CheckedItemsChanged;
87      base.DeregisterContentEvents();
88    }
89
90    protected virtual void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
91      // sync listview
92      foreach (var item in checkedItems.Items)
93        variablesListView.Items[item.Index].Checked = Content.VariableItemList.ItemChecked(item.Value);
94    }
95    private void variablesListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
96      // sync checked item list
97      var variable = (StringValue)e.Item.Tag;
98      Content.VariableItemList.SetItemCheckedState(variable, e.Item.Checked);
99    }
100
101    private void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
102      switch (e.Type) {
103        case DataPreprocessingChangedEventType.DeleteColumn:
104          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
105          break;
106        case DataPreprocessingChangedEventType.AddColumn:
107          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
108          break;
109        case DataPreprocessingChangedEventType.ChangeColumn:
110        case DataPreprocessingChangedEventType.ChangeItem:
111          UpdateVariable(Content.PreprocessingData.GetVariableName(e.Column));
112          break;
113        default:
114          ResetAllVariables();
115          break;
116      }
117    }
118
119    protected virtual void AddVariable(string name) {
120      Content.VariableItemList.Add(new StringValue(name));
121      if (!Content.PreprocessingData.InputVariables.Contains(name) && Content.PreprocessingData.TargetVariable != name) {
122        var listViewItem = variablesListView.FindItemWithText(name, false, 0, false);
123        listViewItem.ForeColor = Color.LightGray;
124      }
125    }
126    protected virtual void RemoveVariable(string name) {
127      var stringValue = Content.VariableItemList.SingleOrDefault(n => n.Value == name);
128      if (stringValue != null)
129        Content.VariableItemList.Remove(stringValue);
130    }
131    protected virtual void UpdateVariable(string name) {
132    }
133    protected virtual void ResetAllVariables() {
134    }
135
136    protected virtual void CheckedChangedUpdate() {
137
138    }
139
140    private void checkInputsTargetButton_Click(object sender, System.EventArgs e) {
141      suppressCheckedChangedUpdate = true;
142      foreach (var name in Content.VariableItemList) {
143        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
144        Content.VariableItemList.SetItemCheckedState(name, isInputTarget);
145      }
146      suppressCheckedChangedUpdate = false;
147      CheckedChangedUpdate();
148    }
149
150    private void checkAllButton_Click(object sender, System.EventArgs e) {
151      suppressCheckedChangedUpdate = true;
152      foreach (var name in Content.VariableItemList) {
153        Content.VariableItemList.SetItemCheckedState(name, true);
154      }
155      suppressCheckedChangedUpdate = false;
156      CheckedChangedUpdate();
157    }
158
159    private void uncheckAllButton_Click(object sender, System.EventArgs e) {
160      suppressCheckedChangedUpdate = true;
161      foreach (var name in Content.VariableItemList) {
162        Content.VariableItemList.SetItemCheckedState(name, false);
163      }
164      suppressCheckedChangedUpdate = false;
165      CheckedChangedUpdate();
166    }
167  }
168}
169
170
Note: See TracBrowser for help on using the repository browser.