Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10583 was 10583, checked in by rstoll, 11 years ago
  • copied StringConvertibleMatrixView since we need to override dataGridView_CellValidating and dataGridView_CellParsing as well as PasteValuesToDataGridView
  • optimising event subscription, do not refresh if event is triggered by the view itself
File size: 7.1 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using System.Drawing;
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 int lastClickedRow;
35    private int lastClickedColumn;
36
37    private bool notOwnEvent = true;
38
39    public new IDataGridContent Content {
40      get { return (IDataGridContent)base.Content; }
41      set { base.Content = value; }
42    }
43
44    private IList<int> _highlightedRowIndices;
45    public IList<int> highlightedRowIndices
46    {
47        get { return _highlightedRowIndices; }
48        set
49        {
50            _highlightedRowIndices = value;
51            Refresh();
52        }
53    }
54
55    public DataGridContentView() {
56      InitializeComponent();
57      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
58      dataGridView.CellPainting      += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
59      contextMenuCell.Items.Add(ShowHideColumns);
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64    }
65
66
67    protected override void RegisterContentEvents() {
68      base.RegisterContentEvents();
69      Content.Changed += Content_Changed;
70    }
71
72    protected override void DeregisterContentEvents() {
73      base.DeregisterContentEvents();
74      Content.Changed -= Content_Changed;
75    }
76
77    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
78      if (notOwnEvent) {
79        dataGridView.Refresh();
80      }
81    }
82
83    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
84      if (!dataGridView.ReadOnly) {
85        string errorMessage;
86        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
87          e.Cancel = true;
88          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
89        }
90      }
91    }
92
93    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
94      notOwnEvent = false;
95      base.dataGridView_CellParsing(sender, e);
96      notOwnEvent = true;
97    }
98
99    protected override void PasteValuesToDataGridView() {
100      notOwnEvent = false;
101      base.PasteValuesToDataGridView();
102      notOwnEvent = true;
103    }
104
105
106    protected override void SetEnabledStateOfControls() {
107      base.SetEnabledStateOfControls();
108      rowsTextBox.ReadOnly = true;
109      columnsTextBox.ReadOnly = true;
110    }
111
112    private void btnApplySort_Click(object sender, System.EventArgs e) {
113      Content.PreprocessingDataManipulation.ReOrderToIndices(virtualRowIndices);
114      OnContentChanged();
115    }
116
117    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
118      if (Content == null) return;
119      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
120        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
121          contextMenu.Show(MousePosition);
122        } else {
123          interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows);
124          contextMenuCell.Show(MousePosition);
125          lastClickedColumn = e.ColumnIndex;
126          lastClickedRow = e.RowIndex;
127        }
128      }
129    }
130
131    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
132    {
133        if (Content == null) return;
134        if (e.RowIndex < 0) return;
135        if (e.ColumnIndex < 0) return;
136        if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
137        if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
138
139        int rowIndex = virtualRowIndices[e.RowIndex];
140
141        Color backColor = e.CellStyle.BackColor;
142
143        if (highlightedRowIndices != null && highlightedRowIndices.Contains(rowIndex))
144        {
145            backColor = Color.Pink;
146        }
147
148        using (Brush backColorBrush = new SolidBrush(backColor))
149        {
150            Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
151            e.Graphics.FillRectangle(backColorBrush, bounds);
152        }
153
154        using (Brush gridBrush = new SolidBrush(Color.LightGray))
155        {
156            Pen gridLinePen = new Pen(gridBrush);
157            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
158                   e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
159                   e.CellBounds.Bottom - 1);
160            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
161                e.CellBounds.Top, e.CellBounds.Right - 1,
162                e.CellBounds.Bottom);
163        }
164       
165        e.PaintContent(e.CellBounds);
166        e.Handled = true;
167    }
168
169    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
170      btnApplySort.Enabled = sortedColumns.Any();
171      return base.Sort(sortedColumns);
172    }
173
174    protected override void ClearSorting() {
175      btnApplySort.Enabled = false;
176      base.ClearSorting();
177    }
178
179    private void ReplaceWithAverage_Click(object sender, EventArgs e) {
180      Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(lastClickedColumn, new List<int>() { lastClickedRow });
181    }
182
183    private void ReplaceWithMedian_Click(object sender, EventArgs e) {
184      Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(lastClickedColumn, new List<int>() { lastClickedRow });
185    }
186
187    private void ReplaceWithRandom_Click(object sender, EventArgs e) {
188      Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(lastClickedColumn, new List<int>() { lastClickedRow });
189    }
190
191    private void ReplaceWithMostCommon_Click(object sender, EventArgs e) {
192      Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(lastClickedColumn, new List<int>() { lastClickedRow });
193    }
194
195    private void ReplaceWithInterpolation_Click(object sender, EventArgs e) {
196      Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(lastClickedColumn, new List<int>() { lastClickedRow });
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.