]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemmodelbase.h
Merge remote-tracking branch 'origin/KDE/4.11'
[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 <libdolphin_export.h>
27
28 #include <QHash>
29 #include <QObject>
30 #include <QSet>
31 #include <QVariant>
32
33 class QMimeData;
34
35 struct KItemRange
36 {
37 KItemRange(int index = 0, int count = 0);
38 int index;
39 int count;
40
41 bool operator == (const KItemRange& other) const;
42 };
43 typedef QList<KItemRange> KItemRangeList;
44
45 /**
46 * @brief Base class for model implementations used by KItemListView and KItemListController.
47 *
48 * An item-model consists of a variable number of items. The number of items
49 * is given by KItemModelBase::count(). The data of an item is accessed by a unique index
50 * with KItemModelBase::data(). The indexes are integer-values counting from 0 to the
51 * KItemModelBase::count() - 1.
52 *
53 * One item consists of a variable number of role/value-pairs.
54 *
55 * A model can optionally provide sorting- and grouping-capabilities.
56 *
57 * Also optionally it is possible to provide a tree of items by implementing the methods
58 * setExpanded(), isExpanded(), isExpandable() and expandedParentsCount().
59 */
60 class LIBDOLPHINPRIVATE_EXPORT KItemModelBase : public QObject
61 {
62 Q_OBJECT
63
64 public:
65 KItemModelBase(QObject* parent = 0);
66 explicit KItemModelBase(const QByteArray& sortRole, QObject* parent = 0);
67 virtual ~KItemModelBase();
68
69 /** @return The number of items. */
70 virtual int count() const = 0;
71
72 virtual QHash<QByteArray, QVariant> data(int index) const = 0;
73
74 /**
75 * Sets the data for the item at \a index to the given \a values. Returns true
76 * if the data was set on the item; returns false otherwise.
77 *
78 * The default implementation does not set the data, and will always return
79 * false.
80 */
81 virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
82
83 /**
84 * Enables/disables the grouped sorting. The method KItemModelBase::onGroupedSortingChanged() will be
85 * called so that model-implementations can react on the grouped-sorting change. Afterwards the
86 * signal groupedSortingChanged() will be emitted. If the grouped sorting is enabled, the method
87 * KItemModelBase::groups() must be implemented.
88 */
89 void setGroupedSorting(bool grouped);
90 bool groupedSorting() const;
91
92 /**
93 * Sets the sort-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
94 * called so that model-implementations can react on the sort-role change. Afterwards the
95 * signal sortRoleChanged() will be emitted.
96 */
97 void setSortRole(const QByteArray& role);
98 QByteArray sortRole() const;
99
100 /**
101 * Sets the sort order to \a order. The method KItemModelBase::onSortOrderChanged() will be
102 * called so that model-implementations can react on the sort order change. Afterwards the
103 * signal sortOrderChanged() will be emitted.
104 */
105 void setSortOrder(Qt::SortOrder order);
106 Qt::SortOrder sortOrder() const;
107
108 /**
109 * @return Translated description for the \p role. The description is e.g. used
110 * for the header in KItemListView.
111 */
112 virtual QString roleDescription(const QByteArray& role) const;
113
114 /**
115 * @return List of group headers. Each list-item consists of the index of the item
116 * that represents the first item of a group and a value represented
117 * as QVariant. The value is shown by an instance of KItemListGroupHeader.
118 * Per default an empty list is returned.
119 */
120 virtual QList<QPair<int, QVariant> > groups() const;
121
122 /**
123 * Expands the item with the index \a index if \a expanded is true.
124 * If \a expanded is false the item will be collapsed.
125 *
126 * Per default no expanding of items is implemented. When implementing
127 * this method it is mandatory to overwrite KItemModelBase::isExpandable()
128 * and KItemListView::supportsExpandableItems() to return true.
129 *
130 * @return True if the operation has been successful.
131 */
132 virtual bool setExpanded(int index, bool expanded);
133
134 /**
135 * @return True if the item with the index \a index is expanded.
136 * Per default no expanding of items is implemented. When implementing
137 * this method it is mandatory to overwrite KItemModelBase::isExpandable()
138 * and KItemListView::supportsExpandableItems() to return true.
139 */
140 virtual bool isExpanded(int index) const;
141
142 /**
143 * @return True if expanding and collapsing of the item with the index \a index
144 * is supported. Per default false is returned.
145 */
146 virtual bool isExpandable(int index) const;
147
148 /**
149 * @return Number of expanded parent items for the item with the given index.
150 * Per default 0 is returned.
151 */
152 virtual int expandedParentsCount(int index) const;
153
154 /**
155 * @return MIME-data for the items given by \a indexes. The default implementation
156 * returns 0. The ownership of the returned instance is in the hand of the
157 * caller of this method. The method must be implemented if dragging of
158 * items should be possible.
159 */
160 virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
161
162 /**
163 * @return Reimplement this to return the index for the first item
164 * beginning with string typed in through the keyboard, -1 if not found.
165 * @param text the text which has been typed in through the keyboard
166 * @param startFromIndex the index from which to start searching from
167 */
168 virtual int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const;
169
170 /**
171 * @return True, if the item with the index \a index basically supports dropping.
172 * Per default false is returned.
173 *
174 * The information is used only to give a visual feedback during a drag operation
175 * and not to decide whether a drop event gets emitted. It is it is still up to
176 * the receiver of KItemListController::itemDropEvent() to decide how to handle
177 * the drop event.
178 */
179 // TODO: Should the MIME-data be passed too so that the model can do a more specific
180 // decision whether it accepts the drop?
181 virtual bool supportsDropping(int index) const;
182
183 signals:
184 /**
185 * Is emitted if one or more items have been inserted. Each item-range consists
186 * of:
187 * - an index where items have been inserted
188 * - the number of inserted items.
189 * The index of each item-range represents the index of the model
190 * before the items have been inserted.
191 *
192 * For the item-ranges it is assured that:
193 * - They don't overlap
194 * - The index of item-range n is smaller than the index of item-range n + 1.
195 */
196 void itemsInserted(const KItemRangeList& itemRanges);
197
198 /**
199 * Is emitted if one or more items have been removed. Each item-range consists
200 * of:
201 * - an index where items have been inserted
202 * - the number of inserted items.
203 * The index of each item-range represents the index of the model
204 * before the items have been removed.
205 *
206 * For the item-ranges it is assured that:
207 * - They don't overlap
208 * - The index of item-range n is smaller than the index of item-range n + 1.
209 */
210 void itemsRemoved(const KItemRangeList& itemRanges);
211
212 /**
213 * Is emitted if one ore more items get moved.
214 * @param itemRange Item-range that gets moved to a new position.
215 * @param movedToIndexes New positions for each element of the item-range.
216 *
217 * For example if the model has 10 items and the items 0 and 1 get exchanged
218 * with the items 5 and 6 then the parameters look like this:
219 * - itemRange: has the index 0 and a count of 7.
220 * - movedToIndexes: Contains the seven values 5, 6, 2, 3, 4, 0, 1
221 *
222 * This signal implies that the groups might have changed. Therefore,
223 * gropusChanged() is not emitted if this signal is emitted.
224 */
225 void itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes);
226
227 void itemsChanged(const KItemRangeList& itemRanges, const QSet<QByteArray>& roles);
228
229 /**
230 * Is emitted if the groups have changed, even though the order of the
231 * items has not been modified.
232 */
233 void groupsChanged();
234
235 void groupedSortingChanged(bool current);
236 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
237 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
238
239 protected:
240 /**
241 * Is invoked if the grouped sorting has been changed by KItemModelBase::setGroupedSorting(). Allows
242 * to react on the changed grouped sorting before the signal groupedSortingChanged() will be emitted.
243 */
244 virtual void onGroupedSortingChanged(bool current);
245
246 /**
247 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
248 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
249 * The implementation must assure that the items are sorted by the role given by \a current.
250 * Usually the most efficient way is to emit a
251 * itemsRemoved() signal for all items, reorder the items internally and to emit a
252 * itemsInserted() signal afterwards.
253 */
254 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
255
256 /**
257 * Is invoked if the sort order has been changed by KItemModelBase::setSortOrder(). Allows
258 * to react on the changed sort order before the signal sortOrderChanged() will be emitted.
259 * The implementation must assure that the items are sorted by the order given by \a current.
260 * Usually the most efficient way is to emit a
261 * itemsRemoved() signal for all items, reorder the items internally and to emit a
262 * itemsInserted() signal afterwards.
263 */
264 virtual void onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
265
266 private:
267 bool m_groupedSorting;
268 QByteArray m_sortRole;
269 Qt::SortOrder m_sortOrder;
270 };
271
272 inline Qt::SortOrder KItemModelBase::sortOrder() const
273 {
274 return m_sortOrder;
275 }
276
277 #endif
278
279