Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingCheckedVariablesView.cs @ 16573

Last change on this file since 16573 was 15973, checked in by gkronber, 6 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

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