Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalCollectionView.cs @ 17564

Last change on this file since 17564 was 17564, checked in by mkommend, 4 years ago

#2971: Minor changes in interval collection and interval interpreter.

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
23using System.Windows.Forms;
24using HeuristicLab.MainForm;
25using HeuristicLab.MainForm.WindowsForms;
26
27namespace HeuristicLab.Problems.DataAnalysis.Views {
28
29  [View("IntervalCollection View")]
30  [Content(typeof(IntervalCollection), true)]
31  public sealed partial class IntervalCollectionView : AsynchronousContentView {
32
33    public new IntervalCollection Content {
34      get => (IntervalCollection)base.Content;
35      set => base.Content = value;
36    }
37
38    public DataGridView DataGridView {
39      get => dataGridView;
40    }
41
42    public IntervalCollectionView() {
43      InitializeComponent();
44      dataGridView.AutoGenerateColumns = false;
45      dataGridView.AllowUserToAddRows = false;
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content == null) {
51        DataGridView.Rows.Clear();
52        DataGridView.Columns.Clear();
53      } else {
54        UpdateData();
55      }
56    }
57
58    protected override void SetEnabledStateOfControls() {
59      base.SetEnabledStateOfControls();
60      dataGridView.Enabled = Content != null;
61      dataGridView.ReadOnly = ReadOnly;
62    }
63
64    private void UpdateData() {
65      DataGridView.Rows.Clear();
66      DataGridView.Columns.Clear();
67      SetColumnNames();
68
69
70      var variablesCount = Content.Count;
71
72      DataGridViewRow[] rows = new DataGridViewRow[variablesCount];
73      for (var i = 0; i < variablesCount; ++i) {
74        var row = new DataGridViewRow();
75        rows[i] = row;
76      }
77      dataGridView.Rows.AddRange(rows);
78
79      int j = 0;
80      foreach (var variableInterval in Content.GetVariableIntervals()) {
81        dataGridView.Rows[j].HeaderCell.Value = variableInterval.Item1;
82        dataGridView.Rows[j].Cells[0].Value = variableInterval.Item2.LowerBound;
83        dataGridView.Rows[j].Cells[1].Value = variableInterval.Item2.UpperBound;
84        j++;
85      }
86
87      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
88      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
89      dataGridView.Enabled = true;
90      //Disable Sorting
91      foreach (DataGridViewColumn column in dataGridView.Columns) {
92        column.SortMode = DataGridViewColumnSortMode.NotSortable;
93      }
94    }
95
96    private void SetColumnNames() {
97      dataGridView.Columns.Add("lowerBound", "Lower Bound");
98      dataGridView.Columns.Add("upperBound", "Upper Bound");
99    }
100
101    private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
102      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
103
104      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
105
106      var lowerBound = e.ColumnIndex == 0 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).LowerBound;
107      var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).UpperBound;
108
109      // update if there was a change
110      if (lowerBound != Content.GetInterval(key).LowerBound ||
111        upperBound != Content.GetInterval(key).UpperBound)
112        Content.SetInterval(key, new Interval(lowerBound, upperBound));
113    }
114
115    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
116      if (!double.TryParse(e.FormattedValue.ToString(), out var value)) {
117        e.Cancel = true;
118        dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value.";
119        return;
120      } else {
121        var lowerBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
122        var upperBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
123        if (e.ColumnIndex == 1 && value < lowerBound || e.ColumnIndex == 0 && value > upperBound) {
124          e.Cancel = true;
125          dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
126        }
127      }
128    }
129
130    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
131      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.