Qt 6.x
The Qt SDK
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{
28 Q_PROPERTY(bool hasModelChildren READ hasModelChildren CONSTANT FINAL)
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
46 const QV4::FunctionObject *b, const QV4::Value *thisObject,
47 const QV4::Value *argv, int argc);
49 const QV4::FunctionObject *b, const QV4::Value *thisObject,
50 const QV4::Value *argv, int argc);
51
53 const QV4::FunctionObject *b, const QV4::Value *thisObject,
54 const QV4::Value *argv, int argc);
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:
82 : model(model)
84 , signalOffset(0)
85 {
86 }
87
88 bool notify(
89 const QQmlAdaptorModel &,
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
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
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());
178 proto->defineAccessorProperty(QStringLiteral("index"), QQmlAdaptorModelEngineData::get_index, nullptr);
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)));
194 p->setGetter(g);
195 p->setSetter(s);
197 }
198 prototype.set(v4, proto);
199 }
200
201 // QAbstractDynamicMetaObject
202
203 void objectDestroyed(QObject *) override
204 {
205 release();
206 }
207
208 int metaCall(QObject *object, QMetaObject::Call call, int id, void **arguments) override
209 {
210 return static_cast<QQmlDMAbstractItemModelData *>(object)->metaCall(call, id, arguments);
211 }
212
213 int rowCount(const QQmlAdaptorModel &model) const override
214 {
215 if (const QAbstractItemModel *aim = model.aim())
216 return aim->rowCount(model.rootIndex);
217 return 0;
218 }
219
220 int columnCount(const QQmlAdaptorModel &model) const override
221 {
222 if (const QAbstractItemModel *aim = model.aim())
223 return aim->columnCount(model.rootIndex);
224 return 0;
225 }
226
227 void cleanup(QQmlAdaptorModel &) const override
228 {
229 release();
230 }
231
232 QVariant value(const QQmlAdaptorModel &model, int index, const QString &role) const override
233 {
234 if (!metaObject) {
235 VDMAbstractItemModelDataType *dataType = const_cast<VDMAbstractItemModelDataType *>(this);
236 dataType->initializeMetaType(model);
237 }
238
239 if (const QAbstractItemModel *aim = model.aim()) {
242
243 const auto it = roleNames.find(role.toUtf8()), end = roleNames.end();
244 if (it != roleNames.end())
245 return modelIndex.data(*it);
246
247 if (role.isEmpty() || role == QLatin1String("modelData")) {
248 if (roleNames.size() == 1)
249 return modelIndex.data(roleNames.begin().value());
250
251 QVariantMap modelData;
252 for (auto jt = roleNames.begin(); jt != end; ++jt)
253 modelData.insert(QString::fromUtf8(jt.key()), modelIndex.data(jt.value()));
254 return modelData;
255 }
256
257 if (role == QLatin1String("hasModelChildren"))
258 return QVariant(aim->hasChildren(modelIndex));
259 }
260 return QVariant();
261 }
262
264 {
265 if (const QAbstractItemModel *aim = model.aim())
266 return QVariant::fromValue(aim->parent(model.rootIndex));
267 return QVariant();
268 }
269
270 QVariant modelIndex(const QQmlAdaptorModel &model, int index) const override
271 {
272 if (const QAbstractItemModel *aim = model.aim())
275 return QVariant();
276 }
277
278 bool canFetchMore(const QQmlAdaptorModel &model) const override
279 {
280 if (const QAbstractItemModel *aim = model.aim())
281 return aim->canFetchMore(model.rootIndex);
282 return false;
283 }
284
285 void fetchMore(QQmlAdaptorModel &model) const override
286 {
287 if (QAbstractItemModel *aim = model.aim())
288 aim->fetchMore(model.rootIndex);
289 }
290
294 int index, int row, int column) override
295 {
296 if (!metaObject)
299 }
300
302 {
303 QMetaObjectBuilder builder;
304 QQmlAdaptorModelEngineData::setModelDataType<QQmlDMAbstractItemModelData>(&builder, this);
305
306 const QByteArray propertyType = QByteArrayLiteral("QVariant");
307 const QAbstractItemModel *aim = model.aim();
309 for (QHash<int, QByteArray>::const_iterator it = names.begin(), cend = names.end(); it != cend; ++it) {
310 const int propertyId = propertyRoles.size();
311 propertyRoles.append(it.key());
312 roleNames.insert(it.value(), it.key());
313 QQmlAdaptorModelEngineData::addProperty(&builder, propertyId, it.value(), propertyType);
314 }
315
316 metaObject.reset(builder.toMetaObject());
317 *static_cast<QMetaObject *>(this) = *metaObject;
320 }
321
330};
331
333
334#endif // QQMLDMABSTRACTITEMMODELDATA_P_H
virtual QHash< int, QByteArray > roleNames() const
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qhash.h:1135
\inmodule QtCore
Definition qhash.h:818
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1202
qsizetype size() const noexcept
Returns the number of items in the hash.
Definition qhash.h:925
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:1209
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1258
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:1205
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
bool removeOne(const AT &t)
Definition qlist.h:581
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
void reserve(qsizetype size)
Definition qlist.h:746
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
QMetaObject * toMetaObject() const
Converts this meta object builder into a concrete QMetaObject.
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:90
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)
const VDMAbstractItemModelDataType * type() const
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:127
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5857
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QByteArray toUtf8() const &
Definition qstring.h:563
ObjectType::Data * allocate(Args &&... args)
Definition qv4mm_p.h:199
void set(ExecutionEngine *engine, const Value &value)
void append(const T &t)
\inmodule QtCore
Definition qvariant.h:64
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:531
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.
quint64 ReturnedValue
@ 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]
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean GLboolean g
GLuint name
GLenum GLenum GLsizei void GLsizei void * column
GLuint GLuint * names
GLenum GLenum GLsizei void * row
GLdouble s
[6]
Definition qopenglext.h:235
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:70
#define RETURN_RESULT(r)
QGraphicsItem * item
QList< QTreeWidgetItem * > items
QJSValue global
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:955
bool contains(const AT &t) const noexcept
Definition qlist.h:44
\inmodule QtCore
QMetaType metaType() const
static void activate(QObject *sender, int signal_index, void **argv)
Definition qobject.cpp:4057
MemoryManager * memoryManager
ExecutionContext * rootContext() const
Heap::String * newString(const QString &s=QString())
Heap::Object * newObject()
ReturnedValue throwTypeError()
ExecutionEngine * engine
const T * as() const
Definition qv4value_p.h:132