Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12214 was 11044, checked in by sbreuer, 11 years ago

show remaining values in filter preview

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