]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemmodelbase.h
Merge branch 'Applications/18.12'
[dolphin.git] / src / kitemviews / kitemmodelbase.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #ifndef KITEMMODELBASE_H
24 #define KITEMMODELBASE_H
25
26 #include "dolphin_export.h"
27 #include "kitemviews/kitemrange.h"
28 #include "kitemviews/kitemset.h"
29
30 #include <QHash>
31 #include <QObject>
32 #include <QUrl>
33 #include <QVariant>
34
35 class QMimeData;
36
37 /**
38 * @brief Base class for model implementations used by KItemListView and KItemListController.
39 *
40 * An item-model consists of a variable number of items. The number of items
41 * is given by KItemModelBase::count(). The data of an item is accessed by a unique index
42 * with KItemModelBase::data(). The indexes are integer-values counting from 0 to the
43 * KItemModelBase::count() - 1.
44 *
45 * One item consists of a variable number of role/value-pairs.
46 *
47 * A model can optionally provide sorting- and grouping-capabilities.
48 *
49 * Also optionally it is possible to provide a tree of items by implementing the methods
50 * setExpanded(), isExpanded(), isExpandable() and expandedParentsCount().
51 */
52 class DOLPHIN_EXPORT KItemModelBase : public QObject
53 {
54 Q_OBJECT
55
56 public:
57 explicit KItemModelBase(QObject* parent = nullptr);
58 explicit KItemModelBase(const QByteArray& sortRole, QObject* parent = nullptr);
59 ~KItemModelBase() override;
60
61 /** @return The number of items. */
62 virtual int count() const = 0;
63
64 virtual QHash<QByteArray, QVariant> data(int index) const = 0;
65
66 /**
67 * Sets the data for the item at \a index to the given \a values. Returns true
68 * if the data was set on the item; returns false otherwise.
69 *
70 * The default implementation does not set the data, and will always return
71 * false.
72 */
73 virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
74
75 /**
76 * Enables/disables the grouped sorting. The method KItemModelBase::onGroupedSortingChanged() will be
77 * called so that model-implementations can react on the grouped-sorting change. Afterwards the
78 * signal groupedSortingChanged() will be emitted. If the grouped sorting is enabled, the method
79 * KItemModelBase::groups() must be implemented.
80 */
81 void setGroupedSorting(bool grouped);
82 bool groupedSorting() const;
83
84 /**
85 * Sets the sort-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
86 * called so that model-implementations can react on the sort-role change. Afterwards the
87 * signal sortRoleChanged() will be emitted.
88 * The implementation should resort only if \a resortItems is true.
89 */
90 void setSortRole(const QByteArray& role, bool resortItems = true);
91 QByteArray sortRole() const;
92
93 /**
94 * Sets the sort order to \a order. The method KItemModelBase::onSortOrderChanged() will be
95 * called so that model-implementations can react on the sort order change. Afterwards the
96 * signal sortOrderChanged() will be emitted.
97 */
98 void setSortOrder(Qt::SortOrder order);
99 Qt::SortOrder sortOrder() const;
100
101 /**
102 * @return Translated description for the \p role. The description is e.g. used
103 * for the header in KItemListView.
104 */
105 virtual QString roleDescription(const QByteArray& role) const;
106
107 /**
108 * @return List of group headers. Each list-item consists of the index of the item
109 * that represents the first item of a group and a value represented
110 * as QVariant. The value is shown by an instance of KItemListGroupHeader.
111 * Per default an empty list is returned.
112 */
113 virtual QList<QPair<int, QVariant> > groups() const;
114
115 /**
116 * Expands the item with the index \a index if \a expanded is true.
117 * If \a expanded is false the item will be collapsed.
118 *
119 * Per default no expanding of items is implemented. When implementing
120 * this method it is mandatory to overwrite KItemModelBase::isExpandable()
121 * and KItemListView::supportsExpandableItems() to return true.
122 *
123 * @return True if the operation has been successful.
124 */
125 virtual bool setExpanded(int index, bool expanded);
126
127 /**
128 * @return True if the item with the index \a index is expanded.
129 * Per default no expanding of items is implemented. When implementing
130 * this method it is mandatory to overwrite KItemModelBase::isExpandable()
131 * and KItemListView::supportsExpandableItems() to return true.
132 */
133 virtual bool isExpanded(int index) const;
134
135 /**
136 * @return True if expanding and collapsing of the item with the index \a index
137 * is supported. Per default false is returned.
138 */
139 virtual bool isExpandable(int index) const;
140
141 /**
142 * @return Number of expanded parent items for the item with the given index.
143 * Per default 0 is returned.
144 */
145 virtual int expandedParentsCount(int index) const;
146
147 /**
148 * @return MIME-data for the items given by \a indexes. The default implementation
149 * returns 0. The ownership of the returned instance is in the hand of the
150 * caller of this method. The method must be implemented if dragging of
151 * items should be possible.
152 */
153 virtual QMimeData* createMimeData(const KItemSet& indexes) const;
154
155 /**
156 * @return Reimplement this to return the index for the first item
157 * beginning with string typed in through the keyboard, -1 if not found.
158 * @param text the text which has been typed in through the keyboard
159 * @param startFromIndex the index from which to start searching from
160 */
161 virtual int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const;
162
163 /**
164 * @return True, if the item with the index \a index basically supports dropping.
165 * Per default false is returned.
166 *
167 * The information is used only to give a visual feedback during a drag operation
168 * and not to decide whether a drop event gets emitted. It is it is still up to
169 * the receiver of KItemListController::itemDropEvent() to decide how to handle
170 * the drop event.
171 */
172 // TODO: Should the MIME-data be passed too so that the model can do a more specific
173 // decision whether it accepts the drop?
174 virtual bool supportsDropping(int index) const;
175
176 /**
177 * @return An internal mimetype to signal that an itemDropEvent() should be rejected by
178 * the receiving model.
179 *
180 * This mimeType can be used in createMimeData() to notify that the
181 * drop-onto-items events should be ignored, while the drop-between-items
182 * ones should be still accepted.
183 */
184 QString blacklistItemDropEventMimeType() const;
185
186 /**
187 * @return URL of the item at the specified index
188 */
189 virtual QUrl url(int index) const;
190
191 /**
192 * @return True, if item at specified index is a directory
193 */
194 virtual bool isDir(int index) const;
195
196 /**
197 * @return Parent directory of the items that are shown
198 */
199 virtual QUrl directory() const;
200 signals:
201 /**
202 * Is emitted if one or more items have been inserted. Each item-range consists
203 * of:
204 * - an index where items have been inserted
205 * - the number of inserted items.
206 * The index of each item-range represents the index of the model
207 * before the items have been inserted.
208 *
209 * For the item-ranges it is assured that:
210 * - They don't overlap
211 * - The index of item-range n is smaller than the index of item-range n + 1.
212 */
213 void itemsInserted(const KItemRangeList& itemRanges);
214
215 /**
216 * Is emitted if one or more items have been removed. Each item-range consists
217 * of:
218 * - an index where items have been inserted
219 * - the number of inserted items.
220 * The index of each item-range represents the index of the model
221 * before the items have been removed.
222 *
223 * For the item-ranges it is assured that:
224 * - They don't overlap
225 * - The index of item-range n is smaller than the index of item-range n + 1.
226 */
227 void itemsRemoved(const KItemRangeList& itemRanges);
228
229 /**
230 * Is emitted if one ore more items get moved.
231 * @param itemRange Item-range that gets moved to a new position.
232 * @param movedToIndexes New positions for each element of the item-range.
233 *
234 * For example if the model has 10 items and the items 0 and 1 get exchanged
235 * with the items 5 and 6 then the parameters look like this:
236 * - itemRange: has the index 0 and a count of 7.
237 * - movedToIndexes: Contains the seven values 5, 6, 2, 3, 4, 0, 1
238 *
239 * This signal implies that the groups might have changed. Therefore,
240 * gropusChanged() is not emitted if this signal is emitted.
241 */
242 void itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes);
243
244 void itemsChanged(const KItemRangeList& itemRanges, const QSet<QByteArray>& roles);
245
246 /**
247 * Is emitted if the groups have changed, even though the order of the
248 * items has not been modified.
249 */
250 void groupsChanged();
251
252 void groupedSortingChanged(bool current);
253 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
254 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
255
256 protected:
257 /**
258 * Is invoked if the grouped sorting has been changed by KItemModelBase::setGroupedSorting(). Allows
259 * to react on the changed grouped sorting before the signal groupedSortingChanged() will be emitted.
260 */
261 virtual void onGroupedSortingChanged(bool current);
262
263 /**
264 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
265 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
266 * The implementation must assure that the items are sorted by the role given by \a current.
267 * Usually the most efficient way is to emit a
268 * itemsRemoved() signal for all items, reorder the items internally and to emit a
269 * itemsInserted() signal afterwards.
270 * The implementation should resort only if \a resortItems is true.
271 */
272 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous, bool resortItems = true);
273
274 /**
275 * Is invoked if the sort order has been changed by KItemModelBase::setSortOrder(). Allows
276 * to react on the changed sort order before the signal sortOrderChanged() will be emitted.
277 * The implementation must assure that the items are sorted by the order given by \a current.
278 * Usually the most efficient way is to emit a
279 * itemsRemoved() signal for all items, reorder the items internally and to emit a
280 * itemsInserted() signal afterwards.
281 */
282 virtual void onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
283
284 private:
285 bool m_groupedSorting;
286 QByteArray m_sortRole;
287 Qt::SortOrder m_sortOrder;
288 };
289
290 inline Qt::SortOrder KItemModelBase::sortOrder() const
291 {
292 return m_sortOrder;
293 }
294
295 #endif
296
297