Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/Views/ErcOptionsView.cs @ 15692

Last change on this file since 15692 was 15032, checked in by pkimmesw, 7 years ago

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File size: 3.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Views {
2  using System.Linq;
3  using Core;
4  using Core.Views;
5  using MainForm;
6
7  [View("ERC Options")]
8  [Content(typeof(ErcOptions), true)]
9  public partial class ErcOptionsView : NamedItemView {
10    private const string PERCENTAGE_FORMAT = "0%";
11    private CheckedItemCollection<IErcItem> ercItems;
12
13    public ErcOptionsView() {
14      InitializeComponent();
15
16      possibilityTextBox.Validating += PossibilityTextBoxValidating;
17      possibilityTextBox.KeyDown += PossibilityTextBoxKeyDown;
18    }
19
20    private void PossibilityTextBoxKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
21      if (e.KeyCode == System.Windows.Forms.Keys.Enter) {
22        Validate();
23      }
24    }
25
26    private void PossibilityTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e) {
27      var text = possibilityTextBox.Text.Trim();
28      if (text.EndsWith("%")) {
29        text = text.Substring(0, text.Length - 1);
30      }
31
32      double value;
33      if (double.TryParse(text, out value)) {
34        if (value > 1) value /= 100.0;
35        possibilityTextBox.Text = value.ToString(PERCENTAGE_FORMAT);
36      } else {
37        possibilityTextBox.Text = "0%";
38        e.Cancel = true;
39      }
40
41      Content.ErcProbability = value;
42    }
43
44    public new ErcOptions Content
45    {
46      get { return (ErcOptions)base.Content; }
47      set
48      {
49        ClearOldContent();
50        base.Content = value;
51      }
52    }
53
54    protected override void OnReadOnlyChanged() {
55      base.OnReadOnlyChanged();
56
57      possibilityTextBox.ReadOnly = ReadOnly;
58    }
59
60    protected override void OnContentChanged() {
61      if (Content == null)
62        return;
63
64      nameTextBox.Text = Content.Name;
65      possibilityTextBox.Text = Content.ErcProbability.ToString(PERCENTAGE_FORMAT);
66
67      var ercOptions = typeof(ErcOptions)
68        .GetProperties()
69        .Where(p => typeof(IErcItem).IsAssignableFrom(p.PropertyType))
70        .Select(p => (IErcItem)p.GetValue(Content))
71        .ToArray();
72
73      ercItems = new CheckedItemCollection<IErcItem>(ercOptions);
74
75      ercItems.ForEach(item => {
76        item.EnabledChanged += OptionEnabledChanged;
77        ercItems.SetItemCheckedState(item, item.IsEnabled);
78      });
79
80      ercItems.CheckedItemsChanged += ErcItemsCheckedItemsChanged;
81      ErcOptionListView.Content = new ReadOnlyCheckedItemCollection<IErcItem>(ercItems);
82    }
83
84    private void ClearOldContent() {
85      if (ercItems != null) {
86        ercItems.ForEach(item => item.EnabledChanged -= OptionEnabledChanged);
87        ercItems.CheckedItemsChanged -= ErcItemsCheckedItemsChanged;
88        ercItems = null;
89      }
90    }
91
92    private void ErcItemsCheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IErcItem> e) {
93      foreach (var item in e.Items)
94        item.IsEnabled = ercItems.ItemChecked(item);
95    }
96
97    private void OptionEnabledChanged(object sender, bool state) {
98      var item = (IErcItem)sender;
99
100      // avoid unnecessary state change events
101      if (ercItems.ItemChecked(item) != state)
102        ercItems.SetItemCheckedState(item, state);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.