Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmldmabstractitemmodeldata_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 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 QQMLDMABSTRACTITEMMODELDATA_P_H
5#define QQMLDMABSTRACTITEMMODELDATA_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 purely as an
12// implementation detail. 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 <private/qqmladaptormodelenginedata_p.h>
19#include <private/qqmldelegatemodel_p_p.h>
20#include <private/qobject_p.h>
21
23
26{
31
32public:
36 int index, int row, int column);
37
38 int metaCall(QMetaObject::Call call, int id, void **arguments);
39 bool hasModelChildren() const;
40
41 QV4::ReturnedValue get() override;
42 void setValue(const QString &role, const QVariant &value) override;
43 bool resolveIndex(const QQmlAdaptorModel &model, int idx) override;
44
45 static QV4::ReturnedValue get_property(
46 const QV4::FunctionObject *b, const QV4::Value *thisObject,
47 const QV4::Value *argv, int argc);
48 static QV4::ReturnedValue set_property(
49 const QV4::FunctionObject *b, const QV4::Value *thisObject,
50 const QV4::Value *argv, int argc);
51
52 static QV4::ReturnedValue get_modelData(
53 const QV4::FunctionObject *b, const QV4::Value *thisObject,
54 const QV4::Value *argv, int argc);
55 static QV4::ReturnedValue set_modelData(
56 const QV4::FunctionObject *b, const QV4::Value *thisObject,
57 const QV4::Value *argv, int argc);
58
59 QVariant modelData() const;
60 void setModelData(const QVariant &modelData);
61
62 const VDMAbstractItemModelDataType *type() const { return m_type; }
63
66
67private:
68 QVariant value(int role) const;
69 void setValue(int role, const QVariant &value);
70
72 QVector<QVariant> m_cachedData;
73};
74
76 : public QQmlRefCounted<VDMAbstractItemModelDataType>
79{
80public:
87
88 bool notify(
89 const QQmlAdaptorModel &,
90 const QList<QQmlDelegateModelItem *> &items,
91 int index,
92 int count,
93 const QVector<int> &roles) const override
94 {
95 bool changed = roles.isEmpty() && !watchedRoles.isEmpty();
96 if (!changed && !watchedRoles.isEmpty() && watchedRoleIds.isEmpty()) {
97 QList<int> roleIds;
98 for (const QByteArray &r : watchedRoles) {
100 if (it != roleNames.end())
101 roleIds << it.value();
102 }
103 const_cast<VDMAbstractItemModelDataType *>(this)->watchedRoleIds = roleIds;
104 }
105
106 QVector<int> signalIndexes;
107 for (int i = 0; i < roles.size(); ++i) {
108 const int role = roles.at(i);
109 if (!changed && watchedRoleIds.contains(role))
110 changed = true;
111
112 int propertyId = propertyRoles.indexOf(role);
113 if (propertyId != -1)
114 signalIndexes.append(propertyId + signalOffset);
115 }
116 if (roles.isEmpty()) {
117 const int propertyRolesCount = propertyRoles.size();
118 signalIndexes.reserve(propertyRolesCount);
119 for (int propertyId = 0; propertyId < propertyRolesCount; ++propertyId)
120 signalIndexes.append(propertyId + signalOffset);
121 }
122
123 QVarLengthArray<QQmlGuard<QQmlDMAbstractItemModelData>> guardedItems;
124 for (const auto item : items) {
125 Q_ASSERT(qobject_cast<QQmlDMAbstractItemModelData *>(item) == item);
126 guardedItems.append(static_cast<QQmlDMAbstractItemModelData *>(item));
127 }
128
129 for (const auto &item : std::as_const(guardedItems)) {
130 if (item.isNull())
131 continue;
132
133 const int idx = item->modelIndex();
134 if (idx >= index && idx < index + count) {
135 for (int i = 0; i < signalIndexes.size(); ++i)
136 QMetaObject::activate(item, signalIndexes.at(i), nullptr);
137 emit item->modelDataChanged();
138 }
139 }
140 return changed;
141 }
142
145 const QList<QByteArray> &oldRoles,
146 const QList<QByteArray> &newRoles) const override
147 {
148 VDMAbstractItemModelDataType *dataType = const_cast<VDMAbstractItemModelDataType *>(this);
149
150 dataType->watchedRoleIds.clear();
151 for (const QByteArray &oldRole : oldRoles)
152 dataType->watchedRoles.removeOne(oldRole);
153 dataType->watchedRoles += newRoles;
154 }
155
157 {
158 QV4::Scope scope(b);
160 if (!o)
161 RETURN_RESULT(scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object")));
162
163 const QQmlAdaptorModel *const model
164 = static_cast<QQmlDMAbstractItemModelData *>(o->d()->item)->type()->model;
165 if (o->d()->item->index >= 0) {
166 if (const QAbstractItemModel *const aim = model->aim())
167 RETURN_RESULT(QV4::Encode(aim->hasChildren(aim->index(o->d()->item->index, 0, model->rootIndex))));
168 }
170 }
171
172
174 {
175 QV4::ExecutionEngine *v4 = data->v4;
176 QV4::Scope scope(v4);
177 QV4::ScopedObject proto(scope, v4->newObject());
179 proto->defineAccessorProperty(QStringLiteral("hasModelChildren"), get_hasModelChildren, nullptr);
180 proto->defineAccessorProperty(QStringLiteral("modelData"),
183 QV4::ScopedProperty p(scope);
184
186 for (iterator it = roleNames.constBegin(), end = roleNames.constEnd(); it != end; ++it) {
187 const qsizetype propertyId = propertyRoles.indexOf(it.value());
188 const QByteArray &propertyName = it.key();
189
190 QV4::ScopedString name(scope, v4->newString(QString::fromUtf8(propertyName)));
192 scope,
196 scope,
199 p->setGetter(g);
200 p->setSetter(s);
202 }
203 prototype.set(v4, proto);
204 }
205
206 // QAbstractDynamicMetaObject
207
208 void objectDestroyed(QObject *) override
209 {
210 release();
211 }
212
213 int metaCall(QObject *object, QMetaObject::Call call, int id, void **arguments) override
214 {
215 return static_cast<QQmlDMAbstractItemModelData *>(object)->metaCall(call, id, arguments);
216 }
217
218 int rowCount(const QQmlAdaptorModel &model) const override
219 {
220 if (const QAbstractItemModel *aim = model.aim())
221 return aim->rowCount(model.rootIndex);
222 return 0;
223 }
224
225 int columnCount(const QQmlAdaptorModel &model) const override
226 {
227 if (const QAbstractItemModel *aim = model.aim())
228 return aim->columnCount(model.rootIndex);
229 return 0;
230 }
231
232 void cleanup(QQmlAdaptorModel &) const override
233 {
234 release();
235 }
236
237 QVariant value(const QQmlAdaptorModel &model, int index, const QString &role) const override
238 {
239 if (!metaObject) {
240 VDMAbstractItemModelDataType *dataType = const_cast<VDMAbstractItemModelDataType *>(this);
241 dataType->initializeMetaType(model);
242 }
243
244 if (const QAbstractItemModel *aim = model.aim()) {
247
248 const auto it = roleNames.find(role.toUtf8()), end = roleNames.end();
249 if (it != roleNames.end())
250 return modelIndex.data(*it);
251
252 if (role.isEmpty() || role == QLatin1String("modelData")) {
253 if (roleNames.size() == 1)
254 return modelIndex.data(roleNames.begin().value());
255
256 QVariantMap modelData;
257 for (auto jt = roleNames.begin(); jt != end; ++jt)
258 modelData.insert(QString::fromUtf8(jt.key()), modelIndex.data(jt.value()));
259 return modelData;
260 }
261
262 if (role == QLatin1String("hasModelChildren"))
263 return QVariant(aim->hasChildren(modelIndex));
264 }
265 return QVariant();
266 }
267
269 {
270 if (const QAbstractItemModel *aim = model.aim())
271 return QVariant::fromValue(aim->parent(model.rootIndex));
272 return QVariant();
273 }
274
275 QVariant modelIndex(const QQmlAdaptorModel &model, int index) const override
276 {
277 if (const QAbstractItemModel *aim = model.aim())
280 return QVariant();
281 }
282
283 bool canFetchMore(const QQmlAdaptorModel &model) const override
284 {
285 if (const QAbstractItemModel *aim = model.aim())
286 return aim->canFetchMore(model.rootIndex);
287 return false;
288 }
289
290 void fetchMore(QQmlAdaptorModel &model) const override
291 {
292 if (QAbstractItemModel *aim = model.aim())
293 aim->fetchMore(model.rootIndex);
294 }
295
298 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType,
299 int index, int row, int column) override
300 {
301 if (!metaObject)
304 }
305
307 {
308 QMetaObjectBuilder builder;
309 QQmlAdaptorModelEngineData::setModelDataType<QQmlDMAbstractItemModelData>(&builder, this);
310
311 const QByteArray propertyType = QByteArrayLiteral("QVariant");
312 const QAbstractItemModel *aim = model.aim();
313 const QHash<int, QByteArray> names = aim ? aim->roleNames() : QHash<int, QByteArray>();
314 for (QHash<int, QByteArray>::const_iterator it = names.begin(), cend = names.end(); it != cend; ++it) {
315 const int propertyId = propertyRoles.size();
316 propertyRoles.append(it.key());
317 roleNames.insert(it.value(), it.key());
318 QQmlAdaptorModelEngineData::addProperty(&builder, propertyId, it.value(), propertyType);
319 }
320
321 metaObject.reset(builder.toMetaObject());
322 *static_cast<QMetaObject *>(this) = *metaObject;
325 }
326
328 QList<int> propertyRoles;
329 QList<int> watchedRoleIds;
330 QList<QByteArray> watchedRoles;
331 QHash<QByteArray, int> roleNames;
335};
336
338
339#endif // QQMLDMABSTRACTITEMMODELDATA_P_H
virtual QHash< int, QByteArray > roleNames() const
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qhash.h:1145
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
qsizetype size() const noexcept
Returns the number of items in the hash.
Definition qhash.h:927
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1291
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1215
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
QMetaObject * toMetaObject() const
Converts this meta object builder into a concrete QMetaObject.
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
static void addProperty(QMetaObjectBuilder *builder, int propertyId, const QByteArray &propertyName, const QByteArray &propertyType)
static QV4::ReturnedValue get_index(const QV4::FunctionObject *f, const QV4::Value *thisObject, const QV4::Value *, int)
QQmlPropertyCache::ConstPtr propertyCache
QScopedPointer< QMetaObject, QScopedPointerPodDeleter > metaObject
QAbstractItemModel * aim()
int rowAt(int index) const
QTypeRevision modelItemRevision
int columnAt(int index) const
QPersistentModelIndex rootIndex
int metaCall(QMetaObject::Call call, int id, void **arguments)
static QV4::ReturnedValue get_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
void setValue(const QString &role, const QVariant &value) override
static QV4::ReturnedValue get_property(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
static QV4::ReturnedValue set_property(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
void setModelData(const QVariant &modelData)
bool resolveIndex(const QQmlAdaptorModel &model, int idx) override
static QV4::ReturnedValue set_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
QQmlRefPointer< QQmlDelegateModelItemMetaType > const metaType
static Ptr createStandalone(const QMetaObject *, QTypeRevision metaObjectRevision=QTypeRevision::zero())
Creates a standalone QQmlPropertyCache of metaObject.
T * data() const noexcept
Returns the value of the pointer referenced by this object.
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
Deletes the existing object it is pointing to (if any), and sets its pointer to other.
iterator begin()
Definition qset.h:136
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
QByteArray toUtf8() const &
Definition qstring.h:634
ObjectType::Data * allocate(Args &&... args)
Definition qv4mm_p.h:298
void set(ExecutionEngine *engine, const Value &value)
\inmodule QtCore
Definition qvariant.h:65
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
QVariant parentModelIndex(const QQmlAdaptorModel &model) const override
void cleanup(QQmlAdaptorModel &) const override
int columnCount(const QQmlAdaptorModel &model) const override
bool notify(const QQmlAdaptorModel &, const QList< QQmlDelegateModelItem * > &items, int index, int count, const QVector< int > &roles) const override
QVariant value(const QQmlAdaptorModel &model, int index, const QString &role) const override
static QV4::ReturnedValue get_hasModelChildren(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int)
int rowCount(const QQmlAdaptorModel &model) const override
bool canFetchMore(const QQmlAdaptorModel &model) const override
int metaCall(QObject *object, QMetaObject::Call call, int id, void **arguments) override
void initializeConstructor(QQmlAdaptorModelEngineData *const data)
void initializeMetaType(const QQmlAdaptorModel &model)
VDMAbstractItemModelDataType(QQmlAdaptorModel *model)
void fetchMore(QQmlAdaptorModel &model) const override
QVariant modelIndex(const QQmlAdaptorModel &model, int index) const override
void replaceWatchedRoles(QQmlAdaptorModel &, const QList< QByteArray > &oldRoles, const QList< QByteArray > &newRoles) const override
QQmlDelegateModelItem * createItem(QQmlAdaptorModel &model, const QQmlRefPointer< QQmlDelegateModelItemMetaType > &metaType, int index, int row, int column) override
QSet< QString >::iterator it
QList< QVariant > arguments
Combined button and popup list for selecting options.
Scoped< FunctionObject > ScopedFunctionObject
quint64 ReturnedValue
Scoped< String > ScopedString
@ Attr_NotConfigurable
@ Attr_NotEnumerable
@ Attr_Accessor
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define QT_ANONYMOUS_PROPERTY(...)
Definition qobject_p.h:38
GLboolean GLboolean GLboolean b
GLuint index
[2]
GLboolean r
[2]
GLuint GLuint end
GLenum GLenum GLsizei count
GLuint object
[3]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLboolean GLboolean g
GLuint name
GLenum GLenum GLsizei void GLsizei void * column
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLuint * names
GLenum GLenum GLsizei void * row
GLfloat GLfloat p
[1]
static const int oldRoles[7]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SIGNALS
#define emit
ptrdiff_t qsizetype
Definition qtypes.h:165
#define RETURN_RESULT(r)
QGraphicsItem * item
QList< QTreeWidgetItem * > items
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:962
bool contains(const AT &t) const noexcept
Definition qlist.h:45
\inmodule QtCore
QMetaType metaType() const
static void activate(QObject *sender, int signal_index, void **argv)
Definition qobject.cpp:4193
MemoryManager * memoryManager
Heap::Object * newObject()
Heap::String * newString(char16_t c)
ReturnedValue throwTypeError()
void defineAccessorProperty(const QString &name, VTable::Call getter, VTable::Call setter)
void insertMember(StringOrSymbol *s, const Value &v, PropertyAttributes attributes=Attr_Data)
ExecutionEngine * engine
const T * as() const
Definition qv4value_p.h:132