1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #ifndef KITEMMODELBASE_H
24 #define KITEMMODELBASE_H
26 #include <libdolphin_export.h>
37 KItemRange(int index
= 0, int count
= 0);
41 bool operator == (const KItemRange
& other
) const;
43 typedef QList
<KItemRange
> KItemRangeList
;
46 * @brief Base class for model implementations used by KItemListView and KItemListController.
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.
53 * One item consists of a variable number of role/value-pairs.
55 * A model can optionally provide sorting- and grouping-capabilities.
57 class LIBDOLPHINPRIVATE_EXPORT KItemModelBase
: public QObject
62 KItemModelBase(QObject
* parent
= 0);
63 KItemModelBase(const QByteArray
& sortRole
, QObject
* parent
= 0);
64 virtual ~KItemModelBase();
66 /** @return The number of items. */
67 virtual int count() const = 0;
69 virtual QHash
<QByteArray
, QVariant
> data(int index
) const = 0;
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.
75 * The default implementation does not set the data, and will always return
78 virtual bool setData(int index
, const QHash
<QByteArray
, QVariant
>& values
);
81 * Enables/disables the grouped sorting. The method KItemModelBase::onGroupedSortingChanged() will be
82 * called so that model-implementations can react on the grouped-sorting change. Afterwards the
83 * signal groupedSortingChanged() will be emitted. If the grouped sorting is enabled, the method
84 * KItemModelBase::groups() must be implemented.
86 void setGroupedSorting(bool grouped
);
87 bool groupedSorting() const;
90 * Sets the sort-role to \a role. The method KItemModelBase::onSortRoleChanged() will be
91 * called so that model-implementations can react on the sort-role change. Afterwards the
92 * signal sortRoleChanged() will be emitted.
94 void setSortRole(const QByteArray
& role
);
95 QByteArray
sortRole() const;
98 * Sets the sort order to \a order. The method KItemModelBase::onSortOrderChanged() will be
99 * called so that model-implementations can react on the sort order change. Afterwards the
100 * signal sortOrderChanged() will be emitted.
102 void setSortOrder(Qt::SortOrder order
);
103 Qt::SortOrder
sortOrder() const;
106 * @return Translated description for the \p role. The description is e.g. used
107 * for the header in KItemListView.
109 virtual QString
roleDescription(const QByteArray
& role
) const;
111 virtual QList
<QPair
<int, QVariant
> > groups() const;
114 * @return MIME-data for the items given by \a indexes. The default implementation
115 * returns 0. The ownership of the returned instance is in the hand of the
116 * caller of this method.
118 virtual QMimeData
* createMimeData(const QSet
<int>& indexes
) const;
121 * @return Reimplement this to return the index for the first item
122 * beginning with string typed in through the keyboard, -1 if not found.
123 * @param text the text which has been typed in through the keyboard
124 * @param startFromIndex the index from which to start searching from
126 virtual int indexForKeyboardSearch(const QString
& text
, int startFromIndex
= 0) const;
129 * @return True, if the item with the index \a index basically supports dropping.
130 * Per default false is returned.
132 * The information is used only to give a visual feedback during a drag operation
133 * and not to decide whether a drop event gets emitted. It is it is still up to
134 * the receiver of KItemListController::itemDropEvent() to decide how to handle
137 // TODO: Should the MIME-data be passed too so that the model can do a more specific
138 // decision whether it accepts the drop?
139 virtual bool supportsDropping(int index
) const;
143 * Is emitted if one or more items have been inserted. Each item-range consists
145 * - an index where items have been inserted
146 * - the number of inserted items.
147 * The index of each item-range represents the index of the model
148 * before the items have been inserted.
150 * For the item-ranges it is assured that:
151 * - They don't overlap
152 * - The index of item-range n is smaller than the index of item-range n + 1.
154 void itemsInserted(const KItemRangeList
& itemRanges
);
157 * Is emitted if one or more items have been removed. Each item-range consists
159 * - an index where items have been inserted
160 * - the number of inserted items.
161 * The index of each item-range represents the index of the model
162 * before the items have been removed.
164 * For the item-ranges it is assured that:
165 * - They don't overlap
166 * - The index of item-range n is smaller than the index of item-range n + 1.
168 void itemsRemoved(const KItemRangeList
& itemRanges
);
171 * Is emitted if one ore more items get moved.
172 * @param itemRange Item-range that gets moved to a new position.
173 * @param movedToIndexes New positions for each element of the item-range.
175 * For example if the model has 10 items and the items 0 and 1 get exchanged
176 * with the items 5 and 6 then the parameters look like this:
177 * - itemRange: has the index 0 and a count of 7.
178 * - movedToIndexes: Contains the seven values 5, 6, 2, 3, 4, 0, 1
180 void itemsMoved(const KItemRange
& itemRange
, const QList
<int>& movedToIndexes
);
182 void itemsChanged(const KItemRangeList
& itemRanges
, const QSet
<QByteArray
>& roles
);
184 void groupedSortingChanged(bool current
);
185 void sortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
);
186 void sortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
);
190 * Is invoked if the grouped sorting has been changed by KItemModelBase::setGroupedSorting(). Allows
191 * to react on the changed grouped sorting before the signal groupedSortingChanged() will be emitted.
193 virtual void onGroupedSortingChanged(bool current
);
196 * Is invoked if the sort role has been changed by KItemModelBase::setSortRole(). Allows
197 * to react on the changed sort role before the signal sortRoleChanged() will be emitted.
198 * The implementation must assure that the items are sorted by the role given by \a current.
199 * Usually the most efficient way is to emit a
200 * itemsRemoved() signal for all items, reorder the items internally and to emit a
201 * itemsInserted() signal afterwards.
203 virtual void onSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
);
206 * Is invoked if the sort order has been changed by KItemModelBase::setSortOrder(). Allows
207 * to react on the changed sort order before the signal sortOrderChanged() will be emitted.
208 * The implementation must assure that the items are sorted by the order given by \a current.
209 * Usually the most efficient way is to emit a
210 * itemsRemoved() signal for all items, reorder the items internally and to emit a
211 * itemsInserted() signal afterwards.
213 virtual void onSortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
);
216 bool m_groupedSorting
;
217 QByteArray m_sortRole
;
218 Qt::SortOrder m_sortOrder
;
221 inline Qt::SortOrder
KItemModelBase::sortOrder() const