1 /*
2     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef MARBLE_KDESCENDANTSPROXYMODEL_H
8 #define MARBLE_KDESCENDANTSPROXYMODEL_H
9 
10 #include <QAbstractProxyModel>
11 
12 #include "marble_export.h"
13 
14 // namespace added to avoid symbol clashes with KF5::ItemModels
15 namespace Marble
16 {
17 
18 class KDescendantsProxyModelPrivate;
19 
20 /**
21 @brief Proxy Model for restructuring a Tree into a list.
22 
23 A KDescendantsProxyModel may be used to alter how the items in the tree are presented.
24 
25 Given a model which is represented as a tree:
26 
27 The KDescendantsProxyModel restructures the sourceModel to represent it as a flat list.
28 
29 @code
30 // ... Create an entityTreeModel
31 KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
32 descProxy->setSourceModel(entityTree);
33 view->setModel(descProxy);
34 @endcode
35 
36 KDescendantEntitiesProxyModel can also display the ancestors of the index in the source model as part of its display.
37 
38 @code
39 // ... Create an entityTreeModel
40 KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
41 descProxy->setSourceModel(entityTree);
42 
43 // #### This is new
44 descProxy->setDisplayAncestorData(true);
45 descProxy->setDisplayAncestorSeparator(QString(" / "));
46 
47 view->setModel(descProxy);
48 
49 @endcode
50 
51 @since 4.6
52 @author Stephen Kelly <steveire@gmail.com>
53 */
54 class MARBLE_EXPORT KDescendantsProxyModel : public QAbstractProxyModel
55 {
56     Q_OBJECT
57 
58 public:
59 
60     /**
61      * Creates a new descendant entities proxy model.
62      *
63      * @param parent The parent object.
64      */
65     explicit KDescendantsProxyModel(QObject *parent = nullptr);
66 
67     /**
68      * Destroys the descendant entities proxy model.
69      */
70     ~KDescendantsProxyModel() override;
71 
72     /**
73      * Sets the source @p model of the proxy.
74      */
75     void setSourceModel(QAbstractItemModel *model) Q_DECL_OVERRIDE;
76 
77 #if 0
78     /**
79      * @deprecated
80      *
81      * This method does nothing.
82      */
83     void setRootIndex(const QModelIndex &index);
84 #endif
85 
86     /**
87      * Set whether to show ancestor data in the model. If @p display is true, then
88      * a source model which is displayed as
89      *
90      * @code
91      *  -> "Item 0-0" (this is row-depth)
92      *  -> -> "Item 0-1"
93      *  -> -> "Item 1-1"
94      *  -> -> -> "Item 0-2"
95      *  -> -> -> "Item 1-2"
96      *  -> "Item 1-0"
97      * @endcode
98      *
99      * will be displayed as
100      *
101      * @code
102      *  -> *Item 0-0"
103      *  -> "Item 0-0 / Item 0-1"
104      *  -> "Item 0-0 / Item 1-1"
105      *  -> "Item 0-0 / Item 1-1 / Item 0-2"
106      *  -> "Item 0-0 / Item 1-1 / Item 1-2"
107      *  -> "Item 1-0"
108      * @endcode
109      *
110      * If @p display is false, the proxy will show
111      *
112      * @code
113      *  -> *Item 0-0"
114      *  -> "Item 0-1"
115      *  -> "Item 1-1"
116      *  -> "Item 0-2"
117      *  -> "Item 1-2"
118      *  -> "Item 1-0"
119      * @endcode
120      *
121      * Default is false.
122      */
123     void setDisplayAncestorData(bool display);
124 
125     /**
126      * Whether ancestor data will be displayed.
127      */
128     bool displayAncestorData() const;
129 
130     /**
131      * Sets the ancestor @p separator used between data of ancestors.
132      */
133     void setAncestorSeparator(const QString &separator);
134 
135     /**
136      * Separator used between data of ancestors.
137      */
138     QString ancestorSeparator() const;
139 
140     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const Q_DECL_OVERRIDE;
141     QModelIndex mapToSource(const QModelIndex &proxyIndex) const Q_DECL_OVERRIDE;
142 
143     Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
144     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
145     int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
146     QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
147 
148     QMimeData *mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE;
149     QStringList mimeTypes() const Q_DECL_OVERRIDE;
150 
151     bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
152     QModelIndex index(int, int, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
153     QModelIndex parent(const QModelIndex &) const Q_DECL_OVERRIDE;
154     int columnCount(const QModelIndex &index = QModelIndex()) const Q_DECL_OVERRIDE;
155 
156     Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
157 
158     /**
159     Reimplemented to match all descendants.
160     */
161     QModelIndexList match(const QModelIndex &start, int role, const QVariant &value,
162                                   int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap)) const Q_DECL_OVERRIDE;
163 
164 private:
165     Q_DECLARE_PRIVATE(KDescendantsProxyModel)
166     //@cond PRIVATE
167     KDescendantsProxyModelPrivate *d_ptr;
168 
169     Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeInserted(const QModelIndex &, int, int))
170     Q_PRIVATE_SLOT(d_func(), void sourceRowsInserted(const QModelIndex &, int, int))
171     Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeRemoved(const QModelIndex &, int, int))
172     Q_PRIVATE_SLOT(d_func(), void sourceRowsRemoved(const QModelIndex &, int, int))
173     Q_PRIVATE_SLOT(d_func(), void sourceRowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int))
174     Q_PRIVATE_SLOT(d_func(), void sourceRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int))
175     Q_PRIVATE_SLOT(d_func(), void sourceModelAboutToBeReset())
176     Q_PRIVATE_SLOT(d_func(), void sourceModelReset())
177     Q_PRIVATE_SLOT(d_func(), void sourceLayoutAboutToBeChanged())
178     Q_PRIVATE_SLOT(d_func(), void sourceLayoutChanged())
179     Q_PRIVATE_SLOT(d_func(), void sourceDataChanged(const QModelIndex &, const QModelIndex &))
180     Q_PRIVATE_SLOT(d_func(), void sourceModelDestroyed())
181 
182     Q_PRIVATE_SLOT(d_func(), void processPendingParents())
183 
184     // Make these private, they shouldn't be called by applications
185 //   virtual bool insertRows(int , int, const QModelIndex & = QModelIndex());
186 //   virtual bool insertColumns(int, int, const QModelIndex & = QModelIndex());
187 //   virtual bool removeRows(int, int, const QModelIndex & = QModelIndex());
188 //   virtual bool removeColumns(int, int, const QModelIndex & = QModelIndex());
189 
190     //@endcond
191 };
192 
193 }
194 
195 #endif
196