source: trunk/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 16511

Last change on this file since 16511 was 16511, checked in by pfleck, 4 years ago

#2845 Temporarily removed null check to avoid the permanently disabled project selector in Hive.

File size: 6.5 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.SuspendRepaint();
117      Control.Enabled = false;
118      Parent = Control.Parent;
119      BringToFront();
120      Control.ResumeRepaint(true);
121      Visible = true;
122    }
123
124    private void HideProgress() {
125      if (Control.InvokeRequired) {
126        Control.Invoke((Action)HideProgress);
127        return;
128      }
129      //if (Parent == null) return;
130
131      Visible = false;
132      Control.SuspendRepaint();
133      Control.Enabled = true;
134      Control.ResumeRepaint(true);
135      Parent = null;
136    }
137
138    private void UpdateProgressState() {
139      if (Control.InvokeRequired) {
140        Control.Invoke((Action)UpdateProgressState);
141        return;
142      }
143
144      if (Content.ProgressState != ProgressState.Finished)
145        ShowProgress();
146      else
147        HideProgress();
148    }
149
150    private void UpdateProgressMessage() {
151      if (Control.InvokeRequired) {
152        Control.Invoke((Action)UpdateProgressMessage);
153        return;
154      }
155
156      messageLabel.Text = content.Message;
157    }
158
159    private void UpdateProgressValue() {
160      if (InvokeRequired) {
161        Invoke((Action)UpdateProgressValue);
162        return;
163      }
164
165      switch (Content.ProgressMode) {
166        case ProgressMode.Determinate:
167          progressBar.Style = ProgressBarStyle.Continuous;
168          progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
169          break;
170        case ProgressMode.Indeterminate:
171          progressBar.Style = ProgressBarStyle.Marquee;
172          progressBar.Value = 0;
173          break;
174        default:
175          throw new NotImplementedException($"Invalid Progress Mode: {content.ProgressMode}");
176      }
177    }
178
179    private void UpdateButtonsState() {
180      if (Control.InvokeRequired) {
181        Control.Invoke((Action)UpdateButtonsState);
182        return;
183      }
184
185      stopButton.Visible = Content.CanBeStopped;
186      stopButton.Enabled = Content.CanBeStopped && content.ProgressState == ProgressState.Started;
187
188      cancelButton.Visible = Content.CanBeCanceled;
189      cancelButton.Enabled = Content.CanBeCanceled && content.ProgressState == ProgressState.Started;
190    }
191
192    private void stopButton_Click(object sender, EventArgs e) {
193      Content.Stop();
194    }
195    private void cancelButton_Click(object sender, EventArgs e) {
196      Content.Cancel();
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.