Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.DataAnalysis/3.2/DatasetView.cs @ 3199

Last change on this file since 3199 was 2311, checked in by mkommend, 15 years ago
  • implemented GetVariableValues in dataset
  • added row number in rowheaders of dataset view

(ticket #726)

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Core;
25using HeuristicLab.PluginInfrastructure;
26
27namespace HeuristicLab.DataAnalysis {
28  public partial class DatasetView : EditorBase {
29    public Dataset Dataset {
30      get { return (Dataset)Item; }
31      set {
32        Item = value;
33        Refresh();
34      }
35    }
36    public DatasetView()
37      : base() {
38      InitializeComponent();
39      // format all cells with the round-trip formatter to make sure that values that are exported and imported to
40      // another C# app (HL2) have the same numeric value
41      dataGridView.DefaultCellStyle.Format = "r";
42
43      //events for virtual mode of datagrid
44      this.dataGridView.VirtualMode = true;
45      this.dataGridView.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView_CellValueNeeded);
46    }
47
48    public DatasetView(Dataset dataset)
49      : this() {
50      this.Dataset = dataset;
51      DiscoveryService discovery = new DiscoveryService();
52      IDatasetManipulator[] manipuators = discovery.GetInstances<IDatasetManipulator>();
53      contextMenuStrip.Items.Add(new ToolStripSeparator());
54      foreach (IDatasetManipulator manipulator in manipuators) {
55        contextMenuStrip.Items.Add(new ToolStripButton(manipulator.Action, null, delegate(object source, EventArgs args) {
56          manipulator.Execute(Dataset);
57          Refresh();
58        }));
59      }     
60    }
61
62    protected override void UpdateControls() {
63      base.UpdateControls();
64      if (Dataset != null) {
65        // DataGridView is bitching around. When it's columnCount (maybe also rowCount) is changed it creates
66        // new column objects and they have SortMode set to 'automatic'. However this is not allowed if the
67        // selectionmode is set to 'ColumnHeaderSelect' at the same time, resulting in an exception.
68        // A solution is to set the SelectionMode to CellSelect before any changes. After the columns
69        // have been updated (and their SortMode set to 'NotSortable') we switch back to SelectionMode=ColumnHeaderSelect.
70        dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
71        //dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
72        int rows = Dataset.Rows;
73        int columns = Dataset.Columns;
74        nameTextBox.Text = Dataset.Name;
75        rowsTextBox.Text = rows + "";
76        columnsTextBox.Text = columns + "";
77        dataGridView.ColumnCount = columns;
78        dataGridView.RowCount = rows;
79        //for (int i = 0; i < rows; i++) {
80        //  for (int j = 0; j < columns; j++) {
81        //    dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
82        //    dataGridView.Rows[i].HeaderCell.Value = i.ToString();
83        //  }
84        //}
85        for (int i = 0; i < columns; i++) {
86          dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; // SortMode has to be NotSortable to allow ColumnHeaderSelect
87          dataGridView.Columns[i].Name = GetColumnName(i);
88          dataGridView.Columns[i].HeaderText = GetColumnName(i) + System.Environment.NewLine + "(" + i + ")";
89          dataGridView.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter;
90        }
91        dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect; // switch back to column selection
92        //dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
93      } else {
94        rowsTextBox.Text = "1";
95        columnsTextBox.Text = "1";
96        dataGridView.ColumnCount = 1;
97        dataGridView.RowCount = 1;
98      }
99      UpdateRowHeaders();
100      this.dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
101    }
102
103    private void UpdateRowHeaders() {
104      for (int i = dataGridView.FirstDisplayedScrollingRowIndex; i < dataGridView.FirstDisplayedScrollingRowIndex + dataGridView.DisplayedRowCount(true); i++)
105        dataGridView.Rows[i].HeaderCell.Value = i.ToString();
106      this.dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
107      dataGridView.Invalidate();
108    }
109
110    private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
111      UpdateRowHeaders();
112    }
113
114    private void dataGridView_Resize(object sender, EventArgs e) {
115      UpdateRowHeaders();
116    }
117
118    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
119      if (ValidateData((string)e.FormattedValue)) {
120        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
121        e.Cancel = false;
122      } else {
123        e.Cancel = true;
124      }
125    }
126
127    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
128      e.Value = Dataset.GetValue(e.RowIndex, e.ColumnIndex);
129    }
130
131    private void SetArrayElement(int row, int column, string element) {
132      double result;
133      double.TryParse(element, out result);
134      if (result != Dataset.GetValue(row, column)) {
135        Dataset.SetValue(row, column, result);
136      }
137    }
138
139    private bool ValidateData(string element) {
140      double result;
141      return element != null && double.TryParse(element, out result);
142    }
143
144    private void scaleValuesToolStripMenuItem_Click(object sender, EventArgs e) {
145      foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
146        Dataset.ScaleVariable(column.Index);
147        column.Name = GetColumnName(column.Index) + " [scaled]";
148      }
149      Refresh();
150    }
151
152    private void originalValuesToolStripMenuItem_Click(object sender, EventArgs e) {
153      foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
154        Dataset.UnscaleVariable(column.Index);
155        column.Name = GetColumnName(column.Index);
156      }
157      Refresh();     
158    }
159
160    private string GetColumnName(int index) {
161      if(Dataset.Columns == dataGridView.Columns.Count) {
162        return Dataset.GetVariableName(index);
163      } else {
164        return "Var " + index;
165      }
166    }
167
168    private void showScalingToolStripMenuItem_Click(object sender, EventArgs e) {
169      ManualScalingControl scalingControl = new ManualScalingControl(false);
170      double[,] scalingParameters = new double[2, Dataset.Columns];
171      for(int i = 0; i < Dataset.Columns; i++) {
172        scalingParameters[0, i] = Dataset.ScalingFactor[i];
173        scalingParameters[1, i] = Dataset.ScalingOffset[i];
174      }
175      scalingControl.Data = scalingParameters;
176      scalingControl.ShowDialog();
177    }
178
179    private void scaleValuesmanuallyToolStripMenuItem_Click(object sender, EventArgs e) {
180      ManualScalingControl scalingControl = new ManualScalingControl(true);
181      double[,] scalingParameters = new double[2, Dataset.Columns];
182      for(int i = 0; i < Dataset.Columns; i++) {
183        scalingParameters[0, i] = Dataset.ScalingFactor[i];
184        scalingParameters[1, i] = Dataset.ScalingOffset[i];
185      }
186      scalingControl.Data = scalingParameters;
187      if(scalingControl.ShowDialog() == DialogResult.OK) {
188        for(int i = 0; i < Dataset.Columns; i++) {
189          Dataset.ScaleVariable(i, scalingControl.Data[0, i], scalingControl.Data[1, i]);
190        }
191      }
192      Refresh();
193    }
194
195 
196  }
197}
Note: See TracBrowser for help on using the repository browser.