Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971 Changed some views

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