Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 18242

Last change on this file since 18242 was 17500, checked in by mkommend, 5 years ago

#2973: Merged r17276, r17426, r17430, r17488 into stable.

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