1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 | internal sealed partial class ProgressView : UserControl {
|
---|
27 | private const int defaultControlHeight = 88;
|
---|
28 | private const int collapsedControlHeight = 55;
|
---|
29 |
|
---|
30 | private readonly Control control;
|
---|
31 | public Control Control {
|
---|
32 | get { return control; }
|
---|
33 | }
|
---|
34 |
|
---|
35 | private readonly IProgress content;
|
---|
36 | public IProgress Content {
|
---|
37 | get { return content; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public ProgressView(Control control, IProgress content)
|
---|
41 | : base() {
|
---|
42 | if (control == null) throw new ArgumentNullException("control");
|
---|
43 | if (content == null) throw new ArgumentNullException("content");
|
---|
44 | InitializeComponent();
|
---|
45 |
|
---|
46 | this.control = control;
|
---|
47 | this.content = content;
|
---|
48 |
|
---|
49 | if (content.ProgressState != ProgressState.Finished)
|
---|
50 | ShowProgress();
|
---|
51 | RegisterContentEvents();
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected override void Dispose(bool disposing) {
|
---|
55 | DeregisterContentEvents();
|
---|
56 | HideProgress();
|
---|
57 |
|
---|
58 | if (disposing && (components != null)) {
|
---|
59 | components.Dispose();
|
---|
60 | }
|
---|
61 | base.Dispose(disposing);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void RegisterContentEvents() {
|
---|
65 | content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
|
---|
66 | content.MessageChanged += new EventHandler(Content_MessageChanged);
|
---|
67 | content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged);
|
---|
68 | content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged);
|
---|
69 | content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged);
|
---|
70 | content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
|
---|
71 | }
|
---|
72 | private void DeregisterContentEvents() {
|
---|
73 | content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
|
---|
74 | content.MessageChanged -= new EventHandler(Content_MessageChanged);
|
---|
75 | content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged);
|
---|
76 | content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged);
|
---|
77 | content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged);
|
---|
78 | content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void Content_ProgressStateChanged(object sender, EventArgs e) {
|
---|
82 | UpdateProgressState();
|
---|
83 | UpdateButtonsState();
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void Content_MessageChanged(object sender, EventArgs e) {
|
---|
87 | UpdateProgressMessage();
|
---|
88 | }
|
---|
89 |
|
---|
90 | private void Content_ProgressBarModeChanged(object sender, EventArgs e) {
|
---|
91 | UpdateProgressValue();
|
---|
92 | }
|
---|
93 | private void Content_ProgressValueChanged(object sender, EventArgs e) {
|
---|
94 | UpdateProgressValue();
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void Content_CanBeStoppedChanged(object sender, EventArgs e) {
|
---|
98 | UpdateButtonsState();
|
---|
99 | }
|
---|
100 | private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
|
---|
101 | UpdateButtonsState();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void ShowProgress() {
|
---|
105 | if (control.InvokeRequired) {
|
---|
106 | control.Invoke((Action)ShowProgress);
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | if (Parent != null) return;
|
---|
110 |
|
---|
111 | int height = (content.CanBeStopped || content.CanBeCanceled) ? Height : collapsedControlHeight;
|
---|
112 | Left = (control.ClientRectangle.Width / 2) - (Width / 2);
|
---|
113 | Top = (control.ClientRectangle.Height / 2) - (height / 2);
|
---|
114 | Anchor = AnchorStyles.None;
|
---|
115 |
|
---|
116 | UpdateProgressMessage();
|
---|
117 | UpdateProgressValue();
|
---|
118 | UpdateButtonsState();
|
---|
119 |
|
---|
120 | control.Enabled = false;
|
---|
121 | Parent = control.Parent;
|
---|
122 | BringToFront();
|
---|
123 | Visible = true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void HideProgress() {
|
---|
127 | if (control.InvokeRequired) {
|
---|
128 | control.Invoke((Action)HideProgress);
|
---|
129 | return;
|
---|
130 | }
|
---|
131 | if (Parent == null) return;
|
---|
132 |
|
---|
133 | Visible = false;
|
---|
134 | control.Enabled = true;
|
---|
135 | Parent = null;
|
---|
136 | }
|
---|
137 |
|
---|
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();
|
---|
148 | }
|
---|
149 |
|
---|
150 | private void UpdateProgressMessage() {
|
---|
151 | if (control.InvokeRequired) {
|
---|
152 | control.Invoke((Action)UpdateProgressMessage);
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | messageLabel.Text = content.Message;
|
---|
157 | }
|
---|
158 |
|
---|
159 | private void UpdateProgressValue() {
|
---|
160 | if (InvokeRequired) {
|
---|
161 | Invoke((Action)UpdateProgressValue);
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (content.ProgressBarMode == ProgressBarMode.Continuous) {
|
---|
166 | progressBar.Style = ProgressBarStyle.Continuous;
|
---|
167 | progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
|
---|
168 | } else {
|
---|
169 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
170 | progressBar.Value = 0;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void UpdateButtonsState() {
|
---|
175 | if (control.InvokeRequired) {
|
---|
176 | control.Invoke((Action)UpdateButtonsState);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | stopButton.Visible = content.CanBeStopped;
|
---|
181 | stopButton.Enabled = content.CanBeStopped && content.ProgressState == ProgressState.Started;
|
---|
182 |
|
---|
183 | cancelButton.Visible = content.CanBeCanceled;
|
---|
184 | cancelButton.Enabled = content.CanBeCanceled && content.ProgressState == ProgressState.Started;
|
---|
185 |
|
---|
186 | if (content.CanBeStopped || content.CanBeCanceled) {
|
---|
187 | Height = defaultControlHeight;
|
---|
188 | } else {
|
---|
189 | Height = collapsedControlHeight;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
194 | content.Stop();
|
---|
195 | }
|
---|
196 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
197 | content.Cancel();
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|