Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/StartPage.cs @ 11048

Last change on this file since 11048 was 11048, checked in by jkarder, 10 years ago

#2195:

  • redesigned start page
  • added scripting samples
File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.IO;
25using System.Linq;
26using System.Reflection;
27using System.Threading;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.MainForm;
32using HeuristicLab.Persistence.Default.Xml;
33
34namespace HeuristicLab.Optimizer {
35  [View("Start Page")]
36  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
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
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);
49
50    private IProgress progress;
51
52    public StartPage() {
53      InitializeComponent();
54    }
55
56    protected override void OnInitialized(EventArgs e) {
57      base.OnInitialized(e);
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);
67      }
68      catch (Exception) { }
69
70      samplesListView.Enabled = false;
71      samplesListView.Groups.Add(StandardProblemsGroup);
72      samplesListView.Groups.Add(DataAnalysisGroup);
73      samplesListView.Groups.Add(ScriptsGroup);
74      samplesListView.Groups.Add(UncategorizedGroup);
75      FillGroupLookup();
76
77      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
78
79      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
80    }
81
82    protected override void OnClosing(FormClosingEventArgs e) {
83      base.OnClosing(e);
84      if (e.CloseReason == CloseReason.UserClosing) {
85        e.Cancel = true;
86        this.Hide();
87      }
88    }
89
90    private void LoadSamples(object state) {
91      progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(samplesListView, "Loading...");
92      try {
93        var assembly = Assembly.GetExecutingAssembly();
94        var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(SampleNameSuffix));
95        int count = samples.Count();
96        string path = Path.GetTempFileName();
97
98        foreach (var entry in GroupLookup) {
99          var group = entry.Key;
100          var sampleList = entry.Value;
101          foreach (var sampleName in sampleList) {
102            string resourceName = SampleNamePrefix + sampleName + SampleNameSuffix;
103            LoadSample(resourceName, assembly, path, group, count);
104          }
105        }
106
107        var categorizedSamples = GroupLookup.Select(x => x.Value).SelectMany(x => x).Select(x => SampleNamePrefix + x + SampleNameSuffix);
108        var uncategorizedSamples = samples.Except(categorizedSamples);
109
110        foreach (var resourceName in uncategorizedSamples) {
111          LoadSample(resourceName, assembly, path, UncategorizedGroup, count);
112        }
113
114        OnAllSamplesLoaded();
115      }
116      finally {
117        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
118      }
119    }
120
121    private void LoadSample(string name, Assembly assembly, string path, ListViewGroup group, int count) {
122      try {
123        using (var stream = assembly.GetManifestResourceStream(name)) {
124          WriteStreamToTempFile(stream, path);
125          var item = XmlParser.Deserialize<INamedItem>(path);
126          OnSampleLoaded(item, group, 1.0 / count);
127        }
128      }
129      catch (Exception) { }
130    }
131
132    private void FillGroupLookup() {
133      var standardProblems = new List<string>(
134        new[] { "ES_Griewank", "GA_TSP", "GA_VRP", "GE_ArtificialAnt",
135                "IslandGA_TSP", "LS_Knapsack", "PSO_Schwefel", "RAPGA_JSSP",
136                "SA_Rastrigin", "SGP_SantaFe", "SS_VRP", "TS_TSP", "TS_VRP", "VNS_TSP"
137        });
138      GroupLookup[StandardProblemsGroup] = standardProblems;
139      var dataAnalysisProblems = new List<string>(new[] { "GE_SymbReg", "GPR", "SGP_SymbClass", "SGP_SymbReg" });
140      GroupLookup[DataAnalysisGroup] = dataAnalysisProblems;
141      var scripts = new List<string>(new[] { "GA_QAP_Script", "GUI_Automation_Script", "OSGA_Rastrigin_Script" });
142      GroupLookup[ScriptsGroup] = scripts;
143    }
144
145    private void OnSampleLoaded(INamedItem sample, ListViewGroup group, double progress) {
146      if (InvokeRequired)
147        Invoke(new Action<INamedItem, ListViewGroup, double>(OnSampleLoaded), sample, group, progress);
148      else {
149        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description }, group);
150        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
151        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
152        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
153        item.Tag = sample;
154        samplesListView.Items.Add(item);
155        this.progress.ProgressValue += progress;
156      }
157    }
158    private void OnAllSamplesLoaded() {
159      if (InvokeRequired)
160        Invoke(new Action(OnAllSamplesLoaded));
161      else {
162        samplesListView.Enabled = samplesListView.Items.Count > 0;
163        if (samplesListView.Items.Count > 0) {
164          for (int i = 0; i < samplesListView.Columns.Count; i++)
165            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
166        }
167      }
168    }
169
170    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
171      System.Diagnostics.Process.Start(e.LinkText);
172    }
173
174    private void samplesListView_DoubleClick(object sender, EventArgs e) {
175      if (samplesListView.SelectedItems.Count == 1)
176        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
177    }
178    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
179      ListViewItem listViewItem = (ListViewItem)e.Item;
180      IItem item = (IItem)listViewItem.Tag;
181      DataObject data = new DataObject();
182      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
183      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
184    }
185
186    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
187      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
188      Properties.Settings.Default.Save();
189    }
190
191    #region Helpers
192    private void WriteStreamToTempFile(Stream stream, string path) {
193      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
194        int cnt = 0;
195        byte[] buffer = new byte[32 * 1024];
196        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
197          output.Write(buffer, 0, cnt);
198      }
199    }
200    #endregion
201  }
202}
Note: See TracBrowser for help on using the repository browser.