0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-23 16:22:58 +03:00
LimeReport/lrdview/XmlModel.cpp

72 lines
1.8 KiB
C++
Raw Normal View History

2021-09-11 19:03:57 +03:00
#include "XmlModel.h"
2021-09-11 19:03:57 +03:00
#include <QDebug>
XmlModel::XmlModel(QByteArray* data)
{
2021-09-11 19:03:57 +03:00
if (data) {
m_doc.setContent(*data);
initModel();
}
}
void XmlModel::setXMLData(QByteArray* data)
{
2021-09-11 19:03:57 +03:00
if (data) {
beginResetModel();
m_doc.setContent(*data);
initModel();
endResetModel();
}
}
void XmlModel::initModel()
{
2021-09-11 19:03:57 +03:00
m_items = m_doc.firstChildElement("items");
parseHeaders();
}
void XmlModel::parseHeaders()
{
m_fields.clear();
QDomNode root = m_doc.firstChildElement("items");
QDomNode item = root.firstChild();
for (int i = 0; i < item.childNodes().count(); ++i) {
2021-09-11 19:03:57 +03:00
QDomNode attr = item.childNodes().item(i);
m_fields.append(attr.nodeName());
2021-09-11 19:03:57 +03:00
}
}
QModelIndex XmlModel::index(int row, int column, const QModelIndex& parent) const
2021-09-11 19:03:57 +03:00
{
if (m_fields.isEmpty())
return QModelIndex();
return createIndex(row, column);
}
QModelIndex XmlModel::parent(const QModelIndex& child) const { return QModelIndex(); }
2021-09-11 19:03:57 +03:00
int XmlModel::rowCount(const QModelIndex& parent) const { return m_items.childNodes().count(); }
2021-09-11 19:03:57 +03:00
int XmlModel::columnCount(const QModelIndex& parent) const { return m_fields.count(); }
2021-09-11 19:03:57 +03:00
QVariant XmlModel::data(const QModelIndex& index, int role) const
2021-09-11 19:03:57 +03:00
{
if (role == Qt::DisplayRole) {
if (index.isValid()) {
2021-09-11 19:03:57 +03:00
QDomNode data = m_items.childNodes().at(index.row()).childNodes().item(index.column());
return data.toElement().text();
} else
return QVariant();
} else
return QVariant();
2021-09-11 19:03:57 +03:00
}
QVariant XmlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
2021-09-11 19:03:57 +03:00
return m_fields[section];
} else
return QVariant();
2021-09-11 19:03:57 +03:00
}