Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971

  • Disabled variable input field in IntervalConstraintView
  • Added update definition method in IntervalConstraintView
  • Added onChanged event for definition
File size: 9.5 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 sealed 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      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 += new EventHandler(Content_Changed);
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.Changed -= new EventHandler(Content_Changed);
58      base.DeregisterContentEvents();
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
64      upperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
65      expressionInput.Enabled = Content != null && !Locked && !ReadOnly;
66      definitionInput.Enabled = Content != null && !Locked && !ReadOnly;
67      definitionInput.ReadOnly = true;
68      expressionInput.ReadOnly = true;
69      variableInput.ReadOnly = true;
70    }
71
72
73
74    #region helpers
75
76    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
77      input = input.ToLower();
78      switch (input) {
79        case "inf.":
80        case "+inf.":
81        case "Infinity":
82          return double.PositiveInfinity;
83        case "-inf.":
84        case "-Infinity":
85          return double.NegativeInfinity;
86        default: {
87          if (double.TryParse(input, out var value))
88            return value;
89          else {
90            errorProvider.SetError(control, "Invalid Input: Value must be a double!");
91            return double.NaN;
92          }
93        }
94      }
95    }
96
97    private void UpdateControls() {
98      if (Content == null) {
99        expressionInput.Text = string.Empty;
100        lowerboundInput.Text = string.Empty;
101        upperboundInput.Text = string.Empty;
102        incllowerboundInput.Checked = true;
103        inclupperboundInput.Checked = true;
104        return;
105      }
106
107      expressionInput.Text = Content.Expression;
108      definitionInput.Text = Content.Definition;
109      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
110      upperboundInput.Text = Content.Interval.UpperBound.ToString();
111      incllowerboundInput.Checked = Content.InclusiveLowerBound;
112      inclupperboundInput.Checked = Content.InclusiveUpperBound;
113      variableInput.Text = Content.Variable;
114      if (!Content.IsDerivation) {
115        numberderivationInput.Visible = false;
116        numberderivationLabel.Visible = false;
117      } else {
118        numberderivationLabel.Visible = true;
119        numberderivationInput.Visible = true;
120        numberderivationInput.Enabled = true;
121        numberderivationInput.SelectedItem = Content.NumberOfDerivation;
122      }
123
124      ischeckedCheckBox.Checked = Content.Enabled;
125    }
126
127    private static string UpdateExpression(IntervalConstraint constraint) {
128      var expression = "";
129
130      if (!constraint.IsDerivation) {
131        expression = string.Format("{0} in {1}{2} .. {3}{4}",
132          constraint.Variable,
133          (constraint.InclusiveLowerBound) ? "[" : "]",
134          constraint.Interval.LowerBound,
135          constraint.Interval.UpperBound,
136          (constraint.InclusiveUpperBound) ? "]" : "[");
137      } else {
138        expression = string.Format("\u2202{5}Target/\u2202{0}{6} in {1}{2} .. {3}{4}",
139          constraint.Variable,
140          (constraint.InclusiveLowerBound) ? "[" : "]",
141          constraint.Interval.LowerBound,
142          constraint.Interval.UpperBound,
143          (constraint.InclusiveUpperBound) ? "]" : "[",
144          GetDerivationString(constraint.numberOfDerivation),
145          GetDerivationString(constraint.numberOfDerivation));
146      }
147
148      return expression;
149    }
150
151    private static string UpdateDefintion(IntervalConstraint constraint) {
152      var definition = "";
153
154      if (!constraint.IsDerivation) {
155        return "Target " + constraint.Variable;
156      }
157
158      definition = $"\u2202{GetDerivationString(constraint.numberOfDerivation)}Target/\u2202{constraint.Variable}{GetDerivationString(constraint.numberOfDerivation)}";
159
160      return definition;
161    }
162
163    private static string GetDerivationString(int derivation) {
164      switch (derivation) {
165        case 1:
166          return "";
167        case 2:
168          return "²";
169        case 3:
170          return "³";
171        default:
172          return "";
173      }
174    }
175
176    #endregion
177
178    #region control event handlers
179    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
180      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
181      if (double.IsNaN(value)) {
182        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
183        e.Cancel = true;
184        return;
185      }
186
187      if (value > Content.Interval.UpperBound) {
188        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
189        e.Cancel = true;
190        return;
191      }
192
193      errorProvider.SetError(lowerboundInput, string.Empty);
194      e.Cancel = false;
195    }
196
197    private void lowerboundInput_Validated(object sender, EventArgs e) {
198      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
199      if (!double.IsNaN(value)) {
200        Content.Interval = new Interval(value, Content.Interval.UpperBound);
201        var exp = UpdateExpression(Content);
202        Content.Name = exp;
203        Content.Expression = exp;
204      }
205    }
206
207    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
208      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
209      if (double.IsNaN(value)) {
210        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
211        e.Cancel = true;
212        return;
213      }
214
215      if (value < Content.Interval.LowerBound) {
216        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
217        e.Cancel = true;
218        return;
219      }
220
221      errorProvider.SetError(upperboundInput, string.Empty);
222      e.Cancel = false;
223    }
224
225    private void upperboundInput_Validated(object sender, EventArgs e) {
226      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
227      if (!double.IsNaN(value)) {
228        Content.Interval = new Interval(Content.Interval.LowerBound, value);
229        var exp = UpdateExpression(Content);
230        Content.Name = exp;
231        Content.Expression = exp;
232      }
233    }
234
235    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
236        Content.InclusiveLowerBound = incllowerboundInput.Checked;
237        var exp = UpdateExpression(Content);
238        Content.Name = exp;
239        Content.Expression = exp;
240    }
241
242    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
243        Content.InclusiveUpperBound = inclupperboundInput.Checked;
244        var exp = UpdateExpression(Content);
245        Content.Name = exp;
246        Content.Expression = exp;
247    }
248
249    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
250        Content.Enabled = ischeckedCheckBox.Checked;
251    }
252
253    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
254      if ((int)numberderivationInput.SelectedItem == 1)
255        Content.numberOfDerivation = 1;
256      else if ((int)numberderivationInput.SelectedItem == 2)
257        Content.numberOfDerivation = 2;
258      else if ((int)numberderivationInput.SelectedItem == 3)
259        Content.numberOfDerivation = 3;
260
261      var exp = UpdateExpression(Content);
262      var def = UpdateDefintion(Content);
263      Content.Name = exp;
264      Content.Expression = exp;
265      Content.Definition = def;
266    }
267
268    #endregion
269
270    #region content event handlers
271
272    private void Content_Changed(object sender, EventArgs e) {
273      UpdateControls();
274    }
275    #endregion
276  }
277}
Note: See TracBrowser for help on using the repository browser.