Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/FilterView.cs @ 15224

Last change on this file since 15224 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.3 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.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Core.Views;
27using HeuristicLab.DataPreprocessing.Filter;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.DataPreprocessing.Views {
31  [View("CheckedFilterCollection View")]
32  [Content(typeof(FilterContent), true)]
33  public partial class FilterView : ItemView {
34
35    public FilterView() {
36      InitializeComponent();
37      tbTotal.Text = "0";
38      tbRemaining.Text = "0";
39      tbPercentage.Text = "0%";
40    }
41
42    private void InitData() {
43      checkedFilterView.Content = Content.Filters;
44      checkedFilterView.Content.ItemsAdded += Content_ItemsAdded;
45      checkedFilterView.Content.ItemsRemoved += Content_ItemsRemoved;
46      checkedFilterView.Content.CheckedItemsChanged += Content_CheckedItemsChanged;
47    }
48
49    public new FilterContent Content {
50      get { return (FilterContent)base.Content; }
51      set { base.Content = value; }
52    }
53
54    protected override void OnContentChanged() {
55      base.OnContentChanged();
56      if (Content != null) {
57        InitData();
58        UpdateFilterInfo();
59      }
60    }
61
62    private void Content_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
63      if (Content != null) {
64        foreach (IFilter filter in e.Items) {
65          filter.Active = checkedFilterView.Content.ItemChecked(filter);
66        }
67        UpdateFilterInfo();
68      }
69    }
70
71    private void UpdateFilterInfo() {
72      List<IFilter> filters = Content.Filters.ToList();
73      int activeFilters = filters.Count(c => c.Active);
74      applyFilterButton.Enabled = (activeFilters > 0);
75      rBtnAnd.Enabled = (activeFilters > 0);
76      rBtnOr.Enabled = (activeFilters > 0);
77      Content.FilterLogic.Reset();
78      bool[] result = Content.FilterLogic.Preview(filters, rBtnAnd.Checked);
79
80      int filteredCnt = result.Count(c => !c);
81
82      tbRemaining.Text = filteredCnt.ToString();
83      double percentage = result.Length == 0 ? 0.0 : filteredCnt * 100 / (double)result.Length;
84      tbPercentage.Text = String.Format("{0:0.0000}%", percentage);
85      tbTotal.Text = result.Length.ToString();
86    }
87
88    private void applyFilterButton_Click(object sender, EventArgs e) {
89      if (Content != null) {
90        List<IFilter> filters = Content.Filters.ToList();
91        //apply filters
92        Content.FilterLogic.Apply(filters, rBtnAnd.Checked);
93        //deactivate checked filters
94        filters = checkedFilterView.Content.CheckedItems.ToList();
95        foreach (IFilter filter in filters) {
96          checkedFilterView.Content.SetItemCheckedState(filter, false);
97          filter.Active = false;
98        }
99        UpdateFilterInfo();
100      }
101    }
102
103    //whenever a new filter is added the preprocessing data is set to the filter
104    private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IFilter> e) {
105      if (Content != null) {
106        foreach (IFilter filter in e.Items) {
107          filter.ConstrainedValue = Content.FilterLogic.PreprocessingData;
108        }
109      }
110    }
111
112    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IFilter> e) {
113      if (Content != null) {
114        UpdateFilterInfo();
115      }
116    }
117
118    private void rBtnAnd_CheckedChanged(object sender, EventArgs e) {
119      if (Content != null) {
120        UpdateFilterInfo();
121        Content.IsAndCombination = rBtnAnd.Checked;
122      }
123    }
124
125  }
126}
Note: See TracBrowser for help on using the repository browser.