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 | |
4 | #ifndef QSTANDARDITEMMODEL_P_H |
5 | #define QSTANDARDITEMMODEL_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists for the convenience |
12 | // of other Qt classes. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtGui/qstandarditemmodel.h> |
19 | |
20 | #include <QtGui/private/qtguiglobal_p.h> |
21 | #include "private/qabstractitemmodel_p.h" |
22 | |
23 | #include <QtCore/qlist.h> |
24 | #include <QtCore/qpair.h> |
25 | #include <QtCore/qstack.h> |
26 | #include <QtCore/qvariant.h> |
27 | #include <QtCore/qdebug.h> |
28 | |
29 | QT_REQUIRE_CONFIG(standarditemmodel); |
30 | |
31 | QT_BEGIN_NAMESPACE |
32 | |
33 | class QStandardItemData |
34 | { |
35 | public: |
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 | }; |
45 | Q_DECLARE_TYPEINFO(QStandardItemData, Q_RELOCATABLE_TYPE); |
46 | |
47 | #ifndef QT_NO_DATASTREAM |
48 | |
49 | inline QDataStream &operator>>(QDataStream &in, QStandardItemData &data) |
50 | { |
51 | in >> data.role; |
52 | in >> data.value; |
53 | return in; |
54 | } |
55 | |
56 | inline QDataStream &operator<<(QDataStream &out, const QStandardItemData &data) |
57 | { |
58 | out << data.role; |
59 | out << data.value; |
60 | return out; |
61 | } |
62 | |
63 | inline 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 | |
74 | class QStandardItemPrivate |
75 | { |
76 | Q_DECLARE_PUBLIC(QStandardItem) |
77 | public: |
78 | inline QStandardItemPrivate() |
79 | : model(nullptr), |
80 | parent(nullptr), |
81 | rows(0), |
82 | columns(0), |
83 | q_ptr(nullptr), |
84 | lastKnownIndex(-1) |
85 | { } |
86 | |
87 | inline int childIndex(int row, int column) const { |
88 | if ((row < 0) || (column < 0) |
89 | || (row >= rowCount()) || (column >= columnCount())) { |
90 | return -1; |
91 | } |
92 | return (row * columnCount()) + column; |
93 | } |
94 | inline int childIndex(const QStandardItem *child) const { |
95 | const int lastChild = children.size() - 1; |
96 | int &childsLastIndexInParent = child->d_func()->lastKnownIndex; |
97 | if (childsLastIndexInParent != -1 && childsLastIndexInParent <= lastChild) { |
98 | if (children.at(i: childsLastIndexInParent) == child) |
99 | return childsLastIndexInParent; |
100 | } else { |
101 | childsLastIndexInParent = lastChild / 2; |
102 | } |
103 | |
104 | // assuming the item is in the vicinity of the previous index, iterate forwards and |
105 | // backwards through the children |
106 | int backwardIter = childsLastIndexInParent - 1; |
107 | int forwardIter = childsLastIndexInParent; |
108 | for (;;) { |
109 | if (forwardIter <= lastChild) { |
110 | if (children.at(i: forwardIter) == child) { |
111 | childsLastIndexInParent = forwardIter; |
112 | break; |
113 | } |
114 | ++forwardIter; |
115 | } else if (backwardIter < 0) { |
116 | childsLastIndexInParent = -1; |
117 | break; |
118 | } |
119 | if (backwardIter >= 0) { |
120 | if (children.at(i: backwardIter) == child) { |
121 | childsLastIndexInParent = backwardIter; |
122 | break; |
123 | } |
124 | --backwardIter; |
125 | } |
126 | } |
127 | return childsLastIndexInParent; |
128 | } |
129 | QPair<int, int> position() const; |
130 | void setChild(int row, int column, QStandardItem *item, |
131 | bool emitChanged = false); |
132 | inline int rowCount() const { |
133 | return rows; |
134 | } |
135 | inline int columnCount() const { |
136 | return columns; |
137 | } |
138 | void childDeleted(QStandardItem *child); |
139 | |
140 | void setModel(QStandardItemModel *mod); |
141 | |
142 | inline void setParentAndModel( |
143 | QStandardItem *par, |
144 | QStandardItemModel *mod) { |
145 | setModel(mod); |
146 | parent = par; |
147 | } |
148 | |
149 | void changeFlags(bool enable, Qt::ItemFlags f); |
150 | void setItemData(const QMap<int, QVariant> &roles); |
151 | QMap<int, QVariant> itemData() const; |
152 | |
153 | bool insertRows(int row, int count, const QList<QStandardItem*> &items); |
154 | bool insertRows(int row, const QList<QStandardItem*> &items); |
155 | bool insertColumns(int column, int count, const QList<QStandardItem*> &items); |
156 | |
157 | void sortChildren(int column, Qt::SortOrder order); |
158 | |
159 | QStandardItemModel *model; |
160 | QStandardItem *parent; |
161 | QList<QStandardItemData> values; |
162 | QList<QStandardItem *> children; |
163 | int rows; |
164 | int columns; |
165 | |
166 | QStandardItem *q_ptr; |
167 | |
168 | mutable int lastKnownIndex; // this is a cached value |
169 | }; |
170 | |
171 | class QStandardItemModelPrivate : public QAbstractItemModelPrivate |
172 | { |
173 | Q_DECLARE_PUBLIC(QStandardItemModel) |
174 | |
175 | public: |
176 | QStandardItemModelPrivate(); |
177 | ~QStandardItemModelPrivate(); |
178 | |
179 | void init(); |
180 | |
181 | inline QStandardItem *createItem() const { |
182 | return itemPrototype ? itemPrototype->clone() : new QStandardItem; |
183 | } |
184 | |
185 | inline QStandardItem *itemFromIndex(const QModelIndex &index) const { |
186 | Q_Q(const QStandardItemModel); |
187 | if (!index.isValid()) |
188 | return root.data(); |
189 | if (index.model() != q) |
190 | return nullptr; |
191 | QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer()); |
192 | if (parent == nullptr) |
193 | return nullptr; |
194 | return parent->child(row: index.row(), column: index.column()); |
195 | } |
196 | |
197 | void sort(QStandardItem *parent, int column, Qt::SortOrder order); |
198 | void itemChanged(QStandardItem *item, const QList<int> &roles = QList<int>()); |
199 | void rowsAboutToBeInserted(QStandardItem *parent, int start, int end); |
200 | void columnsAboutToBeInserted(QStandardItem *parent, int start, int end); |
201 | void rowsAboutToBeRemoved(QStandardItem *parent, int start, int end); |
202 | void columnsAboutToBeRemoved(QStandardItem *parent, int start, int end); |
203 | void rowsInserted(QStandardItem *parent, int row, int count); |
204 | void columnsInserted(QStandardItem *parent, int column, int count); |
205 | void rowsRemoved(QStandardItem *parent, int row, int count); |
206 | void columnsRemoved(QStandardItem *parent, int column, int count); |
207 | |
208 | void _q_emitItemChanged(const QModelIndex &topLeft, |
209 | const QModelIndex &bottomRight); |
210 | |
211 | void decodeDataRecursive(QDataStream &stream, QStandardItem *item); |
212 | |
213 | QList<QStandardItem *> columnHeaderItems; |
214 | QList<QStandardItem *> rowHeaderItems; |
215 | QHash<int, QByteArray> roleNames; |
216 | QScopedPointer<QStandardItem> root; |
217 | const QStandardItem *itemPrototype; |
218 | Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QStandardItemModelPrivate, int, sortRole, Qt::DisplayRole) |
219 | }; |
220 | |
221 | QT_END_NAMESPACE |
222 | |
223 | #endif // QSTANDARDITEMMODEL_P_H |
224 | |