Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/10/17 14:30:37 (7 years ago)
Author:
pfleck
Message:

#2845

  • Fixed/Added Progress Cancellation/Stopping
  • Added Visible property to progress and ProgressBarMode.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/EnhancedProgress/HeuristicLab.MainForm/3.3/Progress.cs

    r15400 r15415  
    2424namespace HeuristicLab.MainForm {
    2525  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 
    4826    private ProgressState progressState;
    4927    public ProgressState ProgressState {
     
    5735    }
    5836
     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 visible;
     73    public bool Visible {
     74      get { return visible; }
     75      set {
     76        if (visible != value) {
     77          visible = value;
     78          OnVisibleChanged();
     79        }
     80      }
     81    }
     82
     83    private bool canBeStopped;
     84    public bool CanBeStopped {
     85      get { return canBeStopped; }
     86      set {
     87        if (canBeStopped != value) {
     88          canBeStopped = value;
     89          OnCanBeStoppedChanged();
     90        }
     91      }
     92    }
     93
    5994    private bool canBeCanceled;
    6095    public bool CanBeCanceled {
     
    70105    public Progress() {
    71106      progressState = ProgressState.Finished;
     107      canBeStopped = false;
    72108      canBeCanceled = false;
    73     }
    74     public Progress(string status)
    75       : this() {
    76       this.status = status;
    77     }
    78     public Progress(string status, ProgressState state)
    79       : this() {
    80       this.status = status;
    81       this.progressState = state;
    82     }
    83 
     109      progressBarMode = ProgressBarMode.Marquee;
     110      progressValue = 0.0;
     111    }
     112
     113    public void Start(string message) {
     114      ProgressState = ProgressState.Started;
     115      ProgressBarMode = ProgressBarMode.Marquee;
     116      Message = message;
     117      Visible = true;
     118    }
     119    public void Start(string message, double progressValue) {
     120      ProgressState = ProgressState.Started;
     121      ProgressBarMode = ProgressBarMode.Continuous;
     122      ProgressValue = progressValue;
     123      Message = message;
     124      Visible = true;
     125    }
     126
     127    public void Finish() {
     128      if (ProgressBarMode == ProgressBarMode.Continuous && ProgressValue != 1.0)
     129        ProgressValue = 1.0;
     130      ProgressState = ProgressState.Finished;
     131      Visible = false;
     132    }
     133
     134    public void Stop() {
     135      if (canBeStopped) {
     136        ProgressState = ProgressState.Stopped;
     137        OnStopRequested();
     138      } else throw new NotSupportedException("This progress cannot be stopped.");
     139    }
    84140    public void Cancel() {
    85       if (canBeCanceled)
     141      if (canBeCanceled) {
     142        ProgressState = ProgressState.Canceled;
    86143        OnCancelRequested();
    87     }
    88 
    89     public void Finish() {
    90       if (ProgressValue != 1.0) ProgressValue = 1.0;
    91       ProgressState = ProgressState.Finished;
    92     }
    93 
    94     public void Start() {
    95       ProgressValue = 0.0;
    96       ProgressState = ProgressState.Started;
    97     }
    98 
    99     public void Start(string status) {
    100       Start();
    101       Status = status;
     144      } else throw new NotSupportedException("This progress cannot be canceled.");
    102145    }
    103146
    104147    #region Event Handler
    105     public event EventHandler StatusChanged;
    106     private void OnStatusChanged() {
    107       var handler = StatusChanged;
     148    public event EventHandler ProgressStateChanged;
     149    private void OnProgressStateChanged() {
     150      var handler = ProgressStateChanged;
     151      if (handler != null) handler(this, EventArgs.Empty);
     152    }
     153
     154    public event EventHandler MessageChanged;
     155    private void OnMessageChanged() {
     156      var handler = MessageChanged;
     157      if (handler != null) handler(this, EventArgs.Empty);
     158    }
     159
     160    public event EventHandler ProgressBarModeChanged;
     161    private void OnProgressBarModeChanged() {
     162      var handler = ProgressBarModeChanged;
    108163      if (handler != null) handler(this, EventArgs.Empty);
    109164    }
     
    115170    }
    116171
    117     public event EventHandler ProgressStateChanged;
    118     private void OnProgressStateChanged() {
    119       var handler = ProgressStateChanged;
     172    public event EventHandler VisibleChanged;
     173    private void OnVisibleChanged() {
     174      var handler = VisibleChanged;
     175      if (handler != null) handler(this, EventArgs.Empty);
     176    }
     177
     178    public event EventHandler CanBeStoppedChanged;
     179    private void OnCanBeStoppedChanged() {
     180      var handler = CanBeStoppedChanged;
    120181      if (handler != null) handler(this, EventArgs.Empty);
    121182    }
     
    127188    }
    128189
     190    public event EventHandler StopRequested;
     191    private void OnStopRequested() {
     192      var handler = StopRequested;
     193      if (handler != null) handler(this, EventArgs.Empty);
     194    }
     195
    129196    public event EventHandler CancelRequested;
    130197    private void OnCancelRequested() {
    131198      var handler = CancelRequested;
    132       if (handler != null) throw new NotSupportedException("Cancel request was ignored.");
    133       else handler(this, EventArgs.Empty);
     199      if (handler != null) handler(this, EventArgs.Empty);
    134200    }
    135201    #endregion
Note: See TracChangeset for help on using the changeset viewer.