summaryrefslogtreecommitdiffstats
path: root/libgroupview/include/categorizedsortfilterproxymodel.h
blob: 1ef39fc29803d93c9fe3ef1d4c8e7d348d0da81a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * This file is part of the KDE project
 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
 * Copyright (C) 2007 John Tapsell <tapsell@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef KCATEGORIZEDSORTFILTERPROXYMODEL_H
#define KCATEGORIZEDSORTFILTERPROXYMODEL_H

#include <QSortFilterProxyModel>

#include <libgroupview_config.h>

class QItemSelection;


/**
  * This class lets you categorize a view. It is meant to be used along with
  * KCategorizedView class.
  *
  * In general terms all you need to do is to reimplement subSortLessThan() and
  * compareCategories() methods. In order to make categorization work, you need
  * to also call setCategorizedModel() class to enable it, since the categorization
  * is disabled by default.
  *
  * @see KCategorizedView
  *
  * @author Rafael Fernández López <ereslibre@kde.org>
  */
class LIBGROUPVIEW_EXPORT KCategorizedSortFilterProxyModel
	: public QSortFilterProxyModel
{
public:
	enum AdditionalRoles
	{
		// Note: use printf "0x%08X\n" $(($RANDOM*$RANDOM))
		// to define additional roles.
		CategoryDisplayRole = 0x17CE990A,  ///< This role is used for asking the category to a given index

		CategorySortRole    = 0x27857E60   ///< This role is used for sorting categories. You can return a
		///< string or a long long value. Strings will be sorted alphabetically
		///< while long long will be sorted by their value. Please note that this
		///< value won't be shown on the view, is only for sorting purposes. What will
		///< be shown as "Category" on the view will be asked with the role
		///< CategoryDisplayRole.
	};

	KCategorizedSortFilterProxyModel ( QObject *parent = 0 );
	virtual ~KCategorizedSortFilterProxyModel();

	/**
	  * Overridden from QSortFilterProxyModel. Sorts the source model using
	  * @p column for the given @p order.
	  */
	virtual void sort ( int column, Qt::SortOrder order = Qt::AscendingOrder );

	/**
	  * @return whether the model is categorized or not. Disabled by default.
	  */
	bool isCategorizedModel() const;

	/**
	  * Enables or disables the categorization feature.
	  *
	  * @param categorizedModel whether to enable or disable the categorization feature.
	  */
	void setCategorizedModel ( bool categorizedModel );

	/**
	  * @return the column being used for sorting.
	  */
	int sortColumn() const;

	/**
	  * @return the sort order being used for sorting.
	  */
	Qt::SortOrder sortOrder() const;

	/**
	  * Set if the sorting using CategorySortRole will use a natural comparison
	  * in the case that strings were returned. If enabled, QString::localeAwareCompare
	  * will be used for sorting.
	  *
	  * @param sortCategoriesByNaturalComparison whether to sort using a natural comparison or not.
	  */
	void setSortCategoriesByNaturalComparison ( bool sortCategoriesByNaturalComparison );

	/**
	  * @return whether it is being used a natural comparison for sorting. Enabled by default.
	  */
	bool sortCategoriesByNaturalComparison() const;

protected:
	/**
	  * Overridden from QSortFilterProxyModel. If you are subclassing
	  * KCategorizedSortFilterProxyModel, you will probably not need to reimplement this
	  * method.
	  *
	  * It calls compareCategories() to sort by category.  If the both items are in the
	  * same category (i.e. compareCategories returns 0), then subSortLessThan is called.
	  *
	  * @return Returns true if the item @p left is less than the item @p right when sorting.
	  *
	  * @warning You usually won't need to reimplement this method when subclassing
	  *          from KCategorizedSortFilterProxyModel.
	  */
	virtual bool lessThan ( const QModelIndex &left, const QModelIndex &right ) const;

	/**
	  * This method has a similar purpose as lessThan() has on QSortFilterProxyModel.
	  * It is used for sorting items that are in the same category.
	  *
	  * @return Returns true if the item @p left is less than the item @p right when sorting.
	  */
	virtual bool subSortLessThan ( const QModelIndex &left, const QModelIndex &right ) const;

	/**
	  * This method compares the category of the @p left index with the category
	  * of the @p right index.
	  *
	  * Internally and if not reimplemented, this method will ask for @p left and
	  * @p right models for role CategorySortRole. In order to correctly sort
	  * categories, the data() metod of the model should return a qlonglong (or numeric) value, or
	  * a QString object. QString objects will be sorted with QString::localeAwareCompare if
	  * sortCategoriesByNaturalComparison() is true.
	  *
	  * @note Please have present that:
	  *       QString(QChar(QChar::ObjectReplacementCharacter)) >
	  *       QString(QChar(QChar::ReplacementCharacter)) >
	  *       [ all possible strings ] >
	  *       QString();
	  *
	  *       This means that QString() will be sorted the first one, while
	  *       QString(QChar(QChar::ObjectReplacementCharacter)) and
	  *       QString(QChar(QChar::ReplacementCharacter)) will be sorted in last
	  *       position.
	  *
	  * @warning Please note that data() method of the model should return always
	  *          information of the same type. If you return a QString for an index,
	  *          you should return always QStrings for all indexes for role CategorySortRole
	  *          in order to correctly sort categories. You can't mix by returning
	  *          a QString for one index, and a qlonglong for other.
	  *
	  * @note If you need a more complex layout, you will have to reimplement this
	  *       method.
	  *
	  * @return A negative value if the category of @p left should be placed before the
	  *         category of @p right. 0 if @p left and @p right are on the same category, and
	  *         a positive value if the category of @p left should be placed after the
	  *         category of @p right.
	  */
	virtual int compareCategories ( const QModelIndex &left, const QModelIndex &right ) const;

private:
	class Private;
	Private *const d;
};


#endif // KCATEGORIZEDSORTFILTERPROXYMODEL_H