Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/DataGridContentView.cs @ 10625

Last change on this file since 10625 was 10623, checked in by sbreuer, 10 years ago
  • highlighting of cells
File size: 9.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.DataPreprocessing.Views {
30  [View("Data Grid Content View")]
31  [Content(typeof(IDataGridContent), true)]
32  public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
33
34    private bool notOwnEvent = true;
35
36    public new IDataGridContent Content {
37      get { return (IDataGridContent)base.Content; }
38      set { base.Content = value; }
39    }
40
41    private IList<int> _highlightedRowIndices;
42    public IList<int> HighlightedRowIndices {
43      get { return _highlightedRowIndices; }
44      set {
45        _highlightedRowIndices = value;
46        Refresh();
47      }
48    }
49
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
59    public DataGridContentView() {
60      InitializeComponent();
61      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
62      dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
63      dataGridView.KeyUp += dataGridView_KeyUp;
64      contextMenuCell.Items.Add(ShowHideColumns);
65      _highlightedCells = new Dictionary<int, IList<int>>();
66      _highlightedRowIndices = new List<int>();
67    }
68
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
83    protected override void OnContentChanged() {
84      base.OnContentChanged();
85    }
86
87
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) {
99      if (notOwnEvent) {
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
114      }
115    }
116
117    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
118      if (!dataGridView.ReadOnly) {
119        string errorMessage;
120        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
121          e.Cancel = true;
122          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
123        }
124      }
125    }
126
127    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
128      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
129    }
130
131    protected override void PasteValuesToDataGridView() {
132      triggersOwnEvent(() => base.PasteValuesToDataGridView());
133    }
134
135    protected override void SetEnabledStateOfControls() {
136      base.SetEnabledStateOfControls();
137      rowsTextBox.ReadOnly = true;
138      columnsTextBox.ReadOnly = true;
139    }
140
141    private void btnApplySort_Click(object sender, System.EventArgs e) {
142      triggersOwnEvent(() => {
143        Content.PreprocessingDataManipulation.ReOrderToIndices(virtualRowIndices);
144        OnContentChanged();
145      });
146    }
147
148    private void triggersOwnEvent(Action action) {
149      notOwnEvent = false;
150      action();
151      notOwnEvent = true;
152    }
153
154
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 {
161          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
162            dataGridView.ClearSelection();
163            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
164          }
165          interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows);
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);
171          contextMenuCell.Show(MousePosition);
172        }
173      }
174    }
175
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;
182
183      int rowIndex = virtualRowIndices[e.RowIndex];
184
185      Color backColor = e.CellStyle.BackColor;
186
187      if (HighlightedRowIndices != null && HighlightedRowIndices.Contains(rowIndex)
188        || HightlightedCells != null && HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
189        backColor = Color.Pink;
190      }
191
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      }
196
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;
209    }
210
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    }
220
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++) {
224        var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
225        if (!selectedCells.ContainsKey(columnIndex)) {
226          selectedCells.Add(columnIndex, new List<int>());
227        }
228        selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
229      }
230      return selectedCells;
231    }
232
233    private void ReplaceWithAverage_Click(object sender, EventArgs e) {
234      Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(GetSelectedCells());
235    }
236
237    private void ReplaceWithMedian_Click(object sender, EventArgs e) {
238      Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(GetSelectedCells());
239    }
240
241    private void ReplaceWithRandom_Click(object sender, EventArgs e) {
242      Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(GetSelectedCells());
243    }
244
245    private void ReplaceWithMostCommon_Click(object sender, EventArgs e) {
246      Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(GetSelectedCells());
247    }
248
249    private void ReplaceWithInterpolation_Click(object sender, EventArgs e) {
250      Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
251    }
252  }
253}
Note: See TracBrowser for help on using the repository browser.