| 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 | |
| 22 | QT_BEGIN_NAMESPACE |
| 23 | |
| 24 | class VDMListDelegateDataType; |
| 25 | |
| 26 | class QQmlDMListAccessorData : public QQmlDelegateModelItem |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | Q_PROPERTY(QVariant modelData READ modelData WRITE setModelData NOTIFY modelDataChanged) |
| 30 | QT_ANONYMOUS_PROPERTY(QVariant READ modelData WRITE setModelData NOTIFY modelDataChanged FINAL) |
| 31 | public: |
| 32 | QQmlDMListAccessorData( |
| 33 | const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType, |
| 34 | VDMListDelegateDataType *dataType, int index, int row, int column, |
| 35 | const QVariant &value); |
| 36 | ~QQmlDMListAccessorData(); |
| 37 | |
| 38 | QVariant modelData() const |
| 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(); |
| 48 | const QQmlDelegateModelItemObject *o = thisObject->as<QQmlDelegateModelItemObject>(); |
| 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(); |
| 58 | const QQmlDelegateModelItemObject *o = thisObject->as<QQmlDelegateModelItemObject>(); |
| 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( |
| 65 | QV4::ExecutionEngine::toVariant(value: argv[0], typeHint: QMetaType {})); |
| 66 | return QV4::Encode::undefined(); |
| 67 | } |
| 68 | |
| 69 | QV4::ReturnedValue get() override |
| 70 | { |
| 71 | QQmlAdaptorModelEngineData *data = QQmlAdaptorModelEngineData::get(engine: v4); |
| 72 | QV4::Scope scope(v4); |
| 73 | QV4::ScopedObject o(scope, v4->memoryManager->allocate<QQmlDelegateModelItemObject>(args: this)); |
| 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 | |
| 83 | Q_SIGNALS: |
| 84 | void modelDataChanged(); |
| 85 | |
| 86 | private: |
| 87 | friend class VDMListDelegateDataType; |
| 88 | QVariant cachedData; |
| 89 | |
| 90 | // Gets cleaned when the metaobject has processed it. |
| 91 | bool cachedDataClean = false; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | class VDMListDelegateDataType final |
| 96 | : public QQmlRefCounted<VDMListDelegateDataType> |
| 97 | , public QQmlAdaptorModel::Accessors |
| 98 | , public QAbstractDynamicMetaObject |
| 99 | { |
| 100 | public: |
| 101 | VDMListDelegateDataType(QQmlAdaptorModel *model) |
| 102 | : model(model) |
| 103 | { |
| 104 | QQmlAdaptorModelEngineData::setModelDataType<QQmlDMListAccessorData>(builder: &builder, metaType: this); |
| 105 | metaObject.reset(other: builder.toMetaObject()); |
| 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()) { |
| 122 | case QQmlListAccessor::Invalid: |
| 123 | return 0; |
| 124 | case QQmlListAccessor::StringList: |
| 125 | case QQmlListAccessor::UrlList: |
| 126 | case QQmlListAccessor::Integer: |
| 127 | return 1; |
| 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | // If there are no properties, we can get modelData itself. |
| 133 | return std::max(a: 1, b: propertyCount() - propertyOffset); |
| 134 | } |
| 135 | |
| 136 | static const QMetaObject *metaObjectFromType(QMetaType type) |
| 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(metaType: 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(data: string, size: 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(key: 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(name: toUtf8(role)); |
| 186 | |
| 187 | if (const QMetaObject *metaObject = metaObjectFromType(type)) { |
| 188 | const int propertyIndex = metaObject->indexOfProperty(name: toUtf8(role)); |
| 189 | if (propertyIndex >= 0) |
| 190 | return metaObject->property(index: propertyIndex).readOnGadget(gadget: 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(index: i).name()) == QAnyStringView(string)) |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | createProperty(name: toUtf8(string), nullptr); |
| 205 | } |
| 206 | |
| 207 | void createMissingProperties(const QVariant *row) |
| 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) |
| 213 | createPropertyIfMissing(string: *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) |
| 217 | createPropertyIfMissing(string: *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) |
| 221 | createPropertyIfMissing(string: metaObject->property(index: i).name()); |
| 222 | } else if (const QMetaObject *metaObject = metaObjectFromType(type)) { |
| 223 | for (int i = 0, end = metaObject->propertyCount(); i < end; ++i) |
| 224 | createPropertyIfMissing(string: metaObject->property(index: i).name()); |
| 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(name: toUtf8(role)); |
| 240 | if (propertyIndex >= 0) |
| 241 | metaObject->property(index: propertyIndex).writeOnGadget(gadget: 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(row: &entry, role); |
| 252 | } |
| 253 | |
| 254 | QQmlDelegateModelItem *createItem( |
| 255 | QQmlAdaptorModel &model, |
| 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 | |
| 285 | QMetaObjectBuilder builder; |
| 286 | QQmlAdaptorModel *model = nullptr; |
| 287 | int propertyOffset = 0; |
| 288 | int signalOffset = 0; |
| 289 | }; |
| 290 | |
| 291 | QT_END_NAMESPACE |
| 292 | |
| 293 | #endif // QQMLDMLISTACCESSORDATA_P_H |
| 294 | |