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