[7582] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Windows.Forms;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
| 26 | public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
|
---|
| 27 | private ContentView parentView;
|
---|
[8111] | 28 |
|
---|
[7582] | 29 | private IProgress progress;
|
---|
| 30 | public IProgress Progress {
|
---|
[8111] | 31 | get { return progress; }
|
---|
[7582] | 32 | set {
|
---|
[8111] | 33 | if (progress == value) return;
|
---|
| 34 | DeregisterProgressEvents();
|
---|
| 35 | progress = value;
|
---|
| 36 | RegisterProgressEvents();
|
---|
| 37 | OnProgressChanged();
|
---|
[7582] | 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[8111] | 41 | private bool cancelEnabled;
|
---|
[7582] | 42 | public bool CancelEnabled {
|
---|
[8111] | 43 | get { return cancelEnabled; }
|
---|
[7582] | 44 | set {
|
---|
[8111] | 45 | cancelEnabled = value;
|
---|
| 46 | SetCancelButtonVisibility();
|
---|
[7582] | 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[8111] | 50 | /// <param name="parentView">This is the view which will be locked while progress is made.</param>
|
---|
[7582] | 51 | public ProgressView(ContentView parentView, IProgress progress) {
|
---|
| 52 | InitializeComponent();
|
---|
[8111] | 53 | Progress = progress;
|
---|
| 54 | CancelEnabled = false;
|
---|
[7582] | 55 |
|
---|
[8111] | 56 | if (parentView != null) {
|
---|
| 57 | this.parentView = parentView;
|
---|
| 58 | this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
|
---|
| 59 | this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
|
---|
| 60 | this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
|
---|
[7582] | 61 |
|
---|
[8111] | 62 | LockBackground();
|
---|
[7582] | 63 |
|
---|
[8111] | 64 | parentView.Controls.Add(this);
|
---|
| 65 | BringToFront();
|
---|
| 66 | }
|
---|
[7582] | 67 | }
|
---|
| 68 |
|
---|
| 69 | private void RegisterProgressEvents() {
|
---|
[8111] | 70 | if (progress == null) return;
|
---|
[7582] | 71 | progress.Finished += new EventHandler(progress_Finished);
|
---|
| 72 | progress.StatusChanged += new EventHandler(progress_StatusChanged);
|
---|
[8111] | 73 | progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
|
---|
[7582] | 74 | }
|
---|
| 75 |
|
---|
| 76 | private void DeregisterProgressEvents() {
|
---|
[8111] | 77 | if (progress == null) return;
|
---|
[7582] | 78 | progress.Finished -= new EventHandler(progress_Finished);
|
---|
| 79 | progress.StatusChanged -= new EventHandler(progress_StatusChanged);
|
---|
[8111] | 80 | progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
|
---|
[7582] | 81 | }
|
---|
| 82 |
|
---|
[8111] | 83 | private void progress_Finished(object sender, EventArgs e) {
|
---|
[7582] | 84 | Finish();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[8111] | 87 | private void progress_StatusChanged(object sender, EventArgs e) {
|
---|
| 88 | UpdateProgressStatus();
|
---|
[7582] | 89 | }
|
---|
| 90 |
|
---|
[8111] | 91 | private void progress_ProgressValueChanged(object sender, EventArgs e) {
|
---|
| 92 | UpdateProgressValue();
|
---|
[7582] | 93 | }
|
---|
| 94 |
|
---|
| 95 | private void LockBackground() {
|
---|
| 96 | if (InvokeRequired) {
|
---|
[8111] | 97 | Invoke((Action)LockBackground);
|
---|
[7582] | 98 | } else {
|
---|
[8111] | 99 | parentView.Locked = true;
|
---|
| 100 | parentView.ReadOnly = true;
|
---|
| 101 | foreach (Control c in this.parentView.Controls)
|
---|
[7582] | 102 | c.Enabled = false;
|
---|
[8111] | 103 | Enabled = true;
|
---|
| 104 | ReadOnly = false;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private void UpdateProgressValue() {
|
---|
| 109 | if (InvokeRequired) Invoke((Action)UpdateProgressValue);
|
---|
| 110 | else {
|
---|
| 111 | double progressValue = progress.ProgressValue;
|
---|
| 112 | if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {
|
---|
| 113 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
| 114 | progressBar.Value = progressBar.Minimum;
|
---|
| 115 | } else {
|
---|
| 116 | progressBar.Style = ProgressBarStyle.Blocks;
|
---|
| 117 | progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
|
---|
[7582] | 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[8111] | 122 | private void UpdateProgressStatus() {
|
---|
| 123 | if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
|
---|
| 124 | else {
|
---|
| 125 | string status = progress.Status;
|
---|
| 126 | statusLabel.Text = progress.Status;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[7582] | 130 | public void Finish() {
|
---|
| 131 | if (InvokeRequired) {
|
---|
| 132 | Invoke(new Action(Finish));
|
---|
| 133 | } else {
|
---|
| 134 | progressBar.Value = progressBar.Maximum;
|
---|
[8111] | 135 | parentView.Controls.Remove(this);
|
---|
[7582] | 136 | parentView.Locked = false;
|
---|
[8111] | 137 | parentView.ReadOnly = false;
|
---|
| 138 | foreach (Control c in this.parentView.Controls)
|
---|
[8135] | 139 | c.Enabled = true;
|
---|
[7583] | 140 | DeregisterProgressEvents();
|
---|
[8111] | 141 | Dispose();
|
---|
[7582] | 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
[8111] | 146 | OnCanceled();
|
---|
[7582] | 147 | Finish();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[8111] | 150 | private void SetCancelButtonVisibility() {
|
---|
| 151 | if (InvokeRequired) {
|
---|
| 152 | Invoke((Action)SetCancelButtonVisibility);
|
---|
| 153 | } else {
|
---|
| 154 | cancelButton.Visible = cancelEnabled;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[7582] | 158 | private void OnProgressChanged() {
|
---|
[8111] | 159 | UpdateProgressStatus();
|
---|
| 160 | UpdateProgressValue();
|
---|
[7582] | 161 | }
|
---|
| 162 |
|
---|
| 163 | public event EventHandler Canceled;
|
---|
| 164 | protected virtual void OnCanceled() {
|
---|
| 165 | var handler = Canceled;
|
---|
| 166 | if (handler != null) Canceled(this, EventArgs.Empty);
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|