| 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 QQMLDATAMODEL_P_H |
| 5 | #define QQMLDATAMODEL_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/qtqmlmodelsglobal_p.h> |
| 19 | #include <private/qqmllistcompositor_p.h> |
| 20 | #include <private/qqmlobjectmodel_p.h> |
| 21 | #include <private/qqmlincubator_p.h> |
| 22 | |
| 23 | #include <QtCore/qabstractitemmodel.h> |
| 24 | #include <QtCore/qstringlist.h> |
| 25 | |
| 26 | QT_REQUIRE_CONFIG(qml_delegate_model); |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | class QQmlChangeSet; |
| 31 | class QQuickPackage; |
| 32 | class QQmlDelegateModelGroup; |
| 33 | class QQmlDelegateModelAttached; |
| 34 | class QQmlDelegateModelPrivate; |
| 35 | |
| 36 | |
| 37 | class Q_QMLMODELS_EXPORT QQmlDelegateModel : public QQmlInstanceModel, public QQmlParserStatus |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | Q_DECLARE_PRIVATE(QQmlDelegateModel) |
| 41 | |
| 42 | Q_PROPERTY(QVariant model READ model WRITE setModel) |
| 43 | Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) |
| 44 | Q_PROPERTY(QString filterOnGroup READ filterGroup WRITE setFilterGroup NOTIFY filterGroupChanged RESET resetFilterGroup) |
| 45 | Q_PROPERTY(QQmlDelegateModelGroup *items READ items CONSTANT) //TODO : worth renaming? |
| 46 | Q_PROPERTY(QQmlDelegateModelGroup *persistedItems READ persistedItems CONSTANT) |
| 47 | Q_PROPERTY(QQmlListProperty<QQmlDelegateModelGroup> groups READ groups CONSTANT) |
| 48 | Q_PROPERTY(QObject *parts READ parts CONSTANT) |
| 49 | Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged) |
| 50 | Q_CLASSINFO("DefaultProperty" , "delegate" ) |
| 51 | QML_NAMED_ELEMENT(DelegateModel) |
| 52 | QML_ADDED_IN_VERSION(2, 1) |
| 53 | QML_ATTACHED(QQmlDelegateModelAttached) |
| 54 | Q_INTERFACES(QQmlParserStatus) |
| 55 | |
| 56 | public: |
| 57 | QQmlDelegateModel(); |
| 58 | QQmlDelegateModel(QQmlContext *, QObject *parent=nullptr); |
| 59 | ~QQmlDelegateModel(); |
| 60 | |
| 61 | void classBegin() override; |
| 62 | void componentComplete() override; |
| 63 | |
| 64 | QVariant model() const; |
| 65 | void setModel(const QVariant &); |
| 66 | |
| 67 | QQmlComponent *delegate() const; |
| 68 | void setDelegate(QQmlComponent *); |
| 69 | |
| 70 | QVariant rootIndex() const; |
| 71 | void setRootIndex(const QVariant &root); |
| 72 | |
| 73 | Q_INVOKABLE QVariant modelIndex(int idx) const; |
| 74 | Q_INVOKABLE QVariant parentModelIndex() const; |
| 75 | |
| 76 | int count() const override; |
| 77 | bool isValid() const override { return delegate() != nullptr; } |
| 78 | QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override; |
| 79 | ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) override; |
| 80 | void cancel(int index) override; |
| 81 | QVariant variantValue(int index, const QString &role) override; |
| 82 | void setWatchedRoles(const QList<QByteArray> &roles) override; |
| 83 | QQmlIncubator::Status incubationStatus(int index) override; |
| 84 | |
| 85 | void drainReusableItemsPool(int maxPoolTime) override; |
| 86 | int poolSize() override; |
| 87 | |
| 88 | int indexOf(QObject *object, QObject *objectContext) const override; |
| 89 | |
| 90 | QString filterGroup() const; |
| 91 | void setFilterGroup(const QString &group); |
| 92 | void resetFilterGroup(); |
| 93 | |
| 94 | QQmlDelegateModelGroup *items(); |
| 95 | QQmlDelegateModelGroup *persistedItems(); |
| 96 | QQmlListProperty<QQmlDelegateModelGroup> groups(); |
| 97 | QObject *parts(); |
| 98 | |
| 99 | const QAbstractItemModel *abstractItemModel() const override; |
| 100 | |
| 101 | bool event(QEvent *) override; |
| 102 | |
| 103 | static QQmlDelegateModelAttached *qmlAttachedProperties(QObject *obj); |
| 104 | |
| 105 | Q_SIGNALS: |
| 106 | void filterGroupChanged(); |
| 107 | void defaultGroupsChanged(); |
| 108 | void rootIndexChanged(); |
| 109 | void delegateChanged(); |
| 110 | |
| 111 | private Q_SLOTS: |
| 112 | void _q_itemsChanged(int index, int count, const QVector<int> &roles); |
| 113 | void _q_itemsInserted(int index, int count); |
| 114 | void _q_itemsRemoved(int index, int count); |
| 115 | void _q_itemsMoved(int from, int to, int count); |
| 116 | void _q_modelAboutToBeReset(); |
| 117 | void _q_rowsInserted(const QModelIndex &,int,int); |
| 118 | void _q_columnsInserted(const QModelIndex &, int, int); |
| 119 | void _q_columnsRemoved(const QModelIndex &, int, int); |
| 120 | void _q_columnsMoved(const QModelIndex &, int, int, const QModelIndex &, int); |
| 121 | void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end); |
| 122 | void _q_rowsRemoved(const QModelIndex &,int,int); |
| 123 | void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int); |
| 124 | void _q_dataChanged(const QModelIndex&,const QModelIndex&,const QVector<int> &); |
| 125 | void _q_layoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint); |
| 126 | |
| 127 | private: |
| 128 | void handleModelReset(); |
| 129 | bool isDescendantOf(const QPersistentModelIndex &desc, const QList<QPersistentModelIndex> &parents) const; |
| 130 | |
| 131 | Q_DISABLE_COPY(QQmlDelegateModel) |
| 132 | }; |
| 133 | |
| 134 | class QQmlDelegateModelGroupPrivate; |
| 135 | class Q_QMLMODELS_EXPORT QQmlDelegateModelGroup : public QObject |
| 136 | { |
| 137 | Q_OBJECT |
| 138 | Q_PROPERTY(int count READ count NOTIFY countChanged) |
| 139 | Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) |
| 140 | Q_PROPERTY(bool includeByDefault READ defaultInclude WRITE setDefaultInclude NOTIFY defaultIncludeChanged) |
| 141 | QML_NAMED_ELEMENT(DelegateModelGroup) |
| 142 | QML_ADDED_IN_VERSION(2, 1) |
| 143 | public: |
| 144 | QQmlDelegateModelGroup(QObject *parent = nullptr); |
| 145 | QQmlDelegateModelGroup(const QString &name, QQmlDelegateModel *model, int compositorType, QObject *parent = nullptr); |
| 146 | ~QQmlDelegateModelGroup(); |
| 147 | |
| 148 | QString name() const; |
| 149 | void setName(const QString &name); |
| 150 | |
| 151 | int count() const; |
| 152 | |
| 153 | bool defaultInclude() const; |
| 154 | void setDefaultInclude(bool include); |
| 155 | |
| 156 | Q_INVOKABLE QJSValue get(int index); |
| 157 | |
| 158 | public Q_SLOTS: |
| 159 | void insert(QQmlV4FunctionPtr); |
| 160 | void create(QQmlV4FunctionPtr); |
| 161 | void resolve(QQmlV4FunctionPtr); |
| 162 | void remove(QQmlV4FunctionPtr); |
| 163 | void addGroups(QQmlV4FunctionPtr); |
| 164 | void removeGroups(QQmlV4FunctionPtr); |
| 165 | void setGroups(QQmlV4FunctionPtr); |
| 166 | void move(QQmlV4FunctionPtr); |
| 167 | |
| 168 | Q_SIGNALS: |
| 169 | void countChanged(); |
| 170 | void nameChanged(); |
| 171 | void defaultIncludeChanged(); |
| 172 | void changed(const QJSValue &removed, const QJSValue &inserted); |
| 173 | private: |
| 174 | Q_DECLARE_PRIVATE(QQmlDelegateModelGroup) |
| 175 | }; |
| 176 | |
| 177 | class QQmlDelegateModelItem; |
| 178 | class QQmlDelegateModelAttachedMetaObject; |
| 179 | class QQmlDelegateModelAttached : public QObject |
| 180 | { |
| 181 | Q_OBJECT |
| 182 | Q_PROPERTY(QQmlDelegateModel *model READ model CONSTANT FINAL) |
| 183 | Q_PROPERTY(QStringList groups READ groups WRITE setGroups NOTIFY groupsChanged FINAL) |
| 184 | Q_PROPERTY(bool isUnresolved READ isUnresolved NOTIFY unresolvedChanged FINAL) |
| 185 | Q_PROPERTY(bool inPersistedItems READ inPersistedItems WRITE setInPersistedItems NOTIFY groupsChanged FINAL) |
| 186 | Q_PROPERTY(bool inItems READ inItems WRITE setInItems NOTIFY groupsChanged FINAL) |
| 187 | Q_PROPERTY(int persistedItemsIndex READ persistedItemsIndex NOTIFY groupsChanged FINAL) |
| 188 | Q_PROPERTY(int itemsIndex READ itemsIndex NOTIFY groupsChanged FINAL) |
| 189 | |
| 190 | public: |
| 191 | QQmlDelegateModelAttached(QObject *parent); |
| 192 | QQmlDelegateModelAttached(QQmlDelegateModelItem *cacheItem, QObject *parent); |
| 193 | ~QQmlDelegateModelAttached() {} |
| 194 | |
| 195 | void resetCurrentIndex(); |
| 196 | void setCacheItem(QQmlDelegateModelItem *item); |
| 197 | |
| 198 | void setInPersistedItems(bool inPersisted); |
| 199 | bool inPersistedItems() const; |
| 200 | int persistedItemsIndex() const; |
| 201 | |
| 202 | void setInItems(bool inItems); |
| 203 | bool inItems() const; |
| 204 | int itemsIndex() const; |
| 205 | |
| 206 | QQmlDelegateModel *model() const; |
| 207 | |
| 208 | QStringList groups() const; |
| 209 | void setGroups(const QStringList &groups); |
| 210 | |
| 211 | bool isUnresolved() const; |
| 212 | |
| 213 | void emitChanges(); |
| 214 | |
| 215 | void emitUnresolvedChanged() { Q_EMIT unresolvedChanged(); } |
| 216 | |
| 217 | Q_SIGNALS: |
| 218 | void groupsChanged(); |
| 219 | void unresolvedChanged(); |
| 220 | |
| 221 | private: |
| 222 | void setInGroup(QQmlListCompositor::Group group, bool inGroup); |
| 223 | |
| 224 | public: |
| 225 | QQmlDelegateModelItem *m_cacheItem; |
| 226 | int m_previousGroups; |
| 227 | int m_currentIndex[QQmlListCompositor::MaximumGroupCount]; |
| 228 | int m_previousIndex[QQmlListCompositor::MaximumGroupCount]; |
| 229 | |
| 230 | friend class QQmlDelegateModelAttachedMetaObject; |
| 231 | }; |
| 232 | |
| 233 | QT_END_NAMESPACE |
| 234 | |
| 235 | #endif // QQMLDATAMODEL_P_H |
| 236 | |