Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.MainForm.WindowsForms/3.3/Dialogs/DefineArithmeticProgressionTimeDialog.cs

Last change on this file was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 3.3 KB
RevLine 
[9041]1#region License Information
2/* HeuristicLab
[17209]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9041]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
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.MainForm.WindowsForms {
30  public partial class DefineArithmeticTimeSpanProgressionDialog : Form {
31    public TimeSpan Minimum { get; private set; }
32    public TimeSpan Maximum { get; private set; }
33    public TimeSpan Step { get; private set; }
34
35    public IEnumerable<TimeSpan> Values {
36      get { return EnumerateProgression().Reverse(); }
37    }
38
39    public DefineArithmeticTimeSpanProgressionDialog() {
40      InitializeComponent();
41      Icon = HeuristicLab.Common.Resources.HeuristicLab.Icon;
42      Minimum = TimeSpan.Zero; Maximum = TimeSpan.FromSeconds(60); Step = TimeSpan.FromSeconds(10);
43    }
44    public DefineArithmeticTimeSpanProgressionDialog(TimeSpan minimum, TimeSpan maximum, TimeSpan step)
45      : this() {
46      Minimum = minimum;
47      Maximum = maximum;
48      Step = step;
49    }
50
51    private void DefineArithmeticTimeSpanProgressionDialog_Load(object sender, System.EventArgs e) {
52      minimumTextBox.Text = TimeSpanHelper.FormatNatural(Minimum, true);
53      maximumTextBox.Text = TimeSpanHelper.FormatNatural(Maximum, true);
54      stepSizeTextBox.Text = TimeSpanHelper.FormatNatural(Step, true);
55    }
56
57    private void textBox_Validating(object sender, CancelEventArgs e) {
58      var textBox = (TextBox)sender;
59      TimeSpan value;
60      errorProvider.SetError(textBox, String.Empty);
61      if (!TimeSpanHelper.TryGetFromNaturalFormat(textBox.Text, out value)) {
62        errorProvider.SetError(textBox, "Please enter a valid timespan.");
63        e.Cancel = true;
64        return;
65      }
66      if (textBox == minimumTextBox) Minimum = value;
67      else if (textBox == maximumTextBox) Maximum = value;
68      else if (textBox == stepSizeTextBox) Step = value;
69      okButton.Enabled = IsValid();
70    }
71
72    private bool IsValid() {
73      return Minimum <= Maximum && Step >= TimeSpan.Zero;
74    }
75
76    private IEnumerable<TimeSpan> EnumerateProgression() {
77      var value = Maximum;
78      var minimumIncluded = false;
79      while (value >= Minimum) {
80        if (value == Minimum) minimumIncluded = true;
81        yield return value;
82
83        if (Step == TimeSpan.Zero) break; // a step size of 0 will only output maximum and minimum
84        value = value.Subtract(Step);
85      }
86      if (!minimumIncluded) {
87        yield return Minimum;
88      }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.