Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 6.5 KB
RevLine 
[8187]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 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 {
[9894]26  internal sealed partial class ProgressView : UserControl {
[9893]27    private readonly Control control;
28    public Control Control {
29      get { return control; }
[8187]30    }
31
[9893]32    private readonly IProgress content;
33    public IProgress Content {
34      get { return content; }
[9849]35    }
36
[9893]37    public ProgressView(Control control, IProgress content)
38      : base() {
[16430]39      if (control == null) throw new ArgumentNullException("control");
40      if (content == null) throw new ArgumentNullException("content");
[8187]41      InitializeComponent();
[9868]42
[9893]43      this.control = control;
44      this.content = content;
[16430]45
46      if (content.ProgressState != ProgressState.Finished)
[9893]47        ShowProgress();
48      RegisterContentEvents();
[8187]49    }
50
[9894]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
[9893]61    private void RegisterContentEvents() {
[16430]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);
[9849]68    }
[9893]69    private void DeregisterContentEvents() {
[16430]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);
[8187]76    }
77
[16430]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
[9893]101    private void ShowProgress() {
102      if (Control.InvokeRequired) {
103        Control.Invoke((Action)ShowProgress);
104        return;
[8187]105      }
[16430]106      if (Parent != null) return;
[9907]107
[9893]108      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
[16430]109      Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
[9893]110      Anchor = AnchorStyles.None;
[8187]111
[16430]112      UpdateProgressMessage();
113      UpdateProgressValue();
114      UpdateButtonsState();
115
[16501]116      Control.SuspendRepaint();
[16430]117      Control.Enabled = false;
[9893]118      Parent = Control.Parent;
119      BringToFront();
[16501]120      Control.ResumeRepaint(true);
[9893]121      Visible = true;
[8187]122    }
123
124    private void HideProgress() {
[16430]125      if (Control.InvokeRequired) {
126        Control.Invoke((Action)HideProgress);
127        return;
[8187]128      }
[16511]129      //if (Parent == null) return;
[8187]130
[16430]131      Visible = false;
[16501]132      Control.SuspendRepaint();
[16430]133      Control.Enabled = true;
[16501]134      Control.ResumeRepaint(true);
[16430]135      Parent = null;
[8187]136    }
137
[16430]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();
[8187]148    }
149
[16430]150    private void UpdateProgressMessage() {
151      if (Control.InvokeRequired) {
152        Control.Invoke((Action)UpdateProgressMessage);
153        return;
[9849]154      }
[8187]155
[16430]156      messageLabel.Text = content.Message;
[8187]157    }
158
[16430]159    private void UpdateProgressValue() {
160      if (InvokeRequired) {
161        Invoke((Action)UpdateProgressValue);
162        return;
163      }
[9907]164
[16430]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}");
[9907]176      }
[8187]177    }
178
[16430]179    private void UpdateButtonsState() {
180      if (Control.InvokeRequired) {
181        Control.Invoke((Action)UpdateButtonsState);
182        return;
[8187]183      }
[16430]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;
[8187]190    }
191
[16430]192    private void stopButton_Click(object sender, EventArgs e) {
193      Content.Stop();
[8187]194    }
195    private void cancelButton_Click(object sender, EventArgs e) {
[16430]196      Content.Cancel();
[8187]197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.