2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #ifndef KFILEITEMMODELROLESUPDATER_H
8 #define KFILEITEMMODELROLESUPDATER_H
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemmodelbase.h"
15 #include "config-dolphin.h"
21 #include <QStringList>
23 class KDirectoryContentsCounter
;
27 class KOverlayIconPlugin
;
39 #include <Baloo/IndexerConfig>
43 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
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.
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.
55 * Determining the roles is done in several phases:
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().
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:
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().
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
78 * 3. Finally, the entire process is repeated for any items that might have
79 * changed in the mean time.
81 class DOLPHIN_EXPORT KFileItemModelRolesUpdater
: public QObject
86 explicit KFileItemModelRolesUpdater(KFileItemModel
*model
, QObject
*parent
= nullptr);
87 ~KFileItemModelRolesUpdater() override
;
89 void setIconSize(const QSize
&size
);
90 QSize
iconSize() const;
92 void setDevicePixelRatio(qreal devicePixelRatio
);
93 qreal
devicePixelRatio() const;
96 * Sets the range of items that are visible currently. The roles
97 * of visible items are resolved first.
99 void setVisibleIndexRange(int index
, int count
);
101 void setMaximumVisibleItems(int count
);
104 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
105 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
108 void setPreviewsShown(bool show
);
109 bool previewsShown() const;
112 * If enabled a small preview gets upscaled to the icon size in case where
113 * the icon size is larger than the preview. Per default enlarging is
116 void setEnlargeSmallPreviews(bool enlarge
);
117 bool enlargeSmallPreviews() const;
120 * If \a paused is set to true the asynchronous resolving of roles will be paused.
121 * State changes during pauses like changing the icon size or the preview-shown
122 * will be remembered and handled after unpausing.
124 void setPaused(bool paused
);
125 bool isPaused() const;
128 * Sets the roles that should be resolved asynchronously.
130 void setRoles(const QSet
<QByteArray
> &roles
);
131 QSet
<QByteArray
> roles() const;
134 * Sets the list of enabled thumbnail plugins that are used for previews.
135 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
138 * For a list of available plugins, call KIO::PreviewJob::availableThumbnailerPlugins().
140 * @see enabledPlugins
142 void setEnabledPlugins(const QStringList
&list
);
145 * Returns the list of enabled thumbnail plugins.
146 * @see setEnabledPlugins
148 QStringList
enabledPlugins() const;
151 * Sets the maximum file size of local files for which
152 * previews will be generated (if enabled). A value of 0
153 * indicates no file size limit.
154 * Per default the value from KConfigGroup "PreviewSettings"
155 * MaximumSize is used, 0 otherwise.
158 void setLocalFileSizePreviewLimit(qlonglong size
);
159 qlonglong
localFileSizePreviewLimit() const;
162 * If set to true, directories contents are scanned to determine their size
165 void setScanDirectories(bool enabled
);
166 bool scanDirectories() const;
169 * Notifies the updater of a change in the hover state on an item.
171 * This will trigger asynchronous loading of the next few thumb sequence images
172 * using a PreviewJob.
174 * @param item URL of the item that is hovered, or an empty URL if no item is hovered.
175 * @param seqIdx The current hover sequence index. While an item is hovered,
176 * this method will be called repeatedly with increasing values
177 * for this parameter.
179 void setHoverSequenceState(const QUrl
&itemUrl
, int seqIdx
);
182 void previewJobFinished(); // For unit testing
185 void slotItemsInserted(const KItemRangeList
&itemRanges
);
186 void slotItemsRemoved(const KItemRangeList
&itemRanges
);
187 void slotItemsMoved(KItemRange itemRange
, const QList
<int> &movedToIndexes
);
188 void slotItemsChanged(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &roles
);
189 void slotSortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
);
192 * Is invoked after a preview has been received successfully.
194 * Note that this is not called for hover sequence previews.
196 * @see startPreviewJob()
198 void slotGotPreview(const KFileItem
&item
, const QPixmap
&pixmap
);
201 * Is invoked after generating a preview has failed.
203 * Note that this is not called for hover sequence previews.
205 * @see startPreviewJob()
207 void slotPreviewFailed(const KFileItem
&item
);
210 * Is invoked when the preview job has been finished. Starts a new preview
211 * job if there are any interesting items without previews left, or updates
212 * the changed items otherwise.
214 * Note that this is not called for hover sequence previews.
216 * @see startPreviewJob()
218 void slotPreviewJobFinished();
221 * Is invoked after a hover sequence preview has been received successfully.
223 void slotHoverSequenceGotPreview(const KFileItem
&item
, const QPixmap
&pixmap
);
226 * Is invoked after generating a hover sequence preview has failed.
228 void slotHoverSequencePreviewFailed(const KFileItem
&item
);
231 * Is invoked when a hover sequence preview job is finished. May start another
232 * job for the next sequence index right away by calling
233 * \a loadNextHoverSequencePreview().
235 * Note that a PreviewJob will only ever generate a single sequence image, due
236 * to limitations of the PreviewJob API.
238 void slotHoverSequencePreviewJobFinished();
241 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
243 void slotOverlaysChanged(const QUrl
&url
, const QStringList
&);
246 * Resolves the sort role of the next item in m_pendingSortRole, applies it
247 * to the model, and invokes itself if there are any pending items left. If
248 * that is not the case, \a startUpdating() is called.
250 void resolveNextSortRole();
253 * Resolves the icon name and (if previews are disabled) all other roles
254 * for the next interesting item. If there are no pending items left, any
255 * changed items are updated.
257 void resolveNextPendingRoles();
260 * Resolves items that have not been resolved yet after the change has been
261 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
264 void resolveRecentlyChangedItems();
266 void applyChangedBalooRoles(const QString
&file
);
267 void applyChangedBalooRolesForItem(const KFileItem
&file
);
269 void slotDirectoryContentsCountReceived(const QString
&path
, int count
, long long size
);
273 * Starts the updating of all roles. The visible items are handled first.
275 void startUpdating();
278 * Loads the icons for the visible items. After 200 ms, the function
279 * stops determining mime types and only loads preliminary icons.
280 * This is a compromise that prevents that
281 * (a) the GUI is blocked for more than 200 ms, and
282 * (b) "unknown" icons could be shown in the view.
284 void updateVisibleIcons();
287 * Creates previews for the items starting from the first item in
288 * m_pendingPreviewItems.
289 * @see slotGotPreview()
290 * @see slotPreviewFailed()
291 * @see slotPreviewJobFinished()
293 void startPreviewJob();
296 * Transforms a raw preview image, applying scale and frame.
298 * @param pixmap A raw preview image from a PreviewJob.
299 * @param overlays the overlays to add to the pixmap
300 * @return The scaled and decorated preview image.
302 QPixmap
transformPreviewPixmap(const QPixmap
&pixmap
);
305 * Starts a PreviewJob for loading the next hover sequence image.
307 void loadNextHoverSequencePreview();
310 * Aborts the currently running hover sequence PreviewJob (if any).
312 void killHoverSequencePreviewJob();
315 * Ensures that icons, previews, and other roles are determined for any
316 * items that have been changed.
318 void updateChangedItems();
321 * Resolves the sort role of the item and applies it to the model.
322 * Despite the name, this handles both sorting and grouping, as
323 * regrouping never happens without resorting at the same time.
325 void applySortRole(int index
);
327 void applySortProgressToModel();
329 enum ResolveHint
{ ResolveFast
, ResolveAll
};
330 bool applyResolvedRoles(int index
, ResolveHint hint
);
331 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
&item
, int index
);
334 * Must be invoked if a property has been changed that affects
335 * the look of the preview. Takes care to update all previews.
337 void updateAllPreviews();
339 void killPreviewJob();
341 QList
<int> indexesToResolve() const;
343 void trimHoverSequenceLoadedItems();
345 void resetSizeData(const int index
, const int size
= 0);
347 void recountDirectoryItems(const QList
<QUrl
> directories
);
352 * enqueue directory size counting for KFileItem item at index
354 void startDirectorySizeCounting(const KFileItem
&item
, int index
);
356 enum State
{ Idle
, Paused
, ResolvingSortRole
, ResolvingAllRoles
, PreviewJobRunning
};
360 // Property changes during pausing must be remembered to be able
361 // to react when unpausing again:
362 bool m_previewChangedDuringPausing
;
363 bool m_iconSizeChangedDuringPausing
;
364 bool m_rolesChangedDuringPausing
;
366 // Property for setPreviewsShown()/previewsShown().
369 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
370 bool m_enlargeSmallPreviews
;
372 // True if the role "iconPixmap" should be cleared when resolving the next
373 // role with resolveRole(). Is necessary if the preview gets disabled
374 // during the roles-updater has been paused by setPaused().
375 bool m_clearPreviews
;
377 // Remembers which items have been handled already, to prevent that
378 // previews and other expensive roles are determined again.
379 QSet
<KFileItem
> m_finishedItems
;
381 KFileItemModel
*m_model
;
383 qreal m_devicePixelRatio
;
384 int m_firstVisibleIndex
;
385 int m_lastVisibleIndex
;
386 int m_maximumVisibleItems
;
387 QSet
<QByteArray
> m_roles
;
388 QSet
<QByteArray
> m_resolvableRoles
;
389 QStringList m_enabledPlugins
;
390 qulonglong m_localFileSizePreviewLimit
;
392 // Items for which the sort role still has to be determined.
393 QSet
<KFileItem
> m_pendingSortRoleItems
;
395 // Indexes of items which still have to be handled by
396 // resolveNextPendingRoles().
397 QList
<int> m_pendingIndexes
;
399 // Items which have been left over from the last call of startPreviewJob().
400 // A new preview job will be started from them once the first one finishes.
401 KFileItemList m_pendingPreviewItems
;
403 KIO::PreviewJob
*m_previewJob
;
405 // Info about the item that the user currently hovers, and the current sequence
406 // index for thumb generation.
407 KFileItem m_hoverSequenceItem
;
408 int m_hoverSequenceIndex
;
409 KIO::PreviewJob
*m_hoverSequencePreviewJob
;
410 int m_hoverSequenceNumSuccessiveFailures
;
411 std::list
<KFileItem
> m_hoverSequenceLoadedItems
;
413 // When downloading or copying large files, the slot slotItemsChanged()
414 // will be called periodically within a quite short delay. To prevent
415 // a high CPU-load by generating e.g. previews for each notification, the update
416 // will be postponed until no file change has been done within a longer period
418 QTimer
*m_recentlyChangedItemsTimer
;
419 QSet
<KFileItem
> m_recentlyChangedItems
;
421 // Items which have not been changed repeatedly recently.
422 QSet
<KFileItem
> m_changedItems
;
424 KDirectoryContentsCounter
*m_directoryContentsCounter
;
426 QList
<KOverlayIconPlugin
*> m_overlayIconsPlugin
;
429 Baloo::FileMonitor
*m_balooFileMonitor
;
430 Baloo::IndexerConfig m_balooConfig
;