]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemmodelbase.h
Fixed documentation of KItemModelBase::itemsMoved()
[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, int count);
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 * A 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/or grouping-capabilities.
56 */
57 class LIBDOLPHINPRIVATE_EXPORT KItemModelBase : public QObject
58 {
59 Q_OBJECT
60
61 public:
62 KItemModelBase(QObject* parent = 0);
63 KItemModelBase(const QByteArray& groupRole, const QByteArray& sortRole, QObject* parent = 0);
64 virtual ~KItemModelBase();
65
66 /** @return The number of items. */
67 virtual int count() const = 0;
68
69 virtual QHash<QByteArray, QVariant> data(int index) const = 0;
70
71 /**
72 * Sets the data for the item at \a index to the given \a values. Returns true
73 * if the data was set on the item; returns false otherwise.
74 *
75 * The default implementation does not set the data, and will always return
76 * false.
77 */
78 virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
79
80 /**
81 * @return True if the model supports grouping of data. Per default false is returned.
82 * If the model should support grouping it is necessary to overwrite
83 * this method to return true and to implement KItemModelBase::onGroupRoleChanged().
84 */
85 virtual bool supportsGrouping() const;
86
87 /**
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 */
92 void setGroupRole(const QByteArray& role);
93 QByteArray groupRole() const;
94
95 /**
96 * @return True if the model supports sorting of data. Per default false is returned.
97 * If the model should support sorting it is necessary to overwrite
98 * this method to return true and to implement KItemModelBase::onSortRoleChanged().
99 */
100 virtual bool supportsSorting() const;
101
102 /**
103 * Sets the sort-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
104 * called so that model-implementations can react on the sort-role change. Afterwards the
105 * signal sortRoleChanged() will be emitted.
106 */
107 void setSortRole(const QByteArray& role);
108 QByteArray sortRole() const;
109
110 /**
111 * Sets the sort order to \a order. The method KItemModelBase::onSortOrderChanged() will be
112 * called so that model-implementations can react on the sort order change. Afterwards the
113 * signal sortOrderChanged() will be emitted.
114 */
115 void setSortOrder(Qt::SortOrder order);
116 Qt::SortOrder sortOrder() const;
117
118 virtual QString roleDescription(const QByteArray& role) const;
119
120 /**
121 * @return MIME-data for the items given by \a indexes. The default implementation
122 * returns 0. The ownership of the returned instance is in the hand of the
123 * caller of this method.
124 */
125 virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
126
127 /**
128 * @return Reimplement this to return the index for the first item
129 * beginning with string typed in through the keyboard, -1 if not found.
130 * @param text the text which has been typed in through the keyboard
131 * @param startFromIndex the index from which to start searching from
132 */
133 virtual int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const;
134
135 /**
136 * @return True, if the item with the index \a index basically supports dropping.
137 * Per default false is returned.
138 *
139 * The information is used only to give a visual feedback during a drag operation
140 * and not to decide whether a drop event gets emitted. It is it is still up to
141 * the receiver of KItemListController::itemDropEvent() to decide how to handle
142 * the drop event.
143 */
144 // TODO: Should the MIME-data be passed too so that the model can do a more specific
145 // decision whether it accepts the drop?
146 virtual bool supportsDropping(int index) const;
147
148 signals:
149 /**
150 * Is emitted if one or more items have been inserted. Each item-range consists
151 * of:
152 * - an index where items have been inserted
153 * - the number of inserted items.
154 * The index of each item-range represents the index of the model
155 * before the items have been inserted.
156 *
157 * For the item-ranges it is assured that:
158 * - They don't overlap
159 * - The index of item-range n is smaller than the index of item-range n + 1.
160 */
161 void itemsInserted(const KItemRangeList& itemRanges);
162
163 /**
164 * Is emitted if one or more items have been removed. Each item-range consists
165 * of:
166 * - an index where items have been inserted
167 * - the number of inserted items.
168 * The index of each item-range represents the index of the model
169 * before the items have been removed.
170 *
171 * For the item-ranges it is assured that:
172 * - They don't overlap
173 * - The index of item-range n is smaller than the index of item-range n + 1.
174 */
175 void itemsRemoved(const KItemRangeList& itemRanges);
176
177 /**
178 * Is emitted if one ore more items get moved.
179 * @param itemRanges Item-ranges that get moved to a new position.
180 * @param movedToIndexes New positions for each element of the item-ranges.
181 *
182 * For example if the model has 10 items and the items 0 and 1 get exchanged
183 * with the items 5 and 6 then the parameters look like this:
184 * - itemRanges: Contains two ranges. The first has the index 0 and a count of
185 * 2 and the second as the index 5 and a count of 2.
186 * - movedToIndexes: Contains the four values 5, 6, 0, 1
187 *
188 * For the item-ranges it is assured that:
189 * - They don't overlap
190 * - The index of item-range n is smaller than the index of item-range n + 1.
191 */
192 void itemsMoved(const KItemRangeList& itemRanges, const QList<int> movedToIndexes);
193
194 void itemsChanged(const KItemRangeList& itemRanges, const QSet<QByteArray>& roles);
195
196 void groupRoleChanged(const QByteArray& current, const QByteArray& previous);
197 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
198 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
199
200 protected:
201 /**
202 * Is invoked if the group role has been changed by KItemModelBase::setGroupRole(). Allows
203 * to react on the changed group role before the signal groupRoleChanged() will be emitted.
204 * The implementation must assure that the items are sorted in a way that they are grouped
205 * by the role given by \a current. Usually the most efficient way is to emit a
206 * itemsRemoved() signal for all items, reorder the items internally and to emit a
207 * itemsInserted() signal afterwards.
208 */
209 virtual void onGroupRoleChanged(const QByteArray& current, const QByteArray& previous);
210
211 /**
212 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
213 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
214 * The implementation must assure that the items are sorted by the role given by \a current.
215 * Usually the most efficient way is to emit a
216 * itemsRemoved() signal for all items, reorder the items internally and to emit a
217 * itemsInserted() signal afterwards.
218 */
219 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
220
221 /**
222 * Is invoked if the sort order has been changed by KItemModelBase::setSortOrder(). Allows
223 * to react on the changed sort order before the signal sortOrderChanged() will be emitted.
224 * The implementation must assure that the items are sorted by the order given by \a current.
225 * Usually the most efficient way is to emit a
226 * itemsRemoved() signal for all items, reorder the items internally and to emit a
227 * itemsInserted() signal afterwards.
228 */
229 virtual void onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
230
231 private:
232 QByteArray m_groupRole;
233 QByteArray m_sortRole;
234 Qt::SortOrder m_sortOrder;
235 };
236
237 inline Qt::SortOrder KItemModelBase::sortOrder() const
238 {
239 return m_sortOrder;
240 }
241
242 #endif
243
244