Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17915 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
RevLine 
[17607]1#region License Information
2
3/* HeuristicLab
[17896]4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[17607]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 {
[17896]31  [View("Shape Constraint View")]
[17887]32  [Content(typeof(ShapeConstraint), true)]
[17896]33  public sealed partial class ShapeConstraintView : ItemView {
[17887]34    public new ShapeConstraint Content {
[17891]35      get => (ShapeConstraint)base.Content;
[17607]36      set => base.Content = value;
37    }
38
[17896]39    public ShapeConstraintView() {
[17607]40      InitializeComponent();
[17891]41      int[] items = { 1, 2, 3 };
[17896]42      numberOfDerivationsComboBox.DataSource = items;
[17607]43    }
44
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
[17907]47      if (Content == null) {
48        this.regionView.Content = null;
49      } else {
50        this.regionView.Content = Content.Regions;
51        UpdateControls();
52      }
[17607]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();
[17891]67      variableInput.Enabled = Content != null && !Locked && !ReadOnly;
[17896]68      numberOfDerivationsComboBox.Enabled = Content != null && !Locked && !ReadOnly;
[17891]69      lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
70      upperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
[17896]71      weightTextBox.Enabled = Content != null && !Locked && !ReadOnly;
[17607]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: {
[17891]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;
[17607]94          }
95      }
96    }
97
98    private void UpdateControls() {
99      if (Content == null) {
100        lowerboundInput.Text = string.Empty;
101        upperboundInput.Text = string.Empty;
[17896]102        weightTextBox.Text = string.Empty;
[17607]103        return;
104      }
105
106      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
107      upperboundInput.Text = Content.Interval.UpperBound.ToString();
[17896]108      weightTextBox.Text = Content.Weight.ToString();
[17607]109
110      variableInput.Text = Content.Variable;
111      if (!Content.IsDerivative) {
[17896]112        numberOfDerivationsComboBox.Enabled = false;
113        numberOfDerivationsComboBox.SelectedItem = null;
114        numberOfDerivationsComboBox.Text = "0";
[17891]115      } else {
[17896]116        numberOfDerivationLabel.Visible = true;
117        numberOfDerivationsComboBox.Visible = true;
118        numberOfDerivationsComboBox.Enabled = true;
119        numberOfDerivationsComboBox.SelectedItem = Content.NumberOfDerivations;
[17607]120      }
[17893]121
122      regionView.Content = Content.Regions;
[17607]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)) {
[17891]132        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be a double value.");
[17607]133        e.Cancel = true;
134        return;
135      }
136
137      if (value > Content.Interval.UpperBound) {
[17891]138        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be smaller than upper bound.");
[17607]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)) {
[17891]156        errorProvider.SetError(upperboundInput, "Invalid Input: upper bound must be a double value.");
[17607]157        e.Cancel = true;
158        return;
159      }
160
161      if (value < Content.Interval.LowerBound) {
[17891]162        errorProvider.SetError(upperboundInput, "Invalid input: upper bound must be bigger than lower bound.");
[17607]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) {
[17896]178      if (numberOfDerivationsComboBox.SelectedItem == null) {
[17891]179        Content.NumberOfDerivations = 0;
[17896]180        numberOfDerivationsComboBox.Enabled = false;
[17607]181        return;
182      }
183
[17907]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      }
[17607]192    }
193
[17893]194    private void weightInput_TextChanged(object sender, EventArgs e) {
[17896]195      var value = ParseDoubleValue(weightTextBox.Text, weightTextBox, errorProvider);
[17893]196      if (!double.IsNaN(value)) Content.Weight = value;
197    }
198
199
[17607]200    #endregion
201
202    #region content event handlers
203
204    private void Content_Changed(object sender, EventArgs e) {
205      UpdateControls();
206    }
207
[17893]208   #endregion
[17607]209  }
210}
Note: See TracBrowser for help on using the repository browser.