Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9946 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
RevLine 
[3163]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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;
[3202]23using System.IO;
24using System.Linq;
25using System.Reflection;
[4068]26using System.Threading;
[3202]27using System.Windows.Forms;
[3376]28using HeuristicLab.Common;
[3202]29using HeuristicLab.Core;
[3163]30using HeuristicLab.MainForm;
[3202]31using HeuristicLab.Persistence.Default.Xml;
[3163]32
33namespace HeuristicLab.Optimizer {
34  [View("Start Page")]
[3202]35  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
[9907]36    private IProgress progress;
37
[3163]38    public StartPage() {
39      InitializeComponent();
40    }
41
42    protected override void OnInitialized(EventArgs e) {
43      base.OnInitialized(e);
[3202]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
[3291]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) {
[9907]71      progress = MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(samplesListView, "Loading...");
[3291]72      Assembly assembly = Assembly.GetExecutingAssembly();
73      var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hl"));
[3298]74      int count = samples.Count();
[3202]75      string path = Path.GetTempFileName();
[3291]76
77      foreach (string name in samples) {
78        try {
79          using (Stream stream = assembly.GetManifestResourceStream(name)) {
80            WriteStreamToTempFile(stream, path);
[3298]81            INamedItem item = XmlParser.Deserialize<INamedItem>(path);
[9907]82            OnSampleLoaded(item, 1.0 / count);
[3202]83          }
84        }
[9907]85        catch (Exception) {
86          MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
87        }
[3202]88      }
[3291]89      OnAllSamplesLoaded();
90    }
[9907]91    private void OnSampleLoaded(INamedItem sample, double progress) {
[3298]92      if (InvokeRequired)
[9907]93        Invoke(new Action<INamedItem, double>(OnSampleLoaded), sample, progress);
[3298]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);
[9907]101        this.progress.ProgressValue += progress;
[3202]102      }
[3291]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        }
[9907]113        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(samplesListView);
[3202]114      }
[3163]115    }
116
[3202]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)
[3557]123        MainFormManager.MainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
[3202]124    }
[3298]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();
[5837]129      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
[3298]130      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
131    }
[3202]132
[3163]133    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
134      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
135      Properties.Settings.Default.Save();
136    }
[3202]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
[3163]148  }
149}
Note: See TracBrowser for help on using the repository browser.