]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemmodelbase.h
Merged very early alpha-version of Dolphin 2.0
[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 typedef QList<KItemRange> KItemRangeList;
40
41 /**
42 * @brief Base class for model implementations used by KItemListView and KItemListController.
43 *
44 * A item-model consists of a variable number of items. The number of items
45 * is given by KItemModelBase::count(). The data of an item is accessed by a unique index
46 * with KItemModelBase::data(). The indexes are integer-values counting from 0 to the
47 * KItemModelBase::count() - 1.
48 *
49 * One item consists of a variable number of role/value-pairs.
50 *
51 * A model can optionally provide sorting- and/or grouping-capabilities.
52 */
53 class LIBDOLPHINPRIVATE_EXPORT KItemModelBase : public QObject
54 {
55 Q_OBJECT
56
57 public:
58 KItemModelBase(QObject* parent = 0);
59 KItemModelBase(const QByteArray& groupRole, const QByteArray& sortRole, QObject* parent = 0);
60 virtual ~KItemModelBase();
61
62 /** @return The number of items. */
63 virtual int count() const = 0;
64
65 virtual QHash<QByteArray, QVariant> data(int index) const = 0;
66
67 /**
68 * Sets the data for the item at \a index to the given \a values. Returns true
69 * if the data was set on the item; returns false otherwise.
70 *
71 * The default implementation does not set the data, and will always return
72 * false.
73 */
74 virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
75
76 /**
77 * @return True if the model supports grouping of data. Per default false is returned.
78 * If the model should support grouping it is necessary to overwrite
79 * this method to return true and to implement KItemModelBase::onGroupRoleChanged().
80 */
81 virtual bool supportsGrouping() const;
82
83 /**
84 * Sets the group-role to \a role. The method KItemModelBase::onGroupRoleChanged() will be
85 * called so that model-implementations can react on the group-role change. Afterwards the
86 * signal groupRoleChanged() will be emitted.
87 */
88 void setGroupRole(const QByteArray& role);
89 QByteArray groupRole() const;
90
91 /**
92 * @return True if the model supports sorting of data. Per default false is returned.
93 * If the model should support sorting it is necessary to overwrite
94 * this method to return true and to implement KItemModelBase::onSortRoleChanged().
95 */
96 virtual bool supportsSorting() const;
97
98 /**
99 * Sets the sor-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
100 * called so that model-implementations can react on the sort-role change. Afterwards the
101 * signal sortRoleChanged() will be emitted.
102 */
103 void setSortRole(const QByteArray& role);
104 QByteArray sortRole() const;
105
106 virtual QString roleDescription(const QByteArray& role) const;
107
108 signals:
109 void itemsInserted(const KItemRangeList& itemRanges);
110 void itemsRemoved(const KItemRangeList& itemRanges);
111 void itemsMoved(const KItemRangeList& itemRanges);
112 void itemsChanged(const KItemRangeList& itemRanges, const QSet<QByteArray>& roles);
113
114 void groupRoleChanged(const QByteArray& current, const QByteArray& previous);
115 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
116
117 protected:
118 /**
119 * Is invoked if the group role has been changed by KItemModelBase::setGroupRole(). Allows
120 * to react on the changed group role before the signal groupRoleChanged() will be emitted.
121 * The implementation must assure that the items are sorted in a way that they are grouped
122 * by the role given by \a current. Usually the most efficient way is to emit a
123 * itemsRemoved() signal for all items, reorder the items internally and to emit a
124 * itemsInserted() signal afterwards.
125 */
126 virtual void onGroupRoleChanged(const QByteArray& current, const QByteArray& previous);
127
128 /**
129 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
130 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
131 * The implementation must assure that the items are sorted by the role given by \a current.
132 * Usually the most efficient way is to emit a
133 * itemsRemoved() signal for all items, reorder the items internally and to emit a
134 * itemsInserted() signal afterwards.
135 */
136 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
137
138 private:
139 QByteArray m_groupRole;
140 QByteArray m_sortRole;
141 };
142
143 #endif
144
145