-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathXlsxTableModel.cpp
159 lines (126 loc) · 3.74 KB
/
XlsxTableModel.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
// QXlsx
// MIT License
// https://github.com/j2doll/QXlsx
// XlsxTableModel.cpp
#include "XlsxTableModel.h"
#include <sstream>
#include <QDebug>
#include <QVariant>
XlsxTableModel::XlsxTableModel(const QList<QString> &colTitle, QList<VLIST> data, QObject *parent)
: QAbstractTableModel(parent)
{
// [1] set name of column
m_colNames = colTitle;
m_roleCount = m_colNames.size();
// [2] set data
for (int dc = 0; dc < data.size(); dc++) {
VLIST vList = data.at(dc);
m_the_data.append(vList);
}
}
void XlsxTableModel::testData()
{
// test code
// | 1 | 2 | 3 | col.count is 3.
// |-------|----|-------|
// | Alpha | 10 | 100.0 | row count is 4.
// | Beta | 20 | 200.0 |
// | Gamma | 30 | 300.0 |
// | Delta | 40 | 400.0 |
// set name of column
m_colNames.append(QString("COL1"));
m_colNames.append(QString("COL2"));
m_colNames.append(QString("COL3"));
m_roleCount = m_colNames.size();
// set data
VLIST list1;
list1.append(QVariant("Alpha"));
list1.append(QVariant(10));
list1.append(QVariant(100.0));
m_the_data.append(list1);
VLIST list2;
list2.append(QVariant("Beta"));
list2.append(QVariant(20));
list2.append(QVariant(200.0));
m_the_data.append(list2);
VLIST list3;
list3.append(QVariant("Gamma"));
list3.append(QVariant(30));
list3.append(QVariant(300.0));
m_the_data.append(list3);
VLIST list4;
list4.append(QVariant("Delta"));
list4.append(QVariant(40));
list4.append(QVariant(400.0));
m_the_data.append(list4);
}
int XlsxTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_the_data.size();
}
int XlsxTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_roleCount;
}
QVariant XlsxTableModel::data(const QModelIndex &index, int role) const
{
// current column & row
int col = index.column();
int row = index.row();
// check boundaries
if (col < 0 || columnCount() <= col || row < 0 || rowCount() <= row) {
qDebug() << "[Warning]"
<< " col=" << col << ", row=" << row;
return QVariant();
}
// Nominal case
QVariant ret;
int cmpRole;
for (int ic = 0; ic < m_the_data.size(); ic++) {
if (row == ic) {
QList<QVariant> vList = m_the_data.at(ic);
for (int jc = 0; jc < vList.size(); jc++) {
int plusOneRole = (int) (Qt::UserRole);
plusOneRole = plusOneRole + 1;
cmpRole = plusOneRole + jc;
if (role == cmpRole) {
QVariant var = vList.at(jc);
if (!var.isValid())
var.setValue(QString(""));
if (var.isNull())
var.setValue(QString(""));
ret = var;
}
}
}
}
qDebug() << "data: "
<< " col=" << col << ", row=" << row << ", role=" << role << ", cmpRole=" << cmpRole
<< ", value=" << ret.toString();
return ret;
}
QHash<int, QByteArray> XlsxTableModel::roleNames() const
{
QHash<int, QByteArray> roles;
for (int ic = 0; ic < m_roleCount; ic++) {
QString strRole = m_colNames.at(ic);
int roleNo = (Qt::UserRole + 1) + ic;
roles.insert(roleNo, strRole.toLatin1());
}
return roles;
}
// Return ordered List of user-defined roles
QStringList XlsxTableModel::customRoleNames()
{
QMap<int, QString> res;
QHashIterator<int, QByteArray> i(roleNames());
while (i.hasNext()) {
i.next();
if (i.key() > Qt::UserRole) {
res[i.key()] = i.value();
}
}
return res.values();
}