Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.DataAnalysis/ManualScalingControl.cs @ 10805

Last change on this file since 10805 was 351, checked in by gkronber, 16 years ago

added missing GPL headers in HeuristicLab.Analysis

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Core;
32
33namespace HeuristicLab.DataAnalysis {
34  public partial class ManualScalingControl : Form {
35    public ManualScalingControl(bool editingAllowed) {
36      InitializeComponent();
37      dataGridView.ReadOnly = !editingAllowed;
38      okButton.Text = editingAllowed ? "Scale" : "Close";
39      cancelButton.Visible = editingAllowed;
40      cancelButton.Text = "Cancel";
41
42      // format all cells with the round-trip formatter to make sure that exported and imported values have
43      // the same numeric value
44      dataGridView.DefaultCellStyle.Format = "r";
45    }
46
47    private double[,] data;
48    public double[,] Data {
49      get {
50        return data;
51      }
52      set {
53        data = value;
54        UpdateControls();
55      }
56    }
57
58    protected void UpdateControls() {
59      Refresh();
60      dataGridView.ColumnCount = data.GetLength(0);
61      dataGridView.RowCount = data.GetLength(1);
62      for(int i = 0; i < dataGridView.RowCount; i++) {
63        for(int j = 0; j < dataGridView.ColumnCount; j++) {
64          dataGridView[j,i].Value = data[j, i];
65        }
66      }
67      dataGridView.Columns[0].HeaderText = "Factor";
68      dataGridView.Columns[1].HeaderText = "Offset";
69    }
70
71    private void button_Click(object sender, EventArgs e) {
72      DialogResult = DialogResult.OK;
73    }
74
75    private void cancelButton_Click(object sender, EventArgs e) {
76      DialogResult = DialogResult.Cancel;
77    }
78
79    // stolen from the internets
80    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
81      if(e.Control && e.KeyCode == Keys.C) {
82        DataObject d = dataGridView.GetClipboardContent();
83        Clipboard.SetDataObject(d);
84        e.Handled = true;
85      } else if(e.Control && e.KeyCode == Keys.V) {
86        string s = Clipboard.GetText();
87        string[] lines = s.Split(new char[] {'\n', '\r'},StringSplitOptions.RemoveEmptyEntries);
88        int row = dataGridView.CurrentCell.RowIndex;
89        int col = dataGridView.CurrentCell.ColumnIndex;
90        foreach(string line in lines) {
91          if(row < dataGridView.RowCount && line.Length > 0) {
92            string[] cells = line.Split(new char[] {'\t',' ',';'},StringSplitOptions.RemoveEmptyEntries);
93            for(int i = 0; i < cells.Length; i++) {
94              if(col + i < this.dataGridView.ColumnCount) {
95                dataGridView[col + i, row].Value = double.Parse(cells[i]);
96              } else {
97                break;
98              }
99            }
100            row++;
101          } else {
102            break;
103          }
104        }
105        e.Handled = true;
106      }
107    }
108
109    private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
110      if(e.RowIndex >= 0 && e.ColumnIndex >= 0) {
111        data[e.ColumnIndex, e.RowIndex] = (double)dataGridView[e.ColumnIndex, e.RowIndex].Value;
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.