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

Last change on this file since 16778 was 16778, checked in by chaider, 18 months ago

#2971

  • Changed field numberOfDerivation
  • Added numberOfDerivation to Expression string
  • Added numberOfDerivation to parser
File size: 7.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Globalization;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.Windows.Forms;
11using HeuristicLab.Core.Views;
12using HeuristicLab.MainForm;
13using HeuristicLab.MainForm.WindowsForms;
14
15namespace HeuristicLab.Problems.DataAnalysis.Views {
16  [View("Interval Constraint Detail View")]
17  [Content(typeof(IntervalConstraint), true)]
18  public partial class IntervalConstraintView : ItemView {
19
20    public new IntervalConstraint Content {
21      get => (IntervalConstraint) base.Content;
22      set => base.Content = value;
23    }
24
25    public IntervalConstraintView() {
26      InitializeComponent();
27      int [] items = {1, 2, 3};
28      expressionInput.ReadOnly = true;
29      definitionInput.ReadOnly = true;
30      derivationInput.Enabled = false;
31      derivationInput.Checked = Content?.IsDerivation ?? false;
32      numberderivationInput.DataSource = items;
33    }
34
35    protected override void OnContentChanged() {
36      base.OnContentChanged();
37      UpdateControls();
38    }
39
40
41    protected override void SetEnabledStateOfControls() {
42      base.SetEnabledStateOfControls();
43      lowerboundInput.Enabled = Content != null && !Locked;
44    }
45
46
47
48    #region helpers
49
50    private double ParseDoubleValue(string input, Control control) {
51      input = input.ToLower();
52      switch (input) {
53        case "inf.":
54        case "+inf.":
55        case "Infinity":
56          return double.PositiveInfinity;
57        case "-inf.":
58        case "-Infinity":
59          return double.NegativeInfinity;
60        default: {
61          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
62            return value;
63          else {
64            errorProvider.SetError(control, "Invalid Input: Value must be a double!");
65            return double.NaN;
66          }
67        }
68      }
69    }
70
71    private void UpdateControls() {
72      if (Content == null) {
73        expressionInput.Text = string.Empty;
74        lowerboundInput.Text = string.Empty;
75        upperboundInput.Text = string.Empty;
76        incllowerboundInput.Checked = true;
77        inclupperboundInput.Checked = true;
78      } else {
79        expressionInput.Text = Content.Expression;
80        definitionInput.Text = Content.Definition;
81        lowerboundInput.Text = Content.Interval.LowerBound.ToString(CultureInfo.InvariantCulture);
82        upperboundInput.Text = Content.Interval.UpperBound.ToString(CultureInfo.InvariantCulture);
83        incllowerboundInput.Checked = Content.InclusiveLowerBound;
84        inclupperboundInput.Checked = Content.InclusiveUpperBound;
85        derivationInput.Checked = Content.IsDerivation;
86        variableInput.Text = Content.Variable;
87        if (!Content.IsDerivation) {
88          numberderivationInput.Enabled = false;
89        } else {
90          numberderivationInput.Enabled = true;
91          numberderivationInput.SelectedItem = Content.NumberOfDerivation;
92        }
93        ischeckedCheckBox.Checked = Content.IsChecked;
94        Content.Changed += Content_Changed;
95      }
96      SetEnabledStateOfControls();
97    }
98
99    private void UpdateExpression() {
100      var expression = "";
101
102      if (!Content.IsDerivation) {
103        expression = string.Format("{0} in {1}{2} .. {3}{4}",
104          Content.Variable,
105          (Content.InclusiveLowerBound) ? "[" : "]",
106          Content.Interval.LowerBound,
107          Content.Interval.UpperBound,
108          (Content.InclusiveUpperBound) ? "]" : "[");
109      } else {
110        expression = string.Format("\u2202{5}Target/\u2202{0}{6} in {1}{2} .. {3}{4}",
111          Content.Variable,
112          (Content.InclusiveLowerBound) ? "[" : "]",
113          Content.Interval.LowerBound,
114          Content.Interval.UpperBound,
115          (Content.InclusiveUpperBound) ? "]" : "[",
116          PrintNumberOfDerivation(Content.numberOfDerivation),
117          PrintNumberOfDerivation(Content.numberOfDerivation));
118      }
119
120      Content.Expression = expression;
121      Content.Name = expression;
122      UpdateControls();
123    }
124
125    private string PrintNumberOfDerivation(int derivation) {
126      switch (derivation) {
127        case 1:
128          return "";
129        case 2:
130          return "²";
131        case 3:
132          return "³";
133        default:
134          return "";
135      }
136    }
137
138    #endregion
139
140    #region control event handlers
141    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
142      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
143      if (!double.IsNaN(value)) {
144        if (value <= Content.Interval.UpperBound) {
145          errorProvider.SetError(lowerboundInput, string.Empty);
146          e.Cancel = false;
147        } else {
148          errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
149          e.Cancel = true;
150        }
151      } else {
152        e.Cancel = true;
153      }
154    }
155
156    private void lowerboundInput_Validated(object sender, EventArgs e) {
157      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
158      if (!double.IsNaN(value)) {
159        Content.Interval = new Interval(value, Content.Interval.UpperBound);
160        UpdateExpression();
161      }
162    }
163
164    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
165      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
166      if (!double.IsNaN(value)) {
167        if (value >= Content.Interval.LowerBound) {
168          errorProvider.SetError(upperboundInput, string.Empty);
169          e.Cancel = false;
170        } else {
171          errorProvider.SetError(lowerboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
172          e.Cancel = true;
173        }
174      } else {
175        e.Cancel = true;
176      }
177    }
178
179    private void upperboundInput_Validated(object sender, EventArgs e) {
180      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
181      if (!double.IsNaN(value)) {
182        Content.Interval = new Interval(Content.Interval.LowerBound, value);
183        UpdateExpression();
184      }
185    }
186
187    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
188      if (Content.InclusiveLowerBound != incllowerboundInput.Checked) {
189        Content.InclusiveLowerBound = incllowerboundInput.Checked;
190        UpdateExpression();
191      }
192     
193     
194    }
195
196    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
197      if (Content.InclusiveUpperBound != inclupperboundInput.Checked) {
198        Content.InclusiveUpperBound = inclupperboundInput.Checked;
199        UpdateExpression();
200      }
201    }
202
203    private void ischeckedCheckBox_CheckStateChanged(object sender, EventArgs e) {
204      if (Content.IsChecked != ischeckedCheckBox.Checked) {
205        Content.IsChecked = ischeckedCheckBox.Checked;
206        UpdateControls();
207      }
208    }
209
210    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
211      if ((int)numberderivationInput.SelectedItem == 1)
212        Content.numberOfDerivation = 1;
213      else if ((int)numberderivationInput.SelectedItem == 2)
214        Content.numberOfDerivation = 2;
215      else if ((int)numberderivationInput.SelectedItem == 3)
216        Content.numberOfDerivation = 3;
217
218      UpdateExpression();
219    }
220
221    #endregion
222
223    #region content event handlers
224
225    private void Content_Changed(object sender, EventArgs e) {
226      UpdateControls();
227    }
228
229    #endregion
230
231
232  }
233}
Note: See TracBrowser for help on using the repository browser.