2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * Based on the Itemviews NG project from Trolltech Labs
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #ifndef KITEMMODELBASE_H
10 #define KITEMMODELBASE_H
12 #include "dolphin_export.h"
13 #include "kitemviews/kitemrange.h"
14 #include "kitemviews/kitemset.h"
24 * @brief Base class for model implementations used by KItemListView and KItemListController.
26 * An item-model consists of a variable number of items. The number of items
27 * is given by KItemModelBase::count(). The data of an item is accessed by a unique index
28 * with KItemModelBase::data(). The indexes are integer-values counting from 0 to the
29 * KItemModelBase::count() - 1.
31 * One item consists of a variable number of role/value-pairs.
33 * A model can optionally provide sorting- and grouping-capabilities.
35 * Also optionally it is possible to provide a tree of items by implementing the methods
36 * setExpanded(), isExpanded(), isExpandable() and expandedParentsCount().
38 class DOLPHIN_EXPORT KItemModelBase
: public QObject
43 explicit KItemModelBase(QObject
*parent
= nullptr);
44 explicit KItemModelBase(const QByteArray
&sortRole
, const QByteArray
&groupRole
, QObject
*parent
= nullptr);
45 ~KItemModelBase() override
;
47 /** @return The number of items. */
48 virtual int count() const = 0;
50 virtual QHash
<QByteArray
, QVariant
> data(int index
) const = 0;
53 * Sets the data for the item at \a index to the given \a values. Returns true
54 * if the data was set on the item; returns false otherwise.
56 * The default implementation does not set the data, and will always return
59 virtual bool setData(int index
, const QHash
<QByteArray
, QVariant
> &values
);
62 * Enables/disables the grouped sorting. The method KItemModelBase::onGroupedSortingChanged() will be
63 * called so that model-implementations can react on the grouped-sorting change. Afterwards the
64 * signal groupedSortingChanged() will be emitted. If the grouped sorting is enabled, the method
65 * KItemModelBase::groups() must be implemented.
67 void setGroupedSorting(bool grouped
);
68 bool groupedSorting() const;
71 * Sets the sort-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
72 * called so that model-implementations can react on the sort-role change. Afterwards the
73 * signal sortRoleChanged() will be emitted.
74 * The implementation should resort only if \a resortItems is true.
76 void setSortRole(const QByteArray
&role
, bool resortItems
= true);
77 QByteArray
sortRole() const;
80 * Sets the sort order to \a order. The method KItemModelBase::onSortOrderChanged() will be
81 * called so that model-implementations can react on the sort order change. Afterwards the
82 * signal sortOrderChanged() will be emitted.
84 void setSortOrder(Qt::SortOrder order
, bool resortItems
= true);
85 Qt::SortOrder
sortOrder() const;
88 * Sets the group-role to \a role. The method KItemModelBase::onGroupRoleChanged() will be
89 * called so that model-implementations can react on the group-role change. Afterwards the
90 * signal groupRoleChanged() will be emitted.
91 * The implementation should regroup only if \a regroupItems is true.
93 void setGroupRole(const QByteArray
&role
, bool regroupItems
= true);
94 QByteArray
groupRole() const;
97 * Sets the group order to \a order. The method KItemModelBase::onGroupOrderChanged() will be
98 * called so that model-implementations can react on the group order change. Afterwards the
99 * signal groupOrderChanged() will be emitted.
101 void setGroupOrder(Qt::SortOrder order
, bool resortItems
= true);
102 Qt::SortOrder
groupOrder() const;
105 * @return Translated description for the \p role. The description is e.g. used
106 * for the header in KItemListView.
108 virtual QString
roleDescription(const QByteArray
&role
) const;
111 * @return List of group headers. Each list-item consists of the index of the item
112 * that represents the first item of a group and a value represented
113 * as QVariant. The value is shown by an instance of KItemListGroupHeader.
114 * Per default an empty list is returned.
116 virtual QList
<QPair
<int, QVariant
>> groups() const;
119 * Expands the item with the index \a index if \a expanded is true.
120 * If \a expanded is false the item will be collapsed.
122 * Per default no expanding of items is implemented. When implementing
123 * this method it is mandatory to overwrite KItemModelBase::isExpandable()
124 * and KItemListView::supportsExpandableItems() to return true.
126 * @return True if the operation has been successful.
128 virtual bool setExpanded(int index
, bool expanded
);
131 * @return True if the item with the index \a index is expanded.
132 * Per default no expanding of items is implemented. When implementing
133 * this method it is mandatory to overwrite KItemModelBase::isExpandable()
134 * and KItemListView::supportsExpandableItems() to return true.
136 virtual bool isExpanded(int index
) const;
139 * @return True if expanding and collapsing of the item with the index \a index
140 * is supported. Per default false is returned.
142 virtual bool isExpandable(int index
) const;
145 * @return Number of expanded parent items for the item with the given index.
146 * Per default 0 is returned.
148 virtual int expandedParentsCount(int index
) const;
151 * @return MIME-data for the items given by \a indexes. The default implementation
152 * returns 0. The ownership of the returned instance is in the hand of the
153 * caller of this method. The method must be implemented if dragging of
154 * items should be possible.
156 virtual QMimeData
*createMimeData(const KItemSet
&indexes
) const;
159 * @return Reimplement this to return the index for the first item
160 * beginning with string typed in through the keyboard, -1 if not found.
161 * @param text the text which has been typed in through the keyboard
162 * @param startFromIndex the index from which to start searching from
164 virtual int indexForKeyboardSearch(const QString
&text
, int startFromIndex
= 0) const;
167 * @return True, if the item with the index \a index basically supports dropping.
168 * Per default false is returned.
170 * The information is used only to give a visual feedback during a drag operation
171 * and not to decide whether a drop event gets emitted. It is it is still up to
172 * the receiver of KItemListController::itemDropEvent() to decide how to handle
175 // TODO: Should the MIME-data be passed too so that the model can do a more specific
176 // decision whether it accepts the drop?
177 virtual bool supportsDropping(int index
) const;
180 * @return True, if the item with the index \a index can be entered in during hover actions.
181 * Per default false is returned.
183 * This is used to check that if the item
184 * we're hovering on is either directory or a desktop file.
186 virtual bool canEnterOnHover(int index
) const;
189 * @return An internal mimetype to signal that an itemDropEvent() should be rejected by
190 * the receiving model.
192 * This mimeType can be used in createMimeData() to notify that the
193 * drop-onto-items events should be ignored, while the drop-between-items
194 * ones should be still accepted.
196 QString
blacklistItemDropEventMimeType() const;
199 * @return URL of the item at the specified index
201 virtual QUrl
url(int index
) const;
204 * @return True, if item at specified index is a directory
206 virtual bool isDir(int index
) const;
209 * @return Parent directory of the items that are shown
211 virtual QUrl
directory() const;
214 * Is emitted if one or more items have been inserted. Each item-range consists
216 * - an index where items have been inserted
217 * - the number of inserted items.
218 * The index of each item-range represents the index of the model
219 * before the items have been inserted.
221 * For the item-ranges it is assured that:
222 * - They don't overlap
223 * - The index of item-range n is smaller than the index of item-range n + 1.
225 void itemsInserted(const KItemRangeList
&itemRanges
);
228 * Is emitted if one or more items have been removed. Each item-range consists
230 * - an index where items have been inserted
231 * - the number of inserted items.
232 * The index of each item-range represents the index of the model
233 * before the items have been removed.
235 * For the item-ranges it is assured that:
236 * - They don't overlap
237 * - The index of item-range n is smaller than the index of item-range n + 1.
239 void itemsRemoved(const KItemRangeList
&itemRanges
);
242 * Is emitted if one ore more items get moved.
243 * @param itemRange Item-range that gets moved to a new position.
244 * @param movedToIndexes New positions for each element of the item-range.
246 * For example if the model has 10 items and the items 0 and 1 get exchanged
247 * with the items 5 and 6 then the parameters look like this:
248 * - itemRange: has the index 0 and a count of 7.
249 * - movedToIndexes: Contains the seven values 5, 6, 2, 3, 4, 0, 1
251 * This signal implies that the groups might have changed. Therefore,
252 * gropusChanged() is not emitted if this signal is emitted.
254 void itemsMoved(const KItemRange
&itemRange
, const QList
<int> &movedToIndexes
);
256 void itemsChanged(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &roles
);
259 * Is emitted if the groups have changed, even though the order of the
260 * items has not been modified.
262 void groupsChanged();
264 void groupedSortingChanged(bool current
);
265 void sortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
);
266 void sortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
);
267 void groupRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
);
268 void groupOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
);
272 * Is invoked if the grouped sorting has been changed by KItemModelBase::setGroupedSorting(). Allows
273 * to react on the changed grouped sorting before the signal groupedSortingChanged() will be emitted.
275 virtual void onGroupedSortingChanged(bool current
);
278 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
279 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
280 * The implementation must assure that the items are sorted by the role given by \a current.
281 * Usually the most efficient way is to emit a
282 * itemsRemoved() signal for all items, reorder the items internally and to emit a
283 * itemsInserted() signal afterwards.
284 * The implementation should resort only if \a resortItems is true.
286 virtual void onSortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
, bool resortItems
= true);
289 * Is invoked if the sort order has been changed by KItemModelBase::setSortOrder(). Allows
290 * to react on the changed sort order before the signal sortOrderChanged() will be emitted.
291 * The implementation must assure that the items are sorted by the order given by \a current.
292 * Usually the most efficient way is to emit a
293 * itemsRemoved() signal for all items, reorder the items internally and to emit a
294 * itemsInserted() signal afterwards.
296 virtual void onSortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
, bool resortItems
= true);
299 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
300 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
301 * The implementation must assure that the items are sorted by the role given by \a current.
302 * Usually the most efficient way is to emit a
303 * itemsRemoved() signal for all items, reorder the items internally and to emit a
304 * itemsInserted() signal afterwards.
305 * The implementation should resort only if \a regroupItems is true.
307 virtual void onGroupRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
, bool resortItems
= true);
310 * Is invoked if the sort order has been changed by KItemModelBase::setSortOrder(). Allows
311 * to react on the changed sort order before the signal sortOrderChanged() will be emitted.
312 * The implementation must assure that the items are sorted by the order given by \a current.
313 * Usually the most efficient way is to emit a
314 * itemsRemoved() signal for all items, reorder the items internally and to emit a
315 * itemsInserted() signal afterwards.
317 virtual void onGroupOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
, bool resortItems
= true);
320 bool m_groupedSorting
;
321 QByteArray m_sortRole
;
322 Qt::SortOrder m_sortOrder
;
323 QByteArray m_groupRole
;
324 Qt::SortOrder m_groupOrder
;
327 inline Qt::SortOrder
KItemModelBase::sortOrder() const
332 inline Qt::SortOrder
KItemModelBase::groupOrder() const