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
qabstractitemmodel_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 QABSTRACTITEMMODEL_P_H
6#define QABSTRACTITEMMODEL_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 QAbstractItemModel*. This header file may change from version
14// to version without notice, or even be removed.
15//
16// We mean it.
17//
18//
19
20#include "QtCore/qabstractitemmodel.h"
21#include "QtCore/private/qobject_p.h"
22#include "QtCore/qstack.h"
23#include "QtCore/qset.h"
24#include "QtCore/qhash.h"
25
27
29
31{
32public:
34 QPersistentModelIndexData(const QModelIndex &idx) : index(idx) {}
37 static QPersistentModelIndexData *create(const QModelIndex &index);
38 static void destroy(QPersistentModelIndexData *data);
39};
40
41namespace QtPrivate {
42// This class is just a wrapper so we can use the fixed qHash() function for QModelIndex.
43struct QModelIndexWrapper // ### Qt 7: Remove again, use QModelIndex directly.
44{
47 Q_IMPLICIT inline operator QModelIndex() const { return index; }
48 friend bool operator==(const QModelIndexWrapper &l, const QModelIndexWrapper &r) { return l.index == r.index; }
49 friend bool operator!=(const QModelIndexWrapper &l, const QModelIndexWrapper &r) { return !(operator==(l,r)); }
50 friend bool operator==(const QModelIndexWrapper &l, const QModelIndex &r) { return l.index == r; }
51 friend bool operator!=(const QModelIndexWrapper &l, const QModelIndex &r) { return !(operator==(l.index,r)); }
52 friend bool operator==(const QModelIndex &l, const QModelIndexWrapper &r) { return l == r.index; }
53 friend bool operator!=(const QModelIndex &l, const QModelIndexWrapper &r) { return !(operator==(l,r.index)); }
54 friend inline size_t qHash(const QtPrivate::QModelIndexWrapper &index, size_t seed = 0) noexcept
55 {
57 }
58};
59}
60
61class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate
62{
63 Q_DECLARE_PUBLIC(QAbstractItemModel)
64
65public:
66 QAbstractItemModelPrivate();
67 ~QAbstractItemModelPrivate();
68
69 static const QAbstractItemModelPrivate *get(const QAbstractItemModel *model) { return model->d_func(); }
70
71 void removePersistentIndexData(QPersistentModelIndexData *data);
72 void movePersistentIndexes(const QList<QPersistentModelIndexData *> &indexes, int change, const QModelIndex &parent,
73 Qt::Orientation orientation);
74 void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
75 void rowsInserted(const QModelIndex &parent, int first, int last);
76 void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
77 void rowsRemoved(const QModelIndex &parent, int first, int last);
78 void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
79 void columnsInserted(const QModelIndex &parent, int first, int last);
80 void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
81 void columnsRemoved(const QModelIndex &parent, int first, int last);
82 static QAbstractItemModel *staticEmptyModel();
83 static bool variantLessThan(const QVariant &v1, const QVariant &v2);
84
85 void itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation);
86 void itemsMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
87 bool allowMove(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
88 bool dropOnItem(const QModelIndex &index, QDataStream &stream);
89
90 // ugly hack for QTreeModel, see QTBUG-94546
91 virtual void executePendingOperations() const;
92
93 inline QModelIndex createIndex(int row, int column, void *data = nullptr) const {
94 return q_func()->createIndex(row, column, data);
95 }
96
97 inline QModelIndex createIndex(int row, int column, int id) const {
98 return q_func()->createIndex(row, column, id);
99 }
100
101 inline bool indexValid(const QModelIndex &index) const {
102 return (index.row() >= 0) && (index.column() >= 0) && (index.model() == q_func());
103 }
104
105 void invalidatePersistentIndexes();
106 void invalidatePersistentIndex(const QModelIndex &index);
107
108 struct Change {
109 constexpr Change() : parent(), first(-1), last(-1), needsAdjust(false) {}
110 constexpr Change(const QModelIndex &p, int f, int l) : parent(p), first(f), last(l), needsAdjust(false) {}
111
112 QModelIndex parent;
113 int first, last;
114
115
116 // In cases such as this:
117 // - A
118 // - B
119 // - C
120 // - - D
121 // - - E
122 // - - F
123 //
124 // If B is moved to above E, C is the source parent in the signal and its row is 2. When the move is
125 // completed however, C is at row 1 and there is no row 2 at the same level in the model at all.
126 // The QModelIndex is adjusted to correct that in those cases before reporting it though the
127 // rowsMoved signal.
128 bool needsAdjust;
129
130 constexpr bool isValid() const { return first >= 0 && last >= 0; }
131 };
132 QStack<Change> changes;
133
134 struct Persistent {
135 Persistent() {}
136 QMultiHash<QtPrivate::QModelIndexWrapper, QPersistentModelIndexData *> indexes;
137 QStack<QList<QPersistentModelIndexData *>> moved;
138 QStack<QList<QPersistentModelIndexData *>> invalidated;
139 void insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data);
140 } persistent;
141
142 bool resetting = false;
143
144 static const QHash<int,QByteArray> &defaultRoleNames();
145};
146Q_DECLARE_TYPEINFO(QAbstractItemModelPrivate::Change, Q_RELOCATABLE_TYPE);
147
148namespace QtPrivate {
149
150/*!
151 \internal
152 This is a workaround for QTBUG-75172.
153
154 Some predefined model roles are supposed to use certain enum/flag
155 types (e.g. fetching Qt::TextAlignmentRole is supposed to return a
156 variant containing a Qt::Alignment object).
157
158 For historical reasons, a plain `int` was used sometimes. This is
159 surprising to end-users and also sloppy on Qt's part; users were
160 forced to use `int` rather than the correct datatype.
161
162 This function tries both the "right" type and plain `int`, for a
163 given QVariant. This fixes the problem (using the correct datatype)
164 but also keeps compatibility with existing code using `int`.
165
166 ### Qt 7: get rid of this. Always use the correct datatype.
167*/
168template <typename T>
169T legacyEnumValueFromModelData(const QVariant &data)
170{
171 static_assert(std::is_enum_v<T>);
172 if (data.userType() == qMetaTypeId<T>()) {
173 return data.value<T>();
174 } else if (std::is_same_v<std::underlying_type_t<T>, int> ||
175 std::is_same_v<std::underlying_type_t<T>, uint>) {
176 return T(data.toInt());
177 }
178
179 return T();
180}
181
182template <typename T>
183T legacyFlagValueFromModelData(const QVariant &data)
184{
185 if (data.userType() == qMetaTypeId<T>()) {
186 return data.value<T>();
187 } else if (std::is_same_v<std::underlying_type_t<typename T::enum_type>, int> ||
188 std::is_same_v<std::underlying_type_t<typename T::enum_type>, uint>) {
189 return T::fromInt(data.toInt());
190 }
191
192 return T();
193}
194
195} // namespace QtPrivate
196
197
198QT_END_NAMESPACE
199
200#endif // QABSTRACTITEMMODEL_P_H
static void destroy(QPersistentModelIndexData *data)
static QPersistentModelIndexData * create(const QModelIndex &index)
QPersistentModelIndexData(const QModelIndex &idx)
T legacyEnumValueFromModelData(const QVariant &data)
T legacyFlagValueFromModelData(const QVariant &data)
QT_REQUIRE_CONFIG(liburing)