Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 10864

Last change on this file since 10864 was 9907, checked in by ascheibe, 11 years ago

#1042

  • the size of the ProgressView is now adjusted if the Cancel button is not shown
  • replaced the progress bar of the start page with the ProgressView
File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22using System;
23using System.Windows.Forms;
24
25namespace 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", "The control is null.");
43      if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
44      InitializeComponent();
45
46      this.control = control;
47      this.content = content;
48      if (content.ProgressState == ProgressState.Started)
49        ShowProgress();
50      RegisterContentEvents();
51    }
52
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
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);
72    }
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);
78    }
79
80    private void ShowProgress() {
81      if (Control.InvokeRequired) {
82        Control.Invoke((Action)ShowProgress);
83        return;
84      }
85      int height = Content.CanBeCanceled ? Height : collapsedControlHeight;
86
87      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
88      Top = (Control.ClientRectangle.Height / 2) - (height / 2);
89      Anchor = AnchorStyles.None;
90
91      control.Enabled = false;
92      Parent = Control.Parent;
93      BringToFront();
94
95      UpdateProgressValue();
96      UpdateProgressStatus();
97      UpdateCancelButton();
98      Visible = true;
99    }
100
101    private void HideProgress() {
102      if (InvokeRequired) Invoke((Action)HideProgress);
103      else {
104        control.Enabled = true;
105        Parent = null;
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) {
119      switch (content.ProgressState) {
120        case ProgressState.Finished: HideProgress(); break;
121        case ProgressState.Canceled: HideProgress(); break;
122        case ProgressState.Started: ShowProgress(); break;
123        default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
124      }
125    }
126
127    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
128      UpdateCancelButton();
129    }
130
131    private void UpdateCancelButton() {
132      cancelButton.Visible = content != null && content.CanBeCanceled;
133      cancelButton.Enabled = content != null && content.CanBeCanceled;
134
135      if (content != null && content.CanBeCanceled) {
136        Height = defaultControlHeight;
137      } else if (content != null && !content.CanBeCanceled) {
138        Height = collapsedControlHeight;
139      }
140    }
141
142    private void UpdateProgressValue() {
143      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
144      else {
145        if (content != null) {
146          double progressValue = content.ProgressValue;
147          if (progressValue <= 0.0 || progressValue > 1.0) {
148            progressBar.Style = ProgressBarStyle.Marquee;
149          } else {
150            progressBar.Style = ProgressBarStyle.Blocks;
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);
159      else if (content != null)
160        statusLabel.Text = content.Status;
161    }
162
163    private void cancelButton_Click(object sender, EventArgs e) {
164      content.Cancel();
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.