[10539] | 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 |
|
---|
[10534] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[10622] | 24 | using System.Drawing;
|
---|
[10534] | 25 | using System.Linq;
|
---|
[10236] | 26 | using System.Windows.Forms;
|
---|
[10672] | 27 | using HeuristicLab.Data;
|
---|
[10719] | 28 | using HeuristicLab.DataPreprocessing.Filter;
|
---|
[10236] | 29 | using HeuristicLab.MainForm;
|
---|
| 30 |
|
---|
[10558] | 31 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
[10236] | 32 | [View("Data Grid Content View")]
|
---|
[10311] | 33 | [Content(typeof(IDataGridContent), true)]
|
---|
[10583] | 34 | public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
|
---|
[10236] | 35 |
|
---|
[10583] | 36 | private bool notOwnEvent = true;
|
---|
[10870] | 37 | private bool isSearching = false;
|
---|
[10934] | 38 | private bool updateOnMouseUp = false;
|
---|
[10762] | 39 | private SearchAndReplaceDialog findAndReplaceDialog;
|
---|
[10698] | 40 | private IFindPreprocessingItemsIterator searchIterator;
|
---|
[10672] | 41 | private string currentSearchText;
|
---|
[10870] | 42 | private ComparisonOperation currentComparisonOperation;
|
---|
[10706] | 43 | private Tuple<int, int> currentCell;
|
---|
[10583] | 44 |
|
---|
[10236] | 45 | public new IDataGridContent Content {
|
---|
| 46 | get { return (IDataGridContent)base.Content; }
|
---|
| 47 | set { base.Content = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[10574] | 50 | private IList<int> _highlightedRowIndices;
|
---|
[10623] | 51 | public IList<int> HighlightedRowIndices {
|
---|
[10585] | 52 | get { return _highlightedRowIndices; }
|
---|
| 53 | set {
|
---|
| 54 | _highlightedRowIndices = value;
|
---|
| 55 | Refresh();
|
---|
| 56 | }
|
---|
[10574] | 57 | }
|
---|
| 58 |
|
---|
[10719] | 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 |
|
---|
[10236] | 68 | public DataGridContentView() {
|
---|
| 69 | InitializeComponent();
|
---|
[10380] | 70 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
[10585] | 71 | dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
|
---|
[10668] | 72 | dataGridView.KeyDown += dataGridView_KeyDown;
|
---|
[10934] | 73 | dataGridView.MouseUp += dataGridView_MouseUp;
|
---|
[10380] | 74 | contextMenuCell.Items.Add(ShowHideColumns);
|
---|
[10623] | 75 | _highlightedRowIndices = new List<int>();
|
---|
[10719] | 76 | _highlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
[10706] | 77 | currentCell = null;
|
---|
[10236] | 78 | }
|
---|
| 79 |
|
---|
[10916] | 80 | protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
| 81 | if (Content != null) {
|
---|
| 82 | if (!isSearching) {
|
---|
[10934] | 83 |
|
---|
| 84 | if (Control.MouseButtons == MouseButtons.Left) {
|
---|
| 85 | updateOnMouseUp = true;
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[10916] | 89 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[10934] | 90 |
|
---|
[10916] | 91 | Content.DataGridLogic.SetSelection(GetSelectedCells());
|
---|
| 92 | }
|
---|
[10870] | 93 | }
|
---|
[10804] | 94 | }
|
---|
| 95 |
|
---|
[10934] | 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 |
|
---|
[10236] | 104 | protected override void OnContentChanged() {
|
---|
| 105 | base.OnContentChanged();
|
---|
[10898] | 106 | // ToDo: Temporarily disabled because needs to much performance
|
---|
| 107 | //DataGridView.AutoResizeColumns();
|
---|
| 108 | //DataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
|
---|
[10911] | 109 | DataGridView.RowHeadersWidth = 70;
|
---|
[10739] | 110 | if (Content == null && findAndReplaceDialog != null) {
|
---|
| 111 | findAndReplaceDialog.Close();
|
---|
| 112 | }
|
---|
[10236] | 113 | }
|
---|
| 114 |
|
---|
[10571] | 115 | protected override void RegisterContentEvents() {
|
---|
| 116 | base.RegisterContentEvents();
|
---|
| 117 | Content.Changed += Content_Changed;
|
---|
[10900] | 118 | Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
|
---|
[10571] | 119 | }
|
---|
| 120 |
|
---|
| 121 | protected override void DeregisterContentEvents() {
|
---|
| 122 | base.DeregisterContentEvents();
|
---|
| 123 | Content.Changed -= Content_Changed;
|
---|
[10900] | 124 | Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
|
---|
[10571] | 125 | }
|
---|
| 126 |
|
---|
[10930] | 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 |
|
---|
[10571] | 140 | void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
[10583] | 141 | if (notOwnEvent) {
|
---|
[10622] | 142 | switch (e.Type) {
|
---|
| 143 | case DataPreprocessingChangedEventType.ChangeColumn:
|
---|
| 144 | case DataPreprocessingChangedEventType.ChangeItem:
|
---|
| 145 | dataGridView.Refresh();
|
---|
| 146 | break;
|
---|
[10817] | 147 | default:
|
---|
| 148 | OnContentChanged();
|
---|
| 149 | break;
|
---|
[10622] | 150 | }
|
---|
[10583] | 151 | }
|
---|
[10852] | 152 | searchIterator = null;
|
---|
[10571] | 153 | }
|
---|
| 154 |
|
---|
[10583] | 155 | protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[10236] | 156 | if (!dataGridView.ReadOnly) {
|
---|
| 157 | string errorMessage;
|
---|
[10916] | 158 | if (Content != null) {
|
---|
[10930] | 159 | if (dataGridView.IsCurrentCellInEditMode && Content.FilterLogic.IsFiltered) {
|
---|
[10900] | 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 | }
|
---|
[10236] | 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[10253] | 172 |
|
---|
[10583] | 173 | protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
[10585] | 174 | triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
|
---|
[10583] | 175 | }
|
---|
| 176 |
|
---|
| 177 | protected override void PasteValuesToDataGridView() {
|
---|
[10585] | 178 | triggersOwnEvent(() => base.PasteValuesToDataGridView());
|
---|
[10583] | 179 | }
|
---|
| 180 |
|
---|
[10317] | 181 | protected override void SetEnabledStateOfControls() {
|
---|
| 182 | base.SetEnabledStateOfControls();
|
---|
| 183 | rowsTextBox.ReadOnly = true;
|
---|
| 184 | columnsTextBox.ReadOnly = true;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[10253] | 187 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
[10585] | 188 | triggersOwnEvent(() => {
|
---|
[10809] | 189 | Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
|
---|
[10585] | 190 | OnContentChanged();
|
---|
| 191 | });
|
---|
[10253] | 192 | }
|
---|
[10345] | 193 |
|
---|
[10585] | 194 | private void triggersOwnEvent(Action action) {
|
---|
| 195 | notOwnEvent = false;
|
---|
| 196 | action();
|
---|
| 197 | notOwnEvent = true;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[10698] | 200 | #region FindAndReplaceDialog
|
---|
[10585] | 201 |
|
---|
[10636] | 202 | private void CreateFindAndReplaceDialog() {
|
---|
[10876] | 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;
|
---|
[10930] | 218 | if (Content.FilterLogic.IsFiltered) {
|
---|
| 219 | findAndReplaceDialog.DisableReplace();
|
---|
| 220 | }
|
---|
[10852] | 221 | }
|
---|
[10636] | 222 | }
|
---|
| 223 |
|
---|
[10916] | 224 | private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
|
---|
[10863] | 225 | if (Content != null) {
|
---|
[10870] | 226 | if (!isSearching && AreMultipleCellsSelected()) {
|
---|
[10863] | 227 | ResetHighlightedCellsBackground();
|
---|
| 228 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 229 | searchIterator = null;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[10705] | 234 | void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
[10719] | 235 | ResetHighlightedCellsBackground();
|
---|
[10852] | 236 | searchIterator = null;
|
---|
[10863] | 237 | DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10705] | 238 | }
|
---|
| 239 |
|
---|
[10672] | 240 | void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
|
---|
[10712] | 241 | if (searchIterator != null && searchIterator.GetCurrent() != null) {
|
---|
[10706] | 242 | Replace(TransformToDictionary(currentCell));
|
---|
[10672] | 243 | }
|
---|
[10636] | 244 | }
|
---|
| 245 |
|
---|
| 246 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
[10672] | 247 | Replace(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
[10636] | 248 | }
|
---|
| 249 |
|
---|
| 250 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
[10876] | 251 | if (searchIterator == null ||
|
---|
| 252 | currentSearchText != findAndReplaceDialog.GetSearchText() ||
|
---|
[10870] | 253 | currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
|
---|
[10698] | 254 | searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
| 255 | currentSearchText = findAndReplaceDialog.GetSearchText();
|
---|
[10870] | 256 | currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10698] | 257 | }
|
---|
[10870] | 258 |
|
---|
[10852] | 259 | if (IsOneCellSelected()) {
|
---|
| 260 | var first = GetSelectedCells().First();
|
---|
| 261 | searchIterator.SetStartCell(first.Key, first.Value[0]);
|
---|
| 262 | }
|
---|
[10712] | 263 |
|
---|
[10705] | 264 | bool moreOccurences = false;
|
---|
[10870] | 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();
|
---|
[10852] | 272 | }
|
---|
[10870] | 273 | currentCell = searchIterator.GetCurrent();
|
---|
| 274 | moreOccurences = searchIterator.MoveNext();
|
---|
| 275 | if (!moreOccurences) {
|
---|
| 276 | searchIterator.Reset();
|
---|
| 277 | }
|
---|
[10852] | 278 | }
|
---|
[10870] | 279 | }
|
---|
[10672] | 280 |
|
---|
[10870] | 281 | dataGridView.ClearSelection();
|
---|
| 282 |
|
---|
[10705] | 283 | if (currentCell != null) {
|
---|
[10852] | 284 | dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
|
---|
[10698] | 285 | }
|
---|
[10852] | 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();
|
---|
[10706] | 300 | }
|
---|
[10852] | 301 | return count;
|
---|
[10636] | 302 | }
|
---|
| 303 |
|
---|
| 304 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
[10870] | 305 | dataGridView.ClearSelection();
|
---|
| 306 | isSearching = true;
|
---|
[10901] | 307 | SuspendRepaint();
|
---|
[10916] | 308 | var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
|
---|
| 309 | foreach (var column in selectedCells) {
|
---|
[10870] | 310 | foreach (var cell in column.Value) {
|
---|
| 311 | dataGridView[column.Key, cell].Selected = true;
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
[10901] | 314 | ResumeRepaint(true);
|
---|
[10870] | 315 | isSearching = false;
|
---|
[10916] | 316 | Content.DataGridLogic.SetSelection(selectedCells);
|
---|
| 317 | //update statistic in base
|
---|
| 318 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[10636] | 319 | }
|
---|
| 320 |
|
---|
[10870] | 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 |
|
---|
[10672] | 346 | private IDictionary<int, IList<int>> FindAll(string match) {
|
---|
[10719] | 347 | bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
|
---|
[10870] | 348 | ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
| 349 | var comparisonFilter = new ComparisonFilter(Content.FilterLogic.PreprocessingData, GetConstraintOperation(comparisonOperation), new StringValue(match), true);
|
---|
[10672] | 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;
|
---|
[10844] | 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;
|
---|
[10719] | 363 | IList<int> selectedList;
|
---|
| 364 | if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
|
---|
[10739] | 365 | foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
|
---|
| 366 | } else if (searchInSelection) {
|
---|
[10719] | 367 | foundCells[i].Clear();
|
---|
| 368 | }
|
---|
[10672] | 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:
|
---|
[10809] | 377 | Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
|
---|
[10672] | 378 | break;
|
---|
| 379 | case ReplaceAction.Average:
|
---|
[10809] | 380 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
|
---|
[10672] | 381 | break;
|
---|
| 382 | case ReplaceAction.Median:
|
---|
[10809] | 383 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
|
---|
[10672] | 384 | break;
|
---|
| 385 | case ReplaceAction.Random:
|
---|
[10809] | 386 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
|
---|
[10672] | 387 | break;
|
---|
| 388 | case ReplaceAction.MostCommon:
|
---|
[10809] | 389 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
|
---|
[10672] | 390 | break;
|
---|
| 391 | case ReplaceAction.Interpolation:
|
---|
[10809] | 392 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
|
---|
[10672] | 393 | break;
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[10698] | 398 | private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
|
---|
[10672] | 399 | var highlightCells = new Dictionary<int, IList<int>>();
|
---|
[10698] | 400 | highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
|
---|
[10672] | 401 | return highlightCells;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[10719] | 404 | private void ResetHighlightedCellsBackground() {
|
---|
| 405 | HightlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[10698] | 408 | #endregion FindAndReplaceDialog
|
---|
| 409 |
|
---|
[10380] | 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) {
|
---|
[10769] | 414 | replaceValueOverColumnToolStripMenuItem.Visible = false;
|
---|
[10627] | 415 | contextMenuCell.Show(MousePosition);
|
---|
[10380] | 416 | } else {
|
---|
[10590] | 417 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
| 418 | dataGridView.ClearSelection();
|
---|
| 419 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
| 420 | }
|
---|
[10812] | 421 |
|
---|
[10621] | 422 | var columnIndices = new HashSet<int>();
|
---|
| 423 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
| 424 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
| 425 | }
|
---|
[10875] | 426 |
|
---|
| 427 | replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
|
---|
| 428 |
|
---|
[10812] | 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 =
|
---|
[10820] | 437 | interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
|
---|
| 438 | && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
|
---|
[10812] | 439 | && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
|
---|
| 440 |
|
---|
[10769] | 441 | replaceValueOverColumnToolStripMenuItem.Visible = true;
|
---|
[10380] | 442 | contextMenuCell.Show(MousePosition);
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[10585] | 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;
|
---|
[10870] | 453 | if (HighlightedRowIndices == null) return;
|
---|
[10574] | 454 |
|
---|
[10585] | 455 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
[10574] | 456 |
|
---|
[10585] | 457 | Color backColor = e.CellStyle.BackColor;
|
---|
[10574] | 458 |
|
---|
[10719] | 459 | if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
|
---|
| 460 | backColor = Color.LightGray;
|
---|
| 461 | }
|
---|
[10574] | 462 |
|
---|
[10585] | 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 | }
|
---|
[10712] | 467 |
|
---|
[10585] | 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;
|
---|
[10574] | 480 | }
|
---|
| 481 |
|
---|
[10668] | 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 |
|
---|
[10345] | 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 | }
|
---|
[10380] | 511 |
|
---|
[10672] | 512 | private IDictionary<int, IList<int>> GetSelectedCells() {
|
---|
| 513 | IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
|
---|
[10590] | 514 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
[10622] | 515 | var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
|
---|
| 516 | if (!selectedCells.ContainsKey(columnIndex)) {
|
---|
| 517 | selectedCells.Add(columnIndex, new List<int>());
|
---|
[10590] | 518 | }
|
---|
| 519 | selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
|
---|
| 520 | }
|
---|
| 521 | return selectedCells;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[10769] | 524 | private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
|
---|
[10812] | 525 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
|
---|
[10380] | 526 | }
|
---|
[10809] | 527 | private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
|
---|
| 528 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
|
---|
| 529 | }
|
---|
[10380] | 530 |
|
---|
[10769] | 531 | private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 532 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
|
---|
[10380] | 533 | }
|
---|
[10809] | 534 | private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
|
---|
| 535 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
|
---|
| 536 | }
|
---|
[10380] | 537 |
|
---|
[10769] | 538 | private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 539 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
|
---|
[10380] | 540 | }
|
---|
[10809] | 541 | private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
|
---|
| 542 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
|
---|
| 543 | }
|
---|
[10380] | 544 |
|
---|
[10769] | 545 | private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 546 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
|
---|
[10380] | 547 | }
|
---|
[10809] | 548 | private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
|
---|
| 549 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
|
---|
| 550 | }
|
---|
[10380] | 551 |
|
---|
[10769] | 552 | private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 553 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
[10380] | 554 | }
|
---|
[10769] | 555 |
|
---|
| 556 | private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
|
---|
[10820] | 557 | Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
|
---|
[10769] | 558 | }
|
---|
| 559 |
|
---|
[10764] | 560 | private void btnSearch_Click(object sender, EventArgs e) {
|
---|
[10762] | 561 | CreateFindAndReplaceDialog();
|
---|
| 562 | findAndReplaceDialog.ActivateSearch();
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | private void btnReplace_Click(object sender, EventArgs e) {
|
---|
| 566 | CreateFindAndReplaceDialog();
|
---|
| 567 | findAndReplaceDialog.ActivateReplace();
|
---|
| 568 | }
|
---|
[10236] | 569 | }
|
---|
| 570 | }
|
---|