mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-18 04:54:38 +03:00
chore: re-format code
This commit is contained in:
@@ -27,107 +27,86 @@
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
|
||||
QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent)
|
||||
{
|
||||
QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent) {
|
||||
mParent = parent;
|
||||
}
|
||||
|
||||
QJsonTreeItem::~QJsonTreeItem()
|
||||
{
|
||||
QJsonTreeItem::~QJsonTreeItem() {
|
||||
qDeleteAll(mChilds);
|
||||
}
|
||||
|
||||
void QJsonTreeItem::appendChild(QJsonTreeItem *item)
|
||||
{
|
||||
void QJsonTreeItem::appendChild(QJsonTreeItem *item) {
|
||||
mChilds.append(item);
|
||||
}
|
||||
|
||||
QJsonTreeItem *QJsonTreeItem::child(int row)
|
||||
{
|
||||
QJsonTreeItem *QJsonTreeItem::child(int row) {
|
||||
return mChilds.value(row);
|
||||
}
|
||||
|
||||
QJsonTreeItem *QJsonTreeItem::parent()
|
||||
{
|
||||
QJsonTreeItem *QJsonTreeItem::parent() {
|
||||
return mParent;
|
||||
}
|
||||
|
||||
int QJsonTreeItem::childCount() const
|
||||
{
|
||||
int QJsonTreeItem::childCount() const {
|
||||
return mChilds.count();
|
||||
}
|
||||
|
||||
int QJsonTreeItem::row() const
|
||||
{
|
||||
int QJsonTreeItem::row() const {
|
||||
if (mParent)
|
||||
return mParent->mChilds.indexOf(const_cast<QJsonTreeItem *>(this));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void QJsonTreeItem::setKey(const QString &key)
|
||||
{
|
||||
void QJsonTreeItem::setKey(const QString &key) {
|
||||
mKey = key;
|
||||
}
|
||||
|
||||
void QJsonTreeItem::setValue(const QString &value)
|
||||
{
|
||||
void QJsonTreeItem::setValue(const QString &value) {
|
||||
mValue = value;
|
||||
}
|
||||
|
||||
void QJsonTreeItem::setType(const QJsonValue::Type &type)
|
||||
{
|
||||
void QJsonTreeItem::setType(const QJsonValue::Type &type) {
|
||||
mType = type;
|
||||
}
|
||||
|
||||
QString QJsonTreeItem::key() const
|
||||
{
|
||||
QString QJsonTreeItem::key() const {
|
||||
return mKey;
|
||||
}
|
||||
|
||||
QString QJsonTreeItem::value() const
|
||||
{
|
||||
QString QJsonTreeItem::value() const {
|
||||
return mValue;
|
||||
}
|
||||
|
||||
QJsonValue::Type QJsonTreeItem::type() const
|
||||
{
|
||||
QJsonValue::Type QJsonTreeItem::type() const {
|
||||
return mType;
|
||||
}
|
||||
|
||||
QJsonTreeItem *QJsonTreeItem::load(const QJsonValue &value, QJsonTreeItem *parent)
|
||||
{
|
||||
QJsonTreeItem *QJsonTreeItem::load(const QJsonValue &value, QJsonTreeItem *parent) {
|
||||
QJsonTreeItem *rootItem = new QJsonTreeItem(parent);
|
||||
rootItem->setKey("root");
|
||||
|
||||
if (value.isObject())
|
||||
{
|
||||
if (value.isObject()) {
|
||||
// Get all QJsonValue childs
|
||||
for (QString key : value.toObject().keys())
|
||||
{
|
||||
for (QString key: value.toObject().keys()) {
|
||||
QJsonValue v = value.toObject().value(key);
|
||||
QJsonTreeItem *child = load(v, rootItem);
|
||||
child->setKey(key);
|
||||
child->setType(v.type());
|
||||
rootItem->appendChild(child);
|
||||
}
|
||||
}
|
||||
else if (value.isArray())
|
||||
{
|
||||
} else if (value.isArray()) {
|
||||
// Get all QJsonValue childs
|
||||
int index = 0;
|
||||
|
||||
for (QJsonValue v : value.toArray())
|
||||
{
|
||||
for (QJsonValue v: value.toArray()) {
|
||||
QJsonTreeItem *child = load(v, rootItem);
|
||||
child->setKey(QString::number(index));
|
||||
child->setType(v.type());
|
||||
rootItem->appendChild(child);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
rootItem->setValue(value.toVariant().toString());
|
||||
rootItem->setType(value.type());
|
||||
}
|
||||
@@ -137,75 +116,61 @@ QJsonTreeItem *QJsonTreeItem::load(const QJsonValue &value, QJsonTreeItem *paren
|
||||
|
||||
//=========================================================================
|
||||
|
||||
QJsonModel::QJsonModel(QObject *parent) : QAbstractItemModel(parent), mRootItem{ new QJsonTreeItem }
|
||||
{
|
||||
QJsonModel::QJsonModel(QObject *parent) : QAbstractItemModel(parent), mRootItem{new QJsonTreeItem} {
|
||||
mHeaders.append("key");
|
||||
mHeaders.append("value");
|
||||
}
|
||||
|
||||
QJsonModel::QJsonModel(const QString &fileName, QObject *parent) : QAbstractItemModel(parent), mRootItem{ new QJsonTreeItem }
|
||||
{
|
||||
QJsonModel::QJsonModel(const QString &fileName, QObject *parent) : QAbstractItemModel(parent), mRootItem{new QJsonTreeItem} {
|
||||
mHeaders.append("key");
|
||||
mHeaders.append("value");
|
||||
load(fileName);
|
||||
}
|
||||
|
||||
QJsonModel::QJsonModel(QIODevice *device, QObject *parent) : QAbstractItemModel(parent), mRootItem{ new QJsonTreeItem }
|
||||
{
|
||||
QJsonModel::QJsonModel(QIODevice *device, QObject *parent) : QAbstractItemModel(parent), mRootItem{new QJsonTreeItem} {
|
||||
mHeaders.append("key");
|
||||
mHeaders.append("value");
|
||||
load(device);
|
||||
}
|
||||
|
||||
QJsonModel::QJsonModel(const QByteArray &json, QObject *parent) : QAbstractItemModel(parent), mRootItem{ new QJsonTreeItem }
|
||||
{
|
||||
QJsonModel::QJsonModel(const QByteArray &json, QObject *parent) : QAbstractItemModel(parent), mRootItem{new QJsonTreeItem} {
|
||||
mHeaders.append("key");
|
||||
mHeaders.append("value");
|
||||
loadJson(json);
|
||||
}
|
||||
|
||||
QJsonModel::~QJsonModel()
|
||||
{
|
||||
QJsonModel::~QJsonModel() {
|
||||
delete mRootItem;
|
||||
}
|
||||
|
||||
bool QJsonModel::load(const QString &fileName)
|
||||
{
|
||||
bool QJsonModel::load(const QString &fileName) {
|
||||
QFile file(fileName);
|
||||
bool success = false;
|
||||
|
||||
if (file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
success = load(&file);
|
||||
file.close();
|
||||
}
|
||||
else
|
||||
} else
|
||||
success = false;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool QJsonModel::load(QIODevice *device)
|
||||
{
|
||||
bool QJsonModel::load(QIODevice *device) {
|
||||
return loadJson(device->readAll());
|
||||
}
|
||||
|
||||
bool QJsonModel::loadJson(const QByteArray &json)
|
||||
{
|
||||
bool QJsonModel::loadJson(const QByteArray &json) {
|
||||
auto const &jdoc = QJsonDocument::fromJson(json);
|
||||
|
||||
if (!jdoc.isNull())
|
||||
{
|
||||
if (!jdoc.isNull()) {
|
||||
beginResetModel();
|
||||
delete mRootItem;
|
||||
|
||||
if (jdoc.isArray())
|
||||
{
|
||||
if (jdoc.isArray()) {
|
||||
mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.array()));
|
||||
mRootItem->setType(QJsonValue::Array);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.object()));
|
||||
mRootItem->setType(QJsonValue::Object);
|
||||
}
|
||||
@@ -218,25 +183,20 @@ bool QJsonModel::loadJson(const QByteArray &json)
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariant QJsonModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
QVariant QJsonModel::data(const QModelIndex &index, int role) const {
|
||||
if (!index.isValid())
|
||||
return QVariant();
|
||||
|
||||
QJsonTreeItem *item = static_cast<QJsonTreeItem *>(index.internalPointer());
|
||||
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (index.column() == 0)
|
||||
return QString("%1").arg(item->key());
|
||||
|
||||
if (index.column() == 1)
|
||||
return QString("%1").arg(item->value());
|
||||
}
|
||||
else if (Qt::EditRole == role)
|
||||
{
|
||||
if (index.column() == 1)
|
||||
{
|
||||
} else if (Qt::EditRole == role) {
|
||||
if (index.column() == 1) {
|
||||
return QString("%1").arg(item->value());
|
||||
}
|
||||
}
|
||||
@@ -244,17 +204,14 @@ QVariant QJsonModel::data(const QModelIndex &index, int role) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool QJsonModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
bool QJsonModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
||||
int col = index.column();
|
||||
|
||||
if (Qt::EditRole == role)
|
||||
{
|
||||
if (col == 1)
|
||||
{
|
||||
if (Qt::EditRole == role) {
|
||||
if (col == 1) {
|
||||
QJsonTreeItem *item = static_cast<QJsonTreeItem *>(index.internalPointer());
|
||||
item->setValue(value.toString());
|
||||
emit dataChanged(index, index, { Qt::EditRole });
|
||||
emit dataChanged(index, index, {Qt::EditRole});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -262,21 +219,17 @@ bool QJsonModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariant QJsonModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
QVariant QJsonModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||
if (role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
|
||||
if (orientation == Qt::Horizontal)
|
||||
{
|
||||
if (orientation == Qt::Horizontal) {
|
||||
return mHeaders.value(section);
|
||||
}
|
||||
else
|
||||
} else
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QModelIndex QJsonModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
QModelIndex QJsonModel::index(int row, int column, const QModelIndex &parent) const {
|
||||
if (!hasIndex(row, column, parent))
|
||||
return QModelIndex();
|
||||
|
||||
@@ -295,8 +248,7 @@ QModelIndex QJsonModel::index(int row, int column, const QModelIndex &parent) co
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
QModelIndex QJsonModel::parent(const QModelIndex &index) const
|
||||
{
|
||||
QModelIndex QJsonModel::parent(const QModelIndex &index) const {
|
||||
if (!index.isValid())
|
||||
return QModelIndex();
|
||||
|
||||
@@ -309,8 +261,7 @@ QModelIndex QJsonModel::parent(const QModelIndex &index) const
|
||||
return createIndex(parentItem->row(), 0, parentItem);
|
||||
}
|
||||
|
||||
int QJsonModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
int QJsonModel::rowCount(const QModelIndex &parent) const {
|
||||
QJsonTreeItem *parentItem;
|
||||
|
||||
if (parent.column() > 0)
|
||||
@@ -324,78 +275,61 @@ int QJsonModel::rowCount(const QModelIndex &parent) const
|
||||
return parentItem->childCount();
|
||||
}
|
||||
|
||||
int QJsonModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
int QJsonModel::columnCount(const QModelIndex &parent) const {
|
||||
Q_UNUSED(parent)
|
||||
return 2;
|
||||
}
|
||||
|
||||
Qt::ItemFlags QJsonModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags QJsonModel::flags(const QModelIndex &index) const {
|
||||
int col = index.column();
|
||||
auto item = static_cast<QJsonTreeItem *>(index.internalPointer());
|
||||
auto isArray = QJsonValue::Array == item->type();
|
||||
auto isObject = QJsonValue::Object == item->type();
|
||||
|
||||
if ((col == 1) && !(isArray || isObject))
|
||||
{
|
||||
if ((col == 1) && !(isArray || isObject)) {
|
||||
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return QAbstractItemModel::flags(index);
|
||||
}
|
||||
}
|
||||
|
||||
QJsonDocument QJsonModel::json() const
|
||||
{
|
||||
QJsonDocument QJsonModel::json() const {
|
||||
auto v = genJson(mRootItem);
|
||||
QJsonDocument doc;
|
||||
|
||||
if (v.isObject())
|
||||
{
|
||||
if (v.isObject()) {
|
||||
doc = QJsonDocument(v.toObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
doc = QJsonDocument(v.toArray());
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
QJsonValue QJsonModel::genJson(QJsonTreeItem *item) const
|
||||
{
|
||||
QJsonValue QJsonModel::genJson(QJsonTreeItem *item) const {
|
||||
auto type = item->type();
|
||||
int nchild = item->childCount();
|
||||
|
||||
if (QJsonValue::Object == type)
|
||||
{
|
||||
if (QJsonValue::Object == type) {
|
||||
QJsonObject jo;
|
||||
|
||||
for (int i = 0; i < nchild; ++i)
|
||||
{
|
||||
for (int i = 0; i < nchild; ++i) {
|
||||
auto ch = item->child(i);
|
||||
auto key = ch->key();
|
||||
jo.insert(key, genJson(ch));
|
||||
}
|
||||
|
||||
return jo;
|
||||
}
|
||||
else if (QJsonValue::Array == type)
|
||||
{
|
||||
} else if (QJsonValue::Array == type) {
|
||||
QJsonArray arr;
|
||||
|
||||
for (int i = 0; i < nchild; ++i)
|
||||
{
|
||||
for (int i = 0; i < nchild; ++i) {
|
||||
auto ch = item->child(i);
|
||||
arr.append(genJson(ch));
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
QJsonValue va(item->value());
|
||||
return va;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user