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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/***************************************************************************
* Copyright (C) 2011 by Peter Penz <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#ifndef KFILEITEMMODEL_H
#define KFILEITEMMODEL_H
#include <libdolphin_export.h>
#include <KFileItemList>
#include <KUrl>
#include <kitemviews/kitemmodelbase.h>
#include <QHash>
class KDirLister;
class QTimer;
/**
* @brief KItemModelBase implementation for KFileItems.
*
* KFileItemModel is connected with one KDirLister. Each time the KDirLister
* emits new items, removes items or changes items the model gets synchronized.
*
* KFileItemModel supports sorting and grouping of items. Additional roles that
* are not part of KFileItem can be added with KFileItemModel::setData().
*
* Also the recursive expansion of sub-directories is supported by
* KFileItemModel::setExpanded().
*/
class LIBDOLPHINPRIVATE_EXPORT KFileItemModel : public KItemModelBase
{
Q_OBJECT
public:
explicit KFileItemModel(KDirLister* dirLister, QObject* parent = 0);
virtual ~KFileItemModel();
virtual int count() const;
virtual QHash<QByteArray, QVariant> data(int index) const;
virtual bool setData(int index, const QHash<QByteArray, QVariant> &values);
/**
* @return True
* @reimp
*/
virtual bool supportsGrouping() const;
/**
* @return True
* @reimp
*/
virtual bool supportsSorting() const;
/**
* @return The file-item for the index \a index. If the index is in a valid
* range it is assured that the file-item is not null. The runtime
* complexity of this call is O(1).
*/
KFileItem fileItem(int index) const;
/**
* @return The index for the file-item \a item. -1 is returned if no file-item
* is found or if the file-item is null. The runtime
* complexity of this call is O(1).
*/
int index(const KFileItem& item) const;
/**
* Clears all items of the model.
*/
void clear();
// TODO: "name" + "isDir" is default in ctor
void setRoles(const QSet<QByteArray>& roles);
QSet<QByteArray> roles() const;
bool setExpanded(int index, bool expanded);
bool isExpanded(int index) const;
bool isExpandable(int index) const;
protected:
virtual void onGroupRoleChanged(const QByteArray& current, const QByteArray& previous);
virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
private slots:
void slotCompleted();
void slotCanceled();
void slotNewItems(const KFileItemList& items);
void slotItemsDeleted(const KFileItemList& items);
void slotClear();
void slotClear(const KUrl& url);
void dispatchPendingItems();
private:
void insertItems(const KFileItemList& items);
void removeItems(const KFileItemList& items);
void removeExpandedItems();
enum Role {
NoRole,
NameRole,
SizeRole,
DateRole,
PermissionsRole,
OwnerRole,
GroupRole,
TypeRole,
DestinationRole,
PathRole,
IsDirRole,
IsExpandedRole,
ExpansionLevelRole,
RolesCount // Mandatory last entry
};
void resetRoles();
Role roleIndex(const QByteArray& role) const;
QHash<QByteArray, QVariant> retrieveData(const KFileItem& item) const;
bool lessThan(const KFileItem& a, const KFileItem& b) const;
void sort(const KFileItemList::iterator& start, const KFileItemList::iterator& end);
int stringCompare(const QString& a, const QString& b) const;
/**
* Compares the expansion level of both items. The "expansion level" is defined
* by the number of parent directories. However simply comparing just the numbers
* is not sufficient, it is also important to check the hierarchy for having
* a correct order like shown in a tree.
*/
int expansionLevelsCompare(const KFileItem& a, const KFileItem& b) const;
/**
* Helper method for expansionLevelCompare().
*/
QString subPath(const KFileItem& item,
const QString& itemPath,
int start,
bool* isDir) const;
bool useMaximumUpdateInterval() const;
private:
QWeakPointer<KDirLister> m_dirLister;
bool m_naturalSorting;
bool m_sortFoldersFirst;
Role m_groupRole;
Role m_sortRole;
Qt::CaseSensitivity m_caseSensitivity;
KFileItemList m_sortedItems; // Allows O(1) access for KFileItemModel::fileItem(int index)
QHash<KFileItem, int> m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
QList<QHash<QByteArray, QVariant> > m_data;
bool m_requestRole[RolesCount];
QTimer* m_minimumUpdateIntervalTimer;
QTimer* m_maximumUpdateIntervalTimer;
KFileItemList m_pendingItemsToInsert;
KFileItemList m_pendingItemsToDelete;
// Stores the smallest expansion level of the root-URL. Is required to calculate
// the "expansionLevel" role in an efficient way. A value < 0 indicates that
// it has not been initialized yet.
mutable int m_rootExpansionLevel;
friend class KFileItemModelTest; // For unit testing
};
#endif
|