Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/04/19 14:45:47 (5 years ago)
Author:
mkommend
Message:

#2845: Merged 16430 into stable.

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs

    r15584 r17062  
    2525namespace HeuristicLab.MainForm.WindowsForms {
    2626  internal sealed partial class ProgressView : UserControl {
    27     private const int defaultControlHeight = 88;
    28     private const int collapsedControlHeight = 55;
    29 
    3027    private readonly Control control;
    3128    public Control Control {
     
    4037    public ProgressView(Control control, IProgress content)
    4138      : 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.");
     39      if (control == null) throw new ArgumentNullException("control");
     40      if (content == null) throw new ArgumentNullException("content");
    4441      InitializeComponent();
    4542
    4643      this.control = control;
    4744      this.content = content;
    48       if (content.ProgressState == ProgressState.Started)
     45
     46      if (content.ProgressState != ProgressState.Finished)
    4947        ShowProgress();
    5048      RegisterContentEvents();
    5149    }
    5250
    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>
    5751    protected override void Dispose(bool disposing) {
    5852      DeregisterContentEvents();
     
    6660
    6761    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);
     62      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
     63      Content.MessageChanged += new EventHandler(Content_MessageChanged);
     64      Content.ProgressBarModeChanged += new EventHandler(Content_ProgressBarModeChanged);
     65      Content.ProgressValueChanged += new EventHandler(Content_ProgressValueChanged);
     66      Content.CanBeStoppedChanged += new EventHandler(Content_CanBeStoppedChanged);
     67      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
    7268    }
    7369    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);
     70      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
     71      Content.MessageChanged -= new EventHandler(Content_MessageChanged);
     72      Content.ProgressBarModeChanged -= new EventHandler(Content_ProgressBarModeChanged);
     73      Content.ProgressValueChanged -= new EventHandler(Content_ProgressValueChanged);
     74      Content.CanBeStoppedChanged -= new EventHandler(Content_CanBeStoppedChanged);
     75      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
     76    }
     77
     78    private void Content_ProgressStateChanged(object sender, EventArgs e) {
     79      UpdateProgressState();
     80      UpdateButtonsState();
     81    }
     82
     83    private void Content_MessageChanged(object sender, EventArgs e) {
     84      UpdateProgressMessage();
     85    }
     86
     87    private void Content_ProgressBarModeChanged(object sender, EventArgs e) {
     88      UpdateProgressValue();
     89    }
     90    private void Content_ProgressValueChanged(object sender, EventArgs e) {
     91      UpdateProgressValue();
     92    }
     93
     94    private void Content_CanBeStoppedChanged(object sender, EventArgs e) {
     95      UpdateButtonsState();
     96    }
     97    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
     98      UpdateButtonsState();
    7899    }
    79100
     
    83104        return;
    84105      }
    85       int height = Content.CanBeCanceled ? Height : collapsedControlHeight;
     106      if (Parent != null) return;
    86107
    87108      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
    88       Top = (Control.ClientRectangle.Height / 2) - (height / 2);
     109      Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
    89110      Anchor = AnchorStyles.None;
    90111
    91       control.Enabled = false;
     112      UpdateProgressMessage();
     113      UpdateProgressValue();
     114      UpdateButtonsState();
     115
     116      Control.Enabled = false;
    92117      Parent = Control.Parent;
    93118      BringToFront();
    94 
    95       UpdateProgressValue();
    96       UpdateProgressStatus();
    97       UpdateCancelButton();
    98119      Visible = true;
    99120    }
    100121
    101122    private void HideProgress() {
    102       if (InvokeRequired) Invoke((Action)HideProgress);
    103       else {
    104         control.Enabled = true;
    105         Parent = null;
    106         Visible = false;
     123      if (Control.InvokeRequired) {
     124        Control.Invoke((Action)HideProgress);
     125        return;
     126      }
     127      if (Parent == null) return;
     128
     129      Visible = false;
     130      Control.Enabled = true;
     131      Parent = null;
     132    }
     133
     134    private void UpdateProgressState() {
     135      if (Control.InvokeRequired) {
     136        Control.Invoke((Action)UpdateProgressState);
     137        return;
     138      }
     139
     140      if (Content.ProgressState != ProgressState.Finished)
     141        ShowProgress();
     142      else
     143        HideProgress();
     144    }
     145
     146    private void UpdateProgressMessage() {
     147      if (Control.InvokeRequired) {
     148        Control.Invoke((Action)UpdateProgressMessage);
     149        return;
     150      }
     151
     152      messageLabel.Text = content.Message;
     153    }
     154
     155    private void UpdateProgressValue() {
     156      if (InvokeRequired) {
     157        Invoke((Action)UpdateProgressValue);
     158        return;
     159      }
     160
     161      switch (Content.ProgressMode) {
     162        case ProgressMode.Determinate:
     163          progressBar.Style = ProgressBarStyle.Continuous;
     164          progressBar.Value = (int)Math.Round(progressBar.Minimum + content.ProgressValue * (progressBar.Maximum - progressBar.Minimum));
     165          break;
     166        case ProgressMode.Indeterminate:
     167          progressBar.Style = ProgressBarStyle.Marquee;
     168          progressBar.Value = 0;
     169          break;
     170        default:
     171          throw new NotImplementedException($"Invalid Progress Mode: {content.ProgressMode}");
    107172      }
    108173    }
    109174
    110     private void progress_StatusChanged(object sender, EventArgs e) {
    111       UpdateProgressStatus();
     175    private void UpdateButtonsState() {
     176      if (Control.InvokeRequired) {
     177        Control.Invoke((Action)UpdateButtonsState);
     178        return;
     179      }
     180
     181      stopButton.Visible = Content.CanBeStopped;
     182      stopButton.Enabled = Content.CanBeStopped && content.ProgressState == ProgressState.Started;
     183
     184      cancelButton.Visible = Content.CanBeCanceled;
     185      cancelButton.Enabled = Content.CanBeCanceled && content.ProgressState == ProgressState.Started;
    112186    }
    113187
    114     private void progress_ProgressValueChanged(object sender, EventArgs e) {
    115       UpdateProgressValue();
     188    private void stopButton_Click(object sender, EventArgs e) {
     189      Content.Stop();
    116190    }
    117 
    118     private void Content_ProgressStateChanged(object sender, EventArgs e) {
    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       }
    125     }
    126 
    127     private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
    128       UpdateCancelButton();
    129     }
    130 
    131     private void UpdateCancelButton() {
    132       cancelButton.Visible = content != null && content.CanBeCanceled;
    133       cancelButton.Enabled = content != null && content.CanBeCanceled;
    134 
    135       if (content != null && content.CanBeCanceled) {
    136         Height = defaultControlHeight;
    137       } else if (content != null && !content.CanBeCanceled) {
    138         Height = collapsedControlHeight;
    139       }
    140     }
    141 
    142     private void UpdateProgressValue() {
    143       // prevent problems with object disposal and invoke as suggested by http://stackoverflow.com/a/18647091
    144       if (!IsHandleCreated) return;
    145       if (InvokeRequired) {
    146         try {
    147           Invoke((Action)UpdateProgressValue);
    148         }
    149         catch (InvalidOperationException) {
    150           // swallow ObjectDisposedException
    151           // which might occur if the invoke call is executed after or while the control is disposing
    152         }
    153       } else {
    154         if (content != null) {
    155           double progressValue = content.ProgressValue;
    156           if (progressValue <= 0.0 || progressValue > 1.0) {
    157             progressBar.Style = ProgressBarStyle.Marquee;
    158           } else {
    159             progressBar.Style = ProgressBarStyle.Blocks;
    160             progressBar.Value =
    161               (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    162           }
    163         }
    164       }
    165     }
    166 
    167     private void UpdateProgressStatus() {
    168       if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
    169       else if (content != null)
    170         statusLabel.Text = content.Status;
    171     }
    172 
    173191    private void cancelButton_Click(object sender, EventArgs e) {
    174       content.Cancel();
     192      Content.Cancel();
    175193    }
    176194  }
  • stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.designer.cs

    r15584 r17062  
    2727    private System.ComponentModel.IContainer components = null;
    2828
    29    
     29
    3030    #region Component Designer generated code
    3131
     
    3636    private void InitializeComponent() {
    3737      this.progressBar = new System.Windows.Forms.ProgressBar();
    38       this.statusLabel = new System.Windows.Forms.Label();
     38      this.messageLabel = new System.Windows.Forms.Label();
     39      this.borderPanel = new System.Windows.Forms.Panel();
     40      this.panel = new System.Windows.Forms.Panel();
     41      this.stopButton = new System.Windows.Forms.Button();
    3942      this.cancelButton = new System.Windows.Forms.Button();
    40       this.panel = new System.Windows.Forms.Panel();
     43      this.borderPanel.SuspendLayout();
    4144      this.panel.SuspendLayout();
    4245      this.SuspendLayout();
     
    4851      this.progressBar.Location = new System.Drawing.Point(3, 3);
    4952      this.progressBar.Name = "progressBar";
    50       this.progressBar.Size = new System.Drawing.Size(352, 23);
     53      this.progressBar.Size = new System.Drawing.Size(366, 23);
    5154      this.progressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
    5255      this.progressBar.TabIndex = 0;
    5356      //
    54       // statusLabel
     57      // messageLabel
    5558      //
    56       this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     59      this.messageLabel.Dock = System.Windows.Forms.DockStyle.Fill;
     60      this.messageLabel.Location = new System.Drawing.Point(0, 0);
     61      this.messageLabel.Name = "messageLabel";
     62      this.messageLabel.Padding = new System.Windows.Forms.Padding(0, 0, 3, 0);
     63      this.messageLabel.Size = new System.Drawing.Size(217, 23);
     64      this.messageLabel.TabIndex = 1;
     65      this.messageLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
     66      //
     67      // borderPanel
     68      //
     69      this.borderPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     70            | System.Windows.Forms.AnchorStyles.Left)
    5771            | System.Windows.Forms.AnchorStyles.Right)));
    58       this.statusLabel.Location = new System.Drawing.Point(3, 33);
    59       this.statusLabel.Name = "statusLabel";
    60       this.statusLabel.Size = new System.Drawing.Size(352, 17);
    61       this.statusLabel.TabIndex = 1;
     72      this.borderPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
     73      this.borderPanel.Controls.Add(this.panel);
     74      this.borderPanel.Controls.Add(this.progressBar);
     75      this.borderPanel.Location = new System.Drawing.Point(0, 0);
     76      this.borderPanel.Name = "borderPanel";
     77      this.borderPanel.Size = new System.Drawing.Size(374, 62);
     78      this.borderPanel.TabIndex = 3;
     79      //
     80      // panel
     81      //
     82      this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     83            | System.Windows.Forms.AnchorStyles.Right)));
     84      this.panel.Controls.Add(this.messageLabel);
     85      this.panel.Controls.Add(this.stopButton);
     86      this.panel.Controls.Add(this.cancelButton);
     87      this.panel.Location = new System.Drawing.Point(3, 32);
     88      this.panel.Name = "panel";
     89      this.panel.Size = new System.Drawing.Size(367, 23);
     90      this.panel.TabIndex = 4;
     91      //
     92      // stopButton
     93      //
     94      this.stopButton.Dock = System.Windows.Forms.DockStyle.Right;
     95      this.stopButton.Location = new System.Drawing.Point(217, 0);
     96      this.stopButton.Name = "stopButton";
     97      this.stopButton.Size = new System.Drawing.Size(75, 23);
     98      this.stopButton.TabIndex = 3;
     99      this.stopButton.Text = "Stop";
     100      this.stopButton.UseVisualStyleBackColor = true;
     101      this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
    62102      //
    63103      // cancelButton
    64104      //
    65       this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
    66       this.cancelButton.Location = new System.Drawing.Point(280, 53);
     105      this.cancelButton.Dock = System.Windows.Forms.DockStyle.Right;
     106      this.cancelButton.Location = new System.Drawing.Point(292, 0);
    67107      this.cancelButton.Name = "cancelButton";
    68108      this.cancelButton.Size = new System.Drawing.Size(75, 23);
     
    72112      this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
    73113      //
    74       // panel
    75       //
    76       this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    77             | System.Windows.Forms.AnchorStyles.Left)
    78             | System.Windows.Forms.AnchorStyles.Right)));
    79       this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    80       this.panel.Controls.Add(this.progressBar);
    81       this.panel.Controls.Add(this.cancelButton);
    82       this.panel.Controls.Add(this.statusLabel);
    83       this.panel.Location = new System.Drawing.Point(0, 0);
    84       this.panel.Name = "panel";
    85       this.panel.Size = new System.Drawing.Size(360, 88);
    86       this.panel.TabIndex = 3;
    87       //
    88114      // ProgressView
    89115      //
    90116      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    91       this.Controls.Add(this.panel);
     117      this.Controls.Add(this.borderPanel);
    92118      this.Name = "ProgressView";
    93       this.Size = new System.Drawing.Size(360, 88);
     119      this.Size = new System.Drawing.Size(374, 62);
     120      this.borderPanel.ResumeLayout(false);
    94121      this.panel.ResumeLayout(false);
    95122      this.ResumeLayout(false);
     
    100127
    101128    private System.Windows.Forms.ProgressBar progressBar;
    102     private System.Windows.Forms.Label statusLabel;
     129    private System.Windows.Forms.Label messageLabel;
     130    private System.Windows.Forms.Panel borderPanel;
     131    private System.Windows.Forms.Button stopButton;
     132    private System.Windows.Forms.Panel panel;
    103133    private System.Windows.Forms.Button cancelButton;
    104     private System.Windows.Forms.Panel panel;
    105134  }
    106135}
Note: See TracChangeset for help on using the changeset viewer.