Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 16605

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

#2845

  • Re-enabled null-check and added exception when showing Progress on control with no parent (Forms and Dialogs).
  • Adapted Progress in HiveResourceSelectorDialog to show the dialog on the selector-View and not on the dialog itself.
File size: 6.7 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 (control.Parent == null) throw new InvalidOperationException("A Progress can only be shown on controls that have a Parent-control. Therefore, Dialogs and Forms cannot have an associated ProgressView.");
41      if (content == null) throw new ArgumentNullException("content");
42      InitializeComponent();
43
44      this.control = control;
45      this.content = content;
46
47      if (content.ProgressState != ProgressState.Finished)
48        ShowProgress();
49      RegisterContentEvents();
50    }
51
52    protected override void Dispose(bool disposing) {
53      DeregisterContentEvents();
54      HideProgress();
55
56      if (disposing && (components != null)) {
57        components.Dispose();
58      }
59      base.Dispose(disposing);
60    }
61
62    private void RegisterContentEvents() {
63      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
64      Content.MessageChanged += new EventHandler(Content_MessageChanged);
65      Content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged);
66      Content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged);
67      Content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged);
68      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
69    }
70    private void DeregisterContentEvents() {
71      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
72      Content.MessageChanged -= new EventHandler(Content_MessageChanged);
73      Content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged);
74      Content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged);
75      Content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged);
76      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
77    }
78
79    private void Content_ProgressStateChanged(object sender, EventArgs e) {
80      UpdateProgressState();
81      UpdateButtonsState();
82    }
83
84    private void Content_MessageChanged(object sender, EventArgs e) {
85      UpdateProgressMessage();
86    }
87
88    private void Content_ProgressBarModeChanged(object sender, EventArgs e) {
89      UpdateProgressValue();
90    }
91    private void Content_ProgressValueChanged(object sender, EventArgs e) {
92      UpdateProgressValue();
93    }
94
95    private void Content_CanBeStoppedChanged(object sender, EventArgs e) {
96      UpdateButtonsState();
97    }
98    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
99      UpdateButtonsState();
100    }
101
102    private void ShowProgress() {
103      if (Control.InvokeRequired) {
104        Control.Invoke((Action)ShowProgress);
105        return;
106      }
107      if (Parent != null) return;
108
109      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
110      Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
111      Anchor = AnchorStyles.None;
112
113      UpdateProgressMessage();
114      UpdateProgressValue();
115      UpdateButtonsState();
116
117      Control.SuspendRepaint();
118      Control.Enabled = false;
119      Parent = Control.Parent;
120      BringToFront();
121      Control.ResumeRepaint(true);
122      Visible = true;
123    }
124
125    private void HideProgress() {
126      if (Control.InvokeRequired) {
127        Control.Invoke((Action)HideProgress);
128        return;
129      }
130      if (Parent == null) return;
131
132      Visible = false;
133      Control.SuspendRepaint();
134      Control.Enabled = true;
135      Control.ResumeRepaint(true);
136      Parent = null;
137    }
138
139    private void UpdateProgressState() {
140      if (Control.InvokeRequired) {
141        Control.Invoke((Action)UpdateProgressState);
142        return;
143      }
144
145      if (Content.ProgressState != ProgressState.Finished)
146        ShowProgress();
147      else
148        HideProgress();
149    }
150
151    private void UpdateProgressMessage() {
152      if (Control.InvokeRequired) {
153        Control.Invoke((Action)UpdateProgressMessage);
154        return;
155      }
156
157      messageLabel.Text = content.Message;
158    }
159
160    private void UpdateProgressValue() {
161      if (InvokeRequired) {
162        Invoke((Action)UpdateProgressValue);
163        return;
164      }
165
166      switch (Content.ProgressMode) {
167        case ProgressMode.Determinate:
168          progressBar.Style = ProgressBarStyle.Continuous;
169          progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
170          break;
171        case ProgressMode.Indeterminate:
172          progressBar.Style = ProgressBarStyle.Marquee;
173          progressBar.Value = 0;
174          break;
175        default:
176          throw new NotImplementedException($"Invalid Progress Mode: {content.ProgressMode}");
177      }
178    }
179
180    private void UpdateButtonsState() {
181      if (Control.InvokeRequired) {
182        Control.Invoke((Action)UpdateButtonsState);
183        return;
184      }
185
186      stopButton.Visible = Content.CanBeStopped;
187      stopButton.Enabled = Content.CanBeStopped && content.ProgressState == ProgressState.Started;
188
189      cancelButton.Visible = Content.CanBeCanceled;
190      cancelButton.Enabled = Content.CanBeCanceled && content.ProgressState == ProgressState.Started;
191    }
192
193    private void stopButton_Click(object sender, EventArgs e) {
194      Content.Stop();
195    }
196    private void cancelButton_Click(object sender, EventArgs e) {
197      Content.Cancel();
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.