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
RevLine 
[16830]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;
[16772]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)]
[16862]31  public sealed partial class IntervalConstraintView : ItemView {
[16772]32
33    public new IntervalConstraint Content {
[16776]34      get => (IntervalConstraint) base.Content;
35      set => base.Content = value;
[16772]36    }
[16774]37
[16772]38    public IntervalConstraintView() {
39      InitializeComponent();
[16778]40      int [] items = {1, 2, 3};
[16772]41      expressionInput.ReadOnly = true;
42      definitionInput.ReadOnly = true;
[16778]43      numberderivationInput.DataSource = items;
[16772]44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      UpdateControls();
49    }
50
[16787]51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.Changed += new EventHandler(Content_Changed);
54    }
[16774]55
[16787]56    protected override void DeregisterContentEvents() {
[16862]57      Content.Changed -= new EventHandler(Content_Changed);
[16787]58      base.DeregisterContentEvents();
59    }
60
[16772]61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
[16862]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;
[16880]69      variableInput.ReadOnly = true;
[16772]70    }
71
[16776]72
73
74    #region helpers
75
[16862]76    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
[16776]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: {
[16862]87          if (double.TryParse(input, out var value))
[16776]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
[16772]97    private void UpdateControls() {
98      if (Content == null) {
99        expressionInput.Text = string.Empty;
100        lowerboundInput.Text = string.Empty;
101        upperboundInput.Text = string.Empty;
[16776]102        incllowerboundInput.Checked = true;
103        inclupperboundInput.Checked = true;
[16862]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;
[16772]117      } else {
[16862]118        numberderivationLabel.Visible = true;
119        numberderivationInput.Visible = true;
120        numberderivationInput.Enabled = true;
121        numberderivationInput.SelectedItem = Content.NumberOfDerivation;
122      }
[16787]123
[16862]124      ischeckedCheckBox.Checked = Content.Enabled;
[16772]125    }
126
[16800]127    private static string UpdateExpression(IntervalConstraint constraint) {
[16776]128      var expression = "";
[16772]129
[16800]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) ? "]" : "[");
[16776]137      } else {
[16778]138        expression = string.Format("\u2202{5}Target/\u2202{0}{6} in {1}{2} .. {3}{4}",
[16800]139          constraint.Variable,
140          (constraint.InclusiveLowerBound) ? "[" : "]",
141          constraint.Interval.LowerBound,
142          constraint.Interval.UpperBound,
143          (constraint.InclusiveUpperBound) ? "]" : "[",
[16862]144          GetDerivationString(constraint.numberOfDerivation),
145          GetDerivationString(constraint.numberOfDerivation));
[16776]146      }
[16774]147
[16800]148      return expression;
[16774]149    }
150
[16880]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
[16862]163    private static string GetDerivationString(int derivation) {
[16778]164      switch (derivation) {
165        case 1:
166          return "";
167        case 2:
168          return "²";
169        case 3:
170          return "³";
171        default:
172          return "";
[16774]173      }
174    }
175
[16778]176    #endregion
[16776]177
[16778]178    #region control event handlers
[16776]179    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
[16862]180      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
[16800]181      if (double.IsNaN(value)) {
182        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
[16776]183        e.Cancel = true;
[16800]184        return;
[16774]185      }
[16800]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;
[16774]195    }
196
[16776]197    private void lowerboundInput_Validated(object sender, EventArgs e) {
[16862]198      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
[16776]199      if (!double.IsNaN(value)) {
200        Content.Interval = new Interval(value, Content.Interval.UpperBound);
[16800]201        var exp = UpdateExpression(Content);
202        Content.Name = exp;
203        Content.Expression = exp;
[16776]204      }
205    }
206
207    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
[16862]208      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
[16800]209      if (double.IsNaN(value)) {
210        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
[16776]211        e.Cancel = true;
[16800]212        return;
[16774]213      }
[16800]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;
[16774]223    }
[16776]224
225    private void upperboundInput_Validated(object sender, EventArgs e) {
[16862]226      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
[16776]227      if (!double.IsNaN(value)) {
228        Content.Interval = new Interval(Content.Interval.LowerBound, value);
[16800]229        var exp = UpdateExpression(Content);
230        Content.Name = exp;
231        Content.Expression = exp;
[16776]232      }
233    }
234
235    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
236        Content.InclusiveLowerBound = incllowerboundInput.Checked;
[16800]237        var exp = UpdateExpression(Content);
238        Content.Name = exp;
239        Content.Expression = exp;
[16776]240    }
241
242    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
[16777]243        Content.InclusiveUpperBound = inclupperboundInput.Checked;
[16800]244        var exp = UpdateExpression(Content);
245        Content.Name = exp;
246        Content.Expression = exp;
[16776]247    }
[16777]248
[16787]249    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
[16800]250        Content.Enabled = ischeckedCheckBox.Checked;
[16777]251    }
252
[16778]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
[16800]261      var exp = UpdateExpression(Content);
[16880]262      var def = UpdateDefintion(Content);
[16800]263      Content.Name = exp;
264      Content.Expression = exp;
[16880]265      Content.Definition = def;
[16778]266    }
267
[16776]268    #endregion
269
[16777]270    #region content event handlers
[16776]271
[16777]272    private void Content_Changed(object sender, EventArgs e) {
273      UpdateControls();
274    }
275    #endregion
[16772]276  }
277}
Note: See TracBrowser for help on using the repository browser.