Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmldmabstractitemmodeldata.cpp
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#include <private/qqmldmabstractitemmodeldata_p.h>
5
7
10 VDMAbstractItemModelDataType *dataType, int index, int row, int column)
11 : QQmlDelegateModelItem(metaType, dataType, index, row, column)
12 , m_type(dataType)
13{
14 if (index == -1)
15 m_cachedData.resize(m_type->propertyRoles.size());
16
17 QObjectPrivate::get(this)->metaObject = m_type;
18
19 m_type->addref();
20}
21
23{
24 if (call == QMetaObject::ReadProperty && id >= m_type->propertyOffset) {
25 const int propertyIndex = id - m_type->propertyOffset;
26 if (index == -1) {
27 if (!m_cachedData.isEmpty())
28 *static_cast<QVariant *>(arguments[0]) = m_cachedData.at(propertyIndex);
29 } else if (*m_type->model) {
30 *static_cast<QVariant *>(arguments[0]) = value(m_type->propertyRoles.at(propertyIndex));
31 }
32 return -1;
33 } else if (call == QMetaObject::WriteProperty && id >= m_type->propertyOffset) {
34 const int propertyIndex = id - m_type->propertyOffset;
35 const QMetaObject *meta = metaObject();
36 if (index == -1) {
37 if (m_cachedData.size() > 1) {
38 m_cachedData[propertyIndex] = *static_cast<QVariant *>(arguments[0]);
39 QMetaObject::activate(this, meta, propertyIndex, nullptr);
40 } else if (m_cachedData.size() == 1) {
41 m_cachedData[0] = *static_cast<QVariant *>(arguments[0]);
42 QMetaObject::activate(this, meta, 0, nullptr);
43 }
44 } else if (*m_type->model) {
45 setValue(m_type->propertyRoles.at(propertyIndex), *static_cast<QVariant *>(arguments[0]));
46 QMetaObject::activate(this, meta, propertyIndex, nullptr);
47 }
49 return -1;
50 } else {
51 return qt_metacall(call, id, arguments);
52 }
53}
54
56{
57 // Used only for initialization of the cached data. Does not have to emit change signals.
58
59 if (m_type->propertyRoles.size() == 1
60 && (role.isEmpty() || role == QLatin1String("modelData"))) {
61 // If the model has only a single role, the modelData is that role.
62 m_cachedData[0] = value;
63 return;
64 }
65
67 if (it != m_type->roleNames.end()) {
68 for (int i = 0; i < m_type->propertyRoles.size(); ++i) {
69 if (m_type->propertyRoles.at(i) == *it) {
70 m_cachedData[i] = value;
71 return;
72 }
73 }
74 }
75}
76
78{
79 if (index == -1) {
80 Q_ASSERT(idx >= 0);
81 m_cachedData.clear();
82 setModelIndex(idx, adaptorModel.rowAt(idx), adaptorModel.columnAt(idx));
83 const QMetaObject *meta = metaObject();
84 const int propertyCount = m_type->propertyRoles.size();
85 for (int i = 0; i < propertyCount; ++i)
86 QMetaObject::activate(this, meta, i, nullptr);
88 return true;
89 } else {
90 return false;
91 }
92}
93
95{
96 QV4::Scope scope(b);
98 if (!o)
99 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
100
101 const qsizetype propertyId = static_cast<const QV4::IndexedBuiltinFunction *>(b)->d()->index;
102
104 if (o->d()->item->index == -1) {
105 if (!modelData->m_cachedData.isEmpty())
106 return scope.engine->fromVariant(modelData->m_cachedData.at(propertyId));
107 } else if (*modelData->m_type->model) {
108 return scope.engine->fromVariant(
109 modelData->value(modelData->m_type->propertyRoles.at(propertyId)));
110 }
111 return QV4::Encode::undefined();
112}
113
115{
116 QV4::Scope scope(b);
118 if (!o)
119 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
120 if (!argc)
121 return scope.engine->throwTypeError();
122
123 const qsizetype propertyId = static_cast<const QV4::IndexedBuiltinFunction *>(b)->d()->index;
124
125 if (o->d()->item->index == -1) {
127 if (!modelData->m_cachedData.isEmpty()) {
128 if (modelData->m_cachedData.size() > 1) {
129 modelData->m_cachedData[propertyId]
131 QMetaObject::activate(o->d()->item, o->d()->item->metaObject(), propertyId, nullptr);
132 } else if (modelData->m_cachedData.size() == 1) {
133 modelData->m_cachedData[0] = QV4::ExecutionEngine::toVariant(argv[0], QMetaType {});
134 QMetaObject::activate(o->d()->item, o->d()->item->metaObject(), 0, nullptr);
135 }
136 emit modelData->modelDataChanged();
137 }
138 }
139 return QV4::Encode::undefined();
140}
141
143 const QV4::FunctionObject *b, const QV4::Value *thisObject,
144 const QV4::Value *argv, int argc)
145{
146 Q_UNUSED(argv)
147 Q_UNUSED(argc)
148
149 QV4::Scope scope(b);
151 scope, thisObject->as<QQmlDelegateModelItemObject>());
152 if (!o)
153 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
154
155 return scope.engine->fromVariant(
156 static_cast<QQmlDMAbstractItemModelData *>(o->d()->item)->modelData());
157}
158
160 const QV4::FunctionObject *b, const QV4::Value *thisObject,
161 const QV4::Value *argv, int argc)
162{
163 QV4::Scope scope(b);
165 if (!o)
166 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
167 if (!argc)
168 return scope.engine->throwTypeError();
169
170 static_cast<QQmlDMAbstractItemModelData *>(o->d()->item)->setModelData(
172
173 return QV4::Encode::undefined();
174}
175
177{
178 if (m_type->propertyRoles.size() == 1) {
179 // If the model has only a single role, the modelData is that role.
180 return index == -1
181 ? m_cachedData.isEmpty() ? QVariant() : m_cachedData[0]
182 : value(m_type->propertyRoles[0]);
183 }
184
185 // If we're using context properties, the model object is also the context object.
186 // In that case we cannot provide modelData. Otherwise return the object itself as modelData.
187 return (contextData->contextObject() == this)
188 ? QVariant()
189 : QVariant::fromValue(this);
190}
191
193{
194 if (m_type->propertyRoles.size() != 1) {
195 qWarning() << "Cannot overwrite model object";
196 return;
197 }
198
199 // If the model has only a single role, the modelData is that role.
200 if (index == -1) {
201 if (m_cachedData.isEmpty())
202 m_cachedData.append(modelData);
203 else
204 m_cachedData[0] = modelData;
205 } else {
206 setValue(m_type->propertyRoles[0], modelData);
207 }
208
209 QMetaObject::activate(this, metaObject(), 0, nullptr);
211}
212
214{
215 if (index >= 0) {
216 if (const QAbstractItemModel *const model = m_type->model->aim())
217 return model->hasChildren(model->index(row, column, m_type->model->rootIndex));
218 }
219 return false;
220}
221
222QVariant QQmlDMAbstractItemModelData::value(int role) const
223{
224 if (const QAbstractItemModel *aim = m_type->model->aim())
225 return aim->index(row, column, m_type->model->rootIndex).data(role);
226 return QVariant();
227}
228
230{
231 if (QAbstractItemModel *aim = m_type->model->aim())
232 aim->setData(aim->index(row, column, m_type->model->rootIndex), value, role);
233}
234
236{
237 if (m_type->prototype.isUndefined()) {
238 QQmlAdaptorModelEngineData * const data = QQmlAdaptorModelEngineData::get(v4);
240 }
241 QV4::Scope scope(v4);
242 QV4::ScopedObject proto(scope, m_type->prototype.value());
243 QV4::ScopedObject o(scope, proto->engine()->memoryManager->allocate<QQmlDelegateModelItemObject>(this));
244 o->setPrototypeOf(proto);
245 ++scriptRef;
246 return o.asReturnedValue();
247}
248
\inmodule QtCore
Definition qhash.h:1093
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1258
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
qsizetype size() const noexcept
Definition qlist.h:386
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
\inmodule QtCore
Definition qmetatype.h:320
QDynamicMetaObjectData * metaObject
Definition qobject.h:77
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:153
QAbstractItemModel * aim()
int rowAt(int index) const
int columnAt(int index) const
QPersistentModelIndex rootIndex
QObject * contextObject() const
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
QQmlDMAbstractItemModelData(const QQmlRefPointer< QQmlDelegateModelItemMetaType > &metaType, VDMAbstractItemModelDataType *dataType, int index, int row, int column)
static QV4::ReturnedValue set_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
QV4::ExecutionEngine * v4
virtual void setModelIndex(int idx, int newRow, int newColumn, bool alwaysEmit=false)
QQmlRefPointer< QQmlContextData > contextData
void addref() const
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
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
ReturnedValue value() const
\inmodule QtCore
Definition qvariant.h:64
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
T value() const &
Definition qvariant.h:511
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
void initializeConstructor(QQmlAdaptorModelEngineData *const data)
QSet< QString >::iterator it
QList< QVariant > arguments
Combined button and popup list for selecting options.
quint64 ReturnedValue
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:162
GLboolean GLboolean GLboolean b
GLuint index
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
#define emit
#define Q_UNUSED(x)
ptrdiff_t qsizetype
Definition qtypes.h:70
obj metaObject() -> className()
\inmodule QtCore
static void activate(QObject *sender, int signal_index, void **argv)
Definition qobject.cpp:4057
static constexpr ReturnedValue undefined()
QV4::ReturnedValue fromVariant(const QVariant &)
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols=true)
ReturnedValue throwTypeError()
ExecutionEngine * engine
const T * as() const
Definition qv4value_p.h:132