Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core.Views/3.3/MovieView.cs @ 18229

Last change on this file since 18229 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.9 KB
RevLine 
[4641]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4641]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.Linq;
24using System.Threading;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Core.Views {
31  [View("Movie View")]
32  public partial class MovieView<T> : ItemView where T : class, IItem {
[4715]33    #region Delay
34    protected class Delay {
35      public string Text { get; private set; }
36      public int Milliseconds { get; private set; }
37
38      public Delay(string text, int milliseconds) {
39        Text = text;
40        Milliseconds = milliseconds;
41      }
42
43      public override string ToString() {
44        return Text;
45      }
46    }
47    #endregion
48
49    protected int delay;
50
[4641]51    public new IItemCollection<T> Content {
52      get { return (IItemCollection<T>)base.Content; }
53      set { base.Content = value; }
54    }
55
56    public MovieView() {
57      InitializeComponent();
[4715]58
59      delayComboBox.Items.Add(new Delay("5s", 5000));
60      delayComboBox.Items.Add(new Delay("2s", 2000));
61      delayComboBox.Items.Add(new Delay("1s", 1000));
62      delayComboBox.Items.Add(new Delay("0.5s", 500));
63      delayComboBox.Items.Add(new Delay("0.1s", 100));
64      delayComboBox.Items.Add(new Delay("0.05s", 50));
65      delayComboBox.Items.Add(new Delay("0.01s", 10));
66      delayComboBox.SelectedIndex = 4;
67      delay = 100;
[4641]68    }
69
70    protected override void DeregisterContentEvents() {
71      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
72      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
73      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
74      base.DeregisterContentEvents();
75    }
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
79      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
80      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
81    }
82
83    protected override void OnContentChanged() {
84      base.OnContentChanged();
85      if (backgroundWorker.IsBusy) backgroundWorker.CancelAsync();
86      if (Content == null) {
87        trackBar.Maximum = 10;
88        viewHost.Content = null;
89      } else {
90        Caption += " (" + Content.GetType().Name + ")";
91        trackBar.Maximum = Content.Count - 1;
92        viewHost.Content = Content.FirstOrDefault();
93      }
94      trackBar.Value = 0;
95      UpdateLables();
96    }
97
98    protected override void SetEnabledStateOfControls() {
99      base.SetEnabledStateOfControls();
[4715]100      firstButton.Enabled = (Content != null) && (Content.Count > 0) && (trackBar.Value != trackBar.Minimum) && (!backgroundWorker.IsBusy);
101      previousButton.Enabled = (Content != null) && (Content.Count > 0) && (trackBar.Value != trackBar.Minimum) && (!backgroundWorker.IsBusy);
[4641]102      trackBar.Enabled = (Content != null) && (Content.Count > 0) && (!backgroundWorker.IsBusy);
[4715]103      nextButton.Enabled = (Content != null) && (Content.Count > 0) && (trackBar.Value != trackBar.Maximum) && (!backgroundWorker.IsBusy);
104      lastButton.Enabled = (Content != null) && (Content.Count > 0) && (trackBar.Value != trackBar.Maximum) && (!backgroundWorker.IsBusy);
[4641]105      playButton.Enabled = (Content != null) && (Content.Count > 0) && (!backgroundWorker.IsBusy);
106      stopButton.Enabled = (Content != null) && (backgroundWorker.IsBusy);
[4715]107      delayComboBox.Enabled = (Content != null) && (Content.Count > 0);
[4641]108    }
109
110    protected override void OnClosed(FormClosedEventArgs e) {
111      base.OnClosed(e);
112      if (backgroundWorker.IsBusy) backgroundWorker.CancelAsync();
113    }
114
115    #region Content Events
116    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
117      if (InvokeRequired)
118        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
119      else {
120        trackBar.Maximum = Content.Count - 1;
121        maximumLabel.Text = trackBar.Maximum.ToString();
[4715]122        if (viewHost.Content == null) viewHost.Content = Content.FirstOrDefault();
123        SetEnabledStateOfControls();
[4641]124      }
125    }
126    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
127      if (InvokeRequired)
128        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
129      else {
130        trackBar.Maximum = Content.Count - 1;
131        maximumLabel.Text = trackBar.Maximum.ToString();
132        if (e.Items.Contains((T)viewHost.Content)) {
133          trackBar.Value = 0;
134          viewHost.Content = Content.FirstOrDefault();
135          UpdateLables();
136        }
[4715]137        SetEnabledStateOfControls();
[4641]138      }
139    }
140    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
141      if (InvokeRequired)
142        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
143      else {
144        trackBar.Maximum = Content.Count - 1;
145        trackBar.Value = 0;
146        viewHost.Content = Content.FirstOrDefault();
147        UpdateLables();
148        SetEnabledStateOfControls();
149      }
150    }
151    #endregion
152
153    #region Control Events
154    protected virtual void trackBar_ValueChanged(object sender, EventArgs e) {
[4715]155      viewHost.Content = Content == null ? null : Content.ElementAtOrDefault(trackBar.Value);
[4641]156      indexLabel.Text = trackBar.Value.ToString();
[4715]157      SetEnabledStateOfControls();
[4641]158    }
159    protected virtual void firstButton_Click(object sender, EventArgs e) {
[4715]160      if (trackBar.Value != trackBar.Minimum) trackBar.Value = trackBar.Minimum;
[4641]161    }
[4715]162    protected virtual void previousButton_Click(object sender, EventArgs e) {
163      if (trackBar.Value != trackBar.Minimum) trackBar.Value--;
164    }
165    protected virtual void nextButton_Click(object sender, EventArgs e) {
166      if (trackBar.Value != trackBar.Maximum) trackBar.Value++;
167    }
[4641]168    protected virtual void lastButton_Click(object sender, EventArgs e) {
[4715]169      if (trackBar.Value != trackBar.Maximum) trackBar.Value = trackBar.Maximum;
[4641]170    }
171    protected virtual void playButton_Click(object sender, EventArgs e) {
172      firstButton.Enabled = false;
[4715]173      previousButton.Enabled = false;
[4641]174      trackBar.Enabled = false;
[4715]175      nextButton.Enabled = false;
[4641]176      lastButton.Enabled = false;
177      playButton.Enabled = false;
178      stopButton.Enabled = true;
179      backgroundWorker.RunWorkerAsync();
180    }
181    protected virtual void stopButton_Click(object sender, EventArgs e) {
[4715]182      stopButton.Enabled = false;
[4641]183      backgroundWorker.CancelAsync();
184    }
[4715]185    protected virtual void delayComboBox_SelectedIndexChanged(object sender, EventArgs e) {
186      Delay selected = delayComboBox.SelectedItem as Delay;
187      if (selected != null) delay = selected.Milliseconds;
188    }
[4641]189    #endregion
190
191    #region BackgroundWorker Events
192    protected virtual void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
193      bool terminate = false;
194      while (!backgroundWorker.CancellationPending && !terminate) {
195        Invoke((Action)delegate() {
196          if (trackBar.Value < trackBar.Maximum) trackBar.Value++;
197          terminate = trackBar.Value == trackBar.Maximum;
198        });
[4715]199        Thread.Sleep(delay);
[4641]200      }
201    }
202    protected virtual void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) {
203      firstButton.Enabled = true;
[4715]204      previousButton.Enabled = true;
[4641]205      trackBar.Enabled = true;
[4715]206      nextButton.Enabled = true;
[4641]207      lastButton.Enabled = true;
208      playButton.Enabled = true;
209      stopButton.Enabled = false;
210    }
211    #endregion
212
213    #region Helpers
214    protected virtual void UpdateLables() {
215      minimumLabel.Text = trackBar.Minimum.ToString();
216      indexLabel.Text = trackBar.Value.ToString();
217      maximumLabel.Text = trackBar.Maximum.ToString();
218    }
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.