Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2845 Combined separate Show methods into single one with enum parameter specifying the ProgressMode. Renamed Continuous and Marquee to Determinate and Indeterminate.

File size: 8.9 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 ProgressMode progressMode;
51    public ProgressMode ProgressMode {
52      get { return progressMode; }
53      set {
54        if (progressMode != value) {
55          progressMode = value;
56          OnProgressBarModeChanged();
57        }
58      }
59    }
60
61    private double progressValue;
62    public double ProgressValue {
63      get { return progressValue; }
64      set {
65        if (progressMode == ProgressMode.Indeterminate)
66          throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Indeterminate-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      progressMode = ProgressMode.Indeterminate;
101      progressValue = 0.0;
102    }
103
104    public void Start(string message, ProgressMode mode = ProgressMode.Determinate) {
105      ProgressState = ProgressState.Started;
106      ProgressMode = mode;
107      if (mode == ProgressMode.Determinate)
108        ProgressValue = 0.0;
109      Message = message;
110    }
111
112    public void Finish() {
113      if (ProgressMode == ProgressMode.Determinate && ProgressValue != 1.0)
114        ProgressValue = 1.0;
115      ProgressState = ProgressState.Finished;
116    }
117
118    public void Stop() {
119      if (canBeStopped) {
120        ProgressState = ProgressState.StopRequested;
121        OnStopRequested();
122      } else throw new NotSupportedException("This progress cannot be stopped.");
123    }
124    public void Cancel() {
125      if (canBeCanceled) {
126        ProgressState = ProgressState.CancelRequested;
127        OnCancelRequested();
128      } else throw new NotSupportedException("This progress cannot be canceled.");
129    }
130
131    #region Show/Hide Progress
132    /// <summary>
133    /// Shows a started Progress on all Views of the specified content.
134    /// </summary>
135    public static IProgress Show(IContent content, string progressMessage, ProgressMode mode = ProgressMode.Determinate, bool addToObjectGraphObjects = true) {
136      var progress = new Progress();
137      progress.Start(progressMessage, mode);
138      AddProgressToContent(content, progress, addToObjectGraphObjects);
139      return progress;
140    }
141
142    /// <summary>
143    /// Shows a started Progress on the specified view.
144    /// </summary>
145    public static IProgress Show(IView view, string progressMessage, ProgressMode mode = ProgressMode.Determinate) {
146      var progress = new Progress();
147      progress.Start(progressMessage, mode);
148      AddProgressToView(view, progress);
149      return progress;
150    }
151    /// <summary>
152    /// Shows a started Progress on the specified control.
153    /// </summary>
154    /// <remarks>Use Progress.Show(IView, ...) if possible.</remarks>
155    public static IProgress ShowOnControl(Control control, string progressMessage, ProgressMode mode = ProgressMode.Determinate) {
156      var progress = new Progress();
157      progress.Start(progressMessage, mode);
158      AddProgressToControl(control, progress);
159      return progress;
160    }
161
162    /// <summary>
163    /// Hides the Progress from all Views of the specified content.
164    /// </summary>
165    public static void Hide(IContent content) {
166      RemoveProgressFromContent(content);
167    }
168    /// <summary>
169    /// Hides the Progress from the specified view.
170    /// </summary>
171    public static void Hide(IView view) {
172      RemoveProgressFromView(view);
173    }
174    /// <summary>
175    /// Hides the Progress from the specified control.
176    /// </summary>
177    /// <remarks>Use Progress.Hide(IView) if possible.</remarks>
178    public static void HideFromControl(Control control) {
179      RemoveProgressFromControl(control);
180    }
181    #endregion
182
183    #region Interface to MainForm
184    public static IProgress AddProgressToContent(IContent content, IProgress progress, bool addToObjectGraphObjects = true) {
185      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToContent(content, progress, addToObjectGraphObjects);
186      return progress;
187    }
188    public static IProgress AddProgressToView(IView view, IProgress progress) {
189      //return AddProgressToControl(MainFormManager.GetMainForm<WindowsForms.MainForm>().GetForm(view), progress);
190      return AddProgressToControl((Control)view, progress);
191    }
192    public static IProgress AddProgressToControl(Control control, IProgress progress) {
193      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToControl(control, progress);
194      return progress;
195    }
196
197    public static void RemoveProgressFromContent(IContent content, bool finishProgress = true) {
198      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromContent(content, finishProgress);
199    }
200    public static void RemoveProgressFromView(IView view, bool finishProgress = true) {
201      //RemoveProgressFromControl(MainFormManager.GetMainForm<WindowsForms.MainForm>().GetForm(view), finishProgress);
202      RemoveProgressFromControl((Control)view, finishProgress);
203    }
204    public static void RemoveProgressFromControl(Control control, bool finishProgress = true) {
205      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromControl(control, finishProgress);
206    }
207    #endregion
208
209    #region Event Handler
210    public event EventHandler ProgressStateChanged;
211    private void OnProgressStateChanged() {
212      var handler = ProgressStateChanged;
213      if (handler != null) handler(this, EventArgs.Empty);
214    }
215
216    public event EventHandler MessageChanged;
217    private void OnMessageChanged() {
218      var handler = MessageChanged;
219      if (handler != null) handler(this, EventArgs.Empty);
220    }
221
222    public event EventHandler ProgressBarModeChanged;
223    private void OnProgressBarModeChanged() {
224      var handler = ProgressBarModeChanged;
225      if (handler != null) handler(this, EventArgs.Empty);
226    }
227
228    public event EventHandler ProgressValueChanged;
229    private void OnProgressChanged() {
230      var handler = ProgressValueChanged;
231      if (handler != null) handler(this, EventArgs.Empty);
232    }
233
234    public event EventHandler CanBeStoppedChanged;
235    private void OnCanBeStoppedChanged() {
236      var handler = CanBeStoppedChanged;
237      if (handler != null) handler(this, EventArgs.Empty);
238    }
239
240    public event EventHandler CanBeCanceledChanged;
241    private void OnCanBeCanceledChanged() {
242      var handler = CanBeCanceledChanged;
243      if (handler != null) handler(this, EventArgs.Empty);
244    }
245
246    public event EventHandler StopRequested;
247    private void OnStopRequested() {
248      var handler = StopRequested;
249      if (handler != null) handler(this, EventArgs.Empty);
250    }
251
252    public event EventHandler CancelRequested;
253    private void OnCancelRequested() {
254      var handler = CancelRequested;
255      if (handler != null) handler(this, EventArgs.Empty);
256    }
257    #endregion
258  }
259}
Note: See TracBrowser for help on using the repository browser.