Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.MainForm/3.3/Progress.cs @ 15446

Last change on this file since 15446 was 15446, checked in by pfleck, 6 years ago

#2845:

  • Start progres with initial progress value in several cases to use the progress value update.
  • Removed object disposal guard in ProgressView because it can block part of the initialization.
File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23
24namespace HeuristicLab.MainForm {
25  public class Progress : IProgress {
26    private ProgressState progressState;
27    public ProgressState ProgressState {
28      get { return progressState; }
29      private set {
30        if (progressState != value) {
31          progressState = value;
32          OnProgressStateChanged();
33        }
34      }
35    }
36
37    private string message;
38    public string Message {
39      get { return message; }
40      set {
41        if (message != value) {
42          message = value;
43          OnMessageChanged();
44        }
45      }
46    }
47
48    private ProgressBarMode progressBarMode;
49    public ProgressBarMode ProgressBarMode {
50      get { return progressBarMode; }
51      set {
52        if (progressBarMode != value) {
53          progressBarMode = value;
54          OnProgressBarModeChanged();
55        }
56      }
57    }
58
59    private double progressValue;
60    public double ProgressValue {
61      get { return progressValue; }
62      set {
63        if (progressBarMode == ProgressBarMode.Marquee)
64          throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Marquee-Mode");
65        if (progressValue != value) {
66          progressValue = Math.Max(Math.Min(value, 1.0), 0.0);
67          OnProgressChanged();
68        }
69      }
70    }
71
72    private bool canBeStopped;
73    public bool CanBeStopped {
74      get { return canBeStopped; }
75      set {
76        if (canBeStopped != value) {
77          canBeStopped = value;
78          OnCanBeStoppedChanged();
79        }
80      }
81    }
82
83    private bool canBeCanceled;
84    public bool CanBeCanceled {
85      get { return canBeCanceled; }
86      set {
87        if (canBeCanceled != value) {
88          canBeCanceled = value;
89          OnCanBeCanceledChanged();
90        }
91      }
92    }
93
94    public Progress() {
95      progressState = ProgressState.Finished;
96      canBeStopped = false;
97      canBeCanceled = false;
98      progressBarMode = ProgressBarMode.Marquee;
99      progressValue = 0.0;
100    }
101
102    /// <summary>
103    /// Starts or restarts an "undefined" progress in marquee-mode
104    /// </summary>
105    public void Start(string message) {
106      ProgressState = ProgressState.Started;
107      ProgressBarMode = ProgressBarMode.Marquee;
108      Message = message;
109    }
110    /// <summary>
111    /// Starts or restarts a progress in continuous-mode
112    /// </summary>
113    public void Start(string message, double progressValue) {
114      ProgressState = ProgressState.Started;
115      ProgressBarMode = ProgressBarMode.Continuous;
116      ProgressValue = progressValue;
117      Message = message;
118    }
119
120    public void Finish() {
121      if (ProgressBarMode == ProgressBarMode.Continuous && ProgressValue != 1.0)
122        ProgressValue = 1.0;
123      ProgressState = ProgressState.Finished;
124    }
125
126    public void Stop() {
127      if (canBeStopped) {
128        ProgressState = ProgressState.StopRequested;
129        OnStopRequested();
130      } else throw new NotSupportedException("This progress cannot be stopped.");
131    }
132    public void Cancel() {
133      if (canBeCanceled) {
134        ProgressState = ProgressState.CancelRequested;
135        OnCancelRequested();
136      } else throw new NotSupportedException("This progress cannot be canceled.");
137    }
138
139    #region Event Handler
140    public event EventHandler ProgressStateChanged;
141    private void OnProgressStateChanged() {
142      var handler = ProgressStateChanged;
143      if (handler != null) handler(this, EventArgs.Empty);
144    }
145
146    public event EventHandler MessageChanged;
147    private void OnMessageChanged() {
148      var handler = MessageChanged;
149      if (handler != null) handler(this, EventArgs.Empty);
150    }
151
152    public event EventHandler ProgressBarModeChanged;
153    private void OnProgressBarModeChanged() {
154      var handler = ProgressBarModeChanged;
155      if (handler != null) handler(this, EventArgs.Empty);
156    }
157
158    public event EventHandler ProgressValueChanged;
159    private void OnProgressChanged() {
160      var handler = ProgressValueChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
163
164    public event EventHandler CanBeStoppedChanged;
165    private void OnCanBeStoppedChanged() {
166      var handler = CanBeStoppedChanged;
167      if (handler != null) handler(this, EventArgs.Empty);
168    }
169
170    public event EventHandler CanBeCanceledChanged;
171    private void OnCanBeCanceledChanged() {
172      var handler = CanBeCanceledChanged;
173      if (handler != null) handler(this, EventArgs.Empty);
174    }
175
176    public event EventHandler StopRequested;
177    private void OnStopRequested() {
178      var handler = StopRequested;
179      if (handler != null) handler(this, EventArgs.Empty);
180    }
181
182    public event EventHandler CancelRequested;
183    private void OnCancelRequested() {
184      var handler = CancelRequested;
185      if (handler != null) handler(this, EventArgs.Empty);
186    }
187    #endregion
188  }
189}
Note: See TracBrowser for help on using the repository browser.