| 1 | // Copyright (C) 2016 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 QQMLLISTMODEL_H |
| 5 | #define QQMLLISTMODEL_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/qtqmlmodelsglobal_p.h> |
| 19 | #include <private/qqmlcustomparser_p.h> |
| 20 | |
| 21 | #include <QtCore/QObject> |
| 22 | #include <QtCore/QStringList> |
| 23 | #include <QtCore/QHash> |
| 24 | #include <QtCore/QList> |
| 25 | #include <QtCore/QVariant> |
| 26 | #include <QtCore/qabstractitemmodel.h> |
| 27 | |
| 28 | #include <private/qv4engine_p.h> |
| 29 | #include <private/qpodvector_p.h> |
| 30 | |
| 31 | QT_REQUIRE_CONFIG(qml_list_model); |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | |
| 36 | class QQmlListModelWorkerAgent; |
| 37 | class ListModel; |
| 38 | class ListLayout; |
| 39 | |
| 40 | namespace QV4 { |
| 41 | struct ModelObject; |
| 42 | } |
| 43 | |
| 44 | class Q_QMLMODELS_EXPORT QQmlListModel : public QAbstractListModel |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | Q_PROPERTY(int count READ count NOTIFY countChanged) |
| 48 | Q_PROPERTY(bool dynamicRoles READ dynamicRoles WRITE setDynamicRoles) |
| 49 | Q_PROPERTY(QObject *agent READ agent CONSTANT REVISION(2, 14)) |
| 50 | QML_NAMED_ELEMENT(ListModel) |
| 51 | QML_ADDED_IN_VERSION(2, 0) |
| 52 | QML_CUSTOMPARSER |
| 53 | |
| 54 | public: |
| 55 | QQmlListModel(QObject *parent=nullptr); |
| 56 | ~QQmlListModel(); |
| 57 | |
| 58 | QModelIndex index(int row, int column, const QModelIndex &parent) const override; |
| 59 | int rowCount(const QModelIndex &parent) const override; |
| 60 | QVariant data(const QModelIndex &index, int role) const override; |
| 61 | bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; |
| 62 | QHash<int,QByteArray> roleNames() const override; |
| 63 | |
| 64 | QVariant data(int index, int role) const; |
| 65 | int count() const; |
| 66 | |
| 67 | Q_INVOKABLE void clear(); |
| 68 | Q_INVOKABLE void remove(QQmlV4FunctionPtr args); |
| 69 | Q_INVOKABLE void append(QQmlV4FunctionPtr args); |
| 70 | Q_INVOKABLE void insert(QQmlV4FunctionPtr args); |
| 71 | Q_INVOKABLE QJSValue get(int index) const; |
| 72 | Q_INVOKABLE void set(int index, const QJSValue &value); |
| 73 | Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value); |
| 74 | Q_INVOKABLE void move(int from, int to, int count); |
| 75 | Q_INVOKABLE void sync(); |
| 76 | |
| 77 | QQmlListModelWorkerAgent *agent(); |
| 78 | |
| 79 | bool dynamicRoles() const { return m_dynamicRoles; } |
| 80 | void setDynamicRoles(bool enableDynamicRoles); |
| 81 | |
| 82 | ListModel *listModel() const { return m_listModel; } |
| 83 | |
| 84 | Q_SIGNALS: |
| 85 | void countChanged(); |
| 86 | |
| 87 | private: |
| 88 | friend class QQmlListModelParser; |
| 89 | friend class QQmlListModelWorkerAgent; |
| 90 | friend class ModelObject; |
| 91 | friend struct QV4::ModelObject; |
| 92 | friend class ModelNodeMetaObject; |
| 93 | friend class ListModel; |
| 94 | friend class ListElement; |
| 95 | friend class DynamicRoleModelNode; |
| 96 | friend class DynamicRoleModelNodeMetaObject; |
| 97 | friend struct StringOrTranslation; |
| 98 | |
| 99 | // Constructs a flat list model for a worker agent |
| 100 | QQmlListModel(QQmlListModel *orig, QQmlListModelWorkerAgent *agent); |
| 101 | QQmlListModel(const QQmlListModel *owner, ListModel *data, QV4::ExecutionEngine *engine, QObject *parent=nullptr); |
| 102 | |
| 103 | QV4::ExecutionEngine *engine() const; |
| 104 | |
| 105 | inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); } |
| 106 | |
| 107 | mutable QQmlListModelWorkerAgent *m_agent; |
| 108 | mutable QV4::ExecutionEngine *m_engine; |
| 109 | QQmlRefPointer<QV4::ExecutableCompilationUnit> m_compilationUnit; |
| 110 | bool m_mainThread; |
| 111 | bool m_primary; |
| 112 | |
| 113 | bool m_dynamicRoles; |
| 114 | |
| 115 | ListLayout *m_layout; |
| 116 | ListModel *m_listModel; |
| 117 | std::unique_ptr<QPropertyNotifier> translationChangeHandler; |
| 118 | |
| 119 | QVector<class DynamicRoleModelNode *> m_modelObjects; |
| 120 | QVector<QString> m_roles; |
| 121 | |
| 122 | struct ElementSync |
| 123 | { |
| 124 | DynamicRoleModelNode *src = nullptr; |
| 125 | DynamicRoleModelNode *target = nullptr; |
| 126 | int srcIndex = -1; |
| 127 | int targetIndex = -1; |
| 128 | QVector<int> changedRoles; |
| 129 | }; |
| 130 | |
| 131 | static bool sync(QQmlListModel *src, QQmlListModel *target); |
| 132 | static QQmlListModel *createWithOwner(QQmlListModel *newOwner); |
| 133 | |
| 134 | void emitItemsChanged(int index, int count, const QVector<int> &roles); |
| 135 | void emitItemsAboutToBeInserted(int index, int count); |
| 136 | void emitItemsInserted(); |
| 137 | |
| 138 | void removeElements(int index, int removeCount); |
| 139 | |
| 140 | void updateTranslations(); |
| 141 | }; |
| 142 | |
| 143 | // ### FIXME |
| 144 | class QQmlListElement : public QObject |
| 145 | { |
| 146 | Q_OBJECT |
| 147 | QML_NAMED_ELEMENT(ListElement) |
| 148 | QML_ADDED_IN_VERSION(2, 0) |
| 149 | }; |
| 150 | |
| 151 | class QQmlListModelParser : public QQmlCustomParser |
| 152 | { |
| 153 | public: |
| 154 | enum PropertyType { |
| 155 | Invalid, |
| 156 | Boolean, |
| 157 | Number, |
| 158 | String, |
| 159 | Script |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | QQmlListModelParser() : QQmlCustomParser(QQmlCustomParser::AcceptsSignalHandlers) {} |
| 164 | |
| 165 | void verifyBindings( |
| 166 | const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit, |
| 167 | const QList<const QV4::CompiledData::Binding *> &bindings) override; |
| 168 | void applyBindings( |
| 169 | QObject *obj, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit, |
| 170 | const QList<const QV4::CompiledData::Binding *> &bindings) override; |
| 171 | |
| 172 | private: |
| 173 | bool verifyProperty( |
| 174 | const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit, |
| 175 | const QV4::CompiledData::Binding *binding); |
| 176 | // returns true if a role was set |
| 177 | bool applyProperty( |
| 178 | const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit, |
| 179 | const QV4::CompiledData::Binding *binding, ListModel *model, int outterElementIndex); |
| 180 | |
| 181 | static bool definesEmptyList(const QString &); |
| 182 | |
| 183 | QString listElementTypeName; |
| 184 | }; |
| 185 | |
| 186 | template<> |
| 187 | inline QQmlCustomParser *qmlCreateCustomParser<QQmlListModel>() |
| 188 | { |
| 189 | return new QQmlListModelParser; |
| 190 | } |
| 191 | |
| 192 | QT_END_NAMESPACE |
| 193 | |
| 194 | #endif // QQMLLISTMODEL_H |
| 195 | |