| 1 | // Copyright (C) 2018 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 "qqmladaptormodel_p.h" |
| 5 | |
| 6 | #include <private/qqmldmabstractitemmodeldata_p.h> |
| 7 | #include <private/qqmldmlistaccessordata_p.h> |
| 8 | #include <private/qqmldmobjectdata_p.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QQmlAdaptorModel::Accessors::~Accessors() |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | QQmlAdaptorModel::QQmlAdaptorModel() |
| 17 | : QQmlGuard<QObject>(QQmlAdaptorModel::objectDestroyedImpl, nullptr) |
| 18 | , accessors(&m_nullAccessors) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | QQmlAdaptorModel::~QQmlAdaptorModel() |
| 23 | { |
| 24 | accessors->cleanup(*this); |
| 25 | } |
| 26 | |
| 27 | void QQmlAdaptorModel::setModel(const QVariant &variant) |
| 28 | { |
| 29 | accessors->cleanup(*this); |
| 30 | |
| 31 | // Don't use variant anymore after this. list may transform it. |
| 32 | list.setList(variant); |
| 33 | |
| 34 | modelStrongReference.clear(); |
| 35 | |
| 36 | if (QObject *object = qvariant_cast<QObject *>(v: list.list())) { |
| 37 | if (QQmlData *ddata = QQmlData::get(object)) |
| 38 | modelStrongReference = ddata->jsWrapper; |
| 39 | setObject(object); |
| 40 | if (qobject_cast<QAbstractItemModel *>(object)) |
| 41 | accessors = new VDMAbstractItemModelDataType(this); |
| 42 | else |
| 43 | accessors = new VDMObjectDelegateDataType(this); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | switch (list.type()) { |
| 48 | case QQmlListAccessor::ListProperty: |
| 49 | setObject(static_cast<const QQmlListReference *>(list.list().constData())->object()); |
| 50 | if (QQmlData *ddata = QQmlData::get(object: object())) |
| 51 | modelStrongReference = ddata->jsWrapper; |
| 52 | accessors = new VDMObjectDelegateDataType(this); |
| 53 | break; |
| 54 | case QQmlListAccessor::ObjectList: |
| 55 | case QQmlListAccessor::ObjectSequence: |
| 56 | setObject(nullptr); |
| 57 | accessors = new VDMObjectDelegateDataType(this); |
| 58 | break; |
| 59 | case QQmlListAccessor::StringList: |
| 60 | case QQmlListAccessor::UrlList: |
| 61 | case QQmlListAccessor::VariantList: |
| 62 | case QQmlListAccessor::Integer: |
| 63 | case QQmlListAccessor::ValueSequence: |
| 64 | setObject(nullptr); |
| 65 | accessors = new VDMListDelegateDataType(this); |
| 66 | break; |
| 67 | case QQmlListAccessor::Instance: |
| 68 | case QQmlListAccessor::Invalid: |
| 69 | setObject(nullptr); |
| 70 | accessors = &m_nullAccessors; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void QQmlAdaptorModel::invalidateModel() |
| 76 | { |
| 77 | accessors->cleanup(*this); |
| 78 | accessors = &m_nullAccessors; |
| 79 | // Don't clear the model object as we still need the guard to clear the list variant if the |
| 80 | // object is destroyed. |
| 81 | } |
| 82 | |
| 83 | bool QQmlAdaptorModel::isValid() const |
| 84 | { |
| 85 | return accessors != &m_nullAccessors; |
| 86 | } |
| 87 | |
| 88 | int QQmlAdaptorModel::count() const |
| 89 | { |
| 90 | return rowCount() * columnCount(); |
| 91 | } |
| 92 | |
| 93 | int QQmlAdaptorModel::rowCount() const |
| 94 | { |
| 95 | return qMax(a: 0, b: accessors->rowCount(*this)); |
| 96 | } |
| 97 | |
| 98 | int QQmlAdaptorModel::columnCount() const |
| 99 | { |
| 100 | return qMax(a: 0, b: accessors->columnCount(*this)); |
| 101 | } |
| 102 | |
| 103 | int QQmlAdaptorModel::rowAt(int index) const |
| 104 | { |
| 105 | int count = rowCount(); |
| 106 | return count <= 0 ? -1 : index % count; |
| 107 | } |
| 108 | |
| 109 | int QQmlAdaptorModel::columnAt(int index) const |
| 110 | { |
| 111 | int count = rowCount(); |
| 112 | return count <= 0 ? -1 : index / count; |
| 113 | } |
| 114 | |
| 115 | int QQmlAdaptorModel::indexAt(int row, int column) const |
| 116 | { |
| 117 | return column * rowCount() + row; |
| 118 | } |
| 119 | |
| 120 | void QQmlAdaptorModel::useImportVersion(QTypeRevision revision) |
| 121 | { |
| 122 | modelItemRevision = revision; |
| 123 | } |
| 124 | |
| 125 | void QQmlAdaptorModel::objectDestroyedImpl(QQmlGuardImpl *guard) |
| 126 | { |
| 127 | auto This = static_cast<QQmlAdaptorModel *>(guard); |
| 128 | This->setModel(QVariant()); |
| 129 | } |
| 130 | |
| 131 | QT_END_NAMESPACE |
| 132 |
