-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQJsonModel.hpp
More file actions
86 lines (69 loc) · 2.45 KB
/
QJsonModel.hpp
File metadata and controls
86 lines (69 loc) · 2.45 KB
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
/*
** Copyright 2017 ViVoka
**
** Made by leroy_v
** Mail <leroy_v@vivoka.com>
**
** vivoka.com
*/
#pragma once
// Qt includes -----------------------------------------------------------------
#include <QAbstractItemModel>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QIcon>
// Qt forward declarations -----------------------------------------------------
QT_BEGIN_NAMESPACE
class QJsonModel;
class QJsonItem;
QT_END_NAMESPACE
class QJsonTreeItem
{
public:
QJsonTreeItem(QJsonTreeItem * parent = nullptr);
~QJsonTreeItem();
void appendChild(QJsonTreeItem * item);
QJsonTreeItem * child(int row);
QJsonTreeItem * parent();
void setKey(const QString & key);
void setValue(const QString & value);
void setType(const QJsonValue::Type & type);
int childCount() const { return _childs.count(); }
int row() const { return _parent ? _parent->_childs.indexOf(const_cast<QJsonTreeItem *>(this)) : 0; }
QString key() const { return _key; }
QString value() const { return _value; }
QJsonValue::Type type() const { return _type; }
public:
static QJsonTreeItem * load(const QJsonValue & value,
QJsonTreeItem * parent = nullptr);
static QString jsonTypeToString(QJsonValue::Type type);
private:
QString _key;
QString _value;
QJsonValue::Type _type;
QList<QJsonTreeItem *> _childs;
QJsonTreeItem * _parent;
};
//---------------------------------------------------
class QJsonModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit QJsonModel(QObject * parent = nullptr);
bool load(const QString & fileName);
bool load(QIODevice * device);
bool loadJson(const QByteArray & json);
QVariant data(const QModelIndex & index, int role) const override;
QVariant headerData(int, Qt::Orientation, int) const override;
QModelIndex index(int row, int column,const QModelIndex & parent = {}) const override;
QModelIndex parent(const QModelIndex & index) const override;
bool setData(const QModelIndex & index, const QVariant & value, int role) override;
int rowCount(const QModelIndex & parent = {}) const override;
int columnCount(const QModelIndex & = {}) const override;
Qt::ItemFlags flags(const QModelIndex & index) const override;
private:
QJsonTreeItem * _rootItem;
QJsonDocument _document;
};