[8187] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Windows.Forms;
|
---|
| 24 |
|
---|
| 25 | namespace 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");
|
---|
[16512] | 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.");
|
---|
[16430] | 41 | if (content == null) throw new ArgumentNullException("content");
|
---|
[8187] | 42 | InitializeComponent();
|
---|
[9868] | 43 |
|
---|
[9893] | 44 | this.control = control;
|
---|
| 45 | this.content = content;
|
---|
[16430] | 46 |
|
---|
| 47 | if (content.ProgressState != ProgressState.Finished)
|
---|
[9893] | 48 | ShowProgress();
|
---|
| 49 | RegisterContentEvents();
|
---|
[8187] | 50 | }
|
---|
| 51 |
|
---|
[9894] | 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 |
|
---|
[9893] | 62 | private void RegisterContentEvents() {
|
---|
[16430] | 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);
|
---|
[9849] | 69 | }
|
---|
[9893] | 70 | private void DeregisterContentEvents() {
|
---|
[16430] | 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);
|
---|
[8187] | 77 | }
|
---|
| 78 |
|
---|
[16430] | 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 |
|
---|
[9893] | 102 | private void ShowProgress() {
|
---|
| 103 | if (Control.InvokeRequired) {
|
---|
| 104 | Control.Invoke((Action)ShowProgress);
|
---|
| 105 | return;
|
---|
[8187] | 106 | }
|
---|
[16430] | 107 | if (Parent != null) return;
|
---|
[9907] | 108 |
|
---|
[9893] | 109 | Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
|
---|
[16430] | 110 | Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
|
---|
[9893] | 111 | Anchor = AnchorStyles.None;
|
---|
[8187] | 112 |
|
---|
[16430] | 113 | UpdateProgressMessage();
|
---|
| 114 | UpdateProgressValue();
|
---|
| 115 | UpdateButtonsState();
|
---|
| 116 |
|
---|
[16501] | 117 | Control.SuspendRepaint();
|
---|
[16430] | 118 | Control.Enabled = false;
|
---|
[9893] | 119 | Parent = Control.Parent;
|
---|
| 120 | BringToFront();
|
---|
[16501] | 121 | Control.ResumeRepaint(true);
|
---|
[9893] | 122 | Visible = true;
|
---|
[8187] | 123 | }
|
---|
| 124 |
|
---|
| 125 | private void HideProgress() {
|
---|
[16430] | 126 | if (Control.InvokeRequired) {
|
---|
| 127 | Control.Invoke((Action)HideProgress);
|
---|
| 128 | return;
|
---|
[8187] | 129 | }
|
---|
[16512] | 130 | if (Parent == null) return;
|
---|
[8187] | 131 |
|
---|
[16430] | 132 | Visible = false;
|
---|
[16501] | 133 | Control.SuspendRepaint();
|
---|
[16430] | 134 | Control.Enabled = true;
|
---|
[16501] | 135 | Control.ResumeRepaint(true);
|
---|
[16430] | 136 | Parent = null;
|
---|
[8187] | 137 | }
|
---|
| 138 |
|
---|
[16430] | 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();
|
---|
[8187] | 149 | }
|
---|
| 150 |
|
---|
[16430] | 151 | private void UpdateProgressMessage() {
|
---|
| 152 | if (Control.InvokeRequired) {
|
---|
| 153 | Control.Invoke((Action)UpdateProgressMessage);
|
---|
| 154 | return;
|
---|
[9849] | 155 | }
|
---|
[8187] | 156 |
|
---|
[16430] | 157 | messageLabel.Text = content.Message;
|
---|
[8187] | 158 | }
|
---|
| 159 |
|
---|
[16430] | 160 | private void UpdateProgressValue() {
|
---|
| 161 | if (InvokeRequired) {
|
---|
| 162 | Invoke((Action)UpdateProgressValue);
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
[9907] | 165 |
|
---|
[16430] | 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}");
|
---|
[9907] | 177 | }
|
---|
[8187] | 178 | }
|
---|
| 179 |
|
---|
[16430] | 180 | private void UpdateButtonsState() {
|
---|
| 181 | if (Control.InvokeRequired) {
|
---|
| 182 | Control.Invoke((Action)UpdateButtonsState);
|
---|
| 183 | return;
|
---|
[8187] | 184 | }
|
---|
[16430] | 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;
|
---|
[8187] | 191 | }
|
---|
| 192 |
|
---|
[16430] | 193 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
| 194 | Content.Stop();
|
---|
[8187] | 195 | }
|
---|
| 196 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
[16430] | 197 | Content.Cancel();
|
---|
[8187] | 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|