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;
|
---|
28 |
|
---|
29 | private IProgress progress;
|
---|
30 | public IProgress Progress {
|
---|
31 | get { return progress; }
|
---|
32 | set {
|
---|
33 | if (progress == value) return;
|
---|
34 | DeregisterProgressEvents();
|
---|
35 | progress = value;
|
---|
36 | RegisterProgressEvents();
|
---|
37 | OnProgressChanged();
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private bool cancelEnabled;
|
---|
42 | public bool CancelEnabled {
|
---|
43 | get { return cancelEnabled; }
|
---|
44 | set {
|
---|
45 | cancelEnabled = value;
|
---|
46 | SetCancelButtonVisibility();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <param name="parentView">This is the view which will be locked while progress is made.</param>
|
---|
51 | public ProgressView(ContentView parentView, IProgress progress) {
|
---|
52 | InitializeComponent();
|
---|
53 | Progress = progress;
|
---|
54 | CancelEnabled = false;
|
---|
55 |
|
---|
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;
|
---|
61 |
|
---|
62 | LockBackground();
|
---|
63 |
|
---|
64 | parentView.Controls.Add(this);
|
---|
65 | BringToFront();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void RegisterProgressEvents() {
|
---|
70 | if (progress == null) return;
|
---|
71 | progress.Finished += new EventHandler(progress_Finished);
|
---|
72 | progress.StatusChanged += new EventHandler(progress_StatusChanged);
|
---|
73 | progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void DeregisterProgressEvents() {
|
---|
77 | if (progress == null) return;
|
---|
78 | progress.Finished -= new EventHandler(progress_Finished);
|
---|
79 | progress.StatusChanged -= new EventHandler(progress_StatusChanged);
|
---|
80 | progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void progress_Finished(object sender, EventArgs e) {
|
---|
84 | Finish();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void progress_StatusChanged(object sender, EventArgs e) {
|
---|
88 | UpdateProgressStatus();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void progress_ProgressValueChanged(object sender, EventArgs e) {
|
---|
92 | UpdateProgressValue();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void LockBackground() {
|
---|
96 | if (InvokeRequired) {
|
---|
97 | Invoke((Action)LockBackground);
|
---|
98 | } else {
|
---|
99 | parentView.Locked = true;
|
---|
100 | parentView.ReadOnly = true;
|
---|
101 | foreach (Control c in this.parentView.Controls)
|
---|
102 | c.Enabled = false;
|
---|
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));
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
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 |
|
---|
130 | public void Finish() {
|
---|
131 | if (InvokeRequired) {
|
---|
132 | Invoke(new Action(Finish));
|
---|
133 | } else {
|
---|
134 | progressBar.Value = progressBar.Maximum;
|
---|
135 | parentView.Controls.Remove(this);
|
---|
136 | parentView.Locked = false;
|
---|
137 | parentView.ReadOnly = false;
|
---|
138 | foreach (Control c in this.parentView.Controls)
|
---|
139 | c.Enabled = false;
|
---|
140 | DeregisterProgressEvents();
|
---|
141 | Dispose();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
146 | OnCanceled();
|
---|
147 | Finish();
|
---|
148 | }
|
---|
149 |
|
---|
150 | private void SetCancelButtonVisibility() {
|
---|
151 | if (InvokeRequired) {
|
---|
152 | Invoke((Action)SetCancelButtonVisibility);
|
---|
153 | } else {
|
---|
154 | cancelButton.Visible = cancelEnabled;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void OnProgressChanged() {
|
---|
159 | UpdateProgressStatus();
|
---|
160 | UpdateProgressValue();
|
---|
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 | }
|
---|