]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodel.h
Rough draft for getting back drag and drop support
[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 * @return The file-item for the index \a index. If the index is in a valid
71 * range it is assured that the file-item is not null. The runtime
72 * complexity of this call is O(1).
73 */
74 KFileItem fileItem(int index) const;
75
76 /**
77 * @return The index for the file-item \a item. -1 is returned if no file-item
78 * is found or if the file-item is null. The runtime
79 * complexity of this call is O(1).
80 */
81 int index(const KFileItem& item) const;
82
83 /**
84 * Clears all items of the model.
85 */
86 void clear();
87
88 // TODO: "name" + "isDir" is default in ctor
89 void setRoles(const QSet<QByteArray>& roles);
90 QSet<QByteArray> roles() const;
91
92 bool setExpanded(int index, bool expanded);
93 bool isExpanded(int index) const;
94 bool isExpandable(int index) const;
95
96 protected:
97 virtual void onGroupRoleChanged(const QByteArray& current, const QByteArray& previous);
98 virtual void onSortRoleChanged(const QByteArray& current, const QByteArray& previous);
99
100 private slots:
101 void slotCompleted();
102 void slotCanceled();
103 void slotNewItems(const KFileItemList& items);
104 void slotItemsDeleted(const KFileItemList& items);
105 void slotClear();
106 void slotClear(const KUrl& url);
107
108 void dispatchPendingItems();
109
110 private:
111 void insertItems(const KFileItemList& items);
112 void removeItems(const KFileItemList& items);
113
114 void removeExpandedItems();
115
116 enum Role {
117 NoRole,
118 NameRole,
119 SizeRole,
120 DateRole,
121 PermissionsRole,
122 OwnerRole,
123 GroupRole,
124 TypeRole,
125 DestinationRole,
126 PathRole,
127 IsDirRole,
128 IsExpandedRole,
129 ExpansionLevelRole,
130 RolesCount // Mandatory last entry
131 };
132
133 void resetRoles();
134
135 Role roleIndex(const QByteArray& role) const;
136
137 QHash<QByteArray, QVariant> retrieveData(const KFileItem& item) const;
138
139 bool lessThan(const KFileItem& a, const KFileItem& b) const;
140 void sort(const KFileItemList::iterator& start, const KFileItemList::iterator& end);
141 int stringCompare(const QString& a, const QString& b) const;
142
143 /**
144 * Compares the expansion level of both items. The "expansion level" is defined
145 * by the number of parent directories. However simply comparing just the numbers
146 * is not sufficient, it is also important to check the hierarchy for having
147 * a correct order like shown in a tree.
148 */
149 int expansionLevelsCompare(const KFileItem& a, const KFileItem& b) const;
150
151 /**
152 * Helper method for expansionLevelCompare().
153 */
154 QString subPath(const KFileItem& item,
155 const QString& itemPath,
156 int start,
157 bool* isDir) const;
158
159 bool useMaximumUpdateInterval() const;
160
161 private:
162 QWeakPointer<KDirLister> m_dirLister;
163
164 bool m_naturalSorting;
165 bool m_sortFoldersFirst;
166
167 Role m_groupRole;
168 Role m_sortRole;
169 Qt::CaseSensitivity m_caseSensitivity;
170
171 KFileItemList m_sortedItems; // Allows O(1) access for KFileItemModel::fileItem(int index)
172 QHash<KFileItem, int> m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
173 QList<QHash<QByteArray, QVariant> > m_data;
174
175 bool m_requestRole[RolesCount];
176
177 QTimer* m_minimumUpdateIntervalTimer;
178 QTimer* m_maximumUpdateIntervalTimer;
179 KFileItemList m_pendingItemsToInsert;
180 KFileItemList m_pendingItemsToDelete;
181
182 // Stores the smallest expansion level of the root-URL. Is required to calculate
183 // the "expansionLevel" role in an efficient way. A value < 0 indicates that
184 // it has not been initialized yet.
185 mutable int m_rootExpansionLevel;
186
187 friend class KFileItemModelTest; // For unit testing
188 };
189
190 #endif
191
192