Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/ShapeConstraintView.cs

Last change on this file was 18086, checked in by mkommend, 3 years ago

#2521: Merged trunk changes into branch.

File size: 7.3 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 && Content.Variable != String.Empty;
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      } else {
115        numberOfDerivationsComboBox.Enabled = true;
116        numberOfDerivationsComboBox.SelectedItem = Content.NumberOfDerivations;
117      }
118
119      if (Content.Regions.Count > 0) {
120        regionView.Content = Content.Regions;
121        regionLabel.Visible = true;
122        regionView.Visible = true;
123      } else {
124        regionLabel.Visible = false;
125        regionView.Visible = false;
126      }
127    }
128
129    #endregion
130
131    #region control event handlers
132
133    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
134      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
135      if (double.IsNaN(value)) {
136        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be a double value.");
137        e.Cancel = true;
138        return;
139      }
140
141      if (value > Content.Interval.UpperBound) {
142        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be smaller than upper bound.");
143        e.Cancel = true;
144        return;
145      }
146
147      errorProvider.SetError(lowerboundInput, string.Empty);
148      e.Cancel = false;
149    }
150
151    private void lowerboundInput_Validated(object sender, EventArgs e) {
152      var value =
153        ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
154      if (!double.IsNaN(value)) Content.Interval = new Interval(value, Content.Interval.UpperBound);
155    }
156
157    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
158      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
159      if (double.IsNaN(value)) {
160        errorProvider.SetError(upperboundInput, "Invalid Input: upper bound must be a double value.");
161        e.Cancel = true;
162        return;
163      }
164
165      if (value < Content.Interval.LowerBound) {
166        errorProvider.SetError(upperboundInput, "Invalid input: upper bound must be bigger than lower bound.");
167        e.Cancel = true;
168        return;
169      }
170
171      errorProvider.SetError(upperboundInput, string.Empty);
172      e.Cancel = false;
173    }
174
175    private void upperboundInput_Validated(object sender, EventArgs e) {
176      var value =
177        ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
178      if (!double.IsNaN(value)) Content.Interval = new Interval(Content.Interval.LowerBound, value);
179    }
180
181    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
182      if (numberOfDerivationsComboBox.SelectedItem == null) {
183        Content.NumberOfDerivations = 0;
184        numberOfDerivationsComboBox.Enabled = false;
185        return;
186      }
187
188      if (Content != null) {
189        if ((int)numberOfDerivationsComboBox.SelectedItem == 1)
190          Content.NumberOfDerivations = 1;
191        else if ((int)numberOfDerivationsComboBox.SelectedItem == 2)
192          Content.NumberOfDerivations = 2;
193        else if ((int)numberOfDerivationsComboBox.SelectedItem == 3)
194          Content.NumberOfDerivations = 3;
195      }
196    }
197
198    private void weightInput_TextChanged(object sender, EventArgs e) {
199      var value = ParseDoubleValue(weightTextBox.Text, weightTextBox, errorProvider);
200      if (!double.IsNaN(value)) Content.Weight = value;
201    }
202
203    private void variableInput_TextChanged(object sender, EventArgs e) {
204      var value = variableInput.Text;
205      if (!String.IsNullOrEmpty(value)) Content.Variable = value;
206    }
207
208    #endregion
209
210    #region content event handlers
211
212    private void Content_Changed(object sender, EventArgs e) {
213      UpdateControls();
214    }
215
216
217    #endregion
218  }
219}
Note: See TracBrowser for help on using the repository browser.