Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalConstraintView.cs @ 16830

Last change on this file since 16830 was 16830, checked in by chaider, 5 years ago

#2971 Fixed review comments

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using System.ComponentModel;
23using System.Globalization;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Problems.DataAnalysis.Views {
29  [View("Interval Constraint Detail View")]
30  [Content(typeof(IntervalConstraint), true)]
31  public partial class IntervalConstraintView : ItemView {
32
33    public new IntervalConstraint Content {
34      get => (IntervalConstraint) base.Content;
35      set => base.Content = value;
36    }
37
38    public IntervalConstraintView() {
39      InitializeComponent();
40      int [] items = {1, 2, 3};
41      expressionInput.ReadOnly = true;
42      definitionInput.ReadOnly = true;
43      derivationInput.Enabled = false;
44      derivationInput.Checked = false;
45      numberderivationInput.DataSource = items;
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      UpdateControls();
51    }
52
53    protected override void RegisterContentEvents() {
54      base.RegisterContentEvents();
55      Content.Changed += new EventHandler(Content_Changed);
56    }
57
58    protected override void DeregisterContentEvents() {
59      base.DeregisterContentEvents();
60      Content.Changed -= new EventHandler(Content_Changed);
61    }
62
63    protected override void SetEnabledStateOfControls() {
64      base.SetEnabledStateOfControls();
65      lowerboundInput.Enabled = Content != null && !Locked;
66    }
67
68
69
70    #region helpers
71
72    private double ParseDoubleValue(string input, Control control) {
73      input = input.ToLower();
74      switch (input) {
75        case "inf.":
76        case "+inf.":
77        case "Infinity":
78          return double.PositiveInfinity;
79        case "-inf.":
80        case "-Infinity":
81          return double.NegativeInfinity;
82        default: {
83          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
84            return value;
85          else {
86            errorProvider.SetError(control, "Invalid Input: Value must be a double!");
87            return double.NaN;
88          }
89        }
90      }
91    }
92
93    private void UpdateControls() {
94      if (Content == null) {
95        expressionInput.Text = string.Empty;
96        lowerboundInput.Text = string.Empty;
97        upperboundInput.Text = string.Empty;
98        incllowerboundInput.Checked = true;
99        inclupperboundInput.Checked = true;
100      } else {
101        expressionInput.Text = Content.Expression;
102        definitionInput.Text = Content.Definition;
103        lowerboundInput.Text = Content.Interval.LowerBound.ToString(CultureInfo.InvariantCulture);
104        upperboundInput.Text = Content.Interval.UpperBound.ToString(CultureInfo.InvariantCulture);
105        incllowerboundInput.Checked = Content.InclusiveLowerBound;
106        inclupperboundInput.Checked = Content.InclusiveUpperBound;
107        derivationInput.Checked = Content.IsDerivation;
108        variableInput.Text = Content.Variable;
109        if (!Content.IsDerivation) {
110          numberderivationInput.Enabled = false;
111        } else {
112          numberderivationInput.Enabled = true;
113          numberderivationInput.SelectedItem = Content.NumberOfDerivation;
114        }
115
116        ischeckedCheckBox.Checked = Content.Enabled;
117      }
118      SetEnabledStateOfControls();
119    }
120
121    private static string UpdateExpression(IntervalConstraint constraint) {
122      var expression = "";
123
124      if (!constraint.IsDerivation) {
125        expression = string.Format("{0} in {1}{2} .. {3}{4}",
126          constraint.Variable,
127          (constraint.InclusiveLowerBound) ? "[" : "]",
128          constraint.Interval.LowerBound,
129          constraint.Interval.UpperBound,
130          (constraint.InclusiveUpperBound) ? "]" : "[");
131      } else {
132        expression = string.Format("\u2202{5}Target/\u2202{0}{6} in {1}{2} .. {3}{4}",
133          constraint.Variable,
134          (constraint.InclusiveLowerBound) ? "[" : "]",
135          constraint.Interval.LowerBound,
136          constraint.Interval.UpperBound,
137          (constraint.InclusiveUpperBound) ? "]" : "[",
138          PrintNumberOfDerivation(constraint.numberOfDerivation),
139          PrintNumberOfDerivation(constraint.numberOfDerivation));
140      }
141
142      return expression;
143    }
144
145    private static string PrintNumberOfDerivation(int derivation) {
146      switch (derivation) {
147        case 1:
148          return "";
149        case 2:
150          return "²";
151        case 3:
152          return "³";
153        default:
154          return "";
155      }
156    }
157
158    #endregion
159
160    #region control event handlers
161    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
162      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
163      if (double.IsNaN(value)) {
164        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
165        e.Cancel = true;
166        return;
167      }
168
169      if (value > Content.Interval.UpperBound) {
170        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
171        e.Cancel = true;
172        return;
173      }
174
175      errorProvider.SetError(lowerboundInput, string.Empty);
176      e.Cancel = false;
177    }
178
179    private void lowerboundInput_Validated(object sender, EventArgs e) {
180      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
181      if (!double.IsNaN(value)) {
182        Content.Interval = new Interval(value, Content.Interval.UpperBound);
183        var exp = UpdateExpression(Content);
184        Content.Name = exp;
185        Content.Expression = exp;
186      }
187    }
188
189    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
190      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
191      if (double.IsNaN(value)) {
192        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
193        e.Cancel = true;
194        return;
195      }
196
197      if (value < Content.Interval.LowerBound) {
198        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
199        e.Cancel = true;
200        return;
201      }
202
203      errorProvider.SetError(upperboundInput, string.Empty);
204      e.Cancel = false;
205    }
206
207    private void upperboundInput_Validated(object sender, EventArgs e) {
208      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
209      if (!double.IsNaN(value)) {
210        Content.Interval = new Interval(Content.Interval.LowerBound, value);
211        var exp = UpdateExpression(Content);
212        Content.Name = exp;
213        Content.Expression = exp;
214      }
215    }
216
217    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
218      if (Content.InclusiveLowerBound != incllowerboundInput.Checked) {
219        Content.InclusiveLowerBound = incllowerboundInput.Checked;
220        var exp = UpdateExpression(Content);
221        Content.Name = exp;
222        Content.Expression = exp;
223      }
224     
225     
226    }
227
228    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
229      if (Content.InclusiveUpperBound != inclupperboundInput.Checked) {
230        Content.InclusiveUpperBound = inclupperboundInput.Checked;
231        var exp = UpdateExpression(Content);
232        Content.Name = exp;
233        Content.Expression = exp;
234      }
235    }
236
237    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
238      if (Content.Enabled != ischeckedCheckBox.Checked) {
239        Content.Enabled = ischeckedCheckBox.Checked;
240        //UpdateControls();
241      }
242    }
243
244    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
245      if ((int)numberderivationInput.SelectedItem == 1)
246        Content.numberOfDerivation = 1;
247      else if ((int)numberderivationInput.SelectedItem == 2)
248        Content.numberOfDerivation = 2;
249      else if ((int)numberderivationInput.SelectedItem == 3)
250        Content.numberOfDerivation = 3;
251
252      var exp = UpdateExpression(Content);
253      Content.Name = exp;
254      Content.Expression = exp;
255    }
256
257    #endregion
258
259    #region content event handlers
260
261    private void Content_Changed(object sender, EventArgs e) {
262      UpdateControls();
263    }
264
265
266    #endregion
267
268  }
269}
Note: See TracBrowser for help on using the repository browser.