Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3764 was 3764, checked in by mkommend, 14 years ago

adapted view captions (ticket #893)

File size: 6.5 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using System.Drawing;
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    public override bool ReadOnly {
48      get { return base.ReadOnly; }
49      set { /*not needed because results are always readonly */}
50    }
51
52    /// <summary>
53    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
54    /// </summary>
55    public RunView() {
56      InitializeComponent();
57      base.ReadOnly = true;
58    }
59
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.Changed += new EventHandler(Content_Changed);
63    }
64    protected override void DeregisterContentEvents() {
65      base.DeregisterContentEvents();
66      Content.Changed -= new EventHandler(Content_Changed);
67    }
68    private void Content_Changed(object sender, EventArgs e) {
69      if (InvokeRequired)
70        this.Invoke(new EventHandler(Content_Changed), sender, e);
71      else
72        UpdateColorPictureBox();
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      FillListView();
78      viewHost.ViewType = null;
79      viewHost.Content = null;
80      if (Content != null)
81        UpdateColorPictureBox();
82
83      SetEnabledStateOfControls();
84    }
85    protected override void OnReadOnlyChanged() {
86      base.OnReadOnlyChanged();
87      SetEnabledStateOfControls();
88    }
89    protected override void OnLockedChanged() {
90      base.OnLockedChanged();
91      SetEnabledStateOfControls();
92    }
93    private void SetEnabledStateOfControls() {
94      listView.Enabled = Content != null;
95      viewHost.Enabled = Content != null;
96      changeColorButton.Enabled = Content != null;
97      showAlgorithmButton.Enabled = Content != null && !Locked;
98    }
99
100    private void changeColorButton_Click(object sender, EventArgs e) {
101      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
102        this.Content.Color = this.colorDialog.Color;
103      }
104    }
105    private void UpdateColorPictureBox() {
106      this.colorDialog.Color = this.Content.Color;
107      this.colorPictureBox.Image = this.GenerateImage(colorPictureBox.Width, colorPictureBox.Height, this.Content.Color);
108    }
109    private Image GenerateImage(int width, int height, Color fillColor) {
110      Image colorImage = new Bitmap(width, height);
111      using (Graphics gfx = Graphics.FromImage(colorImage)) {
112        using (SolidBrush brush = new SolidBrush(fillColor)) {
113          gfx.FillRectangle(brush, 0, 0, width, height);
114        }
115      }
116      return colorImage;
117    }
118
119    private void FillListView() {
120      listView.Items.Clear();
121      listView.SmallImageList.Images.Clear();
122      if (Content != null) {
123        foreach (string key in Content.Parameters.Keys)
124          CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"]);
125        foreach (string key in Content.Results.Keys)
126          CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"]);
127        if (listView.Items.Count > 0) {
128          for (int i = 0; i < listView.Columns.Count; i++)
129            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
130        }
131      }
132    }
133
134    private void CreateListViewItem(string name, IItem value, ListViewGroup group) {
135      ListViewItem item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
136      item.Tag = value;
137      item.Group = group;
138      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : value.ItemImage);
139      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
140      listView.Items.Add(item);
141    }
142
143    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
144      if (listView.SelectedItems.Count == 1) {
145        viewHost.ViewType = null;
146        viewHost.Content = (IContent)listView.SelectedItems[0].Tag;
147      } else {
148        viewHost.Content = null;
149      }
150    }
151    private void listView_DoubleClick(object sender, EventArgs e) {
152      if (listView.SelectedItems.Count == 1) {
153        IItem item = (IItem)listView.SelectedItems[0].Tag;
154        IContentView view = MainFormManager.MainForm.ShowContent(item);
155        if (view != null) {
156          view.ReadOnly = ReadOnly;
157          view.Locked = Locked;
158        }
159      }
160    }
161    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
162      if (!Locked) {
163        ListViewItem listViewItem = (ListViewItem)e.Item;
164        IItem item = (IItem)listViewItem.Tag;
165        if (item != null) {
166          DataObject data = new DataObject();
167          data.SetData("Type", item.GetType());
168          data.SetData("Value", item);
169          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
170        }
171      }
172    }
173    private void showAlgorithmButton_Click(object sender, EventArgs e) {
174      if (!Locked) {
175        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
176      }
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.