1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-30 05:02:52 +08:00
QXlsx/HelloAndroid/XlsxTableModel.cpp

160 lines
3.7 KiB
C++
Raw Normal View History

2018-08-14 09:07:09 +09:00
// QXlsx
// MIT License
// https://github.com/j2doll/QXlsx
2018-07-13 13:56:11 +09:00
// XlsxTableModel.cpp
2018-07-12 18:15:23 +09:00
2018-07-13 13:56:11 +09:00
#include "XlsxTableModel.h"
2018-07-12 18:15:23 +09:00
#include <sstream>
#include <QDebug>
#include <QVariant>
2018-07-12 18:15:23 +09:00
XlsxTableModel::XlsxTableModel(const QList<QString> &colTitle, QList<VLIST> data, QObject *parent)
2018-07-12 18:15:23 +09:00
: QAbstractTableModel(parent)
{
// [1] set name of column
2018-07-13 09:17:28 +09:00
m_colNames = colTitle;
m_roleCount = m_colNames.size();
2018-07-13 09:17:28 +09:00
// [2] set data
2018-07-13 09:17:28 +09:00
for (int dc = 0; dc < data.size(); dc++) {
VLIST vList = data.at(dc);
m_the_data.append(vList);
}
2018-07-12 18:15:23 +09:00
}
2018-07-13 13:56:11 +09:00
void XlsxTableModel::testData()
2018-07-12 18:15:23 +09:00
{
// 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"));
2018-07-12 18:15:23 +09:00
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);
2018-07-12 18:15:23 +09:00
VLIST list2;
list2.append(QVariant("Beta"));
list2.append(QVariant(20));
list2.append(QVariant(200.0));
m_the_data.append(list2);
2018-07-12 18:15:23 +09:00
VLIST list3;
list3.append(QVariant("Gamma"));
list3.append(QVariant(30));
list3.append(QVariant(300.0));
m_the_data.append(list3);
2018-07-12 18:15:23 +09:00
VLIST list4;
list4.append(QVariant("Delta"));
list4.append(QVariant(40));
list4.append(QVariant(400.0));
m_the_data.append(list4);
2018-07-12 18:15:23 +09:00
}
int XlsxTableModel::rowCount(const QModelIndex &parent) const
2018-07-12 18:15:23 +09:00
{
Q_UNUSED(parent)
return m_the_data.size();
}
int XlsxTableModel::columnCount(const QModelIndex &parent) const
2018-07-12 18:15:23 +09:00
{
Q_UNUSED(parent)
return m_roleCount;
}
QVariant XlsxTableModel::data(const QModelIndex &index, int role) const
2018-07-12 18:15:23 +09:00
{
// current column & row
int col = index.column();
int row = index.row();
2024-02-29 21:21:47 +01:00
// check boundaries
if (col < 0 || columnCount() <= col || row < 0 || rowCount() <= row) {
qDebug() << "[Warning]"
<< " col=" << col << ", row=" << row;
2018-07-12 18:15:23 +09:00
return QVariant();
}
// Nominal case
QVariant ret;
int cmpRole;
for (int ic = 0; ic < m_the_data.size(); ic++) {
if (row == ic) {
2018-07-12 18:15:23 +09:00
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) {
2018-07-12 18:15:23 +09:00
QVariant var = vList.at(jc);
if (!var.isValid())
var.setValue(QString(""));
if (var.isNull())
var.setValue(QString(""));
2018-07-12 18:15:23 +09:00
ret = var;
}
}
}
}
qDebug() << "data: "
<< " col=" << col << ", row=" << row << ", role=" << role << ", cmpRole=" << cmpRole
<< ", value=" << ret.toString();
2018-07-12 18:15:23 +09:00
return ret;
}
2018-07-13 13:56:11 +09:00
QHash<int, QByteArray> XlsxTableModel::roleNames() const
2018-07-12 18:15:23 +09:00
{
QHash<int, QByteArray> roles;
for (int ic = 0; ic < m_roleCount; ic++) {
2018-07-12 18:15:23 +09:00
QString strRole = m_colNames.at(ic);
int roleNo = (Qt::UserRole + 1) + ic;
roles.insert(roleNo, strRole.toLatin1());
2018-07-12 18:15:23 +09:00
}
return roles;
}
// Return ordered List of user-defined roles
2018-07-13 13:56:11 +09:00
QStringList XlsxTableModel::customRoleNames()
2018-07-12 18:15:23 +09:00
{
QMap<int, QString> res;
QHashIterator<int, QByteArray> i(roleNames());
while (i.hasNext()) {
2018-07-12 18:15:23 +09:00
i.next();
if (i.key() > Qt::UserRole) {
res[i.key()] = i.value();
2018-07-12 18:15:23 +09:00
}
}
return res.values();
}