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