]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
9078c8e0d64cdc89607b27b656c46967a919865d
[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 "dolphin_export.h"
24 #include "kitemviews/kitemmodelbase.h"
25
26 #include <KFileItem>
27 #include <config-baloo.h>
28
29 #include <QObject>
30 #include <QSet>
31 #include <QSize>
32 #include <QStringList>
33
34 class KDirectoryContentsCounter;
35 class KFileItemModel;
36 class QPixmap;
37 class QTimer;
38 class KOverlayIconPlugin;
39
40 namespace KIO {
41 class PreviewJob;
42 }
43
44 #ifdef HAVE_BALOO
45 namespace Baloo
46 {
47 class FileMonitor;
48 }
49 #include <Baloo/IndexerConfig>
50 #endif
51
52 /**
53 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
54 *
55 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
56 * the permissions. Creating previews or determining the MIME-type can be quite expensive
57 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
58 *
59 * To prevent a huge CPU and I/O load, these roles are not updated for all
60 * items, but only for the visible items, some items around the visible area,
61 * and the items on the first and last pages of the view. This is a compromise
62 * that aims to minimize the risk that the user sees items with unknown icons
63 * in the view when scrolling or pressing Home or End.
64 *
65 * Determining the roles is done in several phases:
66 *
67 * 1. If the sort role is "slow", it is determined for all items. If this
68 * cannot be finished synchronously in 200 ms, the remaining items are
69 * handled asynchronously by \a resolveNextSortRole().
70 *
71 * 2. The function startUpdating(), which is called if either the sort role
72 * has been successfully determined for all items, or items are inserted
73 * in the view, or the visible items might have changed because items
74 * were removed or moved, tries to determine the icons for all visible
75 * items synchronously for 200 ms. Then:
76 *
77 * (a) If previews are disabled, icons and all other roles are determined
78 * asynchronously for the interesting items. This is done by the
79 * function \a resolveNextPendingRoles().
80 *
81 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
82 * the previews for the interesting items. At the same time, the icons
83 * for these items are determined asynchronously as fast as possible
84 * by \a resolveNextPendingRoles(). This minimizes the risk that the
85 * user sees "unknown" icons when scrolling before the previews have
86 * arrived.
87 *
88 * 3. Finally, the entire process is repeated for any items that might have
89 * changed in the mean time.
90 */
91 class DOLPHIN_EXPORT KFileItemModelRolesUpdater : public QObject
92 {
93 Q_OBJECT
94
95 public:
96 explicit KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent = nullptr);
97 ~KFileItemModelRolesUpdater() override;
98
99 void setIconSize(const QSize& size);
100 QSize iconSize() const;
101
102 /**
103 * Sets the range of items that are visible currently. The roles
104 * of visible items are resolved first.
105 */
106 void setVisibleIndexRange(int index, int count);
107
108 void setMaximumVisibleItems(int count);
109
110 /**
111 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
112 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
113 * role.
114 */
115 void setPreviewsShown(bool show);
116 bool previewsShown() const;
117
118 /**
119 * If enabled a small preview gets upscaled to the icon size in case where
120 * the icon size is larger than the preview. Per default enlarging is
121 * enabled.
122 */
123 void setEnlargeSmallPreviews(bool enlarge);
124 bool enlargeSmallPreviews() const;
125
126 /**
127 * If \a paused is set to true the asynchronous resolving of roles will be paused.
128 * State changes during pauses like changing the icon size or the preview-shown
129 * will be remembered and handled after unpausing.
130 */
131 void setPaused(bool paused);
132 bool isPaused() const;
133
134 /**
135 * Sets the roles that should be resolved asynchronously.
136 */
137 void setRoles(const QSet<QByteArray>& roles);
138 QSet<QByteArray> roles() const;
139
140 /**
141 * Sets the list of enabled thumbnail plugins that are used for previews.
142 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
143 * are used.
144 *
145 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
146 *
147 * @see enabledPlugins
148 */
149 void setEnabledPlugins(const QStringList& list);
150
151 /**
152 * Returns the list of enabled thumbnail plugins.
153 * @see setEnabledPlugins
154 */
155 QStringList enabledPlugins() const;
156
157 private slots:
158 void slotItemsInserted(const KItemRangeList& itemRanges);
159 void slotItemsRemoved(const KItemRangeList& itemRanges);
160 void slotItemsMoved(const KItemRange& itemRange, const QList<int> &movedToIndexes);
161 void slotItemsChanged(const KItemRangeList& itemRanges,
162 const QSet<QByteArray>& roles);
163 void slotSortRoleChanged(const QByteArray& current,
164 const QByteArray& previous);
165
166 /**
167 * Is invoked after a preview has been received successfully.
168 * @see startPreviewJob()
169 */
170 void slotGotPreview(const KFileItem& item, const QPixmap& pixmap);
171
172 /**
173 * Is invoked after generating a preview has failed.
174 * @see startPreviewJob()
175 */
176 void slotPreviewFailed(const KFileItem& item);
177
178 /**
179 * Is invoked when the preview job has been finished. Starts a new preview
180 * job if there are any interesting items without previews left, or updates
181 * the changed items otherwise. *
182 * @see startPreviewJob()
183 */
184 void slotPreviewJobFinished();
185
186 /**
187 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
188 */
189 void slotOverlaysChanged(const QUrl& url, const QStringList&);
190
191 /**
192 * Resolves the sort role of the next item in m_pendingSortRole, applies it
193 * to the model, and invokes itself if there are any pending items left. If
194 * that is not the case, \a startUpdating() is called.
195 */
196 void resolveNextSortRole();
197
198 /**
199 * Resolves the icon name and (if previews are disabled) all other roles
200 * for the next interesting item. If there are no pending items left, any
201 * changed items are updated.
202 */
203 void resolveNextPendingRoles();
204
205 /**
206 * Resolves items that have not been resolved yet after the change has been
207 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
208 * expires.
209 */
210 void resolveRecentlyChangedItems();
211
212 void applyChangedBalooRoles(const QString& file);
213 void applyChangedBalooRolesForItem(const KFileItem& file);
214
215 void slotDirectoryContentsCountReceived(const QString& path, int count);
216
217 private:
218 /**
219 * Starts the updating of all roles. The visible items are handled first.
220 */
221 void startUpdating();
222
223 /**
224 * Loads the icons for the visible items. After 200 ms, the function
225 * stops determining mime types and only loads preliminary icons.
226 * This is a compromise that prevents that
227 * (a) the GUI is blocked for more than 200 ms, and
228 * (b) "unknown" icons could be shown in the view.
229 */
230 void updateVisibleIcons();
231
232 /**
233 * Creates previews for the items starting from the first item in
234 * m_pendingPreviewItems.
235 * @see slotGotPreview()
236 * @see slotPreviewFailed()
237 * @see slotPreviewJobFinished()
238 */
239 void startPreviewJob();
240
241 /**
242 * Ensures that icons, previews, and other roles are determined for any
243 * items that have been changed.
244 */
245 void updateChangedItems();
246
247 /**
248 * Resolves the sort role of the item and applies it to the model.
249 */
250 void applySortRole(int index);
251
252 void applySortProgressToModel();
253
254 enum ResolveHint {
255 ResolveFast,
256 ResolveAll
257 };
258 bool applyResolvedRoles(int index, ResolveHint hint);
259 QHash<QByteArray, QVariant> rolesData(const KFileItem& item);
260
261 /**
262 * @return The number of items of the path \a path.
263 */
264 int subItemsCount(const QString& path) const;
265
266 /**
267 * Must be invoked if a property has been changed that affects
268 * the look of the preview. Takes care to update all previews.
269 */
270 void updateAllPreviews();
271
272 void killPreviewJob();
273
274 QList<int> indexesToResolve() const;
275
276 private:
277 enum State {
278 Idle,
279 Paused,
280 ResolvingSortRole,
281 ResolvingAllRoles,
282 PreviewJobRunning
283 };
284
285 State m_state;
286
287 // Property changes during pausing must be remembered to be able
288 // to react when unpausing again:
289 bool m_previewChangedDuringPausing;
290 bool m_iconSizeChangedDuringPausing;
291 bool m_rolesChangedDuringPausing;
292
293 // Property for setPreviewsShown()/previewsShown().
294 bool m_previewShown;
295
296 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
297 bool m_enlargeSmallPreviews;
298
299 // True if the role "iconPixmap" should be cleared when resolving the next
300 // role with resolveRole(). Is necessary if the preview gets disabled
301 // during the roles-updater has been paused by setPaused().
302 bool m_clearPreviews;
303
304 // Remembers which items have been handled already, to prevent that
305 // previews and other expensive roles are determined again.
306 QSet<KFileItem> m_finishedItems;
307
308 KFileItemModel* m_model;
309 QSize m_iconSize;
310 int m_firstVisibleIndex;
311 int m_lastVisibleIndex;
312 int m_maximumVisibleItems;
313 QSet<QByteArray> m_roles;
314 QSet<QByteArray> m_resolvableRoles;
315 QStringList m_enabledPlugins;
316
317 // Items for which the sort role still has to be determined.
318 QSet<KFileItem> m_pendingSortRoleItems;
319
320 // Indexes of items which still have to be handled by
321 // resolveNextPendingRoles().
322 QList<int> m_pendingIndexes;
323
324 // Items which have been left over from the last call of startPreviewJob().
325 // A new preview job will be started from them once the first one finishes.
326 KFileItemList m_pendingPreviewItems;
327
328 KIO::PreviewJob* m_previewJob;
329
330 // When downloading or copying large files, the slot slotItemsChanged()
331 // will be called periodically within a quite short delay. To prevent
332 // a high CPU-load by generating e.g. previews for each notification, the update
333 // will be postponed until no file change has been done within a longer period
334 // of time.
335 QTimer* m_recentlyChangedItemsTimer;
336 QSet<KFileItem> m_recentlyChangedItems;
337
338 // Items which have not been changed repeatedly recently.
339 QSet<KFileItem> m_changedItems;
340
341 KDirectoryContentsCounter* m_directoryContentsCounter;
342
343 QList<KOverlayIconPlugin*> m_overlayIconsPlugin;
344
345 #ifdef HAVE_BALOO
346 Baloo::FileMonitor* m_balooFileMonitor;
347 Baloo::IndexerConfig m_balooConfig;
348 #endif
349 };
350
351 #endif
352
353