Give each class a separate header; hide QJsonItem behind include/details folder

This commit is contained in:
S David 2024-05-03 23:07:17 -04:00
parent 257a9be149
commit c728cdc8ca
5 changed files with 313 additions and 248 deletions

View File

@ -61,6 +61,7 @@ endif()
qt_add_library(QJsonModel OBJECT qt_add_library(QJsonModel OBJECT
QJsonModel.cpp QJsonModel.cpp
QJsonTreeItem.cpp
) )

View File

@ -25,215 +25,12 @@
*/ */
#include "QJsonModel.hpp" #include "QJsonModel.hpp"
#include "details/QJsonTreeItem.hpp"
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QFont> #include <QFont>
inline bool contains(const QStringList& list, const QString& value)
{
for (auto val : list)
if (value.contains(val, Qt::CaseInsensitive))
return true;
return false;
}
QJsonTreeItem::QJsonTreeItem(QJsonTreeItem* parent)
{
mParent = parent;
}
QJsonTreeItem::~QJsonTreeItem()
{
qDeleteAll(mChilds);
}
void QJsonTreeItem::appendChild(QJsonTreeItem* item)
{
mChilds.append(item);
}
QJsonTreeItem* QJsonTreeItem::child(int row)
{
return mChilds.value(row);
}
QJsonTreeItem* QJsonTreeItem::parent()
{
return mParent;
}
int QJsonTreeItem::childCount() const
{
return mChilds.count();
}
int QJsonTreeItem::row() const
{
if (mParent)
return mParent->mChilds.indexOf(const_cast<QJsonTreeItem*>(this)
);
return 0;
}
void QJsonTreeItem::setKey(const QString& key)
{
mKey = key;
}
void QJsonTreeItem::setValue(const QVariant& value)
{
mValue = value;
}
void QJsonTreeItem::setType(const QJsonValue::Type& type)
{
mType = type;
}
QString QJsonTreeItem::key() const
{
return mKey;
}
QVariant QJsonTreeItem::value() const
{
return mValue;
}
QJsonValue::Type QJsonTreeItem::type() const
{
return mType;
}
QJsonTreeItem* QJsonTreeItem::load(
const QJsonValue& value, const QStringList& exceptions, QJsonTreeItem* parent
)
{
QJsonTreeItem* rootItem = new QJsonTreeItem(parent);
rootItem->setKey("root");
if (value.isObject()) {
// Get all QJsonValue childs
const QStringList keys =
value.toObject().keys(); // To prevent clazy-range warning
for (const QString& key : keys) {
if (contains(exceptions, key)) {
continue;
}
QJsonValue jsonValue = value.toObject().value(key);
QJsonTreeItem* child =
load(jsonValue, exceptions, rootItem);
child->setKey(key);
child->setType(jsonValue.type());
rootItem->appendChild(child);
}
} else if (value.isArray()) {
// Get all QJsonValue childs
int index = 0;
const QJsonArray array =
value.toArray(); // To prevent clazy-range warning
for (const QJsonValue& jsonValue : array) {
QJsonTreeItem* child =
load(jsonValue, exceptions, rootItem);
child->setKey(QString::number(index));
child->setType(jsonValue.type());
rootItem->appendChild(child);
++index;
}
} else {
rootItem->setValue(value.toVariant());
rootItem->setType(value.type());
}
return rootItem;
}
//=========================================================================
inline uchar hexdig(uint positiveValue)
{
return (
positiveValue < 0xa ? '0' + positiveValue : 'a' + positiveValue - 0xa
);
}
QByteArray escapedString(const QString& text)
{
QByteArray byteArray(text.length(), Qt::Uninitialized);
uchar* cursor =
reinterpret_cast<uchar*>(const_cast<char*>(byteArray.constData()));
const uchar* byteArrayEnd = cursor + byteArray.length();
const ushort* src = reinterpret_cast<const ushort*>(text.constBegin());
const ushort* const end =
reinterpret_cast<const ushort*>(text.constEnd());
while (src != end) {
if (cursor >= byteArrayEnd - 6) {
// ensure we have enough space
int pos = cursor - reinterpret_cast<const uchar*>(
byteArray.constData()
);
byteArray.resize(byteArray.size() * 2);
cursor =
reinterpret_cast<uchar*>(byteArray.data()) + pos;
byteArrayEnd =
reinterpret_cast<const uchar*>(byteArray.constData()
) +
byteArray.length();
}
uint uValue = *src++; // uValue = unsigned value
if (uValue < 0x80) {
if (uValue < 0x20 || uValue == 0x22 || uValue == 0x5c) {
*cursor++ = '\\';
switch (uValue) {
case 0x22:
*cursor++ = '"';
break;
case 0x5c:
*cursor++ = '\\';
break;
case 0x8:
*cursor++ = 'b';
break;
case 0xc:
*cursor++ = 'f';
break;
case 0xa:
*cursor++ = 'n';
break;
case 0xd:
*cursor++ = 'r';
break;
case 0x9:
*cursor++ = 't';
break;
default:
*cursor++ = 'u';
*cursor++ = '0';
*cursor++ = '0';
*cursor++ = hexdig(uValue >> 4);
*cursor++ = hexdig(uValue & 0xf);
}
} else {
*cursor++ = static_cast<uchar>(uValue);
}
} else if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(uValue, cursor, src, end) < 0) {
// failed to get valid utf8 use JSON escape sequence
*cursor++ = '\\';
*cursor++ = 'u';
*cursor++ = hexdig(uValue >> 12 & 0x0f);
*cursor++ = hexdig(uValue >> 8 & 0x0f);
*cursor++ = hexdig(uValue >> 4 & 0x0f);
*cursor++ = hexdig(uValue & 0x0f);
}
}
byteArray.resize(
cursor - reinterpret_cast<const uchar*>(byteArray.constData())
);
return byteArray;
}
QJsonModel::QJsonModel(QObject* parent) QJsonModel::QJsonModel(QObject* parent)
: QAbstractItemModel(parent), mRootItem{ new QJsonTreeItem } : QAbstractItemModel(parent), mRootItem{ new QJsonTreeItem }
{ {
@ -585,33 +382,33 @@ QJsonValue QJsonModel::genJson(QJsonTreeItem* item) const
int nchild = item->childCount(); int nchild = item->childCount();
if (QJsonValue::Object == type) { if (QJsonValue::Object == type) {
QJsonObject jo; QJsonObject jsonObj;
for (int i = 0; i < nchild; ++i) { for (int i = 0; i < nchild; ++i) {
auto ch = item->child(i); auto child = item->child(i);
auto key = ch->key(); auto key = child->key();
jo.insert(key, genJson(ch)); jsonObj.insert(key, genJson(child));
} }
return jo; return jsonObj;
} else if (QJsonValue::Array == type) { } else if (QJsonValue::Array == type) {
QJsonArray arr; QJsonArray arr;
for (int i = 0; i < nchild; ++i) { for (int i = 0; i < nchild; ++i) {
auto ch = item->child(i); auto child = item->child(i);
arr.append(genJson(ch)); arr.append(genJson(child));
} }
return arr; return arr;
} else { } else {
QJsonValue va; QJsonValue value;
switch (item->value().typeId()) { switch (item->value().typeId()) {
case QMetaType::Bool: { case QMetaType::Bool: {
va = item->value().toBool(); value = item->value().toBool();
break; break;
} }
default: default:
va = item->value().toString(); value = item->value().toString();
break; break;
} }
(item->value()); (item->value());
return va; return value;
} }
} }
// clang-format off // clang-format off

