Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The visual representation of a <see cref="Variable"/>.
33  /// </summary>
34  [View("Run View")]
35  [Content(typeof(IRun), true)]
36  public sealed partial class RunView : NamedItemView {
37    /// <summary>
38    /// Gets or sets the variable to represent visually.
39    /// </summary>
40    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
41    /// No own data storage present.</remarks>
42    public new IRun Content {
43      get { return (IRun)base.Content; }
44      set { base.Content = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
49    /// </summary>
50    public RunView() {
51      InitializeComponent();
52    }
53
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.Changed += new EventHandler(Content_Changed);
57    }
58    protected override void DeregisterContentEvents() {
59      base.DeregisterContentEvents();
60      Content.Changed -= new EventHandler(Content_Changed);
61    }
62    private void Content_Changed(object sender, EventArgs e) {
63      if (InvokeRequired)
64        this.Invoke(new EventHandler(Content_Changed), sender, e);
65      else
66        UpdateColorPictureBox();
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      viewHost.Content = null;
72      if (Content != null)
73        UpdateColorPictureBox();
74      FillListView();
75    }
76
77    protected override void SetEnabledStateOfControls() {
78      base.SetEnabledStateOfControls();
79      listView.Enabled = Content != null;
80      viewHost.Enabled = Content != null;
81      changeColorButton.Enabled = Content != null;
82      showAlgorithmButton.Enabled = Content != null && !Locked;
83    }
84
85    private void changeColorButton_Click(object sender, EventArgs e) {
86      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
87        this.Content.Color = this.colorDialog.Color;
88      }
89    }
90    private void UpdateColorPictureBox() {
91      this.colorDialog.Color = this.Content.Color;
92      this.colorPictureBox.Image = this.GenerateImage(colorPictureBox.Width, colorPictureBox.Height, this.Content.Color);
93    }
94    private Image GenerateImage(int width, int height, Color fillColor) {
95      Image colorImage = new Bitmap(width, height);
96      using (Graphics gfx = Graphics.FromImage(colorImage)) {
97        using (SolidBrush brush = new SolidBrush(fillColor)) {
98          gfx.FillRectangle(brush, 0, 0, width, height);
99        }
100      }
101      return colorImage;
102    }
103
104    private string selectedName;
105    private void FillListView() {
106      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
107
108      listView.Items.Clear();
109      listView.SmallImageList.Images.Clear();
110      if (Content != null) {
111        foreach (string key in Content.Parameters.Keys)
112          CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"], selectedName);
113        foreach (string key in Content.Results.Keys)
114          CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"], selectedName);
115        if (listView.Items.Count > 0) {
116          for (int i = 0; i < listView.Columns.Count; i++)
117            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
118          selectedName = null;
119        }
120      }
121    }
122
123    private void CreateListViewItem(string name, IItem value, ListViewGroup group, string selectedName) {
124      ListViewItem item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
125      item.Tag = value;
126      item.Group = group;
127      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : value.ItemImage);
128      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
129      listView.Items.Add(item);
130      if ((selectedName != null) && name.Equals(selectedName)) item.Selected = true;
131    }
132
133    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
134      if (listView.SelectedItems.Count == 1)
135        viewHost.Content = (IContent)listView.SelectedItems[0].Tag;
136      else
137        viewHost.Content = null;
138    }
139    private void listView_DoubleClick(object sender, EventArgs e) {
140      if (listView.SelectedItems.Count == 1) {
141        IItem item = (IItem)listView.SelectedItems[0].Tag;
142        IContentView view = MainFormManager.MainForm.ShowContent(item);
143        if (view != null) {
144          view.ReadOnly = true;
145          view.Locked = Locked;
146        }
147      }
148    }
149    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
150      if (!Locked) {
151        ListViewItem listViewItem = (ListViewItem)e.Item;
152        IItem item = (IItem)listViewItem.Tag;
153        if (item != null) {
154          DataObject data = new DataObject();
155          data.SetData("Type", item.GetType());
156          data.SetData("Value", item);
157          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
158        }
159      }
160    }
161    private void showAlgorithmButton_Click(object sender, EventArgs e) {
162      if (!Locked) {
163        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
164      }
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.