Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimplifierViewsProgress/HeuristicLab.MainForm/3.3/Progress.cs @ 15320

Last change on this file since 15320 was 15320, checked in by pfleck, 7 years ago

#1666 Adapted ProgressView so that 0 is a valid ProgressValue. Small refactorings.

File size: 3.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 string status;
27    public string Status {
28      get { return status; }
29      set {
30        if (status != value) {
31          status = value;
32          OnStatusChanged();
33        }
34      }
35    }
36
37    private double progressValue;
38    public double ProgressValue {
39      get { return progressValue; }
40      set {
41        if (progressValue != value) {
42          progressValue = value;
43          OnProgressChanged();
44        }
45      }
46    }
47
48    private ProgressState progressState;
49    public ProgressState ProgressState {
50      get { return progressState; }
51      private set {
52        if (progressState != value) {
53          progressState = value;
54          OnProgressStateChanged();
55        }
56      }
57    }
58
59    private bool canBeCanceled;
60    public bool CanBeCanceled {
61      get { return canBeCanceled; }
62      set {
63        if (canBeCanceled != value) {
64          canBeCanceled = value;
65          OnCanBeCanceledChanged();
66        }
67      }
68    }
69
70    public Progress(string status = null, ProgressState state = ProgressState.Finished, double progressValue = -1)
71      : base() {
72      this.status = status;
73      this.progressState = state;
74      this.progressValue = progressValue;
75    }
76
77    public void Cancel() {
78      if (canBeCanceled)
79        OnCancelRequested();
80    }
81
82    public void Finish() {
83      if (ProgressValue != 1.0) ProgressValue = 1.0;
84      ProgressState = ProgressState.Finished;
85    }
86
87    public void Start(string status = null, double progressValue = -1.0) {
88      ProgressState = ProgressState.Started;
89      Status = status;
90      ProgressValue = progressValue;
91    }
92
93    #region Event Handler
94    public event EventHandler StatusChanged;
95    private void OnStatusChanged() {
96      var handler = StatusChanged;
97      if (handler != null) handler(this, EventArgs.Empty);
98    }
99
100    public event EventHandler ProgressValueChanged;
101    private void OnProgressChanged() {
102      var handler = ProgressValueChanged;
103      if (handler != null) handler(this, EventArgs.Empty);
104    }
105
106    public event EventHandler ProgressStateChanged;
107    private void OnProgressStateChanged() {
108      var handler = ProgressStateChanged;
109      if (handler != null) handler(this, EventArgs.Empty);
110    }
111
112    public event EventHandler CanBeCanceledChanged;
113    private void OnCanBeCanceledChanged() {
114      var handler = CanBeCanceledChanged;
115      if (handler != null) handler(this, EventArgs.Empty);
116    }
117
118    public event EventHandler CancelRequested;
119    private void OnCancelRequested() {
120      var handler = CancelRequested;
121      if (handler != null) throw new NotSupportedException("Cancel request was ignored.");
122      else handler(this, EventArgs.Empty);
123    }
124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.