Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15940 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: 6.6 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();
[15884]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      //}
[10303]50    }
51
[14384]52    protected bool IsVariableChecked(string name) {
[14270]53      return Content.VariableItemList.CheckedItems.Any(x => x.Value.Value == name);
[10628]54    }
[14384]55    protected IList<string> GetCheckedVariables() {
[15110]56      return Content.VariableItemList.CheckedItems.Select(i => i.Value.Value).ToList();
[14384]57    }
[10628]58
[14381]59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) return;
[10573]62
[15110]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        });
[10818]73      }
[15110]74      variablesListView.ItemChecked += variablesListView_ItemChecked;
[10847]75    }
[14381]76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.PreprocessingData.Changed += PreprocessingData_Changed;
[15110]79      Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
[10717]80    }
[15110]81
[14381]82    protected override void DeregisterContentEvents() {
83      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
[15110]84      Content.VariableItemList.CheckedItemsChanged -= CheckedItemsChanged;
[14381]85      base.DeregisterContentEvents();
[10847]86    }
87
[14381]88    protected virtual void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
[15110]89      // sync listview
90      foreach (var item in checkedItems.Items)
91        variablesListView.Items[item.Index].Checked = Content.VariableItemList.ItemChecked(item.Value);
[10717]92    }
[15110]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    }
[10717]98
[14381]99    private void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
[10628]100      switch (e.Type) {
101        case DataPreprocessingChangedEventType.DeleteColumn:
[10992]102          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
[10628]103          break;
104        case DataPreprocessingChangedEventType.AddColumn:
[10992]105          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
[10628]106          break;
107        case DataPreprocessingChangedEventType.ChangeColumn:
108        case DataPreprocessingChangedEventType.ChangeItem:
[14381]109          UpdateVariable(Content.PreprocessingData.GetVariableName(e.Column));
[10628]110          break;
[10817]111        default:
[14381]112          ResetAllVariables();
[10628]113          break;
[10382]114      }
[10377]115    }
116
[14381]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) {
[15110]120        var listViewItem = variablesListView.FindItemWithText(name, false, 0, false);
[14381]121        listViewItem.ForeColor = Color.LightGray;
[10736]122      }
[10717]123    }
[14381]124    protected virtual void RemoveVariable(string name) {
125      var stringValue = Content.VariableItemList.SingleOrDefault(n => n.Value == name);
[10628]126      if (stringValue != null)
[10818]127        Content.VariableItemList.Remove(stringValue);
[10377]128    }
[15110]129    protected virtual void UpdateVariable(string name) { }
130    protected virtual void ResetAllVariables() { }
131    protected virtual void CheckedChangedUpdate() { }
[10736]132
[15110]133    private void checkInputsTargetButton_Click(object sender, System.EventArgs e) {
134      SuppressCheckedChangedUpdate = true;
135      foreach (var name in Content.VariableItemList) {
[14381]136        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
[15110]137        Content.VariableItemList.SetItemCheckedState(name, isInputTarget);
[10717]138      }
[15110]139      SuppressCheckedChangedUpdate = false;
140      CheckedChangedUpdate();
[10717]141    }
[15110]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);
[10972]147      }
[15110]148      SuppressCheckedChangedUpdate = false;
149      CheckedChangedUpdate();
[10972]150    }
[15110]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);
[10717]156      }
[15110]157      SuppressCheckedChangedUpdate = false;
158      CheckedChangedUpdate();
[10717]159    }
[10303]160  }
161}
162
163
Note: See TracBrowser for help on using the repository browser.