-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsvmodel.cpp
134 lines (119 loc) · 3.03 KB
/
tsvmodel.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
/**
* author trungvv1
*
* date 24 Jul 2015
* class
*
* brief write something about your class
*
*
*/
#include "tsvmodel.h"
TsvModel::TsvModel(const QStringList & headers, QObject* parent)
: QAbstractTableModel(parent), mHeaders(headers)
{
mNumRows = mNumColumns = 0;
}
QVariant TsvModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
if (index.isValid())
{
if (role == Qt::DisplayRole)
{
auto s = QString::number(mValues[row][col], 'f', 2);
if (s.endsWith(".00")) /// round to int
s = s.mid(0, s.length()-3);
if (s.contains(".") && s.length() > 6) /// round to int
s = s.mid(0, s.length()-3);
if (s.length() > 6) /// 111111 = 111e3
s = s.mid(0, 3) + "e" + QString::number(s.length()-3);
return s;
}
else if (role == Qt::TextAlignmentRole)
{
return Qt::AlignCenter;
}
}
return QVariant();
}
bool TsvModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
if (value.toString().isEmpty()) {
return false;
}
double newValue = value.toDouble();
mValues[index.row()][index.column()-1] = newValue;
return true;
}
return false;
}
QVariant TsvModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal)
{
return mHeaders[section];
}
else if (orientation == Qt::Vertical)
{
return section+1;
}
} else if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
return QVariant();
}
int TsvModel::rowCount(const QModelIndex &parent) const
{
return mNumRows;
}
int TsvModel::columnCount(const QModelIndex &parent) const
{
return mNumColumns;
}
bool TsvModel::insertRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginInsertRows(QModelIndex(), row, row + count - 1);
endInsertRows();
return true;
}
bool TsvModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row + count - 1);
endRemoveRows();
return true;
}
Qt::ItemFlags TsvModel::flags(const QModelIndex &index) const
{
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
void TsvModel::setValue(const vector<vector<double>> & values)
{
beginResetModel();
clear();
mNumRows = values.size();
mValues = values;
endResetModel();
}
const vector<vector<double> > &TsvModel::getValue() const
{
return mValues;
}
void TsvModel::setHeaders(const QStringList &headers)
{
beginResetModel();
mHeaders.clear();
mHeaders.append(headers);
mNumColumns = mHeaders.size();
endResetModel();
}
void TsvModel::clear()
{
mNumRows = 0;
mValues.clear();
}