1/*
2 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef KDESCENDANTSPROXYMODEL_P_H
8#define KDESCENDANTSPROXYMODEL_P_H
9
10#include <QAbstractProxyModel>
11
12#include "kitemmodels_export.h"
13
14#include <memory>
15
16class KDescendantsProxyModelPrivate;
17
18/**
19@class KDescendantsProxyModel kdescendantsproxymodel.h KDescendantsProxyModel
20
21@brief Proxy Model for restructuring a Tree into a list.
22
23A KDescendantsProxyModel may be used to alter how the items in the tree are presented.
24
25Given a model which is represented as a tree:
26
27\image html entitytreemodel.png "A plain EntityTreeModel in a view"
28
29The KDescendantsProxyModel restructures the sourceModel to represent it as a flat list.
30
31@code
32// ... Create an entityTreeModel
33KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
34descProxy->setSourceModel(entityTree);
35view->setModel(descProxy);
36@endcode
37
38\image html descendantentitiesproxymodel.png "A KDescendantsProxyModel."
39
40KDescendantEntitiesProxyModel can also display the ancestors of the index in the source model as part of its display.
41
42@code
43// ... Create an entityTreeModel
44KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
45descProxy->setSourceModel(entityTree);
46
47// #### This is new
48descProxy->setDisplayAncestorData(true);
49descProxy->setAncestorSeparator(QString(" / "));
50
51view->setModel(descProxy);
52
53@endcode
54
55\image html descendantentitiesproxymodel-withansecnames.png "A KDescendantsProxyModel with ancestor names."
56
57@since 4.6
58@author Stephen Kelly <steveire@gmail.com>
59*/
60class KITEMMODELS_EXPORT KDescendantsProxyModel : public QAbstractProxyModel
61{
62 Q_OBJECT
63
64 /**
65 * @since 5.62
66 */
67 Q_PROPERTY(QAbstractItemModel *model READ sourceModel WRITE setSourceModel NOTIFY sourceModelChanged)
68 /**
69 * @since 5.62
70 */
71 Q_PROPERTY(bool displayAncestorData READ displayAncestorData WRITE setDisplayAncestorData NOTIFY displayAncestorDataChanged)
72 /**
73 * @since 5.62
74 */
75 Q_PROPERTY(QString ancestorSeparator READ ancestorSeparator WRITE setAncestorSeparator NOTIFY ancestorSeparatorChanged)
76
77 /**
78 * If true, all the nodes in the whole tree will be expanded upon loading and all items
79 * of the source model will be shown in the proxy.
80 * The default value is true.
81 * @since 5.74
82 */
83 Q_PROPERTY(bool expandsByDefault READ expandsByDefault WRITE setExpandsByDefault NOTIFY expandsByDefaultChanged)
84
85public:
86 enum AdditionalRoles {
87 // Note: use printf "0x%08X\n" $(($RANDOM*$RANDOM))
88 // to define additional roles.
89 LevelRole = 0x14823F9A,
90 ExpandableRole = 0x1CA894AD,
91 ExpandedRole = 0x1E413DA4,
92 HasSiblingsRole = 0x1633CE0C,
93 };
94
95 /**
96 * Creates a new descendant entities proxy model.
97 *
98 * @param parent The parent object.
99 */
100 explicit KDescendantsProxyModel(QObject *parent = nullptr);
101
102 /**
103 * Destroys the descendant entities proxy model.
104 */
105 ~KDescendantsProxyModel() override;
106
107 /**
108 * Sets the source @p model of the proxy.
109 */
110 void setSourceModel(QAbstractItemModel *model) override;
111
112 /**
113 * Set whether to show ancestor data in the model. If @p display is true, then
114 * a source model which is displayed as
115 *
116 * @code
117 * -> "Item 0-0" (this is row-depth)
118 * -> -> "Item 0-1"
119 * -> -> "Item 1-1"
120 * -> -> -> "Item 0-2"
121 * -> -> -> "Item 1-2"
122 * -> "Item 1-0"
123 * @endcode
124 *
125 * will be displayed as
126 *
127 * @code
128 * -> *Item 0-0"
129 * -> "Item 0-0 / Item 0-1"
130 * -> "Item 0-0 / Item 1-1"
131 * -> "Item 0-0 / Item 1-1 / Item 0-2"
132 * -> "Item 0-0 / Item 1-1 / Item 1-2"
133 * -> "Item 1-0"
134 * @endcode
135 *
136 * If @p display is false, the proxy will show
137 *
138 * @code
139 * -> *Item 0-0"
140 * -> "Item 0-1"
141 * -> "Item 1-1"
142 * -> "Item 0-2"
143 * -> "Item 1-2"
144 * -> "Item 1-0"
145 * @endcode
146 *
147 * Default is false.
148 */
149 void setDisplayAncestorData(bool display);
150
151 /**
152 * Whether ancestor data will be displayed.
153 */
154 bool displayAncestorData() const;
155
156 /**
157 * Sets the ancestor @p separator used between data of ancestors.
158 */
159 void setAncestorSeparator(const QString &separator);
160
161 /**
162 * Separator used between data of ancestors.
163 */
164 QString ancestorSeparator() const;
165
166 QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
167 QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
168
169 Qt::ItemFlags flags(const QModelIndex &index) const override;
170 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
171 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
172 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
173
174 QMimeData *mimeData(const QModelIndexList &indexes) const override;
175 QStringList mimeTypes() const override;
176
177 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
178 QModelIndex index(int, int, const QModelIndex &parent = QModelIndex()) const override;
179 QModelIndex parent(const QModelIndex &) const override;
180 int columnCount(const QModelIndex &index = QModelIndex()) const override;
181 QHash<int, QByteArray> roleNames() const override;
182
183 /**
184 * If true, all the nodes in the whole tree will be expanded upon loading (default)
185 * @param expand whether we want everything expanded upon load
186 * @since 5.74
187 */
188 void setExpandsByDefault(bool expand);
189
190 /**
191 * @returns true if all the tree nodes are expanded by default upon loading
192 * @since 5.74
193 */
194 bool expandsByDefault() const;
195
196 /**
197 * @returns true if the source index is mapped in the proxy as expanded, therefore it will show its children
198 * @since 5.74
199 */
200 bool isSourceIndexExpanded(const QModelIndex &sourceIndex) const;
201
202 /**
203 * @returns true if the source index is visible in the proxy, meaning all its parent hierarchy is expanded.
204 * @since 5.74
205 */
206 bool isSourceIndexVisible(const QModelIndex &sourceIndex) const;
207
208 /**
209 * Maps a source index as expanded in the proxy, all its children will become visible.
210 * @param sourceIndex an idex of the source model.
211 * @since 5.74
212 */
213 void expandSourceIndex(const QModelIndex &sourceIndex);
214
215 /**
216 * Maps a source index as collapsed in the proxy, all its children will be hidden.
217 * @param sourceIndex an idex of the source model.
218 * @since 5.74
219 */
220 void collapseSourceIndex(const QModelIndex &sourceIndex);
221
222 Qt::DropActions supportedDropActions() const override;
223
224 /**
225 Reimplemented to match all descendants.
226 */
227 virtual QModelIndexList match(const QModelIndex &start,
228 int role,
229 const QVariant &value,
230 int hits = 1,
231 Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap)) const override;
232
233Q_SIGNALS:
234 void sourceModelChanged();
235 void displayAncestorDataChanged();
236 void ancestorSeparatorChanged();
237 void expandsByDefaultChanged(bool expands);
238 void sourceIndexExpanded(const QModelIndex &sourceIndex);
239 void sourceIndexCollapsed(const QModelIndex &sourceIndex);
240
241private:
242 Q_DECLARE_PRIVATE(KDescendantsProxyModel)
243 //@cond PRIVATE
244 std::unique_ptr<KDescendantsProxyModelPrivate> const d_ptr;
245
246 Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeInserted(const QModelIndex &, int, int))
247 Q_PRIVATE_SLOT(d_func(), void sourceRowsInserted(const QModelIndex &, int, int))
248 Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeRemoved(const QModelIndex &, int, int))
249 Q_PRIVATE_SLOT(d_func(), void sourceRowsRemoved(const QModelIndex &, int, int))
250 Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))
251 Q_PRIVATE_SLOT(d_func(), void sourceRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))
252 Q_PRIVATE_SLOT(d_func(), void sourceModelAboutToBeReset())
253 Q_PRIVATE_SLOT(d_func(), void sourceModelReset())
254 Q_PRIVATE_SLOT(d_func(), void sourceLayoutAboutToBeChanged())
255 Q_PRIVATE_SLOT(d_func(), void sourceLayoutChanged())
256 Q_PRIVATE_SLOT(d_func(), void sourceDataChanged(const QModelIndex &, const QModelIndex &))
257 Q_PRIVATE_SLOT(d_func(), void sourceModelDestroyed())
258
259 Q_PRIVATE_SLOT(d_func(), void processPendingParents())
260
261 // Make these private, they shouldn't be called by applications
262 // virtual bool insertRows(int , int, const QModelIndex & = QModelIndex());
263 // virtual bool insertColumns(int, int, const QModelIndex & = QModelIndex());
264 // virtual bool removeRows(int, int, const QModelIndex & = QModelIndex());
265 // virtual bool removeColumns(int, int, const QModelIndex & = QModelIndex());
266
267 //@endcond
268};
269
270#endif
271

source code of kitemmodels/src/core/kdescendantsproxymodel.h