Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.MainForm.WindowsForms/3.3/Progress.cs @ 16314

Last change on this file since 16314 was 16314, checked in by pfleck, 5 years ago

#2845 Moved the entire progress API to the Progress class and made the progress API within the MainForm internal.

File size: 8.6 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.Windows.Forms;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.MainForm {
27  public class Progress : IProgress {
28    private ProgressState progressState;
29    public ProgressState ProgressState {
30      get { return progressState; }
31      private set {
32        if (progressState != value) {
33          progressState = value;
34          OnProgressStateChanged();
35        }
36      }
37    }
38
39    private string message;
40    public string Message {
41      get { return message; }
42      set {
43        if (message != value) {
44          message = value;
45          OnMessageChanged();
46        }
47      }
48    }
49
50    private ProgressBarMode progressBarMode;
51    public ProgressBarMode ProgressBarMode {
52      get { return progressBarMode; }
53      set {
54        if (progressBarMode != value) {
55          progressBarMode = value;
56          OnProgressBarModeChanged();
57        }
58      }
59    }
60
61    private double progressValue;
62    public double ProgressValue {
63      get { return progressValue; }
64      set {
65        if (progressBarMode == ProgressBarMode.Marquee)
66          throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Marquee-Mode");
67        if (progressValue != value) {
68          progressValue = Math.Max(Math.Min(value, 1.0), 0.0);
69          OnProgressChanged();
70        }
71      }
72    }
73
74    private bool canBeStopped;
75    public bool CanBeStopped {
76      get { return canBeStopped; }
77      set {
78        if (canBeStopped != value) {
79          canBeStopped = value;
80          OnCanBeStoppedChanged();
81        }
82      }
83    }
84
85    private bool canBeCanceled;
86    public bool CanBeCanceled {
87      get { return canBeCanceled; }
88      set {
89        if (canBeCanceled != value) {
90          canBeCanceled = value;
91          OnCanBeCanceledChanged();
92        }
93      }
94    }
95
96    public Progress() {
97      progressState = ProgressState.Finished;
98      canBeStopped = false;
99      canBeCanceled = false;
100      progressBarMode = ProgressBarMode.Marquee;
101      progressValue = 0.0;
102    }
103
104    public void Start(string message, double progressValue = 0) {
105      ProgressState = ProgressState.Started;
106      ProgressBarMode = ProgressBarMode.Continuous;
107      ProgressValue = progressValue;
108      Message = message;
109    }
110    public void StartMarquee(string message) {
111      ProgressState = ProgressState.Started;
112      ProgressBarMode = ProgressBarMode.Marquee;
113      Message = message;
114    }
115
116    public void Finish() {
117      if (ProgressBarMode == ProgressBarMode.Continuous && ProgressValue != 1.0)
118        ProgressValue = 1.0;
119      ProgressState = ProgressState.Finished;
120    }
121
122    public void Stop() {
123      if (canBeStopped) {
124        ProgressState = ProgressState.StopRequested;
125        OnStopRequested();
126      } else throw new NotSupportedException("This progress cannot be stopped.");
127    }
128    public void Cancel() {
129      if (canBeCanceled) {
130        ProgressState = ProgressState.CancelRequested;
131        OnCancelRequested();
132      } else throw new NotSupportedException("This progress cannot be canceled.");
133    }
134
135    #region Show/Hide Progress
136    /// <summary>
137    /// Shows a started Progress in Continuous-mode on all Views of the specified content.
138    /// </summary>
139    public static IProgress Show(IContent content, string progressMessage, double initialProgressValue = 0, bool addToObjectGraphObjects = true) {
140      var progress = new Progress();
141      progress.Start(progressMessage, initialProgressValue);
142      AddProgressToContent(content, progress, addToObjectGraphObjects);
143      return progress;
144    }
145    /// <summary>
146    /// Shows a started Progress in Marquee-mode on all Views of the specified content.
147    /// </summary>
148    public static IProgress ShowMarquee(IContent content, string progressMessage, bool addToObjectGraphObjects = true) {
149      var progress = new Progress();
150      progress.StartMarquee(progressMessage);
151      AddProgressToContent(content, progress, addToObjectGraphObjects);
152      return progress;
153    }
154
155    /// <summary>
156    /// Shows a started Progress in Continuous-mode on the specified view.
157    /// </summary>
158    public static IProgress Show(Control control, string progressMessage, double initialProgressValue = 0) {
159      var progress = new Progress();
160      progress.Start(progressMessage, initialProgressValue);
161      AddProgressToView(control, progress);
162      return progress;
163    }
164    /// <summary>
165    /// Shows a started Progress in Marquee-mode on the specified view.
166    /// </summary>
167    public static IProgress ShowMarquee(Control control, string progressMessage) {
168      var progress = new Progress();
169      progress.StartMarquee(progressMessage);
170      AddProgressToView(control, progress);
171      return progress;
172    }
173
174    /// <summary>
175    /// Hides the Progress from all Views of the specified content.
176    /// </summary>
177    public static void Hide(IContent content) {
178      RemoveProgressFromContent(content);
179    }
180    /// <summary>
181    /// Hides the Progress from the specified view.
182    /// </summary>
183    public static void Hide(Control control) {
184      RemoveProgressFromView(control);
185    }
186    #endregion
187
188    #region Interface to from MainForm
189    public static IProgress AddProgressToContent(IContent content, IProgress progress, bool addToObjectGraphObjects = true) {
190      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToContent(content, progress, addToObjectGraphObjects);
191      return progress;
192    }
193    public static IProgress AddProgressToView(Control control, IProgress progress) {
194      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToView(control, progress);
195      return progress;
196    }
197
198    public static void RemoveProgressFromContent(IContent content, bool finishProgress = true) {
199      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromContent(content, finishProgress);
200    }
201    public static void RemoveProgressFromView(Control control, bool finishProgress = true) {
202      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromView(control, finishProgress);
203    }
204    #endregion
205
206    #region Event Handler
207    public event EventHandler ProgressStateChanged;
208    private void OnProgressStateChanged() {
209      var handler = ProgressStateChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
211    }
212
213    public event EventHandler MessageChanged;
214    private void OnMessageChanged() {
215      var handler = MessageChanged;
216      if (handler != null) handler(this, EventArgs.Empty);
217    }
218
219    public event EventHandler ProgressBarModeChanged;
220    private void OnProgressBarModeChanged() {
221      var handler = ProgressBarModeChanged;
222      if (handler != null) handler(this, EventArgs.Empty);
223    }
224
225    public event EventHandler ProgressValueChanged;
226    private void OnProgressChanged() {
227      var handler = ProgressValueChanged;
228      if (handler != null) handler(this, EventArgs.Empty);
229    }
230
231    public event EventHandler CanBeStoppedChanged;
232    private void OnCanBeStoppedChanged() {
233      var handler = CanBeStoppedChanged;
234      if (handler != null) handler(this, EventArgs.Empty);
235    }
236
237    public event EventHandler CanBeCanceledChanged;
238    private void OnCanBeCanceledChanged() {
239      var handler = CanBeCanceledChanged;
240      if (handler != null) handler(this, EventArgs.Empty);
241    }
242
243    public event EventHandler StopRequested;
244    private void OnStopRequested() {
245      var handler = StopRequested;
246      if (handler != null) handler(this, EventArgs.Empty);
247    }
248
249    public event EventHandler CancelRequested;
250    private void OnCancelRequested() {
251      var handler = CancelRequested;
252      if (handler != null) handler(this, EventArgs.Empty);
253    }
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.