Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2719_HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis.Views/3.4/DatastreamAnalysisOptimizerView.cs @ 17980

Last change on this file since 17980 was 17980, checked in by jzenisek, 3 years ago

#2719 merged head of HeuristicLab.Problems.DataAnalysis into branch; added several minor items

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Views;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.DatastreamAnalysis {
33  [View("DatastreamAnalysisOptimizer View", "")]
34  [Content(typeof(DatastreamAnalysisOptimizer), true)]
35  public partial class DatastreamAnalysisOptimizerView : IOptimizerView {
36    public DatastreamAnalysisOptimizerView() {
37      InitializeComponent();
38      Content = new DatastreamAnalysisOptimizer();
39    }
40
41    public new DatastreamAnalysisOptimizer Content {
42      get { return (DatastreamAnalysisOptimizer) base.Content; }
43      set { base.Content = value; }
44    }
45
46    //protected override void Dispose(bool disposing) {
47    //  if (disposing) {
48    //    if (components != null) components.Dispose();
49    //  }
50    //  base.Dispose(disposing);
51    //}
52
53    protected override void OnInitialized(EventArgs e) {
54      // set order of tab pages according to z order.
55      // NOTE: this is required due to a bug in the VS designer.
56      List<Control> tabPages = new List<Control>();
57      for (int i = 0; i < tabControl.Controls.Count; i++) {
58        tabPages.Add(tabControl.Controls[i]);
59      }
60      tabControl.Controls.Clear();
61      foreach(Control control in tabPages)
62        tabControl.Controls.Add(control);
63
64      base.OnInitialized(e);
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      if (Content == null) {
70        ensemblesViewHost.Content = null;
71        datastreamViewHost.Content = null;
72        resultsView.Content = null;
73        runsView.Content = null;
74      } else {
75        ensemblesViewHost.ViewType = null;
76        ensemblesViewHost.Content = Content.Ensembles;
77        datastreamViewHost.ViewType = null;
78        datastreamViewHost.Content = Content.Datastream;
79        resultsView.Content = Content.Results.AsReadOnly();
80        runsView.Content = Content.Runs;
81      }
82    }
83
84    protected override void SetEnabledStateOfControls() {
85      base.SetEnabledStateOfControls();
86
87      resultsView.Enabled = Content != null;
88      runsView.Enabled = Content != null;
89    }
90
91    protected override void OnClosed(FormClosedEventArgs e) {
92      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
93        //The content must be stopped if no other view showing the content is available
94        var optimizers =
95          MainFormManager.MainForm.Views.OfType<IContentView>()
96            .Where(v => v != this)
97            .Select(v => v.Content)
98            .OfType<IOptimizer>();
99        if (!optimizers.Contains(Content)) {
100          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
101          if (!nestedOptimizers.Contains(Content)) Content.Stop();
102        }
103      }
104      base.OnClosed(e);
105    }
106
107    //protected override void startButton_Click(object sender, EventArgs e) {
108    //  base.startButton_Click(sender, e);
109    //}
110
111    protected override void RegisterContentEvents() {
112      base.RegisterContentEvents();
113      Content.EnsemblesChanged += new EventHandler(Content_EnsemblesChanged);
114      Content.DatastreamChanged += new EventHandler(Content_DatastreamChanged);
115    }
116
117    protected override void DeregisterContentEvents() {
118      Content.EnsemblesChanged -= new EventHandler(Content_EnsemblesChanged);
119      Content.DatastreamChanged -= new EventHandler(Content_DatastreamChanged);
120
121      base.DeregisterContentEvents();
122    }
123
124    #region content events
125    protected override void Content_Prepared(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_Prepared), sender, e);
128      else {
129        base.Content_Prepared(sender,e);
130        resultsView.Content = Content.Results.AsReadOnly();
131      }
132    }
133
134    protected void Content_EnsemblesChanged(object sender, EventArgs e) {
135      if (InvokeRequired)
136        Invoke(new EventHandler(Content_EnsemblesChanged), sender, e);
137      else {
138        ensemblesViewHost.ViewType = null;
139        ensemblesViewHost.Content = Content.Ensembles;
140      }
141    }
142
143    protected void Content_DatastreamChanged(object sender, EventArgs e) {
144      if (InvokeRequired)
145        Invoke(new EventHandler(Content_DatastreamChanged), sender, e);
146      else {
147        if (datastreamViewHost.Content != null && Content.Datastream != null &&
148            datastreamViewHost.Content.GetType() != Content.Datastream.GetType())
149          datastreamViewHost.ViewType = null;
150        datastreamViewHost.Content = Content.Datastream;
151      }
152    }
153    #endregion
154
155    #region event handlers
156    private void ensemblesTab_DragDrop(object sender, DragEventArgs e) {
157      if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
158        try {
159          var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
160          if (data is IProxyEnsembleModel) {
161            data = (IProxyEnsembleModel) data;
162            Content.Ensembles.Add((ProxyEnsembleModel) data);
163          }
164        }
165        catch (Exception ex) {
166          MessageBox.Show(ex.Message, "Set ensembles", MessageBoxButtons.OK, MessageBoxIcon.Error);
167        }
168      }
169    }
170
171    private void ensemblesTab_DragEnterOver(object sender, DragEventArgs e) {
172      e.Effect = DragDropEffects.None;
173      if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
174        return;
175
176      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
177
178      if (data is IProxyEnsembleModel) {
179        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
180        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
181        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
182        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
183        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
184      }
185    }
186
187    private void datastreamTab_DragDrop(object sender, DragEventArgs e) {
188      if (e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) {
189        try {
190          var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
191          if (data is IValueParameter) data = ((IValueParameter) data).Value;
192          if (data is IRegressionProblemData)
193            Content.Datastream.ProblemData = (RegressionProblemData) data;
194        }
195        catch (Exception ex) {
196          MessageBox.Show(ex.Message, "Set datastream", MessageBoxButtons.OK, MessageBoxIcon.Error);
197        }
198      }
199    }
200
201    private void datastreamTab_DragEnterOver(object sender, DragEventArgs e) {
202      e.Effect = DragDropEffects.None;
203
204      if (ReadOnly || !e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat))
205        return;
206
207      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
208      if (data is IValueParameter) data = ((IValueParameter) data).Value;
209      if (data is IRegressionProblemData) {
210        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
211        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
212        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
213        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
214        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
215      }
216    }
217
218    #endregion event handlers
219  }
220}
Note: See TracBrowser for help on using the repository browser.