Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.MainForm.WindowsForms/3.3/Dialogs/DefineArithmeticProgressionTimeDialog.cs @ 16724

Last change on this file since 16724 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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