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