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

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

#2845

  • Added an explicit method for StartMarquee.
  • Renamed Add/RemoveOperationProgress methods.
  • Moved progress helpers from MainForm into static Progress methods named Show and Hide.
File size: 8.0 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;
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      MainFormManager.GetMainForm<WindowsForms.MainForm>().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      MainFormManager.GetMainForm<WindowsForms.MainForm>().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      MainFormManager.GetMainForm<WindowsForms.MainForm>().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      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToView(control, progress);
171      return progress;
172    }
173
174    /// <summary>
175    /// Hides the Progress fom all Views of the specified content.
176    /// </summary>
177    public static void Hide(IContent content) {
178      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromContent(content);
179    }
180    /// <summary>
181    /// Hides the Progress fom the specified view.
182    /// </summary>
183    public static void Hide(Control control) {
184      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromView(control);
185    }
186    #endregion
187
188    #region Event Handler
189    public event EventHandler ProgressStateChanged;
190    private void OnProgressStateChanged() {
191      var handler = ProgressStateChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194
195    public event EventHandler MessageChanged;
196    private void OnMessageChanged() {
197      var handler = MessageChanged;
198      if (handler != null) handler(this, EventArgs.Empty);
199    }
200
201    public event EventHandler ProgressBarModeChanged;
202    private void OnProgressBarModeChanged() {
203      var handler = ProgressBarModeChanged;
204      if (handler != null) handler(this, EventArgs.Empty);
205    }
206
207    public event EventHandler ProgressValueChanged;
208    private void OnProgressChanged() {
209      var handler = ProgressValueChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
211    }
212
213    public event EventHandler CanBeStoppedChanged;
214    private void OnCanBeStoppedChanged() {
215      var handler = CanBeStoppedChanged;
216      if (handler != null) handler(this, EventArgs.Empty);
217    }
218
219    public event EventHandler CanBeCanceledChanged;
220    private void OnCanBeCanceledChanged() {
221      var handler = CanBeCanceledChanged;
222      if (handler != null) handler(this, EventArgs.Empty);
223    }
224
225    public event EventHandler StopRequested;
226    private void OnStopRequested() {
227      var handler = StopRequested;
228      if (handler != null) handler(this, EventArgs.Empty);
229    }
230
231    public event EventHandler CancelRequested;
232    private void OnCancelRequested() {
233      var handler = CancelRequested;
234      if (handler != null) handler(this, EventArgs.Empty);
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.