Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalConstraintView.cs @ 17887

Last change on this file since 17887 was 17887, checked in by gkronber, 3 years ago

#3073: intermediate comment while refactoring the branch

File size: 6.9 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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("Interval Constraint Detail View")]
32  [Content(typeof(ShapeConstraint), true)]
33  public sealed partial class IntervalConstraintView : ItemView {
34    public new ShapeConstraint Content {
35      get => (ShapeConstraint) base.Content;
36      set => base.Content = value;
37    }
38
39    public IntervalConstraintView() {
40      InitializeComponent();
41      int[] items = {1, 2, 3};
42      expressionInput.ReadOnly         = true;
43      numberderivationInput.DataSource = items;
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      UpdateControls();
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.Changed += Content_Changed;
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.Changed -= Content_Changed;
58      base.DeregisterContentEvents();
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      expressionInput.Enabled       = Content != null && !Locked && !ReadOnly;
64      variableInput.Enabled         = Content != null && !Locked && !ReadOnly;
65      numberderivationInput.Enabled = Content != null && !Locked && !ReadOnly;
66      lowerboundInput.Enabled       = Content != null && !Locked && !ReadOnly;
67      upperboundInput.Enabled       = Content != null && !Locked && !ReadOnly;
68      ischeckedCheckBox.Enabled     = Content != null && !Locked && !ReadOnly;
69    }
70
71
72    #region helpers
73
74    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
75      input = input.ToLower();
76      switch (input) {
77        case "inf.":
78        case "+inf.":
79        case "Infinity":
80          return double.PositiveInfinity;
81        case "-inf.":
82        case "-Infinity":
83          return double.NegativeInfinity;
84        default: {
85          if (double.TryParse(input, out var value)) {
86            return value;
87          }
88
89          errorProvider.SetError(control, "Invalid Input: Value must be a double!");
90          return double.NaN;
91        }
92      }
93    }
94
95    private void UpdateControls() {
96      if (Content == null) {
97        expressionInput.Text = string.Empty;
98        lowerboundInput.Text = string.Empty;
99        upperboundInput.Text = string.Empty;
100        return;
101      }
102
103      expressionInput.Text = Content.Expression;
104      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
105      upperboundInput.Text = Content.Interval.UpperBound.ToString();
106
107      variableInput.Text = Content.Variable;
108      if (!Content.IsDerivative) {
109        numberderivationInput.Enabled      = false;
110        numberderivationInput.SelectedItem = null;
111        numberderivationInput.Text         = "0";
112      }
113      else {
114        numberderivationLabel.Visible      = true;
115        numberderivationInput.Visible      = true;
116        numberderivationInput.Enabled      = true;
117        numberderivationInput.SelectedItem = Content.NumberOfDerivations;
118      }
119
120      ischeckedCheckBox.Checked = Content.Enabled;
121    }
122
123    #endregion
124
125    #region control event handlers
126
127    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
128      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
129      if (double.IsNaN(value)) {
130        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
131        e.Cancel = true;
132        return;
133      }
134
135      if (value > Content.Interval.UpperBound) {
136        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
137        e.Cancel = true;
138        return;
139      }
140
141      errorProvider.SetError(lowerboundInput, string.Empty);
142      e.Cancel = false;
143    }
144
145    private void lowerboundInput_Validated(object sender, EventArgs e) {
146      var value =
147        ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
148      if (!double.IsNaN(value)) Content.Interval = new Interval(value, Content.Interval.UpperBound);
149    }
150
151    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
152      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
153      if (double.IsNaN(value)) {
154        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
155        e.Cancel = true;
156        return;
157      }
158
159      if (value < Content.Interval.LowerBound) {
160        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
161        e.Cancel = true;
162        return;
163      }
164
165      errorProvider.SetError(upperboundInput, string.Empty);
166      e.Cancel = false;
167    }
168
169    private void upperboundInput_Validated(object sender, EventArgs e) {
170      var value =
171        ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
172      if (!double.IsNaN(value)) Content.Interval = new Interval(Content.Interval.LowerBound, value);
173    }
174
175    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
176      Content.Enabled = ischeckedCheckBox.Checked;
177    }
178
179    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
180      if (numberderivationInput.SelectedItem == null) {
181        Content.NumberOfDerivations   = 0;
182        numberderivationInput.Enabled = false;
183        return;
184      }
185
186      if ((int) numberderivationInput.SelectedItem == 1)
187        Content.NumberOfDerivations = 1;
188      else if ((int) numberderivationInput.SelectedItem == 2)
189        Content.NumberOfDerivations = 2;
190      else if ((int) numberderivationInput.SelectedItem == 3)
191        Content.NumberOfDerivations = 3;
192    }
193
194    #endregion
195
196    #region content event handlers
197
198    private void Content_Changed(object sender, EventArgs e) {
199      UpdateControls();
200    }
201
202    #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.