Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs @ 7669

Last change on this file since 7669 was 7669, checked in by spimming, 13 years ago

#1680: merged changes from trunk into branch

File size: 4.8 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    private IProgress progress;
29    public IProgress Progress {
30      get {
31        return this.progress;
32      }
33      set {
34        if (this.progress != value) {
35          if (this.progress != null) {
36            DeregisterProgressEvents();
37          }
38          this.progress = value;
39          RegisterProgressEvents();
40          OnProgressChanged();
41        }
42      }
43    }
44
45    public bool CancelEnabled {
46      get {
47        return cancelButton.Visible;
48      }
49      set {
50        if (InvokeRequired) {
51          Invoke(new Action<bool>((ce) => { CancelEnabled = ce; }), value);
52        } else {
53          cancelButton.Visible = value;
54        }
55      }
56    }
57
58    /// <param name="parentView">This is the View which will be locked if lockParentView is true</param>
59    public ProgressView(ContentView parentView, IProgress progress) {
60      InitializeComponent();
61      this.parentView = parentView;
62      this.Progress = progress;
63
64      this.CancelEnabled = false;
65
66      progressBar.Style = ProgressBarStyle.Marquee;
67
68      this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
69      this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
70      this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
71
72      LockBackground();
73
74      parentView.Controls.Add(this);
75      this.BringToFront();
76    }
77
78    private void RegisterProgressEvents() {
79      progress.Finished += new EventHandler(progress_Finished);
80      progress.StatusChanged += new EventHandler(progress_StatusChanged);
81      progress.ProgressValueChanged += new EventHandler(progress_ProgressChanged);
82    }
83
84    private void DeregisterProgressEvents() {
85      progress.Finished -= new EventHandler(progress_Finished);
86      progress.StatusChanged -= new EventHandler(progress_StatusChanged);
87      progress.ProgressValueChanged -= new EventHandler(progress_ProgressChanged);
88    }
89
90    void progress_Finished(object sender, EventArgs e) {
91      Finish();
92    }
93
94    void progress_ProgressChanged(object sender, EventArgs e) {
95      if (InvokeRequired) {
96        Invoke(new EventHandler(progress_ProgressChanged), sender, e);
97      } else {
98        progressBar.Style = ProgressBarStyle.Blocks;
99        this.progressBar.Value = Math.Min(this.progressBar.Maximum, (int)(progress.ProgressValue * progressBar.Maximum));
100      }
101    }
102
103    void progress_StatusChanged(object sender, EventArgs e) {
104      if (InvokeRequired) {
105        Invoke(new EventHandler(progress_StatusChanged), sender, e);
106      } else {
107        statusLabel.Text = progress.Status;
108      }
109    }
110
111    private void LockBackground() {
112      if (InvokeRequired) {
113        Invoke(new Action(LockBackground));
114      } else {
115        this.parentView.Locked = true;
116        foreach (Control c in this.parentView.Controls) {
117          c.Enabled = false;
118        }
119        this.Enabled = true;
120        this.ReadOnly = false;
121      }
122    }
123
124    public void Finish() {
125      if (InvokeRequired) {
126        Invoke(new Action(Finish));
127      } else {
128        progressBar.Value = progressBar.Maximum;
129        parentView.Locked = false;
130        foreach (Control c in this.parentView.Controls) {
131          c.Enabled = true;
132        }
133        parentView.Controls.Remove(this);
134        DeregisterProgressEvents();
135        this.Dispose();
136      }
137    }
138
139    private void cancelButton_Click(object sender, EventArgs e) {
140      Finish();
141    }
142
143    private void OnProgressChanged() {
144      progress_StatusChanged(this, EventArgs.Empty);
145      progress_ProgressChanged(this, EventArgs.Empty);
146    }
147
148    public event EventHandler Canceled;
149    protected virtual void OnCanceled() {
150      var handler = Canceled;
151      if (handler != null) Canceled(this, EventArgs.Empty);
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.