149
QJsonTreeItem.cpp Normal file
View File

@ -0,0 +1,149 @@
/* QJsontreeItem.cpp
*
* Copyright (c) 2011 SCHUTZ Sacha
* Copyright © 2024 Saul D. Beniquez
*
* License: The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "details/QJsonTreeItem.hpp"
QJsonTreeItem::QJsonTreeItem(QJsonTreeItem* parent)
{
mParent = parent;
}
QJsonTreeItem::~QJsonTreeItem()
{
qDeleteAll(mChilds);
}
void QJsonTreeItem::appendChild(QJsonTreeItem* item)
{
mChilds.append(item);
}
QJsonTreeItem* QJsonTreeItem::child(int row)
{
return mChilds.value(row);
}
QJsonTreeItem* QJsonTreeItem::parent()
{
return mParent;
}
int QJsonTreeItem::childCount() const
{
return mChilds.count();
}
int QJsonTreeItem::row() const
{
if (mParent)
return mParent->mChilds.indexOf(const_cast<QJsonTreeItem*>(this)
);
return 0;
}
void QJsonTreeItem::setKey(const QString& key)
{
mKey = key;
}
void QJsonTreeItem::setValue(const QVariant& value)
{
mValue = value;
}
void QJsonTreeItem::setType(const QJsonValue::Type& type)
{
mType = type;
}
QString QJsonTreeItem::key() const
{
return mKey;
}
QVariant QJsonTreeItem::value() const
{
return mValue;
}
QJsonValue::Type QJsonTreeItem::type() const
{
return mType;
}
QJsonTreeItem* QJsonTreeItem::load(
const QJsonValue& value, const QStringList& exceptions, QJsonTreeItem* parent
)
{
QJsonTreeItem* rootItem = new QJsonTreeItem(parent);
rootItem->setKey("root");
auto contains = [](const QStringList& list, const QString& value) {
for (auto val : list) {
if (value.contains(val, Qt::CaseInsensitive)) {
return true;
}
}
return false;
};
if (value.isObject()) {
// Get all QJsonValue childs
const QStringList keys =
value.toObject().keys(); // To prevent clazy-range warning
for (const QString& key : keys) {
if (contains(exceptions, key)) {
continue;
}
QJsonValue jsonValue = value.toObject().value(key);
QJsonTreeItem* child =
load(jsonValue, exceptions, rootItem);
child->setKey(key);
child->setType(jsonValue.type());
rootItem->appendChild(child);
}
} else if (value.isArray()) {
// Get all QJsonValue childs
int index = 0;
const QJsonArray kArray =
value.toArray(); // To prevent clazy-range warning
for (const QJsonValue& jsonValue : kArray) {
QJsonTreeItem* child =
load(jsonValue, exceptions, rootItem);
child->setKey(QString::number(index));
child->setType(jsonValue.type());
rootItem->appendChild(child);
++index;
}
} else {
rootItem->setValue(value.toVariant());
rootItem->setType(value.type());
}
return rootItem;
}

View File

@ -36,38 +36,7 @@
class QJsonModel; class QJsonModel;
class QJsonItem; class QJsonItem;
class QJsonTreeItem;
class QJsonTreeItem {
public:
QJsonTreeItem(QJsonTreeItem* parent = nullptr);
~QJsonTreeItem();
void appendChild(QJsonTreeItem* item);
QJsonTreeItem* child(int row);
QJsonTreeItem* parent();
int childCount() const;
int row() const;
void setKey(const QString& key);
void setValue(const QVariant& value);
void setType(const QJsonValue::Type& type);
QString key() const;
QVariant value() const;
QJsonValue::Type type() const;
static QJsonTreeItem* load(
const QJsonValue& value, const QStringList& exceptions = {},
QJsonTreeItem* parent = nullptr
);
protected:
private:
QString mKey;
QVariant mValue;
QJsonValue::Type mType;
QList<QJsonTreeItem*> mChilds;
QJsonTreeItem* mParent = nullptr;
};
//---------------------------------------------------
class QJsonModel : public QAbstractItemModel { class QJsonModel : public QAbstractItemModel {
// NOLINTNEXTLINE // NOLINTNEXTLINE
@ -125,5 +94,88 @@ class QJsonModel : public QAbstractItemModel {
QStringList mExceptions; QStringList mExceptions;
}; };
inline uchar hexdig(uint positiveValue)
{
return (
positiveValue < 0xa ? '0' + positiveValue : 'a' + positiveValue - 0xa
);
}
inline QByteArray escapedString(const QString& text)
{
QByteArray byteArray(text.length(), Qt::Uninitialized);
uchar* cursor =
reinterpret_cast<uchar*>(const_cast<char*>(byteArray.constData()));
const uchar* byteArrayEnd = cursor + byteArray.length();
const ushort* src = reinterpret_cast<const ushort*>(text.constBegin());
const ushort* const kend =
reinterpret_cast<const ushort*>(text.constEnd());
while (src != kend) {
if (cursor >= byteArrayEnd - 6) {
// ensure we have enough space
int pos = cursor - reinterpret_cast<const uchar*>(
byteArray.constData()
);
byteArray.resize(byteArray.size() * 2);
cursor =
reinterpret_cast<uchar*>(byteArray.data()) + pos;
byteArrayEnd =
reinterpret_cast<const uchar*>(byteArray.constData()
) +
byteArray.length();
}
uint uValue = *src++; // uValue = unsigned value
if (uValue < 0x80) {
if (uValue < 0x20 || uValue == 0x22 || uValue == 0x5c) {
*cursor++ = '\\';
switch (uValue) {
case 0x22:
*cursor++ = '"';
break;
case 0x5c:
*cursor++ = '\\';
break;
case 0x8:
*cursor++ = 'b';
break;
case 0xc:
*cursor++ = 'f';
break;
case 0xa:
*cursor++ = 'n';
break;
case 0xd:
*cursor++ = 'r';
break;
case 0x9:
*cursor++ = 't';
break;
default:
*cursor++ = 'u';
*cursor++ = '0';
*cursor++ = '0';
*cursor++ = hexdig(uValue >> 4);
*cursor++ = hexdig(uValue & 0xf);
}
} else {
*cursor++ = static_cast<uchar>(uValue);
}
} else if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(uValue, cursor, src, kend) < 0) {
// failed to get valid utf8 use JSON escape sequence
*cursor++ = '\\';
*cursor++ = 'u';
*cursor++ = hexdig(uValue >> 12 & 0x0f);
*cursor++ = hexdig(uValue >> 8 & 0x0f);
*cursor++ = hexdig(uValue >> 4 & 0x0f);
*cursor++ = hexdig(uValue & 0x0f);
}
}
byteArray.resize(
cursor - reinterpret_cast<const uchar*>(byteArray.constData())
);
return byteArray;
}
// clang-format off // clang-format off
// vim: set foldmethod=syntax foldminlines=10 textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen : // vim: set foldmethod=syntax foldminlines=10 textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -0,0 +1,66 @@
/* QJSonTreeItem.hpp
*
* Copyright (c) 2011 SCHUTZ Sacha
* Copyright © 2024 Saul D. Beniquez
*
* License:
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <QAbstractItemModel>
#include <QIcon>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
class QJsonTreeItem {
public:
QJsonTreeItem(QJsonTreeItem* parent = nullptr);
~QJsonTreeItem();
void appendChild(QJsonTreeItem* item);
QJsonTreeItem* child(int row);
QJsonTreeItem* parent();
int childCount() const;
int row() const;
void setKey(const QString& key);
void setValue(const QVariant& value);
void setType(const QJsonValue::Type& type);
QString key() const;
QVariant value() const;
QJsonValue::Type type() const;
static QJsonTreeItem* load(
const QJsonValue& value, const QStringList& exceptions = {},
QJsonTreeItem* parent = nullptr
);
protected:
private:
QString mKey;
QVariant mValue;
QJsonValue::Type mType;
QList<QJsonTreeItem*> mChilds;
QJsonTreeItem* mParent = nullptr;
};
// clang-format off
// vim: set foldmethod=syntax foldminlines=10 textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :