-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablemodel.cpp
55 lines (50 loc) · 1.41 KB
/
tablemodel.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
#include "tablemodel.h"
#include <QLocale>
#include <QString>
#include <QStringList>
#include <limits>
TableModel::TableModel(Eigen::MatrixXd &matrix)
: QEigenMatrixModel(matrix) {}
TableModel::~TableModel() {}
double TableModel::parseDouble(const QString &data)
{
bool ok = false;
QLocale russian(QLocale::Russian);
double val = data.toDouble(&ok);
if (!ok)
val = russian.toDouble(data, &ok);
return ok ? val : std::numeric_limits<double>::quiet_NaN();
}
void TableModel::setText(const QString &str)
{
qDebug() << str;
auto lst = str.split("\n", Qt::SkipEmptyParts);
size_t size = 0;
QList<QStringList> llst;
for (const auto &i : lst) {
llst.append(i.split("\t", Qt::SkipEmptyParts));
if (llst.last().size() > size)
size = llst.last().size();
}
beginResetModel();
m_matrix.resize(llst.size(), size);
size_t ci = 0;
size_t cj = 0;
for (const auto &i : llst) {
cj = 0;
for (const auto &j : i) {
m_matrix(ci, cj) = parseDouble(j);
++cj;
}
++ci;
}
endResetModel();
emit dataChanged(index(0, 0), index(m_matrix.rows(), m_matrix.cols()));
}
void TableModel::setData(int row, int column, const QVariant &data)
{
beginResetModel();
m_matrix(row, column) = parseDouble(data.toString());
endResetModel();
emit dataChanged(index(row, column), index(row, column));
}