Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimplifierViewsProgress/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 15320

Last change on this file since 15320 was 15320, checked in by pfleck, 5 years ago

#1666 Adapted ProgressView so that 0 is a valid ProgressValue. Small refactorings.

File size: 6.2 KB
Line 
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
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      // prevent problems with object disposal and invoke as suggested by http://stackoverflow.com/a/18647091
144      if (!IsHandleCreated) return;
145      if (InvokeRequired) {
146        try {
147          Invoke((Action)UpdateProgressValue);
148        } catch (InvalidOperationException) {
149          // swallow ObjectDisposedException
150          // which might occur if the invoke call is executed after or while the control is disposing
151        }
152      } else {
153        if (content != null) {
154          double progressValue = content.ProgressValue;
155          if (progressValue < 0.0 || progressValue > 1.0) {
156            progressBar.Style = ProgressBarStyle.Marquee;
157          } else {
158            progressBar.Style = ProgressBarStyle.Blocks;
159            progressBar.Value =
160              (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
161          }
162        }
163      }
164    }
165
166    private void UpdateProgressStatus() {
167      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
168      else if (content != null)
169        statusLabel.Text = content.Status;
170    }
171
172    private void cancelButton_Click(object sender, EventArgs e) {
173      content.Cancel();
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.