Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10863 was 10863, checked in by sbreuer, 10 years ago
  • make selection in datagrid faster
File size: 19.3 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.Data;
28using HeuristicLab.DataPreprocessing.Filter;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.DataPreprocessing.Views {
32  [View("Data Grid Content View")]
33  [Content(typeof(IDataGridContent), true)]
34  public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
35
36    private bool notOwnEvent = true;
37    private SearchAndReplaceDialog findAndReplaceDialog;
38    private IFindPreprocessingItemsIterator searchIterator;
39    private string currentSearchText;
40    private Tuple<int, int> currentCell;
41
42    public new IDataGridContent Content {
43      get { return (IDataGridContent)base.Content; }
44      set { base.Content = value; }
45    }
46
47    private IList<int> _highlightedRowIndices;
48    public IList<int> HighlightedRowIndices {
49      get { return _highlightedRowIndices; }
50      set {
51        _highlightedRowIndices = value;
52        Refresh();
53      }
54    }
55
56    private IDictionary<int, IList<int>> _highlightedCells;
57    public IDictionary<int, IList<int>> HightlightedCells {
58      get { return _highlightedCells; }
59      set {
60        _highlightedCells = value;
61        Refresh();
62      }
63    }
64
65    private IDictionary<int, IList<int>> _highlightedCellsBackground;
66    public IDictionary<int, IList<int>> HightlightedCellsBackground {
67      get { return _highlightedCellsBackground; }
68      set {
69        _highlightedCellsBackground = value;
70        Refresh();
71      }
72    }
73
74    public DataGridContentView() {
75      InitializeComponent();
76      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
77      dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
78      dataGridView.KeyDown += dataGridView_KeyDown;
79      contextMenuCell.Items.Add(ShowHideColumns);
80      _highlightedCells = new Dictionary<int, IList<int>>();
81      _highlightedRowIndices = new List<int>();
82      _highlightedCellsBackground = new Dictionary<int, IList<int>>();
83      currentCell = null;
84      DataGridView.SelectionChanged += DataGridView_SelectionChanged;
85    }
86
87    void DataGridView_SelectionChanged(object sender, EventArgs e) {
88      Content.DataGridLogic.SetSelection(GetSelectedCells());
89    }
90
91    protected override void OnContentChanged() {
92      base.OnContentChanged();
93      if (Content == null && findAndReplaceDialog != null) {
94        findAndReplaceDialog.Close();
95      }
96    }
97
98    protected override void RegisterContentEvents() {
99      base.RegisterContentEvents();
100      Content.Changed += Content_Changed;
101    }
102
103    protected override void DeregisterContentEvents() {
104      base.DeregisterContentEvents();
105      Content.Changed -= Content_Changed;
106    }
107
108    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
109      if (notOwnEvent) {
110        switch (e.Type) {
111          case DataPreprocessingChangedEventType.ChangeColumn:
112          case DataPreprocessingChangedEventType.ChangeItem:
113            dataGridView.Refresh();
114            break;
115          default:
116            OnContentChanged();
117            break;
118        }
119      }
120      searchIterator = null;
121    }
122
123    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
124      if (!dataGridView.ReadOnly) {
125        string errorMessage;
126        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
127          e.Cancel = true;
128          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
129        }
130      }
131    }
132
133    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
134      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
135    }
136
137    protected override void PasteValuesToDataGridView() {
138      triggersOwnEvent(() => base.PasteValuesToDataGridView());
139    }
140
141    protected override void SetEnabledStateOfControls() {
142      base.SetEnabledStateOfControls();
143      rowsTextBox.ReadOnly = true;
144      columnsTextBox.ReadOnly = true;
145    }
146
147    private void btnApplySort_Click(object sender, System.EventArgs e) {
148      triggersOwnEvent(() => {
149        Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
150        OnContentChanged();
151      });
152    }
153
154    private void triggersOwnEvent(Action action) {
155      notOwnEvent = false;
156      action();
157      notOwnEvent = true;
158    }
159
160    #region FindAndReplaceDialog
161
162    private void CreateFindAndReplaceDialog() {
163      findAndReplaceDialog = new SearchAndReplaceDialog();
164      findAndReplaceDialog.Show(this);
165      if (AreMultipleCellsSelected()) {
166        ResetHighlightedCellsBackground();
167        HightlightedCellsBackground = GetSelectedCells();
168        dataGridView.ClearSelection();
169      }
170      findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
171      findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
172      findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
173      findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
174      findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
175      searchIterator = null;
176      DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
177    }
178
179    void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
180      if (Content != null) {
181        if (AreMultipleCellsSelected()) {
182          ResetHighlightedCellsBackground();
183          HightlightedCellsBackground = GetSelectedCells();
184          searchIterator = null;
185        }
186      }
187    }
188
189    void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
190      ResetHighlightedCells();
191      ResetHighlightedCellsBackground();
192      searchIterator = null;
193      DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
194    }
195
196    void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
197      if (searchIterator != null && searchIterator.GetCurrent() != null) {
198        Replace(TransformToDictionary(currentCell));
199      }
200    }
201
202    void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
203      Replace(FindAll(findAndReplaceDialog.GetSearchText()));
204    }
205
206    void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
207      if (searchIterator == null || currentSearchText != findAndReplaceDialog.GetSearchText()) {
208        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
209        currentSearchText = findAndReplaceDialog.GetSearchText();
210      }
211      if (IsOneCellSelected()) {
212        var first = GetSelectedCells().First();
213        searchIterator.SetStartCell(first.Key, first.Value[0]);
214      }
215
216
217      bool moreOccurences = false;
218      do {
219        currentCell = searchIterator.GetCurrent();
220        moreOccurences = searchIterator.MoveNext();
221        if (IsOneCellSelected()) {
222          var first = GetSelectedCells().First();
223          if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
224            if (!moreOccurences) {
225              searchIterator.Reset();
226            }
227            currentCell = searchIterator.GetCurrent();
228            moreOccurences = searchIterator.MoveNext();
229
230          }
231        }
232      } while (moreOccurences && (currentCell == null || !Content.GetValue(currentCell.Item2, currentCell.Item1).Equals(currentSearchText)));
233
234      if (currentCell != null) {
235        dataGridView.ClearSelection();
236        dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
237      } else {
238        ResetHighlightedCells();
239      }
240
241      //if (!moreOccurences) {
242      //  searchIterator.Reset();
243      //  dataGridView.ClearSelection();
244      //  currentCell = searchIterator.GetCurrent();
245      //  dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
246      //}
247    }
248
249    private bool AreMultipleCellsSelected() {
250      return GetSelectedCellCount() > 1;
251    }
252
253    private bool IsOneCellSelected() {
254      return GetSelectedCellCount() == 1;
255    }
256
257    private int GetSelectedCellCount() {
258      int count = 0;
259      foreach (var column in GetSelectedCells()) {
260        count += column.Value.Count();
261      }
262      return count;
263    }
264
265    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
266      HightlightedCells = FindAll(findAndReplaceDialog.GetSearchText());
267    }
268
269    private IDictionary<int, IList<int>> FindAll(string match) {
270      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
271      var comparisonFilter = new ComparisonFilter(Content.FilterLogic.PreprocessingData, Core.ConstraintOperation.Equal, new StringValue(match), true);
272      var filters = new List<Filter.IFilter>() { comparisonFilter };
273      var foundCells = new Dictionary<int, IList<int>>();
274      for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
275        comparisonFilter.ConstraintColumn = i;
276        bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
277        var foundIndices = new List<int>();
278        for (int idx = 0; idx < filteredRows.Length; ++idx) {
279          var notFilteredThusFound = !filteredRows[idx];
280          if (notFilteredThusFound) {
281            foundIndices.Add(idx);
282          }
283        }
284        foundCells[i] = foundIndices;
285        IList<int> selectedList;
286        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
287          foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
288        } else if (searchInSelection) {
289          foundCells[i].Clear();
290        }
291      }
292      return foundCells;
293    }
294
295    private void Replace(IDictionary<int, IList<int>> cells) {
296      if (findAndReplaceDialog != null) {
297        switch (findAndReplaceDialog.GetReplaceAction()) {
298          case ReplaceAction.Value:
299            Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
300            break;
301          case ReplaceAction.Average:
302            Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
303            break;
304          case ReplaceAction.Median:
305            Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
306            break;
307          case ReplaceAction.Random:
308            Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
309            break;
310          case ReplaceAction.MostCommon:
311            Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
312            break;
313          case ReplaceAction.Interpolation:
314            Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
315            break;
316        }
317      }
318    }
319
320    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
321      var highlightCells = new Dictionary<int, IList<int>>();
322      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
323      return highlightCells;
324    }
325
326    private void ResetHighlightedCells() {
327      HightlightedCells = new Dictionary<int, IList<int>>();
328    }
329
330    private void ResetHighlightedCellsBackground() {
331      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
332    }
333
334    #endregion FindAndReplaceDialog
335
336    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
337      if (Content == null) return;
338      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
339        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
340          replaceValueOverColumnToolStripMenuItem.Visible = false;
341          contextMenuCell.Show(MousePosition);
342        } else {
343          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
344            dataGridView.ClearSelection();
345            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
346          }
347
348          var columnIndices = new HashSet<int>();
349          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
350            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
351          }
352          averageToolStripMenuItem_Column.Enabled =
353            averageToolStripMenuItem_Selection.Enabled =
354            medianToolStripMenuItem_Column.Enabled =
355            medianToolStripMenuItem_Selection.Enabled =
356            randomToolStripMenuItem_Column.Enabled =
357            randomToolStripMenuItem_Selection.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
358
359          smoothingToolStripMenuItem_Column.Enabled =
360            interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
361            && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
362            && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
363
364          replaceValueOverColumnToolStripMenuItem.Visible = true;
365          contextMenuCell.Show(MousePosition);
366        }
367      }
368    }
369
370    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
371      if (Content == null) return;
372      if (e.RowIndex < 0) return;
373      if (e.ColumnIndex < 0) return;
374      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
375      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
376      if (HighlightedRowIndices == null && HightlightedCells == null) return;
377
378      int rowIndex = virtualRowIndices[e.RowIndex];
379
380      Color backColor = e.CellStyle.BackColor;
381
382      if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
383        backColor = Color.LightGray;
384      }
385      if (HighlightedRowIndices.Contains(rowIndex) || HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
386        backColor = Color.LightGreen;
387      }
388
389      using (Brush backColorBrush = new SolidBrush(backColor)) {
390        Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
391        e.Graphics.FillRectangle(backColorBrush, bounds);
392      }
393
394      using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
395        Pen gridLinePen = new Pen(gridBrush);
396        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
397               e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
398               e.CellBounds.Bottom - 1);
399        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
400            e.CellBounds.Top, e.CellBounds.Right - 1,
401            e.CellBounds.Bottom);
402      }
403
404      e.PaintContent(e.CellBounds);
405      e.Handled = true;
406    }
407
408    void dataGridView_KeyDown(object sender, KeyEventArgs e) {
409      var selectedRows = dataGridView.SelectedRows;
410      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
411        List<int> rows = new List<int>();
412        for (int i = 0; i < selectedRows.Count; ++i) {
413          rows.Add(selectedRows[i].Index);
414        }
415        triggersOwnEvent(() => {
416          Content.DataGridLogic.DeleteRow(rows);
417          OnContentChanged();
418        });
419      } else if (e.Control && e.KeyCode == Keys.F) {
420        CreateFindAndReplaceDialog();
421        findAndReplaceDialog.ActivateSearch();
422      } else if (e.Control && e.KeyCode == Keys.R) {
423        CreateFindAndReplaceDialog();
424        findAndReplaceDialog.ActivateReplace();
425      }
426    }
427
428    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
429      btnApplySort.Enabled = sortedColumns.Any();
430      return base.Sort(sortedColumns);
431    }
432
433    protected override void ClearSorting() {
434      btnApplySort.Enabled = false;
435      base.ClearSorting();
436    }
437
438    private IDictionary<int, IList<int>> GetSelectedCells() {
439      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
440      for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
441        var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
442        if (!selectedCells.ContainsKey(columnIndex)) {
443          selectedCells.Add(columnIndex, new List<int>());
444        }
445        selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
446      }
447      return selectedCells;
448    }
449
450    private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
451      Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
452    }
453    private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
454      Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
455    }
456
457    private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
458      Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
459    }
460    private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
461      Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
462    }
463
464    private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
465      Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
466    }
467    private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
468      Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
469    }
470
471    private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
472      Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
473    }
474    private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
475      Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
476    }
477
478    private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
479      Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
480    }
481
482    private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
483      Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
484    }
485
486    private void btnSearch_Click(object sender, EventArgs e) {
487      CreateFindAndReplaceDialog();
488      findAndReplaceDialog.ActivateSearch();
489    }
490
491    private void btnReplace_Click(object sender, EventArgs e) {
492      CreateFindAndReplaceDialog();
493      findAndReplaceDialog.ActivateReplace();
494    }
495  }
496}
Note: See TracBrowser for help on using the repository browser.