Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs @ 8159

Last change on this file since 8159 was 8159, checked in by ascheibe, 12 years ago

#1762 removed blind disabling of controls

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
27    private ContentView parentView;
28
29    private IProgress progress;
30    public IProgress Progress {
31      get { return progress; }
32      set {
33        if (progress == value) return;
34        Finish();
35        progress = value;
36        RegisterProgressEvents();
37        ShowProgress();
38        OnProgressChanged();
39      }
40    }
41
42    private bool cancelEnabled;
43    public bool CancelEnabled {
44      get { return cancelEnabled; }
45      set {
46        cancelEnabled = value;
47        SetCancelButtonVisibility();
48      }
49    }
50
51    private void ShowProgress() {
52      if (progress != null) {
53        this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
54        this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
55        this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
56
57        LockBackground();
58
59        if (!parentView.Controls.Contains(this)) {
60          parentView.Controls.Add(this);
61        }
62
63        BringToFront();
64        Visible = true;
65      }
66    }
67
68    public ProgressView(ContentView parentView) {
69      InitializeComponent();
70      CancelEnabled = false;
71
72      if (parentView != null) {
73        this.parentView = parentView;
74      } else {
75        throw new ArgumentNullException("The parent view is null.");
76      }
77    }
78
79    private void RegisterProgressEvents() {
80      if (progress == null) return;
81      progress.Finished += new EventHandler(progress_Finished);
82      progress.StatusChanged += new EventHandler(progress_StatusChanged);
83      progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
84      progress.Canceled += new EventHandler(progress_Canceled);
85    }
86
87    private void DeregisterProgressEvents() {
88      if (progress == null) return;
89      progress.Finished -= new EventHandler(progress_Finished);
90      progress.StatusChanged -= new EventHandler(progress_StatusChanged);
91      progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
92      progress.Canceled -= new EventHandler(progress_Canceled);
93    }
94
95    private void progress_Finished(object sender, EventArgs e) {
96      Finish();
97    }
98
99    private void progress_StatusChanged(object sender, EventArgs e) {
100      UpdateProgressStatus();
101    }
102
103    private void progress_ProgressValueChanged(object sender, EventArgs e) {
104      UpdateProgressValue();
105    }
106
107    void progress_Canceled(object sender, EventArgs e) {
108      Finish();
109    }
110
111    private void LockBackground() {
112      if (InvokeRequired) {
113        Invoke((Action)LockBackground);
114      } else {
115        parentView.Locked = true;
116        parentView.ReadOnly = true;
117        Enabled = true;
118        ReadOnly = false;
119      }
120    }
121
122    private void UpdateProgressValue() {
123      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
124      else {
125        if (progress != null) {
126          double progressValue = progress.ProgressValue;
127          if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {
128            progressBar.Style = ProgressBarStyle.Marquee;
129            progressBar.Value = progressBar.Minimum;
130          } else {
131            progressBar.Style = ProgressBarStyle.Blocks;
132            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
133          }
134        }
135      }
136    }
137
138    private void UpdateProgressStatus() {
139      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
140      else {
141        if (progress != null) {
142          string status = progress.Status;
143          statusLabel.Text = progress.Status;
144        }
145      }
146    }
147
148    private void Finish() {
149      if (InvokeRequired) {
150        Invoke(new Action(Finish));
151      } else {
152        progressBar.Value = progressBar.Maximum;
153        parentView.Controls.Remove(this);
154        parentView.Locked = false;
155        parentView.ReadOnly = false;
156        DeregisterProgressEvents();
157        progress = null;
158        Visible = false;
159      }
160    }
161
162    private void cancelButton_Click(object sender, EventArgs e) {
163      if (progress != null) {
164        progress.CancelRequested = true;
165        cancelButton.Enabled = false;
166      }
167    }
168
169    private void SetCancelButtonVisibility() {
170      if (InvokeRequired) {
171        Invoke((Action)SetCancelButtonVisibility);
172      } else {
173        cancelButton.Visible = cancelEnabled;
174      }
175    }
176
177    private void OnProgressChanged() {
178      UpdateProgressStatus();
179      UpdateProgressValue();
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.