[8187] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12018] | 3 | * Copyright (C) 2002-2015 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 {
|
---|
[9907] | 27 | private const int defaultControlHeight = 88;
|
---|
| 28 | private const int collapsedControlHeight = 55;
|
---|
| 29 |
|
---|
[9893] | 30 | private readonly Control control;
|
---|
| 31 | public Control Control {
|
---|
| 32 | get { return control; }
|
---|
[8187] | 33 | }
|
---|
| 34 |
|
---|
[9893] | 35 | private readonly IProgress content;
|
---|
| 36 | public IProgress Content {
|
---|
| 37 | get { return content; }
|
---|
[9849] | 38 | }
|
---|
| 39 |
|
---|
[9893] | 40 | public ProgressView(Control control, IProgress content)
|
---|
| 41 | : base() {
|
---|
| 42 | if (control == null) throw new ArgumentNullException("control", "The control is null.");
|
---|
| 43 | if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
|
---|
[8187] | 44 | InitializeComponent();
|
---|
[9868] | 45 |
|
---|
[9893] | 46 | this.control = control;
|
---|
| 47 | this.content = content;
|
---|
| 48 | if (content.ProgressState == ProgressState.Started)
|
---|
| 49 | ShowProgress();
|
---|
| 50 | RegisterContentEvents();
|
---|
[8187] | 51 | }
|
---|
| 52 |
|
---|
[9894] | 53 | /// <summary>
|
---|
| 54 | /// Clean up any resources being used.
|
---|
| 55 | /// </summary>
|
---|
| 56 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
| 57 | protected override void Dispose(bool disposing) {
|
---|
| 58 | DeregisterContentEvents();
|
---|
| 59 | HideProgress();
|
---|
| 60 |
|
---|
| 61 | if (disposing && (components != null)) {
|
---|
| 62 | components.Dispose();
|
---|
| 63 | }
|
---|
| 64 | base.Dispose(disposing);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[9893] | 67 | private void RegisterContentEvents() {
|
---|
| 68 | content.StatusChanged += new EventHandler(progress_StatusChanged);
|
---|
| 69 | content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
|
---|
| 70 | content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
|
---|
| 71 | content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
|
---|
[9849] | 72 | }
|
---|
[9893] | 73 | private void DeregisterContentEvents() {
|
---|
| 74 | content.StatusChanged -= new EventHandler(progress_StatusChanged);
|
---|
| 75 | content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
|
---|
| 76 | content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
|
---|
| 77 | content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
|
---|
[8187] | 78 | }
|
---|
| 79 |
|
---|
[9893] | 80 | private void ShowProgress() {
|
---|
| 81 | if (Control.InvokeRequired) {
|
---|
| 82 | Control.Invoke((Action)ShowProgress);
|
---|
| 83 | return;
|
---|
[8187] | 84 | }
|
---|
[9907] | 85 | int height = Content.CanBeCanceled ? Height : collapsedControlHeight;
|
---|
| 86 |
|
---|
[9893] | 87 | Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
|
---|
[9907] | 88 | Top = (Control.ClientRectangle.Height / 2) - (height / 2);
|
---|
[9893] | 89 | Anchor = AnchorStyles.None;
|
---|
[8187] | 90 |
|
---|
[9893] | 91 | control.Enabled = false;
|
---|
| 92 | Parent = Control.Parent;
|
---|
| 93 | BringToFront();
|
---|
[8187] | 94 |
|
---|
[9893] | 95 | UpdateProgressValue();
|
---|
| 96 | UpdateProgressStatus();
|
---|
| 97 | UpdateCancelButton();
|
---|
| 98 | Visible = true;
|
---|
[8187] | 99 | }
|
---|
| 100 |
|
---|
| 101 | private void HideProgress() {
|
---|
| 102 | if (InvokeRequired) Invoke((Action)HideProgress);
|
---|
| 103 | else {
|
---|
[9893] | 104 | control.Enabled = true;
|
---|
| 105 | Parent = null;
|
---|
[8187] | 106 | Visible = false;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private void progress_StatusChanged(object sender, EventArgs e) {
|
---|
| 111 | UpdateProgressStatus();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private void progress_ProgressValueChanged(object sender, EventArgs e) {
|
---|
| 115 | UpdateProgressValue();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private void Content_ProgressStateChanged(object sender, EventArgs e) {
|
---|
[9893] | 119 | switch (content.ProgressState) {
|
---|
| 120 | case ProgressState.Finished: HideProgress(); break;
|
---|
[9849] | 121 | case ProgressState.Canceled: HideProgress(); break;
|
---|
| 122 | case ProgressState.Started: ShowProgress(); break;
|
---|
[9893] | 123 | default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
|
---|
[9849] | 124 | }
|
---|
[8187] | 125 | }
|
---|
| 126 |
|
---|
| 127 | private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
|
---|
[9893] | 128 | UpdateCancelButton();
|
---|
[8187] | 129 | }
|
---|
| 130 |
|
---|
[9893] | 131 | private void UpdateCancelButton() {
|
---|
| 132 | cancelButton.Visible = content != null && content.CanBeCanceled;
|
---|
| 133 | cancelButton.Enabled = content != null && content.CanBeCanceled;
|
---|
[9907] | 134 |
|
---|
| 135 | if (content != null && content.CanBeCanceled) {
|
---|
| 136 | Height = defaultControlHeight;
|
---|
| 137 | } else if (content != null && !content.CanBeCanceled) {
|
---|
| 138 | Height = collapsedControlHeight;
|
---|
| 139 | }
|
---|
[8187] | 140 | }
|
---|
| 141 |
|
---|
| 142 | private void UpdateProgressValue() {
|
---|
| 143 | if (InvokeRequired) Invoke((Action)UpdateProgressValue);
|
---|
| 144 | else {
|
---|
[9893] | 145 | if (content != null) {
|
---|
| 146 | double progressValue = content.ProgressValue;
|
---|
[8187] | 147 | if (progressValue <= 0.0 || progressValue > 1.0) {
|
---|
[9893] | 148 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
[8187] | 149 | } else {
|
---|
[9893] | 150 | progressBar.Style = ProgressBarStyle.Blocks;
|
---|
[8187] | 151 | progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private void UpdateProgressStatus() {
|
---|
| 158 | if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
|
---|
[9893] | 159 | else if (content != null)
|
---|
| 160 | statusLabel.Text = content.Status;
|
---|
[8187] | 161 | }
|
---|
| 162 |
|
---|
| 163 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
[9893] | 164 | content.Cancel();
|
---|
[8187] | 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|