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.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
9 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType,
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 QQmlGuard<QQmlDMAbstractItemModelData> guard(this);
46 setValue(m_type->propertyRoles.at(propertyIndex), *static_cast<QVariant *>(arguments[0]));
47 if (guard.isNull())
48 return -1;
49
50 QMetaObject::activate(this, meta, propertyIndex, nullptr);
51 }
53 return -1;
54 } else {
55 return qt_metacall(call, id, arguments);
56 }
57}
58
60{
61 // Used only for initialization of the cached data. Does not have to emit change signals.
62
63 if (m_type->propertyRoles.size() == 1
64 && (role.isEmpty() || role == QLatin1String("modelData"))) {
65 // If the model has only a single role, the modelData is that role.
66 m_cachedData[0] = value;
67 return;
68 }
69
70 const auto it = m_type->roleNames.constFind(role.toUtf8());
71 if (it != m_type->roleNames.cend()) {
72 for (int i = 0; i < m_type->propertyRoles.size(); ++i) {
73 if (m_type->propertyRoles.at(i) == *it) {
74 m_cachedData[i] = value;
75 return;
76 }
77 }
78 }
79}
80
82{
83 if (index == -1) {
84 Q_ASSERT(idx >= 0);
85 m_cachedData.clear();
86 setModelIndex(idx, adaptorModel.rowAt(idx), adaptorModel.columnAt(idx));
87 const QMetaObject *meta = metaObject();
88 const int propertyCount = m_type->propertyRoles.size();
89 for (int i = 0; i < propertyCount; ++i)
90 QMetaObject::activate(this, meta, i, nullptr);
92 return true;
93 } else {
94 return false;
95 }
96}
97
99{
100 QV4::Scope scope(b);
102 if (!o)
103 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
104
105 const qsizetype propertyId = static_cast<const QV4::IndexedBuiltinFunction *>(b)->d()->index;
106
108 if (o->d()->item->index == -1) {
109 if (!modelData->m_cachedData.isEmpty())
110 return scope.engine->fromVariant(modelData->m_cachedData.at(propertyId));
111 } else if (*modelData->m_type->model) {
112 return scope.engine->fromVariant(
113 modelData->value(modelData->m_type->propertyRoles.at(propertyId)));
114 }
115 return QV4::Encode::undefined();
116}
117
119{
120 QV4::Scope scope(b);
122 if (!o)
123 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
124 if (!argc)
125 return scope.engine->throwTypeError();
126
127 const qsizetype propertyId = static_cast<const QV4::IndexedBuiltinFunction *>(b)->d()->index;
128
129 if (o->d()->item->index == -1) {
131 if (!modelData->m_cachedData.isEmpty()) {
132 if (modelData->m_cachedData.size() > 1) {
133 modelData->m_cachedData[propertyId]
135 QMetaObject::activate(o->d()->item, o->d()->item->metaObject(), propertyId, nullptr);
136 } else if (modelData->m_cachedData.size() == 1) {
137 modelData->m_cachedData[0] = QV4::ExecutionEngine::toVariant(argv[0], QMetaType {});
138 QMetaObject::activate(o->d()->item, o->d()->item->metaObject(), 0, nullptr);
139 }
140 emit modelData->modelDataChanged();
141 }
142 }
143 return QV4::Encode::undefined();
144}
145
147 const QV4::FunctionObject *b, const QV4::Value *thisObject,
148 const QV4::Value *argv, int argc)
149{
150 Q_UNUSED(argv)
151 Q_UNUSED(argc)
152
153 QV4::Scope scope(b);
155 scope, thisObject->as<QQmlDelegateModelItemObject>());
156 if (!o)
157 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
158
159 return scope.engine->fromVariant(
160 static_cast<QQmlDMAbstractItemModelData *>(o->d()->item)->modelData());
161}
162
164 const QV4::FunctionObject *b, const QV4::Value *thisObject,
165 const QV4::Value *argv, int argc)
166{
167 QV4::Scope scope(b);
169 if (!o)
170 return scope.engine->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
171 if (!argc)
172 return scope.engine->throwTypeError();
173
174 static_cast<QQmlDMAbstractItemModelData *>(o->d()->item)->setModelData(
176
177 return QV4::Encode::undefined();
178}
179
181{
182 if (m_type->propertyRoles.size() == 1) {
183 // If the model has only a single role, the modelData is that role.
184 return index == -1
185 ? m_cachedData.isEmpty() ? QVariant() : m_cachedData[0]
186 : value(m_type->propertyRoles[0]);
187 }
188
189 // If we're using context properties, the model object is also the context object.
190 // In that case we cannot provide modelData. Otherwise return the object itself as modelData.
191 return (contextData->contextObject() == this)
192 ? QVariant()
193 : QVariant::fromValue(this);
194}
195
197{
198 if (m_type->propertyRoles.size() != 1) {
199 qWarning() << "Cannot overwrite model object";
200 return;
201 }
202
203 // If the model has only a single role, the modelData is that role.
204 if (index == -1) {
205 if (m_cachedData.isEmpty())
206 m_cachedData.append(modelData);
207 else
208 m_cachedData[0] = modelData;
209 } else {
210 setValue(m_type->propertyRoles[0], modelData);
211 }
212
213 QMetaObject::activate(this, metaObject(), 0, nullptr);
215}
216
218{
219 if (index >= 0) {
220 if (const QAbstractItemModel *const model = m_type->model->aim())
221 return model->hasChildren(model->index(row, column, m_type->model->rootIndex));
222 }
223 return false;
224}
225
226QVariant QQmlDMAbstractItemModelData::value(int role) const
227{
228 if (const QAbstractItemModel *aim = m_type->model->aim())
229 return aim->index(row, column, m_type->model->rootIndex).data(role);
230 return QVariant();
231}
232
234{
235 if (QAbstractItemModel *aim = m_type->model->aim())
236 aim->setData(aim->index(row, column, m_type->model->rootIndex), value, role);
237}
238
240{
241 if (m_type->prototype.isUndefined()) {
242 QQmlAdaptorModelEngineData * const data = QQmlAdaptorModelEngineData::get(v4);
244 }
245 QV4::Scope scope(v4);
246 QV4::ScopedObject proto(scope, m_type->prototype.value());
248 o->setPrototypeOf(proto);
249 ++scriptRef;
250 return o.asReturnedValue();
251}
252
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1299
const_iterator cend() const noexcept
Definition qhash.h:1218
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
\inmodule QtCore
Definition qmetatype.h:341
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:150
QAbstractItemModel * aim()
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:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QByteArray toUtf8() const &
Definition qstring.h:634
ObjectType::Data * allocate(Args &&... args)
Definition qv4mm_p.h:298
ReturnedValue value() const
\inmodule QtCore
Definition qvariant.h:65
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
T value() const &
Definition qvariant.h:516
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
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:166
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:165
obj metaObject() -> className()
\inmodule QtCore
static void activate(QObject *sender, int signal_index, void **argv)
Definition qobject.cpp:4193
static constexpr ReturnedValue undefined()
MemoryManager * memoryManager
QV4::ReturnedValue fromVariant(const QVariant &)
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols=true)
ReturnedValue throwTypeError()
ExecutionEngine * engine() const
ExecutionEngine * engine
const T * as() const
Definition qv4value_p.h:132