1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Data;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 | using HeuristicLab.MainForm.WindowsForms;
|
---|
9 | using System.Windows.Forms;
|
---|
10 | using System.Diagnostics;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Hive.Experiment.Views {
|
---|
13 | public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
|
---|
14 | private ContentView parentView = null;
|
---|
15 | private bool simulateProgress = false;
|
---|
16 |
|
---|
17 | public bool CancelEnabled {
|
---|
18 | get {
|
---|
19 | return cancelButton.Visible;
|
---|
20 | }
|
---|
21 | set {
|
---|
22 | if (InvokeRequired) {
|
---|
23 | Invoke(new Action<bool>((ce) => { CancelEnabled = ce; }), value);
|
---|
24 | } else {
|
---|
25 | cancelButton.Visible = value;
|
---|
26 | }
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | private string status;
|
---|
31 | public string Status {
|
---|
32 | get { return status; }
|
---|
33 | set {
|
---|
34 | if (InvokeRequired) {
|
---|
35 | Invoke(new Action<string>((s) => { Status = s;}), value);
|
---|
36 | } else {
|
---|
37 | status = value;
|
---|
38 | statusLabel.Text = status;
|
---|
39 | }
|
---|
40 |
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public ProgressView(ContentView parentView)
|
---|
45 | : this(parentView, "", false, true) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | public ProgressView(ContentView parentView, string initialStatus)
|
---|
49 | : this(parentView, initialStatus, false, true) {
|
---|
50 | }
|
---|
51 |
|
---|
52 | public ProgressView(ContentView parentView, string initialStatus, bool simulateProgress)
|
---|
53 | : this(parentView, initialStatus, simulateProgress, true) {
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <param name="parentView">This is the View which will be locked if lockParentView is true</param>
|
---|
57 | /// <param name="initialStatus">Status message</param>
|
---|
58 | /// <param name="simulateProgress">If true, the progress will be simulated by a timer which addes some progress every second</param>
|
---|
59 | /// <param name="lockParentView">if true parentView will be locked</param>
|
---|
60 | public ProgressView(ContentView parentView, string initialStatus, bool simulateProgress, bool lockParentView) {
|
---|
61 | InitializeComponent();
|
---|
62 | this.parentView = parentView;
|
---|
63 | this.CancelEnabled = false;
|
---|
64 | this.Status = initialStatus;
|
---|
65 | this.simulateProgress = simulateProgress;
|
---|
66 |
|
---|
67 | if (simulateProgress) {
|
---|
68 | Timer progressSimulator = new Timer();
|
---|
69 | progressSimulator.Interval = 2000;
|
---|
70 | progressSimulator.Tick += new EventHandler(progressSimulator_Tick);
|
---|
71 | progressSimulator.Start();
|
---|
72 | }
|
---|
73 |
|
---|
74 | this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
|
---|
75 | this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
|
---|
76 | this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
|
---|
77 |
|
---|
78 | if (lockParentView)
|
---|
79 | LockBackground();
|
---|
80 |
|
---|
81 | parentView.Controls.Add(this);
|
---|
82 | this.BringToFront();
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void progressSimulator_Tick(object sender, EventArgs e) {
|
---|
86 | progressBar.Value = Math.Min(progressBar.Value + (progressBar.Maximum - progressBar.Value) / 10 + 1, progressBar.Maximum - 1);
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void LockBackground() {
|
---|
90 | this.parentView.Locked = true;
|
---|
91 | foreach (Control c in this.parentView.Controls) {
|
---|
92 | c.Enabled = false;
|
---|
93 | }
|
---|
94 | this.Enabled = true;
|
---|
95 | this.ReadOnly = false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void SetProgress(double progress) {
|
---|
99 | if (InvokeRequired) {
|
---|
100 | Invoke(new Action<double>(SetProgress), progress);
|
---|
101 | } else {
|
---|
102 | this.progressBar.Value = Math.Max(this.progressBar.Value, (int)(progress * progressBar.Maximum));
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void Finish() {
|
---|
107 | if (InvokeRequired) {
|
---|
108 | Invoke(new Action(Finish));
|
---|
109 | } else {
|
---|
110 | progressBar.Value = progressBar.Maximum;
|
---|
111 | parentView.Locked = false;
|
---|
112 | foreach (Control c in this.parentView.Controls) {
|
---|
113 | c.Enabled = true;
|
---|
114 | }
|
---|
115 | parentView.Controls.Remove(this);
|
---|
116 | this.Dispose();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
121 | Finish();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public event EventHandler Canceled;
|
---|
125 | protected virtual void OnCanceled() {
|
---|
126 | var handler = Canceled;
|
---|
127 | if (handler != null) Canceled(this, EventArgs.Empty);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|