source: branches/2965_CancelablePersistence/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 16433

Last change on this file since 16433 was 16433, checked in by pfleck, 12 months ago

#2965 Merged recent trunk changes.
Enabled the prepared hooks that allows to cancel the save file using the recently introduced cancelable progressbars (in FileManager).

File size: 6.4 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;
24
25namespace HeuristicLab.MainForm.WindowsForms {
26  internal sealed partial class ProgressView : UserControl {
27    private readonly Control control;
28    public Control Control {
29      get { return control; }
30    }
31
32    private readonly IProgress content;
33    public IProgress Content {
34      get { return content; }
35    }
36
37    public ProgressView(Control control, IProgress content)
38      : base() {
39      if (control == null) throw new ArgumentNullException("control");
40      if (content == null) throw new ArgumentNullException("content");
41      InitializeComponent();
42
43      this.control = control;
44      this.content = content;
45
46      if (content.ProgressState != ProgressState.Finished)
47        ShowProgress();
48      RegisterContentEvents();
49    }
50
51    protected override void Dispose(bool disposing) {
52      DeregisterContentEvents();
53      HideProgress();
54
55      if (disposing && (components != null)) {
56        components.Dispose();
57      }
58      base.Dispose(disposing);
59    }
60
61    private void RegisterContentEvents() {
62      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
63      Content.MessageChanged += new EventHandler(Content_MessageChanged);
64      Content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged);
65      Content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged);
66      Content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged);
67      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
68    }
69    private void DeregisterContentEvents() {
70      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
71      Content.MessageChanged -= new EventHandler(Content_MessageChanged);
72      Content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged);
73      Content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged);
74      Content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged);
75      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
76    }
77
78    private void Content_ProgressStateChanged(object sender, EventArgs e) {
79      UpdateProgressState();
80      UpdateButtonsState();
81    }
82
83    private void Content_MessageChanged(object sender, EventArgs e) {
84      UpdateProgressMessage();
85    }
86
87    private void Content_ProgressBarModeChanged(object sender, EventArgs e) {
88      UpdateProgressValue();
89    }
90    private void Content_ProgressValueChanged(object sender, EventArgs e) {
91      UpdateProgressValue();
92    }
93
94    private void Content_CanBeStoppedChanged(object sender, EventArgs e) {
95      UpdateButtonsState();
96    }
97    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
98      UpdateButtonsState();
99    }
100
101    private void ShowProgress() {
102      if (Control.InvokeRequired) {
103        Control.Invoke((Action)ShowProgress);
104        return;
105      }
106      if (Parent != null) return;
107
108      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
109      Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
110      Anchor = AnchorStyles.None;
111
112      UpdateProgressMessage();
113      UpdateProgressValue();
114      UpdateButtonsState();
115
116      Control.Enabled = false;
117      Parent = Control.Parent;
118      BringToFront();
119      Visible = true;
120    }
121
122    private void HideProgress() {
123      if (Control.InvokeRequired) {
124        Control.Invoke((Action)HideProgress);
125        return;
126      }
127      if (Parent == null) return;
128
129      Visible = false;
130      Control.Enabled = true;
131      Parent = null;
132    }
133
134    private void UpdateProgressState() {
135      if (Control.InvokeRequired) {
136        Control.Invoke((Action)UpdateProgressState);
137        return;
138      }
139
140      if (Content.ProgressState != ProgressState.Finished)
141        ShowProgress();
142      else
143        HideProgress();
144    }
145
146    private void UpdateProgressMessage() {
147      if (Control.InvokeRequired) {
148        Control.Invoke((Action)UpdateProgressMessage);
149        return;
150      }
151
152      messageLabel.Text = content.Message;
153    }
154
155    private void UpdateProgressValue() {
156      if (InvokeRequired) {
157        Invoke((Action)UpdateProgressValue);
158        return;
159      }
160
161      switch (Content.ProgressMode) {
162        case ProgressMode.Determinate:
163          progressBar.Style = ProgressBarStyle.Continuous;
164          progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
165          break;
166        case ProgressMode.Indeterminate:
167          progressBar.Style = ProgressBarStyle.Marquee;
168          progressBar.Value = 0;
169          break;
170        default:
171          throw new NotImplementedException($"Invalid Progress Mode: {content.ProgressMode}");
172      }
173    }
174
175    private void UpdateButtonsState() {
176      if (Control.InvokeRequired) {
177        Control.Invoke((Action)UpdateButtonsState);
178        return;
179      }
180
181      stopButton.Visible = Content.CanBeStopped;
182      stopButton.Enabled = Content.CanBeStopped && content.ProgressState == ProgressState.Started;
183
184      cancelButton.Visible = Content.CanBeCanceled;
185      cancelButton.Enabled = Content.CanBeCanceled && content.ProgressState == ProgressState.Started;
186    }
187
188    private void stopButton_Click(object sender, EventArgs e) {
189      Content.Stop();
190    }
191    private void cancelButton_Click(object sender, EventArgs e) {
192      Content.Cancel();
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.