Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Optimizer/3.3/StartPage.cs @ 17558

Last change on this file since 17558 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 8.4 KB
RevLine 
[3163]1#region License Information
2/* HeuristicLab
[17209]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3163]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;
[11048]23using System.Collections.Generic;
[3202]24using System.IO;
25using System.Linq;
26using System.Reflection;
[4068]27using System.Threading;
[3202]28using System.Windows.Forms;
[17120]29using HEAL.Attic;
[3376]30using HeuristicLab.Common;
[3202]31using HeuristicLab.Core;
[3163]32using HeuristicLab.MainForm;
33
34namespace HeuristicLab.Optimizer {
35  [View("Start Page")]
[3202]36  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
[11048]37    private const string StandardProblemsGroupName = "Standard Problems";
38    private const string DataAnalysisGroupName = "Data Analysis";
39    private const string ScriptsGroupName = "Scripts";
40    private const string UncategorizedGroupName = "Uncategorized";
41    private const string SampleNamePrefix = "HeuristicLab.Optimizer.Documents.";
42    private const string SampleNameSuffix = ".hl";
43
[11074]44    private readonly Dictionary<ListViewGroup, List<string>> groupLookup = new Dictionary<ListViewGroup, List<string>>();
45    private readonly ListViewGroup standardProblemsGroup = new ListViewGroup(StandardProblemsGroupName);
46    private readonly ListViewGroup dataAnalysisGroup = new ListViewGroup(DataAnalysisGroupName);
47    private readonly ListViewGroup scriptsGroup = new ListViewGroup(ScriptsGroupName);
48    private readonly ListViewGroup uncategorizedGroup = new ListViewGroup(UncategorizedGroupName);
[11048]49
[9907]50    private IProgress progress;
51
[3163]52    public StartPage() {
53      InitializeComponent();
54    }
55
56    protected override void OnInitialized(EventArgs e) {
57      base.OnInitialized(e);
[3202]58      Assembly assembly = Assembly.GetExecutingAssembly();
59      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
60                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
61      titleLabel.Text = "HeuristicLab Optimizer";
62      if (version != null) titleLabel.Text += " " + version.Version;
63
64      try {
65        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
66          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
[12743]67      } catch (Exception) { }
[3202]68
[3291]69      samplesListView.Enabled = false;
[11074]70      samplesListView.Groups.Add(standardProblemsGroup);
71      samplesListView.Groups.Add(dataAnalysisGroup);
72      samplesListView.Groups.Add(scriptsGroup);
73      samplesListView.Groups.Add(uncategorizedGroup);
[11048]74      FillGroupLookup();
75
[3291]76      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
77
78      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
79    }
80
81    protected override void OnClosing(FormClosingEventArgs e) {
82      base.OnClosing(e);
83      if (e.CloseReason == CloseReason.UserClosing) {
84        e.Cancel = true;
85        this.Hide();
86      }
87    }
88
89    private void LoadSamples(object state) {
[16430]90      progress = Progress.ShowOnControl(samplesListView, "Loading...");
[10488]91      try {
[11048]92        var assembly = Assembly.GetExecutingAssembly();
93        var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(SampleNameSuffix));
[10488]94        int count = samples.Count();
[3291]95
[11074]96        foreach (var entry in groupLookup) {
[11048]97          var group = entry.Key;
98          var sampleList = entry.Value;
99          foreach (var sampleName in sampleList) {
100            string resourceName = SampleNamePrefix + sampleName + SampleNameSuffix;
[11074]101            LoadSample(resourceName, assembly, group, count);
[3202]102          }
103        }
[11048]104
[11074]105        var categorizedSamples = groupLookup.Select(x => x.Value).SelectMany(x => x).Select(x => SampleNamePrefix + x + SampleNameSuffix);
[11048]106        var uncategorizedSamples = samples.Except(categorizedSamples);
107
108        foreach (var resourceName in uncategorizedSamples) {
[11074]109          LoadSample(resourceName, assembly, uncategorizedGroup, count);
[11048]110        }
111
[10488]112        OnAllSamplesLoaded();
[12743]113      } finally {
[16430]114        Progress.HideFromControl(samplesListView);
[10488]115      }
[3291]116    }
[11048]117
[11074]118    private void LoadSample(string name, Assembly assembly, ListViewGroup group, int count) {
[17120]119      using (var stream = assembly.GetManifestResourceStream(name)) {
120        var serializer = new ProtoBufSerializer();
121        var item = (NamedItem)serializer.Deserialize(stream, false);
122        OnSampleLoaded(item, group, 1.0 / count);
[11074]123      }
[11048]124    }
125
126    private void FillGroupLookup() {
[14204]127      var standardProblems = new List<string> { "ALPSGA_TSP", "ES_Griewank", "OSES_Griewank", "GA_Grouping", "GA_TSP", "GA_VRP", "GE_ArtificialAnt",
[15223]128                "IslandGA_TSP", "LS_Knapsack", "PSO_Rastrigin", "RAPGA_JSSP",
[14204]129                "SA_Rastrigin", "SGP_SantaFe", "GP_Multiplexer", "SGP_Robocode", "SS_VRP", "TS_TSP", "TS_VRP", "VNS_OP", "VNS_TSP", "GA_BPP"
[11074]130        };
131      groupLookup[standardProblemsGroup] = standardProblems;
[16430]132      var dataAnalysisProblems = new List<string> { "ALPSGP_SymReg", "SGP_SymbClass", "SGP_SymbReg", "OSGP_SymReg", "OSGP_TimeSeries", "GE_SymbReg", "GPR" };
[11074]133      groupLookup[dataAnalysisGroup] = dataAnalysisProblems;
[13228]134      var scripts = new List<string> { "GA_QAP_Script", "GUI_Automation_Script", "OSGA_Rastrigin_Script",
[11514]135                                       "GridSearch_RF_Classification_Script", "GridSearch_RF_Regression_Script",
136                                       "GridSearch_SVM_Classification_Script", "GridSearch_SVM_Regression_Script" };
[11074]137      groupLookup[scriptsGroup] = scripts;
[11048]138    }
139
140    private void OnSampleLoaded(INamedItem sample, ListViewGroup group, double progress) {
[3298]141      if (InvokeRequired)
[11048]142        Invoke(new Action<INamedItem, ListViewGroup, double>(OnSampleLoaded), sample, group, progress);
[3298]143      else {
[11048]144        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description }, group);
[3298]145        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
146        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
147        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
148        item.Tag = sample;
149        samplesListView.Items.Add(item);
[9907]150        this.progress.ProgressValue += progress;
[3202]151      }
[3291]152    }
153    private void OnAllSamplesLoaded() {
154      if (InvokeRequired)
155        Invoke(new Action(OnAllSamplesLoaded));
156      else {
157        samplesListView.Enabled = samplesListView.Items.Count > 0;
158        if (samplesListView.Items.Count > 0) {
159          for (int i = 0; i < samplesListView.Columns.Count; i++)
160            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
161        }
[3202]162      }
[3163]163    }
164
[3202]165    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
166      System.Diagnostics.Process.Start(e.LinkText);
167    }
168
169    private void samplesListView_DoubleClick(object sender, EventArgs e) {
[11067]170      if (samplesListView.SelectedItems.Count == 1) {
171        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
172        try {
173          mainForm.SetWaitCursor();
174          mainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
[12743]175        } finally {
[11067]176          mainForm.ResetWaitCursor();
177        }
178
179      }
[3202]180    }
[3298]181    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
182      ListViewItem listViewItem = (ListViewItem)e.Item;
183      IItem item = (IItem)listViewItem.Tag;
184      DataObject data = new DataObject();
[5837]185      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
[3298]186      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
187    }
[3202]188
[3163]189    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
190      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
191      Properties.Settings.Default.Save();
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.