Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingCheckedVariablesView.cs @ 14401

Last change on this file since 14401 was 14401, checked in by gkronber, 7 years ago

#2650: merged r14378:14400 from trunk to branch

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;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
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 abstract partial class PreprocessingCheckedVariablesView : ItemView {
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
46    protected bool IsVariableChecked(string name) {
47      return Content.VariableItemList.CheckedItems.Any(x => x.Value.Value == name);
48    }
49    protected IList<string> GetCheckedVariables() {
50      return checkedItemList.Content.CheckedItems.Select(i => i.Value.Value).ToList();
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content == null) return;
56
57      if (Content.VariableItemList == null) {
58        Content.VariableItemList = Content.CreateVariableItemList();
59      } else {
60        var checkedNames = Content.VariableItemList.CheckedItems.Select(x => x.Value.Value);
61        Content.VariableItemList = Content.CreateVariableItemList(checkedNames);
62      }
63      Content.VariableItemList.CheckedItemsChanged += CheckedItemsChanged;
64
65      checkedItemList.Content = Content.VariableItemList;
66      var target = Content.PreprocessingData.TargetVariable;
67      var inputAndTarget = Content.PreprocessingData.InputVariables.Union(target != null ? new[] { target } : new string[] { });
68      foreach (var col in Content.PreprocessingData.GetDoubleVariableNames().Except(inputAndTarget)) {
69        var listViewItem = checkedItemList.ItemsListView.FindItemWithText(col);
70        listViewItem.ForeColor = Color.LightGray;
71      }
72    }
73    protected override void RegisterContentEvents() {
74      base.RegisterContentEvents();
75      Content.PreprocessingData.Changed += PreprocessingData_Changed;
76      Content.PreprocessingData.SelectionChanged += PreprocessingData_SelctionChanged;
77    }
78    protected override void DeregisterContentEvents() {
79      Content.PreprocessingData.Changed -= PreprocessingData_Changed;
80      Content.PreprocessingData.SelectionChanged -= PreprocessingData_SelctionChanged;
81      base.DeregisterContentEvents();
82    }
83
84    protected virtual void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
85    }
86
87    private void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
88      switch (e.Type) {
89        case DataPreprocessingChangedEventType.DeleteColumn:
90          RemoveVariable(Content.PreprocessingData.GetVariableName(e.Column));
91          break;
92        case DataPreprocessingChangedEventType.AddColumn:
93          AddVariable(Content.PreprocessingData.GetVariableName(e.Column));
94          break;
95        case DataPreprocessingChangedEventType.ChangeColumn:
96        case DataPreprocessingChangedEventType.ChangeItem:
97          UpdateVariable(Content.PreprocessingData.GetVariableName(e.Column));
98          break;
99        default:
100          ResetAllVariables();
101          break;
102      }
103    }
104
105    protected virtual void AddVariable(string name) {
106      Content.VariableItemList.Add(new StringValue(name));
107      if (!Content.PreprocessingData.InputVariables.Contains(name) && Content.PreprocessingData.TargetVariable != name) {
108        var listViewItem = checkedItemList.ItemsListView.FindItemWithText(name);
109        listViewItem.ForeColor = Color.LightGray;
110      }
111    }
112    protected virtual void RemoveVariable(string name) {
113      var stringValue = Content.VariableItemList.SingleOrDefault(n => n.Value == name);
114      if (stringValue != null)
115        Content.VariableItemList.Remove(stringValue);
116    }
117    protected virtual void UpdateVariable(string name) {
118    }
119    protected virtual void ResetAllVariables() {
120    }
121
122    protected virtual void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
123    }
124
125    #region ContextMenu Events
126    private void variablesListcontextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
127      var data = Content.PreprocessingData;
128      checkInputsTargetToolStripMenuItem.Text = "Check Inputs" + (data.TargetVariable != null ? "+Target" : "");
129      checkOnlyInputsTargetToolStripMenuItem.Text = "Check only Inputs" + (data.TargetVariable != null ? "+Target" : "");
130    }
131    private void checkInputsTargetToolStripMenuItem_Click(object sender, EventArgs e) {
132      foreach (var name in checkedItemList.Content) {
133        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
134        if (isInputTarget) {
135          checkedItemList.Content.SetItemCheckedState(name, true);
136        }
137      }
138    }
139    private void checkOnlyInputsTargetToolStripMenuItem_Click(object sender, EventArgs e) {
140      foreach (var name in checkedItemList.Content) {
141        var isInputTarget = Content.PreprocessingData.InputVariables.Contains(name.Value) || Content.PreprocessingData.TargetVariable == name.Value;
142        checkedItemList.Content.SetItemCheckedState(name, isInputTarget);
143      }
144    }
145    private void checkAllToolStripMenuItem_Click(object sender, EventArgs e) {
146      foreach (var name in checkedItemList.Content) {
147        checkedItemList.Content.SetItemCheckedState(name, true);
148      }
149    }
150    private void uncheckAllToolStripMenuItem_Click(object sender, EventArgs e) {
151      foreach (var name in checkedItemList.Content) {
152        checkedItemList.Content.SetItemCheckedState(name, false);
153      }
154    }
155    #endregion
156  }
157}
158
159
Note: See TracBrowser for help on using the repository browser.