[8833] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16720] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8833] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 |
|
---|
[9379] | 29 | namespace HeuristicLab.Data.Views {
|
---|
[8833] | 30 | public partial class EnhancedStringConvertibleMatrixView : StringConvertibleMatrixView {
|
---|
[8874] | 31 | private bool[] columnVisibility, rowVisibility;
|
---|
[8833] | 32 |
|
---|
| 33 | // sets the visibility of its columns for the next time it is updated
|
---|
[8874] | 34 | public IEnumerable<bool> ColumnVisibility {
|
---|
| 35 | get { return columnVisibility; }
|
---|
| 36 | set { columnVisibility = value.ToArray(); }
|
---|
| 37 | }
|
---|
[8833] | 38 | // sets the visibility of its rows for the next time it is updated
|
---|
[8874] | 39 | public IEnumerable<bool> RowVisibility {
|
---|
| 40 | get { return rowVisibility; }
|
---|
| 41 | set { rowVisibility = value.ToArray(); }
|
---|
| 42 | }
|
---|
[8833] | 43 |
|
---|
[8861] | 44 | public double Maximum { get; set; }
|
---|
| 45 | public double Minimum { get; set; }
|
---|
| 46 |
|
---|
[12151] | 47 | public string FormatPattern { get; set; }
|
---|
| 48 |
|
---|
[8861] | 49 | public new DoubleMatrix Content {
|
---|
| 50 | get { return (DoubleMatrix)base.Content; }
|
---|
[8833] | 51 | set { base.Content = value; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public EnhancedStringConvertibleMatrixView() {
|
---|
| 55 | InitializeComponent();
|
---|
[12151] | 56 | FormatPattern = string.Empty;
|
---|
[8833] | 57 | }
|
---|
| 58 |
|
---|
| 59 | public void ResetVisibility() {
|
---|
| 60 | columnVisibility = null;
|
---|
| 61 | rowVisibility = null;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[12151] | 64 | protected override void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
| 65 | if (Content != null && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
| 66 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
| 67 | e.Value = Content[rowIndex, e.ColumnIndex].ToString(FormatPattern);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[13570] | 71 | public override void UpdateColumnHeaders() {
|
---|
[8833] | 72 | base.UpdateColumnHeaders();
|
---|
| 73 | if (columnVisibility != null && Content != null && columnVisibility.Count() == dataGridView.ColumnCount) {
|
---|
| 74 | int i = 0;
|
---|
| 75 | foreach (var visibility in columnVisibility) {
|
---|
| 76 | dataGridView.Columns[i].Visible = visibility;
|
---|
| 77 | i++;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[13570] | 81 | public override void UpdateRowHeaders() {
|
---|
[8880] | 82 | if (Content == null) return;
|
---|
[9379] | 83 | if (rowVisibility != null && rowVisibility.Count() != dataGridView.RowCount) return;
|
---|
[8880] | 84 |
|
---|
| 85 | for (int index = 0; index < dataGridView.RowCount; index++) {
|
---|
| 86 | dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndices[index]);
|
---|
[9379] | 87 |
|
---|
| 88 | if (rowVisibility != null) {
|
---|
| 89 | dataGridView.Rows[index].Visible = rowVisibility.ElementAt(virtualRowIndices[index]);
|
---|
| 90 | } else {
|
---|
| 91 | dataGridView.Rows[index].Visible = true;
|
---|
| 92 | }
|
---|
[8833] | 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected virtual void ShowHideRows_Click(object sender, EventArgs e) {
|
---|
| 97 | var dialog = new StringConvertibleMatrixRowVisibilityDialog(this.dataGridView.Rows.Cast<DataGridViewRow>());
|
---|
| 98 | dialog.ShowDialog();
|
---|
[8874] | 99 | RowVisibility = dialog.Visibility;
|
---|
[8833] | 100 | }
|
---|
| 101 |
|
---|
| 102 | protected override void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
| 103 | var dialog = new StringConvertibleMatrixColumnVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>());
|
---|
| 104 | dialog.ShowDialog();
|
---|
[8874] | 105 | ColumnVisibility = dialog.Visibility;
|
---|
[8833] | 106 | }
|
---|
| 107 |
|
---|
| 108 | protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
|
---|
| 109 | if (Content == null) return;
|
---|
| 110 | if (e.RowIndex < 0) return;
|
---|
| 111 | if (e.ColumnIndex < 0) return;
|
---|
| 112 | if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
|
---|
| 113 | if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
|
---|
| 114 |
|
---|
| 115 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
[8861] | 116 | Color backColor = GetDataPointColor(Content[rowIndex, e.ColumnIndex], Minimum, Maximum);
|
---|
[8833] | 117 | using (Brush backColorBrush = new SolidBrush(backColor)) {
|
---|
| 118 | e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
|
---|
| 119 | }
|
---|
| 120 | e.PaintContent(e.CellBounds);
|
---|
| 121 | e.Handled = true;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | protected virtual Color GetDataPointColor(double value, double min, double max) {
|
---|
| 125 | if (double.IsNaN(value)) {
|
---|
| 126 | return Color.DarkGray;
|
---|
| 127 | }
|
---|
| 128 | IList<Color> colors = ColorGradient.Colors;
|
---|
| 129 | int index = (int)((colors.Count - 1) * (value - min) / (max - min));
|
---|
| 130 | if (index >= colors.Count) index = colors.Count - 1;
|
---|
| 131 | if (index < 0) index = 0;
|
---|
| 132 | return colors[index];
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|