Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9893 was 9893, checked in by ascheibe, 10 years ago

#1042

  • applied mkommends progress view patch
  • adapted Hive views accordingly
  • made some minor improvements (more to come...)
File size: 5.0 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  public sealed partial class ProgressView : UserControl {
27    private readonly Control control;
28    public Control Control {
29      get { return control; }
30    }
31
32    private readonly IProgress content;
33    public IProgress Content {
34      get { return content; }
35    }
36
37    public ProgressView(Control control, IProgress content)
38      : base() {
39      if (control == null) throw new ArgumentNullException("control", "The control is null.");
40      if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
41      InitializeComponent();
42
43      this.control = control;
44      this.content = content;
45      if (content.ProgressState == ProgressState.Started)
46        ShowProgress();
47      RegisterContentEvents();
48    }
49
50    private void RegisterContentEvents() {
51      content.StatusChanged += new EventHandler(progress_StatusChanged);
52      content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
53      content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
54      content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
55    }
56    private void DeregisterContentEvents() {
57      content.StatusChanged -= new EventHandler(progress_StatusChanged);
58      content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
59      content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
60      content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
61    }
62
63    private void ShowProgress() {
64      if (Control.InvokeRequired) {
65        Control.Invoke((Action)ShowProgress);
66        return;
67      }
68      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
69      Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
70      Anchor = AnchorStyles.None;
71
72      control.Enabled = false;
73      Parent = Control.Parent;
74      BringToFront();
75
76      UpdateProgressValue();
77      UpdateProgressStatus();
78      UpdateCancelButton();
79      Visible = true;
80    }
81
82    private void HideProgress() {
83      if (InvokeRequired) Invoke((Action)HideProgress);
84      else {
85        control.Enabled = true;
86        Parent = null;
87        Visible = false;
88      }
89    }
90
91    private void progress_StatusChanged(object sender, EventArgs e) {
92      UpdateProgressStatus();
93    }
94
95    private void progress_ProgressValueChanged(object sender, EventArgs e) {
96      UpdateProgressValue();
97    }
98
99    private void Content_ProgressStateChanged(object sender, EventArgs e) {
100      switch (content.ProgressState) {
101        case ProgressState.Finished: HideProgress(); break;
102        case ProgressState.Canceled: HideProgress(); break;
103        case ProgressState.Started: ShowProgress(); break;
104        default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
105      }
106    }
107
108    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
109      UpdateCancelButton();
110    }
111
112    private void UpdateCancelButton() {
113      cancelButton.Visible = content != null && content.CanBeCanceled;
114      cancelButton.Enabled = content != null && content.CanBeCanceled;
115    }
116
117    private void UpdateProgressValue() {
118      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
119      else {
120        if (content != null) {
121          double progressValue = content.ProgressValue;
122          if (progressValue <= 0.0 || progressValue > 1.0) {
123            progressBar.Style = ProgressBarStyle.Marquee;
124          } else {
125            progressBar.Style = ProgressBarStyle.Blocks;
126            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
127          }
128        }
129      }
130    }
131
132    private void UpdateProgressStatus() {
133      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
134      else if (content != null)
135        statusLabel.Text = content.Status;
136    }
137
138    private void cancelButton_Click(object sender, EventArgs e) {
139      content.Cancel();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.