]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
be6f2319311cbb56520be4a903f654193d6757b9
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef KFILEITEMMODELROLESUPDATER_H
8 #define KFILEITEMMODELROLESUPDATER_H
9
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemmodelbase.h"
12
13 #include <list>
14
15 #include "config-dolphin.h"
16 #include <KFileItem>
17
18 #include <QObject>
19 #include <QSet>
20 #include <QSize>
21 #include <QStringList>
22
23 class KDirectoryContentsCounter;
24 class KFileItemModel;
25 class QPixmap;
26 class QTimer;
27 class KOverlayIconPlugin;
28
29 namespace KIO
30 {
31 class PreviewJob;
32 }
33
34 #if HAVE_BALOO
35 namespace Baloo
36 {
37 class FileMonitor;
38 }
39 #include <Baloo/IndexerConfig>
40 #endif
41
42 /**
43 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
44 *
45 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
46 * the permissions. Creating previews or determining the MIME-type can be quite expensive
47 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
48 *
49 * To prevent a huge CPU and I/O load, these roles are not updated for all
50 * items, but only for the visible items, some items around the visible area,
51 * and the items on the first and last pages of the view. This is a compromise
52 * that aims to minimize the risk that the user sees items with unknown icons
53 * in the view when scrolling or pressing Home or End.
54 *
55 * Determining the roles is done in several phases:
56 *
57 * 1. If the sort role is "slow", it is determined for all items. If this
58 * cannot be finished synchronously in 200 ms, the remaining items are
59 * handled asynchronously by \a resolveNextSortRole().
60 *
61 * 2. The function startUpdating(), which is called if either the sort role
62 * has been successfully determined for all items, or items are inserted
63 * in the view, or the visible items might have changed because items
64 * were removed or moved, tries to determine the icons for all visible
65 * items synchronously for 200 ms. Then:
66 *
67 * (a) If previews are disabled, icons and all other roles are determined
68 * asynchronously for the interesting items. This is done by the
69 * function \a resolveNextPendingRoles().
70 *
71 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
72 * the previews for the interesting items. At the same time, the icons
73 * for these items are determined asynchronously as fast as possible
74 * by \a resolveNextPendingRoles(). This minimizes the risk that the
75 * user sees "unknown" icons when scrolling before the previews have
76 * arrived.
77 *
78 * 3. Finally, the entire process is repeated for any items that might have
79 * changed in the mean time.
80 */
81 class DOLPHIN_EXPORT KFileItemModelRolesUpdater : public QObject
82 {
83 Q_OBJECT
84
85 public:
86 explicit KFileItemModelRolesUpdater(KFileItemModel *model, QObject *parent = nullptr);
87 ~KFileItemModelRolesUpdater() override;
88
89 void setIconSize(const QSize &size);
90 QSize iconSize() const;
91
92 /**
93 * Sets the range of items that are visible currently. The roles
94 * of visible items are resolved first.
95 */
96 void setVisibleIndexRange(int index, int count);
97
98 void setMaximumVisibleItems(int count);
99
100 /**
101 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
102 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
103 * role.
104 */
105 void setPreviewsShown(bool show);
106 bool previewsShown() const;
107
108 /**
109 * If enabled a small preview gets upscaled to the icon size in case where
110 * the icon size is larger than the preview. Per default enlarging is
111 * enabled.
112 */
113 void setEnlargeSmallPreviews(bool enlarge);
114 bool enlargeSmallPreviews() const;
115
116 /**
117 * If \a paused is set to true the asynchronous resolving of roles will be paused.
118 * State changes during pauses like changing the icon size or the preview-shown
119 * will be remembered and handled after unpausing.
120 */
121 void setPaused(bool paused);
122 bool isPaused() const;
123
124 /**
125 * Sets the roles that should be resolved asynchronously.
126 */
127 void setRoles(const QSet<QByteArray> &roles);
128 QSet<QByteArray> roles() const;
129
130 /**
131 * Sets the list of enabled thumbnail plugins that are used for previews.
132 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
133 * are used.
134 *
135 * For a list of available plugins, call KIO::PreviewJob::availableThumbnailerPlugins().
136 *
137 * @see enabledPlugins
138 */
139 void setEnabledPlugins(const QStringList &list);
140
141 /**
142 * Returns the list of enabled thumbnail plugins.
143 * @see setEnabledPlugins
144 */
145 QStringList enabledPlugins() const;
146
147 /**
148 * Sets the maximum file size of local files for which
149 * previews will be generated (if enabled). A value of 0
150 * indicates no file size limit.
151 * Per default the value from KConfigGroup "PreviewSettings"
152 * MaximumSize is used, 0 otherwise.
153 * @param size
154 */
155 void setLocalFileSizePreviewLimit(qlonglong size);
156 qlonglong localFileSizePreviewLimit() const;
157
158 /**
159 * If set to true, directories contents are scanned to determine their size
160 * Default true
161 */
162 void setScanDirectories(bool enabled);
163 bool scanDirectories() const;
164
165 /**
166 * Notifies the updater of a change in the hover state on an item.
167 *
168 * This will trigger asynchronous loading of the next few thumb sequence images
169 * using a PreviewJob.
170 *
171 * @param item URL of the item that is hovered, or an empty URL if no item is hovered.
172 * @param seqIdx The current hover sequence index. While an item is hovered,
173 * this method will be called repeatedly with increasing values
174 * for this parameter.
175 */
176 void setHoverSequenceState(const QUrl &itemUrl, int seqIdx);
177
178 private Q_SLOTS:
179 void slotItemsInserted(const KItemRangeList &itemRanges);
180 void slotItemsRemoved(const KItemRangeList &itemRanges);
181 void slotItemsMoved(KItemRange itemRange, const QList<int> &movedToIndexes);
182 void slotItemsChanged(const KItemRangeList &itemRanges, const QSet<QByteArray> &roles);
183 void slotSortRoleChanged(const QByteArray &current, const QByteArray &previous);
184
185 /**
186 * Is invoked after a preview has been received successfully.
187 *
188 * Note that this is not called for hover sequence previews.
189 *
190 * @see startPreviewJob()
191 */
192 void slotGotPreview(const KFileItem &item, const QPixmap &pixmap);
193
194 /**
195 * Is invoked after generating a preview has failed.
196 *
197 * Note that this is not called for hover sequence previews.
198 *
199 * @see startPreviewJob()
200 */
201 void slotPreviewFailed(const KFileItem &item);
202
203 /**
204 * Is invoked when the preview job has been finished. Starts a new preview
205 * job if there are any interesting items without previews left, or updates
206 * the changed items otherwise.
207 *
208 * Note that this is not called for hover sequence previews.
209 *
210 * @see startPreviewJob()
211 */
212 void slotPreviewJobFinished();
213
214 /**
215 * Is invoked after a hover sequence preview has been received successfully.
216 */
217 void slotHoverSequenceGotPreview(const KFileItem &item, const QPixmap &pixmap);
218
219 /**
220 * Is invoked after generating a hover sequence preview has failed.
221 */
222 void slotHoverSequencePreviewFailed(const KFileItem &item);
223
224 /**
225 * Is invoked when a hover sequence preview job is finished. May start another
226 * job for the next sequence index right away by calling
227 * \a loadNextHoverSequencePreview().
228 *
229 * Note that a PreviewJob will only ever generate a single sequence image, due
230 * to limitations of the PreviewJob API.
231 */
232 void slotHoverSequencePreviewJobFinished();
233
234 /**
235 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
236 */
237 void slotOverlaysChanged(const QUrl &url, const QStringList &);
238
239 /**
240 * Resolves the sort role of the next item in m_pendingSortRole, applies it
241 * to the model, and invokes itself if there are any pending items left. If
242 * that is not the case, \a startUpdating() is called.
243 */
244 void resolveNextSortRole();
245
246 /**
247 * Resolves the icon name and (if previews are disabled) all other roles
248 * for the next interesting item. If there are no pending items left, any
249 * changed items are updated.
250 */
251 void resolveNextPendingRoles();
252
253 /**
254 * Resolves items that have not been resolved yet after the change has been
255 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
256 * expires.
257 */
258 void resolveRecentlyChangedItems();
259
260 void applyChangedBalooRoles(const QString &file);
261 void applyChangedBalooRolesForItem(const KFileItem &file);
262
263 void slotDirectoryContentsCountReceived(const QString &path, int count, long size);
264
265 private:
266 /**
267 * Starts the updating of all roles. The visible items are handled first.
268 */
269 void startUpdating();
270
271 /**
272 * Loads the icons for the visible items. After 200 ms, the function
273 * stops determining mime types and only loads preliminary icons.
274 * This is a compromise that prevents that
275 * (a) the GUI is blocked for more than 200 ms, and
276 * (b) "unknown" icons could be shown in the view.
277 */
278 void updateVisibleIcons();
279
280 /**
281 * Creates previews for the items starting from the first item in
282 * m_pendingPreviewItems.
283 * @see slotGotPreview()
284 * @see slotPreviewFailed()
285 * @see slotPreviewJobFinished()
286 */
287 void startPreviewJob();
288
289 /**
290 * Transforms a raw preview image, applying scale and frame.
291 *
292 * @param pixmap A raw preview image from a PreviewJob.
293 * @return The scaled and decorated preview image.
294 */
295 QPixmap transformPreviewPixmap(const QPixmap &pixmap);
296
297 /**
298 * Starts a PreviewJob for loading the next hover sequence image.
299 */
300 void loadNextHoverSequencePreview();
301
302 /**
303 * Aborts the currently running hover sequence PreviewJob (if any).
304 */
305 void killHoverSequencePreviewJob();
306
307 /**
308 * Ensures that icons, previews, and other roles are determined for any
309 * items that have been changed.
310 */
311 void updateChangedItems();
312
313 /**
314 * Resolves the sort role of the item and applies it to the model.
315 */
316 void applySortRole(int index);
317
318 void applySortProgressToModel();
319
320 enum ResolveHint { ResolveFast, ResolveAll };
321 bool applyResolvedRoles(int index, ResolveHint hint);
322 QHash<QByteArray, QVariant> rolesData(const KFileItem &item, int index);
323
324 /**
325 * Must be invoked if a property has been changed that affects
326 * the look of the preview. Takes care to update all previews.
327 */
328 void updateAllPreviews();
329
330 void killPreviewJob();
331
332 QList<int> indexesToResolve() const;
333
334 void trimHoverSequenceLoadedItems();
335
336 private:
337 void startDirectorySizeCounting(const KFileItem &item, int index);
338
339 enum State { Idle, Paused, ResolvingSortRole, ResolvingAllRoles, PreviewJobRunning };
340
341 State m_state;
342
343 // Property changes during pausing must be remembered to be able
344 // to react when unpausing again:
345 bool m_previewChangedDuringPausing;
346 bool m_iconSizeChangedDuringPausing;
347 bool m_rolesChangedDuringPausing;
348
349 // Property for setPreviewsShown()/previewsShown().
350 bool m_previewShown;
351
352 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
353 bool m_enlargeSmallPreviews;
354
355 // True if the role "iconPixmap" should be cleared when resolving the next
356 // role with resolveRole(). Is necessary if the preview gets disabled
357 // during the roles-updater has been paused by setPaused().
358 bool m_clearPreviews;
359
360 // Remembers which items have been handled already, to prevent that
361 // previews and other expensive roles are determined again.
362 QSet<KFileItem> m_finishedItems;
363
364 KFileItemModel *m_model;
365 QSize m_iconSize;
366 int m_firstVisibleIndex;
367 int m_lastVisibleIndex;
368 int m_maximumVisibleItems;
369 QSet<QByteArray> m_roles;
370 QSet<QByteArray> m_resolvableRoles;
371 QStringList m_enabledPlugins;
372 qulonglong m_localFileSizePreviewLimit;
373 bool m_scanDirectories;
374
375 // Items for which the sort role still has to be determined.
376 QSet<KFileItem> m_pendingSortRoleItems;
377
378 // Indexes of items which still have to be handled by
379 // resolveNextPendingRoles().
380 QList<int> m_pendingIndexes;
381
382 // Items which have been left over from the last call of startPreviewJob().
383 // A new preview job will be started from them once the first one finishes.
384 KFileItemList m_pendingPreviewItems;
385
386 KIO::PreviewJob *m_previewJob;
387
388 // Info about the item that the user currently hovers, and the current sequence
389 // index for thumb generation.
390 KFileItem m_hoverSequenceItem;
391 int m_hoverSequenceIndex;
392 KIO::PreviewJob *m_hoverSequencePreviewJob;
393 int m_hoverSequenceNumSuccessiveFailures;
394 std::list<KFileItem> m_hoverSequenceLoadedItems;
395
396 // When downloading or copying large files, the slot slotItemsChanged()
397 // will be called periodically within a quite short delay. To prevent
398 // a high CPU-load by generating e.g. previews for each notification, the update
399 // will be postponed until no file change has been done within a longer period
400 // of time.
401 QTimer *m_recentlyChangedItemsTimer;
402 QSet<KFileItem> m_recentlyChangedItems;
403
404 // Items which have not been changed repeatedly recently.
405 QSet<KFileItem> m_changedItems;
406
407 KDirectoryContentsCounter *m_directoryContentsCounter;
408
409 QList<KOverlayIconPlugin *> m_overlayIconsPlugin;
410
411 #if HAVE_BALOO
412 Baloo::FileMonitor *m_balooFileMonitor;
413 Baloo::IndexerConfig m_balooConfig;
414 #endif
415 };
416
417 #endif