Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1721-RandomForestPersistence/HeuristicLab.Optimizer/3.3/StartPage.cs @ 10879

Last change on this file since 10879 was 9907, checked in by ascheibe, 11 years ago

#1042

  • the size of the ProgressView is now adjusted if the Cancel button is not shown
  • replaced the progress bar of the start page with the ProgressView
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.IO;
24using System.Linq;
25using System.Reflection;
26using System.Threading;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Optimizer {
34  [View("Start Page")]
35  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
36    private IProgress progress;
37
38    public StartPage() {
39      InitializeComponent();
40    }
41
42    protected override void OnInitialized(EventArgs e) {
43      base.OnInitialized(e);
44      Assembly assembly = Assembly.GetExecutingAssembly();
45      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
46                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
47      titleLabel.Text = "HeuristicLab Optimizer";
48      if (version != null) titleLabel.Text += " " + version.Version;
49
50      try {
51        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
52          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
53      }
54      catch (Exception) { }
55
56      samplesListView.Enabled = false;
57      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
58
59      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
60    }
61
62    protected override void OnClosing(FormClosingEventArgs e) {
63      base.OnClosing(e);
64      if (e.CloseReason == CloseReason.UserClosing) {
65        e.Cancel = true;
66        this.Hide();
67      }
68    }
69
70    private void LoadSamples(object state) {
71      progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(samplesListView, "Loading...");
72      Assembly assembly = Assembly.GetExecutingAssembly();
73      var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hl"));
74      int count = samples.Count();
75      string path = Path.GetTempFileName();
76
77      foreach (string name in samples) {
78        try {
79          using (Stream stream = assembly.GetManifestResourceStream(name)) {
80            WriteStreamToTempFile(stream, path);
81            INamedItem item = XmlParser.Deserialize<INamedItem>(path);
82            OnSampleLoaded(item, 1.0 / count);
83          }
84        }
85        catch (Exception) {
86          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
87        }
88      }
89      OnAllSamplesLoaded();
90    }
91    private void OnSampleLoaded(INamedItem sample, double progress) {
92      if (InvokeRequired)
93        Invoke(new Action<INamedItem, double>(OnSampleLoaded), sample, progress);
94      else {
95        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description });
96        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
97        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
98        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
99        item.Tag = sample;
100        samplesListView.Items.Add(item);
101        this.progress.ProgressValue += progress;
102      }
103    }
104    private void OnAllSamplesLoaded() {
105      if (InvokeRequired)
106        Invoke(new Action(OnAllSamplesLoaded));
107      else {
108        samplesListView.Enabled = samplesListView.Items.Count > 0;
109        if (samplesListView.Items.Count > 0) {
110          for (int i = 0; i < samplesListView.Columns.Count; i++)
111            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
112        }
113        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
114      }
115    }
116
117    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
118      System.Diagnostics.Process.Start(e.LinkText);
119    }
120
121    private void samplesListView_DoubleClick(object sender, EventArgs e) {
122      if (samplesListView.SelectedItems.Count == 1)
123        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
124    }
125    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
126      ListViewItem listViewItem = (ListViewItem)e.Item;
127      IItem item = (IItem)listViewItem.Tag;
128      DataObject data = new DataObject();
129      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
130      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
131    }
132
133    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
134      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
135      Properties.Settings.Default.Save();
136    }
137
138    #region Helpers
139    private void WriteStreamToTempFile(Stream stream, string path) {
140      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
141        int cnt = 0;
142        byte[] buffer = new byte[32 * 1024];
143        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
144          output.Write(buffer, 0, cnt);
145      }
146    }
147    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.