]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemmodelbase.h
Provide basic rubberband functionality
[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 struct KItemRange
34 {
35 KItemRange(int index, int count);
36 int index;
37 int count;
38
39 bool operator == (const KItemRange& other) const;
40 };
41 typedef QList<KItemRange> KItemRangeList;
42
43 /**
44 * @brief Base class for model implementations used by KItemListView and KItemListController.
45 *
46 * A item-model consists of a variable number of items. The number of items
47 * is given by KItemModelBase::count(). The data of an item is accessed by a unique index
48 * with KItemModelBase::data(). The indexes are integer-values counting from 0 to the
49 * KItemModelBase::count() - 1.
50 *
51 * One item consists of a variable number of role/value-pairs.
52 *
53 * A model can optionally provide sorting- and/or grouping-capabilities.
54 */
55 class LIBDOLPHINPRIVATE_EXPORT KItemModelBase : public QObject
56 {
57 Q_OBJECT
58
59 public:
60 KItemModelBase(QObject* parent = 0);
61 KItemModelBase(const QByteArray& groupRole, const QByteArray& sortRole, QObject* parent = 0);
62 virtual ~KItemModelBase();
63
64 /** @return The number of items. */
65 virtual int count() const = 0;
66
67 virtual QHash<QByteArray, QVariant> data(int index) const = 0;
68
69 /**
70 * Sets the data for the item at \a index to the given \a values. Returns true
71 * if the data was set on the item; returns false otherwise.
72 *
73 * The default implementation does not set the data, and will always return
74 * false.
75 */
76 virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
77
78 /**
79 * @return True if the model supports grouping of data. Per default false is returned.
80 * If the model should support grouping it is necessary to overwrite
81 * this method to return true and to implement KItemModelBase::onGroupRoleChanged().
82 */
83 virtual bool supportsGrouping() const;
84
85 /**
86 * Sets the group-role to \a role. The method KItemModelBase::onGroupRoleChanged() will be
87 * called so that model-implementations can react on the group-role change. Afterwards the
88 * signal groupRoleChanged() will be emitted.
89 */
90 void setGroupRole(const QByteArray& role);
91 QByteArray groupRole() const;
92
93 /**
94 * @return True if the model supports sorting of data. Per default false is returned.
95 * If the model should support sorting it is necessary to overwrite
96 * this method to return true and to implement KItemModelBase::onSortRoleChanged().
97 */
98 virtual bool supportsSorting() const;
99
100 /**
101 * Sets the sor-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
102 * called so that model-implementations can react on the sort-role change. Afterwards the
103 * signal sortRoleChanged() will be emitted.
104 */
105 void setSortRole(const QByteArray& role);
106 QByteArray sortRole() const;
107
108 virtual QString roleDescription(const QByteArray& role) const;
109
110 signals:
111 /**
112 * Is emitted if one or more items have been inserted. Each item-range consists
113 * of:
114 * - an index where items have been inserted
115 * - the number of inserted items.
116 * The index of each item-range represents the index of the model
117 * before the items have been inserted.
118 *
119 * For the item-ranges it is assured that:
120 * - They don't overlap
121 * - The index of item-range n is smaller than the index of item-range n + 1.
122 */
123 void itemsInserted(const KItemRangeList& itemRanges);
124
125 /**
126 * Is emitted if one or more items have been removed. Each item-range consists
127 * of:
128 * - an index where items have been inserted
129 * - the number of inserted items.
130 * The index of each item-range represents the index of the model
131 * before the items have been removed.
132 *
133 * For the item-ranges it is assured that:
134 * - They don't overlap
135 * - The index of item-range n is smaller than the index of item-range n + 1.
136 */
137 void itemsRemoved(const KItemRangeList& itemRanges);
138
139 /**
140 * Is emitted if one ore more items get moved.
141 * @param itemRanges Item-ranges that get moved to a new position.
142 * @param movedToIndexes New position of the ranges.
143 * It is assured that the itemRanges list has the same size as the movedToIndexes list.
144 *
145 * For the item-ranges it is assured that:
146 * - They don't overlap
147 * - The index of item-range n is smaller than the index of item-range n + 1.
148 */
149 void itemsMoved(const KItemRangeList& itemRanges, const QList<int> movedToIndexes);
150
151 void itemsChanged(const KItemRangeList& itemRanges, const QSet<QByteArray>& roles);
152
153 void groupRoleChanged(const QByteArray& current, const QByteArray& previous);
154 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
155
156 protected:
157 /**
158 * Is invoked if the group role has been changed by KItemModelBase::setGroupRole(). Allows
159 * to react on the changed group role before the signal groupRoleChanged() will be emitted.
160 * The implementation must assure that the items are sorted in a way that they are grouped
161 * by the role given by \a current. Usually the most efficient way is to emit a
162 * itemsRemoved() signal for all items, reorder the items internally and to emit a
163 * itemsInserted() signal afterwards.
164 */
165 virtual void onGroupRoleChanged(const QByteArray& current, const QByteArray& previous);
166
167 /**
168 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
169 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
170 * The implementation must assure that the items are sorted by the role given by \a current.
171 * Usually the most efficient way is to emit a
172 * itemsRemoved() signal for all items, reorder the items internally and to emit a
173 * itemsInserted() signal afterwards.
174 */
175 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
176
177 private:
178 QByteArray m_groupRole;
179 QByteArray m_sortRole;
180 };
181
182 #endif
183
184