Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/NamedIntervalsView.cs @ 16844

Last change on this file since 16844 was 16844, checked in by chaider, 5 years ago

#2971

  • removed/reordered usings
  • removed storabletype in ConstantOptimizationEvaluator
  • Added/Changed License informations
File size: 6.0 KB
RevLine 
[12574]1#region License Information
2/* HeuristicLab
[16844]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12574]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
[16830]22
[16544]23using System.Linq;
24using System.Windows.Forms;
[12574]25using HeuristicLab.MainForm;
[16544]26using HeuristicLab.MainForm.WindowsForms;
[7141]27
[16544]28namespace HeuristicLab.Problems.DataAnalysis.Views {
[7141]29
[16544]30  [View("NamedIntervals View")]
31  [Content(typeof(NamedIntervals), true)]
32  public partial class NamedIntervalsView : AsynchronousContentView {
[7141]33
[16544]34    public new NamedIntervals Content {
[16634]35      get => (NamedIntervals)base.Content;
36      set => base.Content = value;
[7141]37    }
38
[16544]39    public DataGridView DataGridView {
40      get => dataGridView;
[12574]41    }
[7141]42
[16544]43    public NamedIntervalsView() {
44      InitializeComponent();
45      dataGridView.AutoGenerateColumns = false;
46      dataGridView.AllowUserToAddRows = false;
[12574]47    }
[7141]48
[16544]49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      if (Content == null) {
52        DataGridView.Rows.Clear();
53        DataGridView.Columns.Clear();
54      } else if (!DataGridView.IsCurrentCellInEditMode) {
55        UpdateData();
56      }
[7141]57    }
58
[16544]59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
61      dataGridView.Enabled = Content != null;
[16586]62      dataGridView.ReadOnly = ReadOnly;
[7141]63    }
64
[16544]65    protected void UpdateData() {
[16627]66      DataGridView.Rows.Clear();
67      DataGridView.Columns.Clear();
68      SetColumnNames();
69
70
[16544]71      var variablesCount = Content.VariableIntervals.Count;
[12574]72
[16544]73      DataGridViewRow[] rows = new DataGridViewRow[variablesCount];
74      for (var i = 0; i < variablesCount; ++i) {
75        var row = new DataGridViewRow();
76        rows[i] = row;
77      }
78      dataGridView.Rows.AddRange(rows);
[12574]79
[16634]80      SetRowsHeader();
[16544]81      FillRows();
82
83      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
84      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
85      dataGridView.Enabled = true;
86
87    }
88
89    protected void FillRows() {
90      for (var i = 0; i < dataGridView.RowCount; ++i) {
91        var key = (string)dataGridView.Rows[i].HeaderCell.Value;
92        dataGridView.Rows[i].Cells[0].Value = Content.VariableIntervals[key].LowerBound;
93        dataGridView.Rows[i].Cells[1].Value = Content.VariableIntervals[key].UpperBound;
[12574]94      }
[16544]95    }
[12574]96
[16634]97    protected void SetRowsHeader() {
[16544]98      for (var i = 0; i < Content.VariableIntervals.Count; ++i) {
99        var item = Content.VariableIntervals.ElementAt(i);
100        dataGridView.Rows[i].HeaderCell.Value = item.Key;
101      }
[7141]102    }
103
[16627]104    protected void SetColumnNames() {
[16544]105      dataGridView.Columns.Add("lowerBound", "Lower Bound");
106      dataGridView.Columns.Add("upperBound", "Upper Bound");
107    }
108
109    private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
[16545]110      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
111
112      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
113
114      var lowerBound = e.ColumnIndex == 0 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].LowerBound;
115      var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].UpperBound;
116
[16634]117      // update if there was a change
118      if (lowerBound != Content.VariableIntervals[key].LowerBound ||
119        upperBound != Content.VariableIntervals[key].UpperBound)
120        Content.VariableIntervals[key] = new Interval(lowerBound, upperBound);
[16544]121    }
122
123    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
[16634]124      // DISABLED VALIDATION OF ENTRIES
125      // because it leads to problems when updating the table
126      // we can assume it's the users fault if he or she enters incorrect values
[16545]127
[16730]128      if (dataGridView.Rows[e.RowIndex].IsNewRow) {
129        return;
130      }
131     
132      if (!double.TryParse(e.FormattedValue.ToString(), out var value)) {
133        e.Cancel = true;
134        dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value.";
135      } else if (string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()) ||
136        string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString())) {
137        // accept any value if one of the cells is still empty
138      } else {
139        var left = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
140        var right = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
141        if (e.ColumnIndex == 1 && value < left || e.ColumnIndex == 0 && value > right) {
142          e.Cancel = true;
143          dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
144        }
145      }
[7141]146    }
[16544]147
148    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
149      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
150    }
[16817]151
152    private void dataGridView_CellValidated(object sender, DataGridViewCellEventArgs e) {
153      var col = e.ColumnIndex;
154      var row = e.RowIndex;
155      if (double.TryParse(dataGridView.Rows[row].Cells[col].Value.ToString(), out var value))
156        dataGridView.Rows[row].Cells[col].Value = value;
157    }
[7141]158  }
159}
Note: See TracBrowser for help on using the repository browser.