Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/ShapeConstraintView.cs @ 17912

Last change on this file since 17912 was 17907, checked in by chaider, 4 years ago

#3073

  • Set cloning constructor to protected in IntervalArithCompiledExpressionBoundsEstimator
  • Add Content == null handle in the OnContentChanged in ShapeConstraintView
File size: 7.1 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.ComponentModel;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Shape Constraint View")]
32  [Content(typeof(ShapeConstraint), true)]
33  public sealed partial class ShapeConstraintView : ItemView {
34    public new ShapeConstraint Content {
35      get => (ShapeConstraint)base.Content;
36      set => base.Content = value;
37    }
38
39    public ShapeConstraintView() {
40      InitializeComponent();
41      int[] items = { 1, 2, 3 };
42      numberOfDerivationsComboBox.DataSource = items;
43    }
44
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
47      if (Content == null) {
48        this.regionView.Content = null;
49      } else {
50        this.regionView.Content = Content.Regions;
51        UpdateControls();
52      }
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.Changed += Content_Changed;
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.Changed -= Content_Changed;
62      base.DeregisterContentEvents();
63    }
64
65    protected override void SetEnabledStateOfControls() {
66      base.SetEnabledStateOfControls();
67      variableInput.Enabled = Content != null && !Locked && !ReadOnly;
68      numberOfDerivationsComboBox.Enabled = Content != null && !Locked && !ReadOnly;
69      lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
70      upperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
71      weightTextBox.Enabled = Content != null && !Locked && !ReadOnly;
72    }
73
74
75    #region helpers
76
77    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
78      input = input.ToLower();
79      switch (input) {
80        case "inf.":
81        case "+inf.":
82        case "Infinity":
83          return double.PositiveInfinity;
84        case "-inf.":
85        case "-Infinity":
86          return double.NegativeInfinity;
87        default: {
88            if (double.TryParse(input, out var value)) {
89              return value;
90            }
91
92            errorProvider.SetError(control, "Invalid input: value must be a double.");
93            return double.NaN;
94          }
95      }
96    }
97
98    private void UpdateControls() {
99      if (Content == null) {
100        lowerboundInput.Text = string.Empty;
101        upperboundInput.Text = string.Empty;
102        weightTextBox.Text = string.Empty;
103        return;
104      }
105
106      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
107      upperboundInput.Text = Content.Interval.UpperBound.ToString();
108      weightTextBox.Text = Content.Weight.ToString();
109
110      variableInput.Text = Content.Variable;
111      if (!Content.IsDerivative) {
112        numberOfDerivationsComboBox.Enabled = false;
113        numberOfDerivationsComboBox.SelectedItem = null;
114        numberOfDerivationsComboBox.Text = "0";
115      } else {
116        numberOfDerivationLabel.Visible = true;
117        numberOfDerivationsComboBox.Visible = true;
118        numberOfDerivationsComboBox.Enabled = true;
119        numberOfDerivationsComboBox.SelectedItem = Content.NumberOfDerivations;
120      }
121
122      regionView.Content = Content.Regions;
123    }
124
125    #endregion
126
127    #region control event handlers
128
129    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
130      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
131      if (double.IsNaN(value)) {
132        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be a double value.");
133        e.Cancel = true;
134        return;
135      }
136
137      if (value > Content.Interval.UpperBound) {
138        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be smaller than upper bound.");
139        e.Cancel = true;
140        return;
141      }
142
143      errorProvider.SetError(lowerboundInput, string.Empty);
144      e.Cancel = false;
145    }
146
147    private void lowerboundInput_Validated(object sender, EventArgs e) {
148      var value =
149        ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
150      if (!double.IsNaN(value)) Content.Interval = new Interval(value, Content.Interval.UpperBound);
151    }
152
153    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
154      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
155      if (double.IsNaN(value)) {
156        errorProvider.SetError(upperboundInput, "Invalid Input: upper bound must be a double value.");
157        e.Cancel = true;
158        return;
159      }
160
161      if (value < Content.Interval.LowerBound) {
162        errorProvider.SetError(upperboundInput, "Invalid input: upper bound must be bigger than lower bound.");
163        e.Cancel = true;
164        return;
165      }
166
167      errorProvider.SetError(upperboundInput, string.Empty);
168      e.Cancel = false;
169    }
170
171    private void upperboundInput_Validated(object sender, EventArgs e) {
172      var value =
173        ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
174      if (!double.IsNaN(value)) Content.Interval = new Interval(Content.Interval.LowerBound, value);
175    }
176
177    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
178      if (numberOfDerivationsComboBox.SelectedItem == null) {
179        Content.NumberOfDerivations = 0;
180        numberOfDerivationsComboBox.Enabled = false;
181        return;
182      }
183
184      if (Content != null) {
185        if ((int)numberOfDerivationsComboBox.SelectedItem == 1)
186          Content.NumberOfDerivations = 1;
187        else if ((int)numberOfDerivationsComboBox.SelectedItem == 2)
188          Content.NumberOfDerivations = 2;
189        else if ((int)numberOfDerivationsComboBox.SelectedItem == 3)
190          Content.NumberOfDerivations = 3;
191      }
192    }
193
194    private void weightInput_TextChanged(object sender, EventArgs e) {
195      var value = ParseDoubleValue(weightTextBox.Text, weightTextBox, errorProvider);
196      if (!double.IsNaN(value)) Content.Weight = value;
197    }
198
199
200    #endregion
201
202    #region content event handlers
203
204    private void Content_Changed(object sender, EventArgs e) {
205      UpdateControls();
206    }
207
208   #endregion
209  }
210}
Note: See TracBrowser for help on using the repository browser.