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
qqmldmlistaccessordata_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 QQMLDMLISTACCESSORDATA_P_H
5#define QQMLDMLISTACCESSORDATA_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
25
27{
31public:
33 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType,
34 VDMListDelegateDataType *dataType, int index, int row, int column,
35 const QVariant &value);
37
39 {
40 return cachedData;
41 }
42
43 void setModelData(const QVariant &data);
44
45 static QV4::ReturnedValue get_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int)
46 {
47 QV4::ExecutionEngine *v4 = b->engine();
49 if (!o)
50 return v4->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
51
52 return v4->fromVariant(static_cast<QQmlDMListAccessorData *>(o->d()->item)->cachedData);
53 }
54
55 static QV4::ReturnedValue set_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
56 {
57 QV4::ExecutionEngine *v4 = b->engine();
59 if (!o)
60 return v4->throwTypeError(QStringLiteral("Not a valid DelegateModel object"));
61 if (!argc)
62 return v4->throwTypeError();
63
64 static_cast<QQmlDMListAccessorData *>(o->d()->item)->setModelData(
67 }
68
70 {
71 QQmlAdaptorModelEngineData *data = QQmlAdaptorModelEngineData::get(v4);
72 QV4::Scope scope(v4);
74 QV4::ScopedObject p(scope, data->listItemProto.value());
75 o->setPrototypeOf(p);
76 ++scriptRef;
77 return o.asReturnedValue();
78 }
79
80 void setValue(const QString &role, const QVariant &value) override;
81 bool resolveIndex(const QQmlAdaptorModel &model, int idx) override;
82
85
86private:
88 QVariant cachedData;
89
90 // Gets cleaned when the metaobject has processed it.
91 bool cachedDataClean = false;
92};
93
94
96 : public QQmlRefCounted<VDMListDelegateDataType>
99{
100public:
102 : model(model)
103 {
104 QQmlAdaptorModelEngineData::setModelDataType<QQmlDMListAccessorData>(&builder, this);
106 *static_cast<QMetaObject *>(this) = *metaObject.data();
107 }
108
109 void cleanup(QQmlAdaptorModel &) const override
110 {
111 release();
112 }
113
114 int rowCount(const QQmlAdaptorModel &model) const override
115 {
116 return model.list.count();
117 }
118
119 int columnCount(const QQmlAdaptorModel &model) const override
120 {
121 switch (model.list.type()) {
123 return 0;
127 return 1;
128 default:
129 break;
130 }
131
132 // If there are no properties, we can get modelData itself.
133 return std::max(1, propertyCount() - propertyOffset);
134 }
135
137 {
138 if (const QMetaObject *metaObject = type.metaObject())
139 return metaObject;
140
141 // NB: This acquires the lock on QQmlMetaTypeData. If we had a QQmlEngine here,
142 // we could use QQmlGadgetPtrWrapper::instance() to avoid this.
143 if (const QQmlValueType *valueType = QQmlMetaType::valueType(type))
144 return valueType->staticMetaObject();
145
146 return nullptr;
147 }
148
149 template<typename String>
150 static QString toQString(const String &string)
151 {
152 if constexpr (std::is_same_v<String, QString>)
153 return string;
154 else if constexpr (std::is_same_v<String, QByteArray>)
155 return QString::fromUtf8(string);
156 else if constexpr (std::is_same_v<String, const char *>)
157 return QString::fromUtf8(string);
158 Q_UNREACHABLE_RETURN(QString());
159 }
160
161 template<typename String>
162 static QByteArray toUtf8(const String &string)
163 {
164 if constexpr (std::is_same_v<String, QString>)
165 return string.toUtf8();
166 else if constexpr (std::is_same_v<String, QByteArray>)
167 return string;
168 else if constexpr (std::is_same_v<String, const char *>)
169 return QByteArray::fromRawData(string, qstrlen(string));
170 Q_UNREACHABLE_RETURN(QByteArray());
171 }
172
173 template<typename String>
174 static QVariant value(const QVariant *row, const String &role)
175 {
176 const QMetaType type = row->metaType();
177 if (type == QMetaType::fromType<QVariantMap>())
178 return row->toMap().value(toQString(role));
179
180 if (type == QMetaType::fromType<QVariantHash>())
181 return row->toHash().value(toQString(role));
182
183 const QMetaType::TypeFlags typeFlags = type.flags();
184 if (typeFlags & QMetaType::PointerToQObject)
185 return row->value<QObject *>()->property(toUtf8(role));
186
188 const int propertyIndex = metaObject->indexOfProperty(toUtf8(role));
189 if (propertyIndex >= 0)
190 return metaObject->property(propertyIndex).readOnGadget(row->constData());
191 }
192
193 return QVariant();
194 }
195
196 template<typename String>
197 void createPropertyIfMissing(const String &string)
198 {
199 for (int i = 0, end = propertyCount(); i < end; ++i) {
200 if (QAnyStringView(property(i).name()) == QAnyStringView(string))
201 return;
202 }
203
204 createProperty(toUtf8(string), nullptr);
205 }
206
208 {
209 const QMetaType type = row->metaType();
210 if (type == QMetaType::fromType<QVariantMap>()) {
211 const QVariantMap map = row->toMap();
212 for (auto it = map.keyBegin(), end = map.keyEnd(); it != end; ++it)
214 } else if (type == QMetaType::fromType<QVariantHash>()) {
215 const QVariantHash map = row->toHash();
216 for (auto it = map.keyBegin(), end = map.keyEnd(); it != end; ++it)
218 } else if (type.flags() & QMetaType::PointerToQObject) {
219 const QMetaObject *metaObject = row->value<QObject *>()->metaObject();
220 for (int i = 0, end = metaObject->propertyCount(); i < end; ++i)
222 } else if (const QMetaObject *metaObject = metaObjectFromType(type)) {
223 for (int i = 0, end = metaObject->propertyCount(); i < end; ++i)
225 }
226 }
227
228 template<typename String>
229 static void setValue(QVariant *row, const String &role, const QVariant &value)
230 {
231 const QMetaType type = row->metaType();
232 if (type == QMetaType::fromType<QVariantMap>()) {
233 static_cast<QVariantMap *>(row->data())->insert(toQString(role), value);
234 } else if (type == QMetaType::fromType<QVariantHash>()) {
235 static_cast<QVariantHash *>(row->data())->insert(toQString(role), value);
236 } else if (type.flags() & QMetaType::PointerToQObject) {
237 row->value<QObject *>()->setProperty(toUtf8(role), value);
238 } else if (const QMetaObject *metaObject = metaObjectFromType(type)) {
239 const int propertyIndex = metaObject->indexOfProperty(toUtf8(role));
240 if (propertyIndex >= 0)
241 metaObject->property(propertyIndex).writeOnGadget(row->data(), value);
242 }
243 }
244
245 QVariant value(const QQmlAdaptorModel &model, int index, const QString &role) const override
246 {
247 const QVariant entry = model.list.at(index);
248 if (role == QLatin1String("modelData") || role.isEmpty())
249 return entry;
250
251 return value(&entry, role);
252 }
253
256 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType,
257 int index, int row, int column) override
258 {
259 const QVariant value = (index >= 0 && index < model.list.count())
260 ? model.list.at(index)
261 : QVariant();
262 return new QQmlDMListAccessorData(metaType, this, index, row, column, value);
263 }
264
265 bool notify(const QQmlAdaptorModel &model, const QList<QQmlDelegateModelItem *> &items, int index, int count, const QVector<int> &) const override
266 {
267 for (auto modelItem : items) {
268 const int modelItemIndex = modelItem->index;
269 if (modelItemIndex < index || modelItemIndex >= index + count)
270 continue;
271
272 auto listModelItem = static_cast<QQmlDMListAccessorData *>(modelItem);
273 QVariant updatedModelData = model.list.at(listModelItem->index);
274 listModelItem->setModelData(updatedModelData);
275 }
276 return true;
277 }
278
279 void emitAllSignals(QQmlDMListAccessorData *accessor) const;
280
281 int metaCall(QObject *object, QMetaObject::Call call, int id, void **arguments) final;
282 int createProperty(const char *name, const char *) final;
283 QMetaObject *toDynamicMetaObject(QObject *accessors) final;
284
289};
290
292
293#endif // QQMLDMLISTACCESSORDATA_P_H
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:409
T value(const Key &key) const noexcept
Definition qhash.h:1054
key_iterator keyBegin() const
Definition qmap.h:606
key_iterator keyEnd() const
Definition qmap.h:607
QMetaObject * toMetaObject() const
Converts this meta object builder into a concrete QMetaObject.
QVariant readOnGadget(const void *gadget) const
bool writeOnGadget(void *gadget, const QVariant &value) const
const char * name() const
Returns this property's name.
\inmodule QtCore
Definition qmetatype.h:341
constexpr TypeFlags flags() const
Definition qmetatype.h:2658
@ PointerToQObject
Definition qmetatype.h:406
\inmodule QtCore
Definition qobject.h:103
QScopedPointer< QMetaObject, QScopedPointerPodDeleter > metaObject
QQmlListAccessor list
void setModelData(const QVariant &data)
QQmlDMListAccessorData(const QQmlRefPointer< QQmlDelegateModelItemMetaType > &metaType, VDMListDelegateDataType *dataType, int index, int row, int column, const QVariant &value)
bool resolveIndex(const QQmlAdaptorModel &model, int idx) override
void setValue(const QString &role, const QVariant &value) override
static QV4::ReturnedValue get_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int)
static QV4::ReturnedValue set_modelData(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
QV4::ReturnedValue get() override
QV4::ExecutionEngine * v4
QQmlRefPointer< QQmlDelegateModelItemMetaType > const metaType
QVariant at(qsizetype) const
qsizetype count() const
static QQmlValueType * valueType(QMetaType metaType)
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.
\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
ObjectType::Data * allocate(Args &&... args)
Definition qv4mm_p.h:298
\inmodule QtCore
Definition qvariant.h:65
void emitAllSignals(QQmlDMListAccessorData *accessor) const
void createMissingProperties(const QVariant *row)
int rowCount(const QQmlAdaptorModel &model) const override
bool notify(const QQmlAdaptorModel &model, const QList< QQmlDelegateModelItem * > &items, int index, int count, const QVector< int > &) const override
VDMListDelegateDataType(QQmlAdaptorModel *model)
int createProperty(const char *name, const char *) final
QMetaObject * toDynamicMetaObject(QObject *accessors) final
static const QMetaObject * metaObjectFromType(QMetaType type)
static QString toQString(const String &string)
QQmlDelegateModelItem * createItem(QQmlAdaptorModel &model, const QQmlRefPointer< QQmlDelegateModelItemMetaType > &metaType, int index, int row, int column) override
int metaCall(QObject *object, QMetaObject::Call call, int id, void **arguments) final
static QVariant value(const QVariant *row, const String &role)
void createPropertyIfMissing(const String &string)
QVariant value(const QQmlAdaptorModel &model, int index, const QString &role) const override
static QByteArray toUtf8(const String &string)
int columnCount(const QQmlAdaptorModel &model) const override
static void setValue(QVariant *row, const String &role, const QVariant &value)
void cleanup(QQmlAdaptorModel &) const override
QMap< QString, QString > map
[6]
cache insert(employee->id(), employee)
QSet< QString >::iterator it
QList< QVariant > arguments
Combined button and popup list for selecting options.
quint64 ReturnedValue
size_t qstrlen(const char *str)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define QT_ANONYMOUS_PROPERTY(...)
Definition qobject_p.h:38
GLboolean GLboolean GLboolean b
GLuint index
[2]
GLuint GLuint end
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLuint name
GLenum GLenum GLsizei void GLsizei void * column
GLuint entry
GLenum GLenum GLsizei void * row
GLfloat GLfloat p
[1]
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SIGNALS
const char property[13]
Definition qwizard.cpp:101
QList< QTreeWidgetItem * > items
args<< 1<< 2;QJSValue threeAgain=fun.call(args);QString fileName="helloworld.qs";QFile scriptFile(fileName);if(!scriptFile.open(QIODevice::ReadOnly)) QTextStream stream(&scriptFile);QString contents=stream.readAll();scriptFile.close();myEngine.evaluate(contents, fileName);myEngine.globalObject().setProperty("myNumber", 123);...QJSValue myNumberPlusOne=myEngine.evaluate("myNumber + 1");QJSValue result=myEngine.evaluate(...);if(result.isError()) qDebug()<< "Uncaught exception at line"<< result.property("lineNumber").toInt()<< ":"<< result.toString();QPushButton *button=new QPushButton;QJSValue scriptButton=myEngine.newQObject(button);myEngine.globalObject().setProperty("button", scriptButton);myEngine.evaluate("button.checkable = true");qDebug()<< scriptButton.property("checkable").toBool();scriptButton.property("show").call();QJSEngine engine;QObject *myQObject=new QObject();myQObject- setProperty)("dynamicProperty", 3)
\inmodule QtCore
int propertyCount() const
Returns the number of properties in this class, including the number of properties provided by each b...
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
QMetaType metaType() const
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
static constexpr const QMetaObject * staticMetaObject()
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()
const T * as() const
Definition qv4value_p.h:132