]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodel.h
Fix size-hint calculation in KFileItemListView
[dolphin.git] / src / kitemviews / kfileitemmodel.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #ifndef KFILEITEMMODEL_H
21 #define KFILEITEMMODEL_H
22
23 #include <libdolphin_export.h>
24 #include <KFileItemList>
25 #include <KUrl>
26 #include <kitemviews/kitemmodelbase.h>
27
28 #include <QHash>
29
30 class KDirLister;
31 class QTimer;
32
33 /**
34 * @brief KItemModelBase implementation for KFileItems.
35 *
36 * KFileItemModel is connected with one KDirLister. Each time the KDirLister
37 * emits new items, removes items or changes items the model gets synchronized.
38 *
39 * KFileItemModel supports sorting and grouping of items. Additional roles that
40 * are not part of KFileItem can be added with KFileItemModel::setData().
41 *
42 * Also the recursive expansion of sub-directories is supported by
43 * KFileItemModel::setExpanded().
44 */
45 class LIBDOLPHINPRIVATE_EXPORT KFileItemModel : public KItemModelBase
46 {
47 Q_OBJECT
48
49 public:
50 explicit KFileItemModel(KDirLister* dirLister, QObject* parent = 0);
51 virtual ~KFileItemModel();
52
53 virtual int count() const;
54 virtual QHash<QByteArray, QVariant> data(int index) const;
55 virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
56
57 /**
58 * @return True
59 * @reimp
60 */
61 virtual bool supportsGrouping() const;
62
63 /**
64 * @return True
65 * @reimp
66 */
67 virtual bool supportsSorting() const;
68
69 /**
70 * Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false).
71 */
72 void setSortFoldersFirst(bool foldersFirst);
73 bool sortFoldersFirst() const;
74
75 /** @reimp */
76 virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
77
78 /** @reimp */
79 virtual int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const;
80
81 /** @reimp */
82 virtual bool supportsDropping(int index) const;
83
84 /** @reimp */
85 virtual QString roleDescription(const QByteArray& role) const;
86
87 /**
88 * @return The file-item for the index \a index. If the index is in a valid
89 * range it is assured that the file-item is not null. The runtime
90 * complexity of this call is O(1).
91 */
92 KFileItem fileItem(int index) const;
93
94 /**
95 * @return The file-item for the url \a url. If no file-item with the given
96 * URL is found KFileItem::isNull() will be true for the returned
97 * file-item. The runtime complexity of this call is O(1).
98 */
99 KFileItem fileItem(const KUrl& url) const;
100
101 /**
102 * @return The index for the file-item \a item. -1 is returned if no file-item
103 * is found or if the file-item is null. The runtime
104 * complexity of this call is O(1).
105 */
106 int index(const KFileItem& item) const;
107
108 /**
109 * @return The index for the URL \a url. -1 is returned if no file-item
110 * is found. The runtime complexity of this call is O(1).
111 */
112 int index(const KUrl& url) const;
113
114 /**
115 * @return Root item of all items.
116 */
117 KFileItem rootItem() const;
118
119 /**
120 * Clears all items of the model.
121 */
122 void clear();
123
124 // TODO: "name" + "isDir" is default in ctor
125 void setRoles(const QSet<QByteArray>& roles);
126 QSet<QByteArray> roles() const;
127
128 bool setExpanded(int index, bool expanded);
129 bool isExpanded(int index) const;
130 bool isExpandable(int index) const;
131 QSet<KUrl> expandedUrls() const;
132 void restoreExpandedUrls(const QSet<KUrl>& urls);
133
134 signals:
135 void loadingCompleted();
136
137 protected:
138 virtual void onGroupRoleChanged(const QByteArray& current, const QByteArray& previous);
139 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
140 virtual void onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
141
142 private slots:
143 void slotCompleted();
144 void slotCanceled();
145 void slotNewItems(const KFileItemList& items);
146 void slotItemsDeleted(const KFileItemList& items);
147 void slotRefreshItems(const QList<QPair<KFileItem, KFileItem> >& items);
148 void slotClear();
149 void slotClear(const KUrl& url);
150
151 void dispatchPendingItemsToInsert();
152
153 private:
154 void insertItems(const KFileItemList& items);
155 void removeItems(const KFileItemList& items);
156
157 void resortAllItems();
158
159 void removeExpandedItems();
160
161 enum Role {
162 NoRole,
163 NameRole,
164 SizeRole,
165 DateRole,
166 PermissionsRole,
167 OwnerRole,
168 GroupRole,
169 TypeRole,
170 DestinationRole,
171 PathRole,
172 IsDirRole,
173 IsExpandedRole,
174 ExpansionLevelRole,
175 RolesCount // Mandatory last entry
176 };
177
178 void resetRoles();
179
180 Role roleIndex(const QByteArray& role) const;
181
182 QHash<QByteArray, QVariant> retrieveData(const KFileItem& item) const;
183
184 bool lessThan(const KFileItem& a, const KFileItem& b) const;
185 void sort(const KFileItemList::iterator& start, const KFileItemList::iterator& end);
186 int stringCompare(const QString& a, const QString& b) const;
187
188 /**
189 * Compares the expansion level of both items. The "expansion level" is defined
190 * by the number of parent directories. However simply comparing just the numbers
191 * is not sufficient, it is also important to check the hierarchy for having
192 * a correct order like shown in a tree.
193 */
194 int expansionLevelsCompare(const KFileItem& a, const KFileItem& b) const;
195
196 /**
197 * Helper method for expansionLevelCompare().
198 */
199 QString subPath(const KFileItem& item,
200 const QString& itemPath,
201 int start,
202 bool* isDir) const;
203
204 bool useMaximumUpdateInterval() const;
205
206 private:
207 QWeakPointer<KDirLister> m_dirLister;
208
209 bool m_naturalSorting;
210 bool m_sortFoldersFirst;
211
212 Role m_groupRole;
213 Role m_sortRole;
214 Qt::CaseSensitivity m_caseSensitivity;
215
216 KFileItemList m_sortedItems; // Allows O(1) access for KFileItemModel::fileItem(int index)
217 QHash<KUrl, int> m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
218 QList<QHash<QByteArray, QVariant> > m_data;
219
220 bool m_requestRole[RolesCount];
221
222 QTimer* m_minimumUpdateIntervalTimer;
223 QTimer* m_maximumUpdateIntervalTimer;
224 KFileItemList m_pendingItemsToInsert;
225 bool m_pendingEmitLoadingCompleted;
226
227 // Stores the smallest expansion level of the root-URL. Is required to calculate
228 // the "expansionLevel" role in an efficient way. A value < 0 indicates that
229 // it has not been initialized yet.
230 mutable int m_rootExpansionLevel;
231
232 // Stores the URLs of the expanded folders.
233 QSet<KUrl> m_expandedUrls;
234
235 // Stores the URLs which have to be expanded in order to restore a previous state of the model.
236 QSet<KUrl> m_restoredExpandedUrls;
237
238 friend class KFileItemModelTest; // For unit testing
239 };
240
241 #endif
242
243