| 1 | // Copyright (C) 2021 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 QQmlTreeModelToTableModel_H |
| 5 | #define QQmlTreeModelToTableModel_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 "qtqmlmodelsglobal_p.h" |
| 19 | |
| 20 | #include <QtCore/qset.h> |
| 21 | #include <QtCore/qpointer.h> |
| 22 | #include <QtCore/qabstractitemmodel.h> |
| 23 | #include <QtCore/qitemselectionmodel.h> |
| 24 | #include <QtCore/qvarlengtharray.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QAbstractItemModel; |
| 29 | |
| 30 | class Q_QMLMODELS_EXPORT QQmlTreeModelToTableModel : public QAbstractItemModel |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged FINAL) |
| 34 | Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex RESET resetRootIndex NOTIFY rootIndexChanged FINAL) |
| 35 | |
| 36 | struct TreeItem; |
| 37 | |
| 38 | public: |
| 39 | explicit QQmlTreeModelToTableModel(QObject *parent = nullptr); |
| 40 | |
| 41 | QAbstractItemModel *model() const; |
| 42 | QModelIndex rootIndex() const; |
| 43 | void setRootIndex(const QModelIndex &idx); |
| 44 | void resetRootIndex(); |
| 45 | |
| 46 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
| 47 | QModelIndex parent(const QModelIndex &child) const override; |
| 48 | |
| 49 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 50 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
| 51 | |
| 52 | enum { |
| 53 | DepthRole = Qt::UserRole - 5, |
| 54 | ExpandedRole, |
| 55 | HasChildrenRole, |
| 56 | HasSiblingRole, |
| 57 | ModelIndexRole |
| 58 | }; |
| 59 | |
| 60 | QHash<int, QByteArray> roleNames() const override; |
| 61 | QVariant data(const QModelIndex &, int role) const override; |
| 62 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 63 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
| 64 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 65 | |
| 66 | void clearModelData(); |
| 67 | |
| 68 | bool isVisible(const QModelIndex &index); |
| 69 | bool childrenVisible(const QModelIndex &index); |
| 70 | |
| 71 | QModelIndex mapToModel(const QModelIndex &index) const; |
| 72 | QModelIndex mapFromModel(const QModelIndex &index) const; |
| 73 | QModelIndex mapToModel(int row) const; |
| 74 | |
| 75 | Q_INVOKABLE QItemSelection selectionForRowRange(const QModelIndex &fromIndex, const QModelIndex &toIndex) const; |
| 76 | |
| 77 | void showModelTopLevelItems(bool doInsertRows = true); |
| 78 | void showModelChildItems(const TreeItem &parent, int start, int end, bool doInsertRows = true, bool doExpandPendingRows = true); |
| 79 | |
| 80 | int itemIndex(const QModelIndex &index) const; |
| 81 | void expandPendingRows(bool doInsertRows = true); |
| 82 | int lastChildIndex(const QModelIndex &index) const; |
| 83 | void removeVisibleRows(int startIndex, int endIndex, bool doRemoveRows = true); |
| 84 | |
| 85 | void dump() const; |
| 86 | bool testConsistency(bool dumpOnFail = false) const; |
| 87 | |
| 88 | using QAbstractItemModel::hasChildren; |
| 89 | |
| 90 | Q_SIGNALS: |
| 91 | void modelChanged(QAbstractItemModel *model); |
| 92 | void rootIndexChanged(); |
| 93 | void expanded(const QModelIndex &index); |
| 94 | void collapsed(const QModelIndex &index); |
| 95 | |
| 96 | public Q_SLOTS: |
| 97 | void expand(const QModelIndex &); |
| 98 | void collapse(const QModelIndex &); |
| 99 | void setModel(QAbstractItemModel *model); |
| 100 | bool isExpanded(const QModelIndex &) const; |
| 101 | bool isExpanded(int row) const; |
| 102 | bool hasChildren(int row) const; |
| 103 | bool hasSiblings(int row) const; |
| 104 | int depthAtRow(int row) const; |
| 105 | void expandRow(int n); |
| 106 | void expandRecursively(int row, int depth); |
| 107 | void collapseRow(int n); |
| 108 | void collapseRecursively(int row); |
| 109 | |
| 110 | private Q_SLOTS: |
| 111 | void modelHasBeenDestroyed(); |
| 112 | void modelHasBeenReset(); |
| 113 | void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles); |
| 114 | void modelLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint); |
| 115 | void modelLayoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint); |
| 116 | void modelRowsAboutToBeInserted(const QModelIndex & parent, int start, int end); |
| 117 | void modelRowsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow); |
| 118 | void modelRowsAboutToBeRemoved(const QModelIndex & parent, int start, int end); |
| 119 | void modelRowsInserted(const QModelIndex & parent, int start, int end); |
| 120 | void modelRowsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow); |
| 121 | void modelRowsRemoved(const QModelIndex & parent, int start, int end); |
| 122 | void modelColumnsAboutToBeInserted(const QModelIndex & parent, int start, int end); |
| 123 | void modelColumnsAboutToBeRemoved(const QModelIndex & parent, int start, int end); |
| 124 | void modelColumnsInserted(const QModelIndex & parent, int start, int end); |
| 125 | void modelColumnsRemoved(const QModelIndex & parent, int start, int end); |
| 126 | |
| 127 | private: |
| 128 | struct TreeItem { |
| 129 | QPersistentModelIndex index; |
| 130 | int depth; |
| 131 | bool expanded; |
| 132 | |
| 133 | explicit TreeItem(const QModelIndex &idx = QModelIndex(), int d = 0, int e = false) |
| 134 | : index(idx), depth(d), expanded(e) |
| 135 | { } |
| 136 | |
| 137 | inline bool operator== (const TreeItem &other) const |
| 138 | { |
| 139 | return this->index == other.index; |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | struct DataChangedParams { |
| 144 | int top; |
| 145 | int bottom; |
| 146 | QVarLengthArray<int, 5> roles; |
| 147 | }; |
| 148 | |
| 149 | struct SignalFreezer { |
| 150 | SignalFreezer(QQmlTreeModelToTableModel *parent) : m_parent(parent) { |
| 151 | m_parent->enableSignalAggregation(); |
| 152 | } |
| 153 | ~SignalFreezer() { m_parent->disableSignalAggregation(); } |
| 154 | |
| 155 | private: |
| 156 | QQmlTreeModelToTableModel *m_parent; |
| 157 | }; |
| 158 | |
| 159 | void enableSignalAggregation(); |
| 160 | void disableSignalAggregation(); |
| 161 | bool isAggregatingSignals() const { return m_signalAggregatorStack > 0; } |
| 162 | void queueDataChanged(int top, int bottom, std::initializer_list<int> roles); |
| 163 | void emitQueuedSignals(); |
| 164 | void connectToModel(); |
| 165 | |
| 166 | QPointer<QAbstractItemModel> m_model = nullptr; |
| 167 | QPersistentModelIndex m_rootIndex; |
| 168 | QList<TreeItem> m_items; |
| 169 | QSet<QPersistentModelIndex> m_expandedItems; |
| 170 | QList<TreeItem> m_itemsToExpand; |
| 171 | mutable int m_lastItemIndex = 0; |
| 172 | bool m_visibleRowsMoved = false; |
| 173 | bool m_modelLayoutChanged = false; |
| 174 | int m_signalAggregatorStack = 0; |
| 175 | QList<DataChangedParams> m_queuedDataChanged; |
| 176 | std::array<QMetaObject::Connection, 15> m_connections; |
| 177 | }; |
| 178 | |
| 179 | QT_END_NAMESPACE |
| 180 | |
| 181 | #endif // QQmlTreeModelToTableModel_H |
| 182 | |