Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing.Views/3.4/FilterView.cs @ 15466

Last change on this file since 15466 was 15466, checked in by pfleck, 6 years ago

#2809: Simplified the overall filtering logic as suggested by bburlacu

  • changed parameter names to actively reflect that filter means "remaining"
  • moved filter combination logic to FilterContent
  • simplified/restructured code
File size: 4.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.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core.Views;
26using HeuristicLab.DataPreprocessing.Filter;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.DataPreprocessing.Views {
30  [View("CheckedFilterCollection View")]
31  [Content(typeof(FilterContent), true)]
32  public partial class FilterView : ItemView {
33    public new FilterContent Content {
34      get { return (FilterContent)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public FilterView() {
39      InitializeComponent();
40      tbTotal.Text = "0";
41      tbRemaining.Text = "0";
42      tbPercentage.Text = "0%";
43    }
44
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
47      if (Content != null) {
48        checkedFilterView.Content = Content.Filters;
49        rBtnAnd.Checked = Content.IsAndCombination;
50        rBtnOr.Checked = !Content.IsAndCombination;
51        UpdateFilter();
52      } else {
53        checkedFilterView.Content = null;
54      }
55    }
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.Filters.ItemsAdded += Content_ItemsAdded;
59      Content.Filters.ItemsRemoved += Content_ItemsRemoved;
60      Content.Filters.CheckedItemsChanged += Content_CheckedItemsChanged;
61    }
62    protected override void DeregisterContentEvents() {
63      Content.Filters.ItemsAdded -= Content_ItemsAdded;
64      Content.Filters.ItemsRemoved -= Content_ItemsRemoved;
65      Content.Filters.CheckedItemsChanged -= Content_CheckedItemsChanged;
66      base.DeregisterContentEvents();
67    }
68
69    private void UpdateFilter() {
70      bool activeFilters = Content.ActiveFilters.Any();
71      applyFilterButton.Enabled = activeFilters;
72
73      int numTotal = Content.PreprocessingData.Rows;
74      int numRemaining = numTotal;
75
76      Content.PreprocessingData.ResetFilter();
77      if (activeFilters) {
78        var remainingRows = Content.GetRemainingRows();
79        Content.PreprocessingData.SetFilter(remainingRows);
80        numRemaining = remainingRows.Count(x => x);
81      }
82
83      tbRemaining.Text = numRemaining.ToString();
84      double ratio = numTotal > 0 ? numRemaining / (double)numTotal : 0.0;
85      tbPercentage.Text = ratio.ToString("P4");
86      tbTotal.Text = numTotal.ToString();
87    }
88
89
90    #region Content Events
91    //whenever a new filter is added the preprocessing data is set to the filter
92    private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
93      if (Content != null) {
94        foreach (IFilter filter in e.Items) {
95          filter.ConstrainedValue = Content.PreprocessingData;
96        }
97      }
98    }
99    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IFilter> e) {
100      if (Content != null) {
101        UpdateFilter();
102      }
103    }
104    private void Content_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
105      if (Content != null) {
106        foreach (IFilter filter in e.Items) {
107          filter.Active = checkedFilterView.Content.ItemChecked(filter);
108        }
109        UpdateFilter();
110      }
111    }
112    #endregion
113
114    #region Controls Events
115    private void rBtnAnd_CheckedChanged(object sender, EventArgs e) {
116      if (Content != null) {
117        Content.IsAndCombination = rBtnAnd.Checked;
118        UpdateFilter();
119      }
120    }
121    private void applyFilterButton_Click(object sender, EventArgs e) {
122      if (Content != null) {
123        //apply filters
124        Content.PreprocessingData.PersistFilter();
125        Content.PreprocessingData.ResetFilter();
126        //deactivate checked filters
127        foreach (var filter in Content.Filters.CheckedItems) {
128          checkedFilterView.Content.SetItemCheckedState(filter, false);
129          filter.Active = false;
130        }
131        UpdateFilter();
132      }
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.