9 #ifndef JSONTREEMODEL_H 10 #define JSONTREEMODEL_H 12 #include <QAbstractItemModel> 13 #include <QJsonObject> 28 class JsonTreeModelNode
37 JsonTreeModelNode(JsonTreeModelNode* parent) : m_parent(parent) {}
38 virtual ~JsonTreeModelNode() {}
40 inline JsonTreeModelNode* parent()
const 43 inline void setParent(JsonTreeModelNode* parent)
44 { Q_ASSERT(parent->type() != Scalar); m_parent = parent; }
46 virtual Type type()
const = 0;
47 virtual QJsonValue value()
const = 0;
51 JsonTreeModelNode* m_parent;
54 class JsonTreeModelScalarNode :
public JsonTreeModelNode
57 JsonTreeModelScalarNode(
const QJsonValue& value, JsonTreeModelNode* parent);
59 QJsonValue value()
const override 62 void setValue(
const QJsonValue& value)
65 Type type()
const override 72 class JsonTreeModelListNode :
public JsonTreeModelNode
75 JsonTreeModelListNode(JsonTreeModelNode* parent) : JsonTreeModelNode(parent) {}
76 JsonTreeModelListNode(
const QJsonArray& array, JsonTreeModelNode* parent);
78 ~JsonTreeModelListNode()
override 81 qDeleteAll(m_childList);
84 inline JsonTreeModelNode* childAt(
int i)
const 85 {
return m_childList[i]; }
87 inline int childCount()
const 88 {
return m_childList.count(); }
90 inline int childPosition(JsonTreeModelNode* child)
const 91 {
return m_childPositions.value(child, -1); }
93 Type type()
const override 96 QJsonValue value()
const override;
99 void registerChild(JsonTreeModelNode* child);
100 void deregisterChild(JsonTreeModelNode* child);
103 QVector<JsonTreeModelNode*> m_childList;
104 QMap<JsonTreeModelNode*, int> m_childPositions;
107 class JsonTreeModelNamedListNode :
public JsonTreeModelListNode
110 JsonTreeModelNamedListNode(
const QJsonObject&
object, JsonTreeModelNode* parent);
112 inline QString childListNodeName(JsonTreeModelNode* child)
const 113 {
return m_childListNodeNames[child]; }
115 inline int namedScalarCount()
const 116 {
return m_namedScalarMap.size(); }
118 inline QJsonValue namedScalarValue(
const QString& name)
const 119 {
return m_namedScalarMap[name]; }
121 inline void setNamedScalarValue(
const QString& name,
const QJsonValue& value)
123 Q_ASSERT(value.type() != QJsonValue::Undefined && value.type() != QJsonValue::Array && value.type() != QJsonValue::Object);
124 m_namedScalarMap[name] = value;
127 Type type()
const override 130 QJsonValue value()
const override;
134 QMap<JsonTreeModelNode*, QString> m_childListNodeNames;
135 QMap<QString, QJsonValue> m_namedScalarMap;
138 class JsonTreeModelWrapperNode :
public JsonTreeModelListNode
141 JsonTreeModelWrapperNode(JsonTreeModelNamedListNode* realNode);
143 QJsonValue value()
const override 144 {
return childAt(0)->value(); }
168 QVariant
headerData(
int section, Qt::Orientation orientation,
int role = Qt::DisplayRole)
const override;
171 QModelIndex
index(
int row,
int column,
const QModelIndex&
parent = QModelIndex())
const override;
172 QModelIndex
parent(
const QModelIndex&
index)
const override;
174 int rowCount(
const QModelIndex&
parent = QModelIndex())
const override;
177 QVariant
data(
const QModelIndex&
index,
int role = Qt::DisplayRole)
const override;
180 bool setData(
const QModelIndex&
index,
const QVariant& value,
int role = Qt::EditRole)
override;
181 Qt::ItemFlags
flags(
const QModelIndex&
index)
const override;
186 QJsonValue
json(
const QModelIndex&
index = QModelIndex())
const;
194 bool isEditable(
const QModelIndex&
index)
const;
196 JsonTreeModelListNode* m_rootNode;
197 QStringList m_headers;
200 #endif // JSONTREEMODEL_H The JsonTreeModel class provides a data model for a JSON document.
Definition: jsontreemodel.h:151
QJsonValue json(const QModelIndex &index=QModelIndex()) const
Returns the JSON value under the given index.
Definition: jsontreemodel.cpp:805
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns data under the given index for the specified role.
Definition: jsontreemodel.cpp:602
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
Definition: jsontreemodel.cpp:558
ScalarColumnSearchMode
This enum controls how setJson() updates the model's column headers.
Definition: jsontreemodel.h:157
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: jsontreemodel.cpp:491
Definition: jsontreemodel.h:159
~JsonTreeModel() override
Destroys the JsonTreeModel and frees its memory.
Definition: jsontreemodel.h:165
QModelIndex parent(const QModelIndex &index) const override
Definition: jsontreemodel.cpp:529
Definition: jsontreemodel.h:160
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: jsontreemodel.cpp:727
void setJson(const QJsonArray &array, ScalarColumnSearchMode searchMode=QuickSearch)
Sets the whole model's internal data structure to the given JSON array.
Definition: jsontreemodel.cpp:846
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: jsontreemodel.cpp:660
QStringList scalarColumns() const
Returns the names of the JSON objects' scalar members that are shown by the model.
Definition: jsontreemodel.h:191
JsonTreeModel(QObject *parent=nullptr)
Constructs an empty JsonTreeModel with the given parent.
Definition: jsontreemodel.cpp:474
void setScalarColumns(const QStringList &columns)
Sets the JSON objects' scalar members that are shown by the model.
Definition: jsontreemodel.cpp:918
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: jsontreemodel.cpp:505
Definition: jsontreemodel.h:161
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns in the model.
Definition: jsontreemodel.cpp:580