Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.DataAnalysis/3.3/DatasetView.cs @ 3380

Last change on this file since 3380 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

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