Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10812 was 10812, checked in by rstoll, 10 years ago
  • Disabled menu such as average in selection and co. if selected cells are only of string columns
  • GetVariance -> using GetValuesWithoutNaN as well
File size: 17.8 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      if (Content != null) {
89        Content.DataGridLogic.SetSelection(GetSelectedCells());
90      }
91    }
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95      if (Content == null && findAndReplaceDialog != null) {
96        findAndReplaceDialog.Close();
97      }
98    }
99
100    protected override void RegisterContentEvents() {
101      base.RegisterContentEvents();
102      Content.Changed += Content_Changed;
103    }
104
105    protected override void DeregisterContentEvents() {
106      base.DeregisterContentEvents();
107      Content.Changed -= Content_Changed;
108    }
109
110    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
111      if (notOwnEvent) {
112        switch (e.Type) {
113          case DataPreprocessingChangedEventType.AddColumn:
114          case DataPreprocessingChangedEventType.AddRow:
115          case DataPreprocessingChangedEventType.DeleteColumn:
116          case DataPreprocessingChangedEventType.DeleteRow:
117          case DataPreprocessingChangedEventType.Any:
118            OnContentChanged();
119            break;
120          case DataPreprocessingChangedEventType.ChangeColumn:
121          case DataPreprocessingChangedEventType.ChangeItem:
122            dataGridView.Refresh();
123            break;
124        }
125      }
126    }
127
128    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
129      if (!dataGridView.ReadOnly) {
130        string errorMessage;
131        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
132          e.Cancel = true;
133          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
134        }
135      }
136    }
137
138    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
139      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
140    }
141
142    protected override void PasteValuesToDataGridView() {
143      triggersOwnEvent(() => base.PasteValuesToDataGridView());
144    }
145
146    protected override void SetEnabledStateOfControls() {
147      base.SetEnabledStateOfControls();
148      rowsTextBox.ReadOnly = true;
149      columnsTextBox.ReadOnly = true;
150    }
151
152    private void btnApplySort_Click(object sender, System.EventArgs e) {
153      triggersOwnEvent(() => {
154        Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
155        OnContentChanged();
156      });
157    }
158
159    private void triggersOwnEvent(Action action) {
160      notOwnEvent = false;
161      action();
162      notOwnEvent = true;
163    }
164
165    #region FindAndReplaceDialog
166
167    private void CreateFindAndReplaceDialog() {
168      findAndReplaceDialog = new SearchAndReplaceDialog();
169      findAndReplaceDialog.Show(this);
170      HightlightedCellsBackground = GetSelectedCells();
171      dataGridView.ClearSelection();
172      findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
173      findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
174      findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
175      findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
176      findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
177    }
178
179    void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
180      ResetHighlightedCells();
181      ResetHighlightedCellsBackground();
182    }
183
184    void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
185      if (searchIterator != null && searchIterator.GetCurrent() != null) {
186        Replace(TransformToDictionary(currentCell));
187      }
188    }
189
190    void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
191      Replace(FindAll(findAndReplaceDialog.GetSearchText()));
192    }
193
194    void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
195      if (searchIterator == null || currentSearchText != findAndReplaceDialog.GetSearchText()) {
196        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
197        currentSearchText = findAndReplaceDialog.GetSearchText();
198      }
199
200      bool moreOccurences = false;
201      do {
202        currentCell = searchIterator.GetCurrent();
203        moreOccurences = searchIterator.MoveNext();
204      } while (moreOccurences && (currentCell == null || !Content.GetValue(currentCell.Item2, currentCell.Item1).Equals(currentSearchText)));
205
206      if (currentCell != null) {
207        HightlightedCells = TransformToDictionary(currentCell);
208        dataGridView.FirstDisplayedCell = dataGridView[currentCell.Item1, currentCell.Item2];
209      } else {
210        ResetHighlightedCells();
211      }
212
213      if (!moreOccurences) {
214        searchIterator.Reset();
215      }
216    }
217
218    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
219      HightlightedCells = FindAll(findAndReplaceDialog.GetSearchText());
220    }
221
222    private IDictionary<int, IList<int>> FindAll(string match) {
223      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
224      var comparisonFilter = new ComparisonFilter(Content.FilterLogic.PreprocessingData, Core.ConstraintOperation.Equal, new StringValue(match), true);
225      var filters = new List<Filter.IFilter>() { comparisonFilter };
226      var foundCells = new Dictionary<int, IList<int>>();
227      for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
228        comparisonFilter.ConstraintColumn = i;
229        bool[] filteredRows = Content.FilterLogic.Preview(filters, true);
230        foundCells[i] = filteredRows.Select((value, index) => new { Index = index, Value = value })
231          .Where(pair => pair.Value)
232          .Select(pair => pair.Index)
233          .ToList();
234        IList<int> selectedList;
235        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
236          foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
237        } else if (searchInSelection) {
238          foundCells[i].Clear();
239        }
240      }
241      return foundCells;
242    }
243
244    private void Replace(IDictionary<int, IList<int>> cells) {
245      if (findAndReplaceDialog != null) {
246        switch (findAndReplaceDialog.GetReplaceAction()) {
247          case ReplaceAction.Value:
248            Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
249            break;
250          case ReplaceAction.Average:
251            Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
252            break;
253          case ReplaceAction.Median:
254            Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
255            break;
256          case ReplaceAction.Random:
257            Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
258            break;
259          case ReplaceAction.MostCommon:
260            Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
261            break;
262          case ReplaceAction.Interpolation:
263            Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
264            break;
265        }
266      }
267    }
268
269    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
270      var highlightCells = new Dictionary<int, IList<int>>();
271      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
272      return highlightCells;
273    }
274
275    private void ResetHighlightedCells() {
276      HightlightedCells = new Dictionary<int, IList<int>>();
277    }
278
279    private void ResetHighlightedCellsBackground() {
280      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
281    }
282
283    #endregion FindAndReplaceDialog
284
285    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
286      if (Content == null) return;
287      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
288        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
289          replaceValueOverColumnToolStripMenuItem.Visible = false;
290          contextMenuCell.Show(MousePosition);
291        } else {
292          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
293            dataGridView.ClearSelection();
294            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
295          }
296
297          var columnIndices = new HashSet<int>();
298          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
299            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
300          }
301          averageToolStripMenuItem_Column.Enabled =
302            averageToolStripMenuItem_Selection.Enabled =
303            medianToolStripMenuItem_Column.Enabled =
304            medianToolStripMenuItem_Selection.Enabled =
305            randomToolStripMenuItem_Column.Enabled =
306            randomToolStripMenuItem_Selection.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
307
308          smoothingToolStripMenuItem_Column.Enabled =
309            interpolationToolStripMenuItem_Column.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows)
310            && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
311
312          replaceValueOverColumnToolStripMenuItem.Visible = true;
313          contextMenuCell.Show(MousePosition);
314        }
315      }
316    }
317
318    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
319      if (Content == null) return;
320      if (e.RowIndex < 0) return;
321      if (e.ColumnIndex < 0) return;
322      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
323      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
324      if (HighlightedRowIndices == null && HightlightedCells == null) return;
325
326      int rowIndex = virtualRowIndices[e.RowIndex];
327
328      Color backColor = e.CellStyle.BackColor;
329
330      if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
331        backColor = Color.LightGray;
332      }
333      if (HighlightedRowIndices.Contains(rowIndex) || HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
334        backColor = Color.LightGreen;
335      }
336
337      using (Brush backColorBrush = new SolidBrush(backColor)) {
338        Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
339        e.Graphics.FillRectangle(backColorBrush, bounds);
340      }
341
342      using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
343        Pen gridLinePen = new Pen(gridBrush);
344        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
345               e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
346               e.CellBounds.Bottom - 1);
347        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
348            e.CellBounds.Top, e.CellBounds.Right - 1,
349            e.CellBounds.Bottom);
350      }
351
352      e.PaintContent(e.CellBounds);
353      e.Handled = true;
354    }
355
356    void dataGridView_KeyDown(object sender, KeyEventArgs e) {
357      var selectedRows = dataGridView.SelectedRows;
358      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
359        List<int> rows = new List<int>();
360        for (int i = 0; i < selectedRows.Count; ++i) {
361          rows.Add(selectedRows[i].Index);
362        }
363        triggersOwnEvent(() => {
364          Content.DataGridLogic.DeleteRow(rows);
365          OnContentChanged();
366        });
367      } else if (e.Control && e.KeyCode == Keys.F) {
368        CreateFindAndReplaceDialog();
369        findAndReplaceDialog.ActivateSearch();
370      } else if (e.Control && e.KeyCode == Keys.R) {
371        CreateFindAndReplaceDialog();
372        findAndReplaceDialog.ActivateReplace();
373      }
374    }
375
376    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
377      btnApplySort.Enabled = sortedColumns.Any();
378      return base.Sort(sortedColumns);
379    }
380
381    protected override void ClearSorting() {
382      btnApplySort.Enabled = false;
383      base.ClearSorting();
384    }
385
386    private IDictionary<int, IList<int>> GetSelectedCells() {
387      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
388      for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
389        var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
390        if (!selectedCells.ContainsKey(columnIndex)) {
391          selectedCells.Add(columnIndex, new List<int>());
392        }
393        selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
394      }
395      return selectedCells;
396    }
397
398    private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
399      Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
400    }
401    private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
402      Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
403    }
404
405    private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
406      Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
407    }
408    private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
409      Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
410    }
411
412    private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
413      Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
414    }
415    private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
416      Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
417    }
418
419    private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
420      Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
421    }
422    private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
423      Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
424    }
425
426    private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
427      Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
428    }
429    private void ReplaceWithInterpolation_Selection_Click(object sender, EventArgs e) {
430      Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
431    }
432
433    private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
434    }
435
436    private void btnSearch_Click(object sender, EventArgs e) {
437      CreateFindAndReplaceDialog();
438      findAndReplaceDialog.ActivateSearch();
439    }
440
441    private void btnReplace_Click(object sender, EventArgs e) {
442      CreateFindAndReplaceDialog();
443      findAndReplaceDialog.ActivateReplace();
444    }
445  }
446}
Note: See TracBrowser for help on using the repository browser.