Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 10147

Last change on this file since 10147 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 7.0 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.ComponentModel;
24using System.Windows.Forms;
25
26namespace HeuristicLab.MainForm.WindowsForms {
27  [View("ProgressView")]
28  [Content(typeof(IProgress), true)]
29  public partial class ProgressView : AsynchronousContentView {
30    private const int DefaultCancelTimeoutMs = 3000;
31    private ContentView parentView;
32
33    [Category("Custom"), Description("The time that the process is allowed to exit.")]
34    [DefaultValue(DefaultCancelTimeoutMs)]
35    public int CancelTimeoutMs { get; set; }
36    private bool ShouldSerializeCancelTimeoutMs() { return CancelTimeoutMs != DefaultCancelTimeoutMs; }
37
38    public new IProgress Content {
39      get { return (IProgress)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ProgressView() {
44      InitializeComponent();
45    }
46    public ProgressView(IProgress progress)
47      : this() {
48      Content = progress;
49    }
50    public ProgressView(ContentView parentView)
51      : this() {
52      if (parentView == null) throw new ArgumentNullException("parentView", "The parent view is null.");
53      this.parentView = parentView;
54    }
55    public ProgressView(ContentView parentView, IProgress progress)
56      : this(parentView) {
57      Content = progress;
58    }
59
60    protected override void RegisterContentEvents() {
61      Content.StatusChanged += new EventHandler(progress_StatusChanged);
62      Content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
63      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
64      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
65      base.RegisterContentEvents();
66    }
67
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70      Content.StatusChanged -= new EventHandler(progress_StatusChanged);
71      Content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
72      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
73      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      if (Content == null) {
79        HideProgress();
80      } else {
81        if (Content.ProgressState == ProgressState.Started)
82          ShowProgress();
83      }
84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();
88      cancelButton.Visible = Content != null && Content.CanBeCanceled;
89      cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly;
90    }
91
92    private void ShowProgress() {
93      if (InvokeRequired) Invoke((Action)ShowProgress);
94      else {
95        if (parentView != null) {
96          this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
97          this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
98          this.Anchor = AnchorStyles.None;
99
100          LockBackground();
101
102          if (!parentView.Controls.Contains(this))
103            parentView.Controls.Add(this);
104
105          BringToFront();
106        }
107        UpdateProgressValue();
108        UpdateProgressStatus();
109        Visible = true;
110      }
111    }
112
113    private void HideProgress() {
114      if (InvokeRequired) Invoke((Action)HideProgress);
115      else {
116        if (parentView != null) {
117          if (parentView.Controls.Contains(this))
118            parentView.Controls.Remove(this);
119
120          UnlockBackground();
121        }
122        Visible = false;
123      }
124    }
125
126    private void progress_StatusChanged(object sender, EventArgs e) {
127      UpdateProgressStatus();
128    }
129
130    private void progress_ProgressValueChanged(object sender, EventArgs e) {
131      UpdateProgressValue();
132    }
133
134    private void Content_ProgressStateChanged(object sender, EventArgs e) {
135      if (Content.ProgressState == ProgressState.Finished
136        || Content.ProgressState == ProgressState.Canceled)
137        HideProgress();
138      if (Content.ProgressState == ProgressState.Started)
139        ShowProgress();
140    }
141
142    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
143      SetEnabledStateOfControls();
144    }
145
146    private void LockBackground() {
147      if (InvokeRequired) {
148        Invoke((Action)LockBackground);
149      } else {
150        parentView.Locked = true;
151        parentView.ReadOnly = true;
152        Locked = false;
153        ReadOnly = false;
154      }
155    }
156
157    private void UnlockBackground() {
158      if (InvokeRequired) Invoke((Action)UnlockBackground);
159      else {
160        parentView.Locked = false;
161        parentView.ReadOnly = false;
162        Locked = true;
163        ReadOnly = true;
164      }
165    }
166
167    private void UpdateProgressValue() {
168      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
169      else {
170        if (Content != null) {
171          double progressValue = Content.ProgressValue;
172          if (progressValue <= 0.0 || progressValue > 1.0) {
173            if (progressBar.Style != ProgressBarStyle.Marquee)
174              progressBar.Style = ProgressBarStyle.Marquee;
175          } else {
176            if (progressBar.Style != ProgressBarStyle.Blocks)
177              progressBar.Style = ProgressBarStyle.Blocks;
178            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
179          }
180        }
181      }
182    }
183
184    private void UpdateProgressStatus() {
185      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
186      else if (Content != null)
187        statusLabel.Text = Content.Status;
188    }
189
190    private void cancelButton_Click(object sender, EventArgs e) {
191      if (Content != null) {
192        try {
193          Content.Cancel(CancelTimeoutMs);
194          ReadOnly = true;
195          cancelButtonTimer.Interval = CancelTimeoutMs;
196          cancelButtonTimer.Start();
197        } catch (NotSupportedException nse) {
198          PluginInfrastructure.ErrorHandling.ShowErrorDialog(nse);
199        }
200      }
201    }
202
203    private void cancelButtonTimer_Tick(object sender, EventArgs e) {
204      cancelButtonTimer.Stop();
205      if (Visible) ReadOnly = false;
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.