9 #ifndef JSONTREEMODEL_H 10 #define JSONTREEMODEL_H 12 #include <QAbstractItemModel> 13 #include <QJsonObject> 47 virtual QJsonValue
value()
const = 0;
59 QJsonValue
value()
const override 81 qDeleteAll(m_childList);
85 {
return m_childList[i]; }
88 {
return m_childList.count(); }
91 {
return m_childPositions.value(child, -1); }
96 QJsonValue
value()
const override;
103 QVector<JsonTreeModelNode*> m_childList;
104 QMap<JsonTreeModelNode*, int> m_childPositions;
113 {
return m_childListNodeNames[child]; }
116 {
return m_namedScalarMap.size(); }
119 {
return m_namedScalarMap[name]; }
123 Q_ASSERT(
value.type() != QJsonValue::Undefined &&
value.type() != QJsonValue::Array &&
value.type() != QJsonValue::Object);
124 m_namedScalarMap[name] =
value;
130 QJsonValue
value()
const override;
134 QMap<JsonTreeModelNode*, QString> m_childListNodeNames;
135 QMap<QString, QJsonValue> m_namedScalarMap;
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;
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
int namedScalarCount() const
Returns the number of scalar elements within this node.
Definition: jsontreemodel.h:115
JsonTreeModelNode(JsonTreeModelNode *parent)
Constructs a new node with the given parent.
Definition: jsontreemodel.h:37
QJsonValue value() const override
Returns the JSON object represented by this node.
Definition: jsontreemodel.cpp:305
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 childPosition(JsonTreeModelNode *child) const
Returns index number of the specified child, or -1 if the child does not belong to this node...
Definition: jsontreemodel.h:90
JsonTreeModelNode * parent() const
Returns this node's parent.
Definition: jsontreemodel.h:40
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
The JsonTreeModelWrapperNode class wraps a top-level JsonTreeModelNamedListNode, to allow its scalar ...
Definition: jsontreemodel.h:138
Type type() const override
Returns JsonTreeModelNode::Scalar.
Definition: jsontreemodel.h:65
QModelIndex parent(const QModelIndex &index) const override
Definition: jsontreemodel.cpp:529
~JsonTreeModelListNode() override
Definition: jsontreemodel.h:78
QJsonValue value() const override
Returns the JSON structure (array or object) represented by this node.
Definition: jsontreemodel.cpp:179
JsonTreeModelNode * childAt(int i) const
Returns the child node at index i.
Definition: jsontreemodel.h:84
Type
This enum describes the type of data represented by a JsonTreeModelNode.
Definition: jsontreemodel.h:31
Definition: jsontreemodel.h:160
JsonTreeModelScalarNode(const QJsonValue &value, JsonTreeModelNode *parent)
Constructs a node under the specified parent to represent the specified scalar value.
Definition: jsontreemodel.cpp:73
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: jsontreemodel.cpp:727
void setValue(const QJsonValue &value)
Replaces the value represented by this node.
Definition: jsontreemodel.h:62
QJsonValue value() const override
Returns the scalar JSON value represented by this node.
Definition: jsontreemodel.h:59
JsonTreeModelNamedListNode(const QJsonObject &object, JsonTreeModelNode *parent)
Constructs a node under the specified parent to represent the specified JSON object.
Definition: jsontreemodel.cpp:236
void setNamedScalarValue(const QString &name, const QJsonValue &value)
Adds or updates a scalar element of the JSON object represented by this node.
Definition: jsontreemodel.h:121
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
QString childListNodeName(JsonTreeModelNode *child) const
Definition: jsontreemodel.h:112
virtual QJsonValue value() const =0
Returns the JSON value represented by this node and its children (if any).
JsonTreeModelNamedListNode represents a JSON object.
Definition: jsontreemodel.h:107
virtual Type type() const =0
Type type() const override
Returns JsonTreeModelNode::Array.
Definition: jsontreemodel.h:93
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: jsontreemodel.cpp:660
Represents scalar JSON values (nulls, Booleans, numbers, and strings).
Definition: jsontreemodel.h:32
int childCount() const
Returns the number of child nodes under this row.
Definition: jsontreemodel.h:87
QStringList scalarColumns() const
Returns the names of the JSON objects' scalar members that are shown by the model.
Definition: jsontreemodel.h:191
JsonTreeModelScalarNode is the most basic element of a JsonTreeModel's internal data.
Definition: jsontreemodel.h:54
JsonTreeModel(QObject *parent=nullptr)
Constructs an empty JsonTreeModel with the given parent.
Definition: jsontreemodel.cpp:474
JsonTreeModelListNode represents a JSON structure (namely an array or an object) and provides the bac...
Definition: jsontreemodel.h:72
void registerChild(JsonTreeModelNode *child)
Puts the child node under this node's hierarchy.
Definition: jsontreemodel.cpp:193
void deregisterChild(JsonTreeModelNode *child)
Removes the child node from this node's hierarchy.
Definition: jsontreemodel.cpp:206
QJsonValue value() const override
Returns the JSON object represented by the wrapped node.
Definition: jsontreemodel.h:143
virtual ~JsonTreeModelNode()
Frees the memory held by this node and its children.
Definition: jsontreemodel.h:38
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
JsonTreeModelNode is the base class for the internal data structure behind JsonTreeModel.
Definition: jsontreemodel.h:28
Definition: jsontreemodel.h:161
Represents JSON objects.
Definition: jsontreemodel.h:33
QJsonValue namedScalarValue(const QString &name) const
Returns the scalar element in this node which has the given name.
Definition: jsontreemodel.h:118
JsonTreeModelWrapperNode(JsonTreeModelNamedListNode *realNode)
Constructs a wrapper for realNode and takes ownership of it.
Definition: jsontreemodel.cpp:326
Represents JSON arrays.
Definition: jsontreemodel.h:34
void setParent(JsonTreeModelNode *parent)
Makes this node a child of parent.
Definition: jsontreemodel.h:43
Type type() const override
Returns JsonTreeModelNode::Object.
Definition: jsontreemodel.h:127
JsonTreeModelListNode(JsonTreeModelNode *parent)
Constructs an empty JsonTreeModelListNode under the specified parent.
Definition: jsontreemodel.h:75
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns in the model.
Definition: jsontreemodel.cpp:580