Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAnalysis/3.2/DatasetView.cs @ 3491

Last change on this file since 3491 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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