Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 13371

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

#2212 updated copyright year

File size: 5.8 KB
RevLine 
[8187]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8187]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 {
[9933]26  internal sealed partial class ProgressView : UserControl {
27    private const int defaultControlHeight = 88;
28    private const int collapsedControlHeight = 55;
[8187]29
[9933]30    private readonly Control control;
31    public Control Control {
32      get { return control; }
33    }
[8187]34
[9933]35    private readonly IProgress content;
36    public IProgress Content {
37      get { return content; }
[8187]38    }
39
[9933]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.");
[8187]44      InitializeComponent();
45
[9933]46      this.control = control;
47      this.content = content;
48      if (content.ProgressState == ProgressState.Started)
49        ShowProgress();
50      RegisterContentEvents();
[8187]51    }
52
[9933]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();
[8187]60
[9933]61      if (disposing && (components != null)) {
62        components.Dispose();
[8187]63      }
[9933]64      base.Dispose(disposing);
[8187]65    }
66
[9933]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);
[8187]72    }
[9933]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    }
[8187]79
80    private void ShowProgress() {
[9933]81      if (Control.InvokeRequired) {
82        Control.Invoke((Action)ShowProgress);
83        return;
84      }
85      int height = Content.CanBeCanceled ? Height : collapsedControlHeight;
[8187]86
[9933]87      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
88      Top = (Control.ClientRectangle.Height / 2) - (height / 2);
89      Anchor = AnchorStyles.None;
[8187]90
[9933]91      control.Enabled = false;
92      Parent = Control.Parent;
93      BringToFront();
[8187]94
[9933]95      UpdateProgressValue();
96      UpdateProgressStatus();
97      UpdateCancelButton();
98      Visible = true;
[8187]99    }
100
101    private void HideProgress() {
102      if (InvokeRequired) Invoke((Action)HideProgress);
103      else {
[9933]104        control.Enabled = true;
105        Parent = null;
[8187]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) {
[9933]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      }
[8187]125    }
126
127    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
[9933]128      UpdateCancelButton();
[8187]129    }
130
[9933]131    private void UpdateCancelButton() {
132      cancelButton.Visible = content != null && content.CanBeCanceled;
133      cancelButton.Enabled = content != null && content.CanBeCanceled;
[8187]134
[9933]135      if (content != null && content.CanBeCanceled) {
136        Height = defaultControlHeight;
137      } else if (content != null && !content.CanBeCanceled) {
138        Height = collapsedControlHeight;
[8187]139      }
140    }
141
142    private void UpdateProgressValue() {
143      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
144      else {
[9933]145        if (content != null) {
146          double progressValue = content.ProgressValue;
[8187]147          if (progressValue <= 0.0 || progressValue > 1.0) {
[9933]148            progressBar.Style = ProgressBarStyle.Marquee;
[8187]149          } else {
[9933]150            progressBar.Style = ProgressBarStyle.Blocks;
[8187]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);
[9933]159      else if (content != null)
160        statusLabel.Text = content.Status;
[8187]161    }
162
163    private void cancelButton_Click(object sender, EventArgs e) {
[9933]164      content.Cancel();
[8187]165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.