-
Notifications
You must be signed in to change notification settings - Fork 7
/
SC_valuelistdialog.cpp
180 lines (136 loc) · 5.5 KB
/
SC_valuelistdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Alex Cope **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "SC_valuelistdialog.h"
#include "ui_valuelistdialog.h"
valueListDialog::valueListDialog(ParameterInstance * par, QWidget *parent) :
QDialog(parent),
ui(new Ui::valueListDialog)
{
ui->setupUi(this);
ui->spinBox->setRange(0, INT_MAX); // set max from the component
ui->spinBox->setValue(par->value.size());
connect(ui->spinBox,SIGNAL(editingFinished()), this, SLOT(updateValSize()));
this->par = par;
this->vals = par->value;
this->inds = par->indices;
this->vModel = new vectorModel();
this->vModel->setPointer(par);
this->ui->tableView->setModel(this->vModel);
QHeaderView * header = this->ui->tableView->horizontalHeader();
header->setStretchLastSection(true);
//header->setResizeMode(0, QHeaderView::Fixed);
header->resizeSection(0, 80);
connect(this->vModel,SIGNAL(setSpinBoxVal(int)), ui->spinBox, SLOT(setValue(int)));
connect(this->ui->import_csv,SIGNAL(clicked()), this, SLOT(import()));
}
valueListDialog::~valueListDialog()
{
delete vModel;
delete ui;
}
void valueListDialog::accept() {
// nothing to do
delete this;
}
void valueListDialog::reject() {
// restore values:
par->value = vals;
par->indices = inds;
delete this;
}
void valueListDialog::import() {
QString fileName = QFileDialog::getOpenFileName(this, tr("Open CSV file for import"), qgetenv("HOME"), tr("CSV files (*.csv *.txt);; All files (*.*)"));
import_csv(fileName);
}
void valueListDialog::keyPressEvent(QKeyEvent *evt)
{
if(evt->key() == Qt::Key_Enter || evt->key() == Qt::Key_Return)
return;
QDialog::keyPressEvent(evt);
}
void valueListDialog::import_csv(QString fileName) {
// open the input csv file for reading
QFile fileIn(fileName);
if( !fileIn.open( QIODevice::ReadOnly ) ) {
QMessageBox msgBox;
msgBox.setText("Could not open the selected file");
msgBox.exec();
return;}
// use textstream so we can read lines into a QString
QTextStream stream(&fileIn);
// number of columns
int numFields = -1;
// column to load
int col = 0;
int counter = 0;
par->value.clear();
par->indices.clear();
// load in the csv line by line
while (!(stream.atEnd())) {
// get a line
QString line = stream.readLine();
// see if it is a comment:
if (line[0] == '#') {
continue;
}
// or a blank line
QString line2 = line;
line2.replace(" ", "");
if (line2 == "\n") {
continue;
}
// not a comment - so begin parsing and create an xml element
QStringList fields = line.split(",");
if (fields.size() > 1 && numFields == -1) {
col = QInputDialog::getInt(this,"Choose column", "Column to load", 1, 1, fields.size()) - 1;
}
if (fields.size() < 1) {
QMessageBox msgBox;
msgBox.setText("CSV file could not be read");
msgBox.exec();
par->value.clear();
par->indices.clear();
this->ui->spinBox->setValue(par->value.size());
return;}
if (numFields == -1) {
numFields = fields.size();
} else if (numFields != fields.size()) {
std::cerr << "something is wrong!\n";
continue;
}
// for the chosen field
float num = fields[col].toFloat();
par->value.push_back(num);
par->indices.push_back(counter);
++counter;
}
fileIn.close();
this->ui->spinBox->setValue(par->value.size());
this->vModel->emitDataChanged();
}
void valueListDialog::updateValSize() {
int val = qobject_cast < QSpinBox * > (sender())->value();
/*bool returnVal;
returnVal = */this->vModel->insertConnRows(val);
}