Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs @ 10934

Last change on this file since 10934 was 10934, checked in by mleitner, 10 years ago

Update correlationmatrix on preprocessing data change - improve performance on datagrid selection.

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