]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
Update the roles if items have been changed
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.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 KFILEITEMMODELROLESUPDATER_H
21 #define KFILEITEMMODELROLESUPDATER_H
22
23 #include <libdolphin_export.h>
24
25 #include <KFileItem>
26 #include <kitemviews/kitemmodelbase.h>
27
28 #include <QObject>
29 #include <QSet>
30 #include <QSize>
31 #include <QStringList>
32
33 class KFileItemModel;
34 class KJob;
35 class QPixmap;
36 class QTimer;
37
38 /**
39 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
40 *
41 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
42 * the permissions. Creating previews or determining the MIME-type can be quite expensive
43 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
44 */
45 class LIBDOLPHINPRIVATE_EXPORT KFileItemModelRolesUpdater : public QObject
46 {
47 Q_OBJECT
48
49 public:
50 explicit KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent = 0);
51 virtual ~KFileItemModelRolesUpdater();
52
53 void setIconSize(const QSize& size);
54 QSize iconSize() const;
55
56 /**
57 * Sets the range of items that are visible currently. The roles
58 * of visible items are resolved first.
59 */
60 void setVisibleIndexRange(int index, int count);
61
62 /**
63 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
64 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
65 * role.
66 */
67 void setPreviewShown(bool show);
68 bool isPreviewShown() const;
69
70 /**
71 * If \a paused is set to true the asynchronous resolving of roles will be paused.
72 * State changes during pauses like changing the icon size or the preview-shown
73 * will be remembered and handled after unpausing.
74 */
75 void setPaused(bool paused);
76 bool isPaused() const;
77
78 /**
79 * Sets the roles that should be resolved asynchronously.
80 */
81 void setRoles(const QSet<QByteArray>& roles);
82 QSet<QByteArray> roles() const;
83
84 /**
85 * Sets the list of enabled thumbnail plugins.
86 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
87 * are used.
88 *
89 * Note that this method doesn't cause already generated previews
90 * to be regenerated.
91 *
92 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
93 *
94 * @see enabledPlugins
95 */
96 void setEnabledPlugins(const QStringList& list);
97
98 /**
99 * Returns the list of enabled thumbnail plugins.
100 * @see setEnabledPlugins
101 */
102 QStringList enabledPlugins() const;
103
104 private slots:
105 void slotItemsInserted(const KItemRangeList& itemRanges);
106 void slotItemsRemoved(const KItemRangeList& itemRanges);
107 void slotItemsChanged(const KItemRangeList& itemRanges,
108 const QSet<QByteArray>& roles);
109
110 /**
111 * Is invoked after a preview has been received successfully.
112 * @see startPreviewJob()
113 */
114 void slotGotPreview(const KFileItem& item, const QPixmap& pixmap);
115
116 /**
117 * Is invoked after generating a preview has failed.
118 * @see startPreviewJob()
119 */
120 void slotPreviewFailed(const KFileItem& item);
121
122 /**
123 * Is invoked when the preview job has been finished and
124 * removes the job from the m_previewJobs list.
125 * @see startPreviewJob()
126 */
127 void slotPreviewJobFinished(KJob* job);
128
129 void resolvePendingRoles();
130 void resolveNextPendingRoles();
131
132 private:
133 /**
134 * Updates the roles for the given item ranges. The roles for the currently
135 * visible items will get updated first.
136 */
137 void startUpdating(const KItemRangeList& itemRanges);
138
139 /**
140 * Creates previews for the items starting from the first item of the
141 * given list.
142 * @see slotGotPreview()
143 * @see slotPreviewFailed()
144 * @see slotPreviewJobFinished()
145 */
146 void startPreviewJob(const KFileItemList& items);
147
148 bool hasPendingRoles() const;
149 void resetPendingRoles();
150 void triggerPendingRolesResolving(int count);
151 void sortAndResolveAllRoles();
152 void sortAndResolvePendingRoles();
153
154 enum ResolveHint {
155 ResolveFast,
156 ResolveAll
157 };
158 bool applyResolvedRoles(const KFileItem& item, ResolveHint hint);
159 QHash<QByteArray, QVariant> rolesData(const KFileItem& item) const;
160
161 KFileItemList sortedItems(const QSet<KFileItem>& items) const;
162
163 static int subDirectoriesCount(const QString& path);
164
165 private:
166 // Property for setPaused()/isPaused().
167 bool m_paused;
168
169 // Property changes during pausing must be remembered to be able
170 // to react when unpausing again:
171 bool m_previewChangedDuringPausing;
172 bool m_iconSizeChangedDuringPausing;
173 bool m_rolesChangedDuringPausing;
174
175 // Property for setPreviewShown()/previewShown().
176 bool m_previewShown;
177
178 // True if the role "iconPixmap" should be cleared when resolving the next
179 // role with resolveRole(). Is necessary if the preview gets disabled
180 // during the roles-updater has been paused by setPaused().
181 bool m_clearPreviews;
182
183 KFileItemModel* m_model;
184 QSize m_iconSize;
185 int m_firstVisibleIndex;
186 int m_lastVisibleIndex;
187 QSet<QByteArray> m_roles;
188 QStringList m_enabledPlugins;
189
190 QSet<KFileItem> m_pendingVisibleItems;
191 QSet<KFileItem> m_pendingInvisibleItems;
192 QList<KJob*> m_previewJobs;
193
194 QTimer* m_resolvePendingRolesTimer;
195 };
196
197 #endif
198
199