Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10629 was 10629, checked in by rstoll, 10 years ago

fix for editing strings

File size: 9.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.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    protected override void OnContentChanged() {
70      base.OnContentChanged();
71    }
72
73    protected override void RegisterContentEvents() {
74      base.RegisterContentEvents();
75      Content.Changed += Content_Changed;
76    }
77
78    protected override void DeregisterContentEvents() {
79      base.DeregisterContentEvents();
80      Content.Changed -= Content_Changed;
81    }
82
83    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
84      if (notOwnEvent) {
85        switch (e.Type) {
86          case DataPreprocessingChangedEventType.AddColumn:
87          case DataPreprocessingChangedEventType.AddRow:
88          case DataPreprocessingChangedEventType.DeleteColumn:
89          case DataPreprocessingChangedEventType.DeleteRow:
90          case DataPreprocessingChangedEventType.Any:
91            OnContentChanged();
92            break;
93          case DataPreprocessingChangedEventType.ChangeColumn:
94          case DataPreprocessingChangedEventType.ChangeItem:
95            dataGridView.Refresh();
96            break;
97        }
98
99      }
100    }
101
102    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
103      if (!dataGridView.ReadOnly) {
104        string errorMessage;
105        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
106          e.Cancel = true;
107          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
108        }
109      }
110    }
111
112    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
113      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
114    }
115
116    protected override void PasteValuesToDataGridView() {
117      triggersOwnEvent(() => base.PasteValuesToDataGridView());
118    }
119
120    protected override void SetEnabledStateOfControls() {
121      base.SetEnabledStateOfControls();
122      rowsTextBox.ReadOnly = true;
123      columnsTextBox.ReadOnly = true;
124    }
125
126    private void btnApplySort_Click(object sender, System.EventArgs e) {
127      triggersOwnEvent(() => {
128        Content.PreprocessingDataManipulation.ReOrderToIndices(virtualRowIndices);
129        OnContentChanged();
130      });
131    }
132
133    private void triggersOwnEvent(Action action) {
134      notOwnEvent = false;
135      action();
136      notOwnEvent = true;
137    }
138
139    private void dataGridView_KeyUp(object sender, KeyEventArgs e) {
140      var selectedRows = dataGridView.SelectedRows;
141      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
142        List<int> rows = new List<int>();
143        for (int i = 0; i < selectedRows.Count; ++i) {
144          rows.Add(selectedRows[i].Index);
145        }
146        triggersOwnEvent(() => {
147          Content.DataGridLogic.DeleteRow(rows);
148          OnContentChanged();
149        });
150      } else if (e.Control && e.KeyCode == Keys.F) {
151
152      } else if (e.Control && e.KeyCode == Keys.H) {
153
154      }
155    }
156
157    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
158      if (Content == null) return;
159      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
160        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
161          replaceValueToolStripMenuItem.Visible = false;
162          contextMenuCell.Show(MousePosition);
163        } else {
164          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
165            dataGridView.ClearSelection();
166            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
167          }
168          interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows);
169          var columnIndices = new HashSet<int>();
170          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
171            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
172          }
173          averageToolStripMenuItem.Enabled = medianToolStripMenuItem.Enabled = randomToolStripMenuItem.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
174          interpolationToolStripMenuItem.Enabled = interpolationToolStripMenuItem.Enabled && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
175          replaceValueToolStripMenuItem.Visible = true;
176          contextMenuCell.Show(MousePosition);
177        }
178      }
179    }
180
181    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
182      if (Content == null) return;
183      if (e.RowIndex < 0) return;
184      if (e.ColumnIndex < 0) return;
185      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
186      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
187
188      int rowIndex = virtualRowIndices[e.RowIndex];
189
190      Color backColor = e.CellStyle.BackColor;
191
192      if (HighlightedRowIndices != null && HighlightedRowIndices.Contains(rowIndex)
193        || HightlightedCells != null && HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
194        backColor = Color.Pink;
195      }
196
197      using (Brush backColorBrush = new SolidBrush(backColor)) {
198        Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
199        e.Graphics.FillRectangle(backColorBrush, bounds);
200      }
201
202      using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
203        Pen gridLinePen = new Pen(gridBrush);
204        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
205               e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
206               e.CellBounds.Bottom - 1);
207        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
208            e.CellBounds.Top, e.CellBounds.Right - 1,
209            e.CellBounds.Bottom);
210      }
211
212      e.PaintContent(e.CellBounds);
213      e.Handled = true;
214    }
215
216    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
217      btnApplySort.Enabled = sortedColumns.Any();
218      return base.Sort(sortedColumns);
219    }
220
221    protected override void ClearSorting() {
222      btnApplySort.Enabled = false;
223      base.ClearSorting();
224    }
225
226    private Dictionary<int, List<int>> GetSelectedCells() {
227      var selectedCells = new Dictionary<int, List<int>>();
228      for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
229        var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
230        if (!selectedCells.ContainsKey(columnIndex)) {
231          selectedCells.Add(columnIndex, new List<int>());
232        }
233        selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
234      }
235      return selectedCells;
236    }
237
238    private void ReplaceWithAverage_Click(object sender, EventArgs e) {
239      Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(GetSelectedCells());
240    }
241
242    private void ReplaceWithMedian_Click(object sender, EventArgs e) {
243      Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(GetSelectedCells());
244    }
245
246    private void ReplaceWithRandom_Click(object sender, EventArgs e) {
247      Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(GetSelectedCells());
248    }
249
250    private void ReplaceWithMostCommon_Click(object sender, EventArgs e) {
251      Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(GetSelectedCells());
252    }
253
254    private void ReplaceWithInterpolation_Click(object sender, EventArgs e) {
255      Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
256    }
257  }
258}
Note: See TracBrowser for help on using the repository browser.