Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstandarditemmodel_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSTANDARDITEMMODEL_P_H
6#define QSTANDARDITEMMODEL_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of other Qt classes. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtGui/qstandarditemmodel.h>
20
21#include <QtGui/private/qtguiglobal_p.h>
22#include "private/qabstractitemmodel_p.h"
23
24#include <QtCore/qlist.h>
25#include <QtCore/qstack.h>
26#include <QtCore/qvariant.h>
27#include <QtCore/qdebug.h>
28
30
31QT_BEGIN_NAMESPACE
32
33class QStandardItemData
34{
35public:
36 inline QStandardItemData() : role(-1) {}
37 inline QStandardItemData(int r, const QVariant &v) :
38 role(r == Qt::EditRole ? Qt::DisplayRole : r), value(v) {}
39 inline QStandardItemData(const std::pair<const int&, const QVariant&> &p) :
40 role(p.first == Qt::EditRole ? Qt::DisplayRole : p.first), value(p.second) {}
41 int role;
42 QVariant value;
43 inline bool operator==(const QStandardItemData &other) const { return role == other.role && value == other.value; }
44};
46
47#ifndef QT_NO_DATASTREAM
48
49inline QDataStream &operator>>(QDataStream &in, QStandardItemData &data)
50{
51 in >> data.role;
52 in >> data.value;
53 return in;
54}
55
56inline QDataStream &operator<<(QDataStream &out, const QStandardItemData &data)
57{
58 out << data.role;
59 out << data.value;
60 return out;
61}
62
63inline QDebug &operator<<(QDebug &debug, const QStandardItemData &data)
64{
65 QDebugStateSaver saver(debug);
66 debug.nospace() << data.role
67 << " "
68 << data.value;
69 return debug.space();
70}
71
72#endif // QT_NO_DATASTREAM
73
75{
76 Q_DECLARE_PUBLIC(QStandardItem)
77public:
79 : model(nullptr),
80 parent(nullptr),
81 rows(0),
82 columns(0),
83 q_ptr(nullptr),
86 { }
87
88 inline int childIndex(int row, int column) const {
89 if ((row < 0) || (column < 0)
90 || (row >= rowCount()) || (column >= columnCount())) {
91 return -1;
92 }
93 return (row * columnCount()) + column;
94 }
95 inline int childIndex(const QStandardItem *child) const {
96 const int lastChild = children.size() - 1;
101 } else {
103 }
104
105 // assuming the item is in the vicinity of the previous index, iterate forwards and
106 // backwards through the children
109 for (;;) {
110 if (forwardIter <= lastChild) {
111 if (children.at(forwardIter) == child) {
113 break;
114 }
115 ++forwardIter;
116 } else if (backwardIter < 0) {
118 break;
119 }
120 if (backwardIter >= 0) {
121 if (children.at(backwardIter) == child) {
123 break;
124 }
125 --backwardIter;
126 }
127 }
129 }
130 std::pair<int, int> position() const;
131 void setChild(int row, int column, QStandardItem *item,
132 bool emitChanged = false);
133 inline int rowCount() const {
134 return rows;
135 }
136 inline int columnCount() const {
137 return columns;
138 }
139 void childDeleted(QStandardItem *child);
140
141 void setModel(QStandardItemModel *mod);
142
143 inline void setParentAndModel(
144 QStandardItem *par,
145 QStandardItemModel *mod) {
146 setModel(mod);
147 parent = par;
148 }
149
150 void changeFlags(bool enable, Qt::ItemFlags f);
152 bool setChildrenCheckState(const QVariant &value);
155 void setItemData(const QMap<int, QVariant> &roles);
156 QMap<int, QVariant> itemData() const;
157
158 bool insertRows(int row, int count, const QList<QStandardItem*> &items);
159 bool insertRows(int row, const QList<QStandardItem*> &items);
160 bool insertColumns(int column, int count, const QList<QStandardItem*> &items);
161
162 void sortChildren(int column, Qt::SortOrder order);
163
168 int rows;
170
172
173 mutable int lastKnownIndex; // this is a cached value
175};
176
178{
179 Q_DECLARE_PUBLIC(QStandardItemModel)
180
181public:
184
185 void init();
186
187 inline QStandardItem *createItem() const {
188 return itemPrototype ? itemPrototype->clone() : new QStandardItem;
189 }
190
191 inline QStandardItem *itemFromIndex(const QModelIndex &index) const {
192 Q_Q(const QStandardItemModel);
193 if (!index.isValid())
194 return root.data();
195 if (index.model() != q)
196 return nullptr;
197 QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer());
198 if (parent == nullptr)
199 return nullptr;
200 return parent->child(index.row(), index.column());
201 }
202
203 void sort(QStandardItem *parent, int column, Qt::SortOrder order);
204 void itemChanged(QStandardItem *item, const QList<int> &roles = QList<int>());
205 void rowsAboutToBeInserted(QStandardItem *parent, int start, int end);
206 void columnsAboutToBeInserted(QStandardItem *parent, int start, int end);
207 void rowsAboutToBeRemoved(QStandardItem *parent, int start, int end);
208 void columnsAboutToBeRemoved(QStandardItem *parent, int start, int end);
209 void rowsInserted(QStandardItem *parent, int row, int count);
210 void columnsInserted(QStandardItem *parent, int column, int count);
211 void rowsRemoved(QStandardItem *parent, int row, int count);
212 void columnsRemoved(QStandardItem *parent, int column, int count);
213
214 void _q_emitItemChanged(const QModelIndex &topLeft,
215 const QModelIndex &bottomRight);
216
217 void decodeDataRecursive(QDataStream &stream, QStandardItem *item);
218
224 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QStandardItemModelPrivate, int, sortRole, Qt::DisplayRole)
225};
226
227QT_END_NAMESPACE
228
229#endif // QSTANDARDITEMMODEL_P_H
void sort(QStandardItem *parent, int column, Qt::SortOrder order)
QList< QStandardItem * > rowHeaderItems
void rowsAboutToBeRemoved(QStandardItem *parent, int start, int end)
QStandardItem * createItem() const
void columnsAboutToBeRemoved(QStandardItem *parent, int start, int end)
void columnsRemoved(QStandardItem *parent, int column, int count)
const QStandardItem * itemPrototype
QStandardItem * itemFromIndex(const QModelIndex &index) const
void columnsInserted(QStandardItem *parent, int column, int count)
void columnsAboutToBeInserted(QStandardItem *parent, int start, int end)
void itemChanged(QStandardItem *item, const QList< int > &roles=QList< int >())
void decodeDataRecursive(QDataStream &stream, QStandardItem *item)
QHash< int, QByteArray > roleNames
void rowsAboutToBeInserted(QStandardItem *parent, int start, int end)
void _q_emitItemChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void rowsRemoved(QStandardItem *parent, int row, int count)
QScopedPointer< QStandardItem > root
QList< QStandardItem * > columnHeaderItems
void rowsInserted(QStandardItem *parent, int row, int count)
bool insertRows(int row, const QList< QStandardItem * > &items)
void setModel(QStandardItemModel *mod)
bool insertColumns(int column, int count, const QList< QStandardItem * > &items)
QList< QStandardItemData > values
void setItemData(const QMap< int, QVariant > &roles)
void setChild(int row, int column, QStandardItem *item, bool emitChanged=false)
QStandardItem * checkStateParent() const
bool setChildrenCheckState(const QVariant &value)
QMap< int, QVariant > itemData() const
void childDeleted(QStandardItem *child)
void setParentAndModel(QStandardItem *par, QStandardItemModel *mod)
bool insertRows(int row, int count, const QList< QStandardItem * > &items)
QStandardItemModel * model
void sortChildren(int column, Qt::SortOrder order)
QList< QStandardItem * > children
void changeFlags(bool enable, Qt::ItemFlags f)
QT_REQUIRE_CONFIG(standarditemmodel)
QDataStream & operator>>(QDataStream &in, QStandardItemData &data)
QDataStream & operator<<(QDataStream &out, const QStandardItemData &data)
Q_DECLARE_TYPEINFO(QStandardItemData, Q_RELOCATABLE_TYPE)