Free cookie consent management tool by TermsFeed Policy Generator

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

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