[10539] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 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 |
|
---|
[10534] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[10622] | 24 | using System.Drawing;
|
---|
[10534] | 25 | using System.Linq;
|
---|
[10236] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
| 28 |
|
---|
[10558] | 29 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
[10236] | 30 | [View("Data Grid Content View")]
|
---|
[10311] | 31 | [Content(typeof(IDataGridContent), true)]
|
---|
[10583] | 32 | public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
|
---|
[10236] | 33 |
|
---|
[10583] | 34 | private bool notOwnEvent = true;
|
---|
| 35 |
|
---|
[10236] | 36 | public new IDataGridContent Content {
|
---|
| 37 | get { return (IDataGridContent)base.Content; }
|
---|
| 38 | set { base.Content = value; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[10574] | 41 | private IList<int> _highlightedRowIndices;
|
---|
[10623] | 42 | public IList<int> HighlightedRowIndices {
|
---|
[10585] | 43 | get { return _highlightedRowIndices; }
|
---|
| 44 | set {
|
---|
| 45 | _highlightedRowIndices = value;
|
---|
| 46 | Refresh();
|
---|
| 47 | }
|
---|
[10574] | 48 | }
|
---|
| 49 |
|
---|
[10623] | 50 | private IDictionary<int, IList<int>> _highlightedCells;
|
---|
| 51 | public IDictionary<int, IList<int>> HightlightedCells {
|
---|
| 52 | get { return _highlightedCells; }
|
---|
| 53 | set {
|
---|
| 54 | _highlightedCells = value;
|
---|
| 55 | Refresh();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[10236] | 59 | public DataGridContentView() {
|
---|
| 60 | InitializeComponent();
|
---|
[10380] | 61 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
[10585] | 62 | dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
|
---|
[10622] | 63 | dataGridView.KeyUp += dataGridView_KeyUp;
|
---|
[10380] | 64 | contextMenuCell.Items.Add(ShowHideColumns);
|
---|
[10623] | 65 | _highlightedCells = new Dictionary<int, IList<int>>();
|
---|
| 66 | _highlightedRowIndices = new List<int>();
|
---|
[10236] | 67 | }
|
---|
| 68 |
|
---|
[10622] | 69 | void dataGridView_KeyUp(object sender, KeyEventArgs e) {
|
---|
| 70 | var selectedRows = dataGridView.SelectedRows;
|
---|
| 71 | if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
|
---|
| 72 | List<int> rows = new List<int>();
|
---|
| 73 | for (int i = 0; i < selectedRows.Count; ++i) {
|
---|
| 74 | rows.Add(selectedRows[i].Index);
|
---|
| 75 | }
|
---|
| 76 | triggersOwnEvent(() => {
|
---|
| 77 | Content.DataGridLogic.DeleteRow(rows);
|
---|
| 78 | OnContentChanged();
|
---|
| 79 | });
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[10236] | 83 | protected override void OnContentChanged() {
|
---|
| 84 | base.OnContentChanged();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[10253] | 87 |
|
---|
[10571] | 88 | protected override void RegisterContentEvents() {
|
---|
| 89 | base.RegisterContentEvents();
|
---|
| 90 | Content.Changed += Content_Changed;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | protected override void DeregisterContentEvents() {
|
---|
| 94 | base.DeregisterContentEvents();
|
---|
| 95 | Content.Changed -= Content_Changed;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
[10583] | 99 | if (notOwnEvent) {
|
---|
[10622] | 100 | switch (e.Type) {
|
---|
| 101 | case DataPreprocessingChangedEventType.AddColumn:
|
---|
| 102 | case DataPreprocessingChangedEventType.AddRow:
|
---|
| 103 | case DataPreprocessingChangedEventType.DeleteColumn:
|
---|
| 104 | case DataPreprocessingChangedEventType.DeleteRow:
|
---|
| 105 | case DataPreprocessingChangedEventType.Any:
|
---|
| 106 | OnContentChanged();
|
---|
| 107 | break;
|
---|
| 108 | case DataPreprocessingChangedEventType.ChangeColumn:
|
---|
| 109 | case DataPreprocessingChangedEventType.ChangeItem:
|
---|
| 110 | dataGridView.Refresh();
|
---|
| 111 | break;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[10583] | 114 | }
|
---|
[10571] | 115 | }
|
---|
| 116 |
|
---|
[10583] | 117 | protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[10236] | 118 | if (!dataGridView.ReadOnly) {
|
---|
| 119 | string errorMessage;
|
---|
[10246] | 120 | if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
|
---|
[10236] | 121 | e.Cancel = true;
|
---|
| 122 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[10253] | 126 |
|
---|
[10583] | 127 | protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
[10585] | 128 | triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
|
---|
[10583] | 129 | }
|
---|
| 130 |
|
---|
| 131 | protected override void PasteValuesToDataGridView() {
|
---|
[10585] | 132 | triggersOwnEvent(() => base.PasteValuesToDataGridView());
|
---|
[10583] | 133 | }
|
---|
| 134 |
|
---|
[10317] | 135 | protected override void SetEnabledStateOfControls() {
|
---|
| 136 | base.SetEnabledStateOfControls();
|
---|
| 137 | rowsTextBox.ReadOnly = true;
|
---|
| 138 | columnsTextBox.ReadOnly = true;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[10253] | 141 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
[10585] | 142 | triggersOwnEvent(() => {
|
---|
| 143 | Content.PreprocessingDataManipulation.ReOrderToIndices(virtualRowIndices);
|
---|
| 144 | OnContentChanged();
|
---|
| 145 | });
|
---|
[10253] | 146 | }
|
---|
[10345] | 147 |
|
---|
[10585] | 148 | private void triggersOwnEvent(Action action) {
|
---|
| 149 | notOwnEvent = false;
|
---|
| 150 | action();
|
---|
| 151 | notOwnEvent = true;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
[10380] | 155 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 156 | if (Content == null) return;
|
---|
| 157 | if (e.Button == System.Windows.Forms.MouseButtons.Right) {
|
---|
| 158 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
| 159 | contextMenu.Show(MousePosition);
|
---|
| 160 | } else {
|
---|
[10590] | 161 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
| 162 | dataGridView.ClearSelection();
|
---|
| 163 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
| 164 | }
|
---|
[10380] | 165 | interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows);
|
---|
[10621] | 166 | var columnIndices = new HashSet<int>();
|
---|
| 167 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
| 168 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
| 169 | }
|
---|
| 170 | averageToolStripMenuItem.Enabled = medianToolStripMenuItem.Enabled = randomToolStripMenuItem.Enabled = interpolationToolStripMenuItem.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
|
---|
[10380] | 171 | contextMenuCell.Show(MousePosition);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[10585] | 176 | protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
|
---|
| 177 | if (Content == null) return;
|
---|
| 178 | if (e.RowIndex < 0) return;
|
---|
| 179 | if (e.ColumnIndex < 0) return;
|
---|
| 180 | if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
|
---|
| 181 | if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
|
---|
[10574] | 182 |
|
---|
[10585] | 183 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
[10574] | 184 |
|
---|
[10585] | 185 | Color backColor = e.CellStyle.BackColor;
|
---|
[10574] | 186 |
|
---|
[10623] | 187 | if (HighlightedRowIndices != null && HighlightedRowIndices.Contains(rowIndex)
|
---|
| 188 | || HightlightedCells != null && HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
|
---|
[10585] | 189 | backColor = Color.Pink;
|
---|
| 190 | }
|
---|
[10574] | 191 |
|
---|
[10585] | 192 | using (Brush backColorBrush = new SolidBrush(backColor)) {
|
---|
| 193 | Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
|
---|
| 194 | e.Graphics.FillRectangle(backColorBrush, bounds);
|
---|
| 195 | }
|
---|
[10574] | 196 |
|
---|
[10585] | 197 | using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
|
---|
| 198 | Pen gridLinePen = new Pen(gridBrush);
|
---|
| 199 | e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
|
---|
| 200 | e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
|
---|
| 201 | e.CellBounds.Bottom - 1);
|
---|
| 202 | e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
|
---|
| 203 | e.CellBounds.Top, e.CellBounds.Right - 1,
|
---|
| 204 | e.CellBounds.Bottom);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | e.PaintContent(e.CellBounds);
|
---|
| 208 | e.Handled = true;
|
---|
[10574] | 209 | }
|
---|
| 210 |
|
---|
[10345] | 211 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
| 212 | btnApplySort.Enabled = sortedColumns.Any();
|
---|
| 213 | return base.Sort(sortedColumns);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | protected override void ClearSorting() {
|
---|
| 217 | btnApplySort.Enabled = false;
|
---|
| 218 | base.ClearSorting();
|
---|
| 219 | }
|
---|
[10380] | 220 |
|
---|
[10590] | 221 | private Dictionary<int, List<int>> GetSelectedCells() {
|
---|
| 222 | var selectedCells = new Dictionary<int, List<int>>();
|
---|
| 223 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
[10622] | 224 | var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
|
---|
| 225 | if (!selectedCells.ContainsKey(columnIndex)) {
|
---|
| 226 | selectedCells.Add(columnIndex, new List<int>());
|
---|
[10590] | 227 | }
|
---|
| 228 | selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
|
---|
| 229 | }
|
---|
| 230 | return selectedCells;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[10380] | 233 | private void ReplaceWithAverage_Click(object sender, EventArgs e) {
|
---|
[10590] | 234 | Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(GetSelectedCells());
|
---|
[10380] | 235 | }
|
---|
| 236 |
|
---|
| 237 | private void ReplaceWithMedian_Click(object sender, EventArgs e) {
|
---|
[10590] | 238 | Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(GetSelectedCells());
|
---|
[10380] | 239 | }
|
---|
| 240 |
|
---|
| 241 | private void ReplaceWithRandom_Click(object sender, EventArgs e) {
|
---|
[10590] | 242 | Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(GetSelectedCells());
|
---|
[10380] | 243 | }
|
---|
| 244 |
|
---|
| 245 | private void ReplaceWithMostCommon_Click(object sender, EventArgs e) {
|
---|
[10590] | 246 | Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(GetSelectedCells());
|
---|
[10380] | 247 | }
|
---|
| 248 |
|
---|
| 249 | private void ReplaceWithInterpolation_Click(object sender, EventArgs e) {
|
---|
[10590] | 250 | Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
[10380] | 251 | }
|
---|
[10236] | 252 | }
|
---|
| 253 | }
|
---|