2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kfileitemmodelrolesupdater.h"
9 #include "dolphindebug.h"
10 #include "kfileitemmodel.h"
11 #include "private/kdirectorycontentscounter.h"
12 #include "private/kpixmapmodifier.h"
15 #include <KConfigGroup>
16 #include <KIO/PreviewJob>
17 #include <KIconLoader>
18 #include <KJobWidgets>
19 #include <KOverlayIconPlugin>
20 #include <KPluginMetaData>
21 #include <KSharedConfig>
23 #include "dolphin_contentdisplaysettings.h"
26 #include "private/kbaloorolesprovider.h"
28 #include <Baloo/FileMonitor>
31 #include <QApplication>
32 #include <QElapsedTimer>
35 #include <QPluginLoader>
39 using namespace std::chrono_literals
;
41 // #define KFILEITEMMODELROLESUPDATER_DEBUG
45 // Maximum time in ms that the KFileItemModelRolesUpdater
46 // may perform a blocking operation
47 const int MaxBlockTimeout
= 200;
49 // If the number of items is smaller than ResolveAllItemsLimit,
50 // the roles of all items will be resolved.
51 const int ResolveAllItemsLimit
= 500;
53 // Not only the visible area, but up to ReadAheadPages before and after
54 // this area will be resolved.
55 const int ReadAheadPages
= 5;
58 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel
*model
, QObject
*parent
)
61 , m_previewChangedDuringPausing(false)
62 , m_iconSizeChangedDuringPausing(false)
63 , m_rolesChangedDuringPausing(false)
64 , m_previewShown(false)
65 , m_enlargeSmallPreviews(true)
66 , m_clearPreviews(false)
70 , m_firstVisibleIndex(0)
71 , m_lastVisibleIndex(-1)
72 , m_maximumVisibleItems(50)
76 , m_localFileSizePreviewLimit(0)
77 , m_pendingSortRoleItems()
79 , m_pendingPreviewItems()
81 , m_hoverSequenceItem()
82 , m_hoverSequenceIndex(0)
83 , m_hoverSequencePreviewJob(nullptr)
84 , m_hoverSequenceNumSuccessiveFailures(0)
85 , m_recentlyChangedItemsTimer(nullptr)
86 , m_recentlyChangedItems()
88 , m_directoryContentsCounter(nullptr)
90 , m_balooFileMonitor(nullptr)
95 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
96 m_enabledPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
97 m_localFileSizePreviewLimit
= static_cast<qulonglong
>(globalConfig
.readEntry("MaximumSize", 0));
99 connect(m_model
, &KFileItemModel::itemsInserted
, this, &KFileItemModelRolesUpdater::slotItemsInserted
);
100 connect(m_model
, &KFileItemModel::itemsRemoved
, this, &KFileItemModelRolesUpdater::slotItemsRemoved
);
101 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
102 connect(m_model
, &KFileItemModel::itemsMoved
, this, &KFileItemModelRolesUpdater::slotItemsMoved
);
103 connect(m_model
, &KFileItemModel::sortRoleChanged
, this, &KFileItemModelRolesUpdater::slotSortRoleChanged
);
105 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
106 // resolving of the roles. Postpone the resolving until no update has been done for 100 ms.
107 m_recentlyChangedItemsTimer
= new QTimer(this);
108 m_recentlyChangedItemsTimer
->setInterval(100ms
);
109 m_recentlyChangedItemsTimer
->setSingleShot(true);
110 connect(m_recentlyChangedItemsTimer
, &QTimer::timeout
, this, &KFileItemModelRolesUpdater::resolveRecentlyChangedItems
);
112 m_resolvableRoles
.insert("size");
113 m_resolvableRoles
.insert("type");
114 m_resolvableRoles
.insert("isExpandable");
116 m_resolvableRoles
+= KBalooRolesProvider::instance().roles();
119 m_directoryContentsCounter
= new KDirectoryContentsCounter(m_model
, this);
120 connect(m_directoryContentsCounter
, &KDirectoryContentsCounter::result
, this, &KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived
);
122 const QString pluginNamespace
= QStringLiteral("kf" QT_STRINGIFY(QT_MAJOR_VERSION
)) + QStringLiteral("/overlayicon");
123 const auto plugins
= KPluginMetaData::findPlugins(pluginNamespace
, {}, KPluginMetaData::AllowEmptyMetaData
);
124 for (const KPluginMetaData
&data
: plugins
) {
125 auto instance
= QPluginLoader(data
.fileName()).instance();
126 auto plugin
= qobject_cast
<KOverlayIconPlugin
*>(instance
);
128 m_overlayIconsPlugin
.append(plugin
);
129 connect(plugin
, &KOverlayIconPlugin::overlaysChanged
, this, &KFileItemModelRolesUpdater::slotOverlaysChanged
);
131 // not our/valid plugin, so delete the created object
137 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
142 void KFileItemModelRolesUpdater::setIconSize(const QSize
&size
)
144 if (size
!= m_iconSize
) {
146 if (m_state
== Paused
) {
147 m_iconSizeChangedDuringPausing
= true;
148 } else if (m_previewShown
) {
149 // An icon size change requires the regenerating of
151 m_finishedItems
.clear();
157 QSize
KFileItemModelRolesUpdater::iconSize() const
162 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index
, int count
)
171 if (index
== m_firstVisibleIndex
&& count
== m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) {
172 // The range has not been changed
176 m_firstVisibleIndex
= index
;
177 m_lastVisibleIndex
= qMin(index
+ count
- 1, m_model
->count() - 1);
182 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count
)
184 m_maximumVisibleItems
= count
;
187 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
189 if (show
== m_previewShown
) {
193 m_previewShown
= show
;
195 m_clearPreviews
= true;
201 bool KFileItemModelRolesUpdater::previewsShown() const
203 return m_previewShown
;
206 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
208 if (enlarge
!= m_enlargeSmallPreviews
) {
209 m_enlargeSmallPreviews
= enlarge
;
210 if (m_previewShown
) {
216 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
218 return m_enlargeSmallPreviews
;
221 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
&list
)
223 if (m_enabledPlugins
!= list
) {
224 m_enabledPlugins
= list
;
225 if (m_previewShown
) {
231 void KFileItemModelRolesUpdater::setPaused(bool paused
)
233 if (paused
== (m_state
== Paused
)) {
241 const bool updatePreviews
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) || m_previewChangedDuringPausing
;
242 const bool resolveAll
= updatePreviews
|| m_rolesChangedDuringPausing
;
244 m_finishedItems
.clear();
247 m_iconSizeChangedDuringPausing
= false;
248 m_previewChangedDuringPausing
= false;
249 m_rolesChangedDuringPausing
= false;
251 if (!m_pendingSortRoleItems
.isEmpty()) {
252 m_state
= ResolvingSortRole
;
253 resolveNextSortRole();
262 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
> &roles
)
264 if (m_roles
!= roles
) {
268 // Check whether there is at least one role that must be resolved
269 // with the help of Baloo. If this is the case, a (quite expensive)
270 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
271 // the role gets watched for changes.
272 const KBalooRolesProvider
&rolesProvider
= KBalooRolesProvider::instance();
273 bool hasBalooRole
= false;
274 QSetIterator
<QByteArray
> it(roles
);
275 while (it
.hasNext()) {
276 const QByteArray
&role
= it
.next();
277 if (rolesProvider
.roles().contains(role
)) {
283 if (hasBalooRole
&& m_balooConfig
.fileIndexingEnabled() && !m_balooFileMonitor
) {
284 m_balooFileMonitor
= new Baloo::FileMonitor(this);
285 connect(m_balooFileMonitor
, &Baloo::FileMonitor::fileMetaDataChanged
, this, &KFileItemModelRolesUpdater::applyChangedBalooRoles
);
286 } else if (!hasBalooRole
&& m_balooFileMonitor
) {
287 delete m_balooFileMonitor
;
288 m_balooFileMonitor
= nullptr;
292 if (m_state
== Paused
) {
293 m_rolesChangedDuringPausing
= true;
300 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
305 bool KFileItemModelRolesUpdater::isPaused() const
307 return m_state
== Paused
;
310 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
312 return m_enabledPlugins
;
315 void KFileItemModelRolesUpdater::setLocalFileSizePreviewLimit(const qlonglong size
)
317 m_localFileSizePreviewLimit
= size
;
320 qlonglong
KFileItemModelRolesUpdater::localFileSizePreviewLimit() const
322 return m_localFileSizePreviewLimit
;
325 void KFileItemModelRolesUpdater::setHoverSequenceState(const QUrl
&itemUrl
, int seqIdx
)
327 const KFileItem item
= m_model
->fileItem(itemUrl
);
329 if (item
!= m_hoverSequenceItem
) {
330 killHoverSequencePreviewJob();
333 m_hoverSequenceItem
= item
;
334 m_hoverSequenceIndex
= seqIdx
;
336 if (!m_previewShown
) {
340 m_hoverSequenceNumSuccessiveFailures
= 0;
342 loadNextHoverSequencePreview();
345 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
&itemRanges
)
350 // Determine the sort role synchronously for as many items as possible.
351 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
352 int insertedCount
= 0;
353 for (const KItemRange
&range
: itemRanges
) {
354 const int lastIndex
= insertedCount
+ range
.index
+ range
.count
- 1;
355 for (int i
= insertedCount
+ range
.index
; i
<= lastIndex
; ++i
) {
356 if (timer
.elapsed() < MaxBlockTimeout
) {
359 m_pendingSortRoleItems
.insert(m_model
->fileItem(i
));
362 insertedCount
+= range
.count
;
365 applySortProgressToModel();
367 // If there are still items whose sort role is unknown, check if the
368 // asynchronous determination of the sort role is already in progress,
369 // and start it if that is not the case.
370 if (!m_pendingSortRoleItems
.isEmpty() && m_state
!= ResolvingSortRole
) {
372 m_state
= ResolvingSortRole
;
373 resolveNextSortRole();
380 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
&itemRanges
)
384 const bool allItemsRemoved
= (m_model
->count() == 0);
387 if (m_balooFileMonitor
) {
388 // Don't let the FileWatcher watch for removed items
389 if (allItemsRemoved
) {
390 m_balooFileMonitor
->clear();
392 QStringList newFileList
;
393 const QStringList oldFileList
= m_balooFileMonitor
->files();
394 for (const QString
&file
: oldFileList
) {
395 if (m_model
->index(QUrl::fromLocalFile(file
)) >= 0) {
396 newFileList
.append(file
);
399 m_balooFileMonitor
->setFiles(newFileList
);
404 if (allItemsRemoved
) {
407 m_finishedItems
.clear();
408 m_pendingSortRoleItems
.clear();
409 m_pendingIndexes
.clear();
410 m_pendingPreviewItems
.clear();
411 m_recentlyChangedItems
.clear();
412 m_recentlyChangedItemsTimer
->stop();
413 m_changedItems
.clear();
414 m_hoverSequenceLoadedItems
.clear();
417 if (!m_model
->showDirectoriesOnly()) {
418 m_directoryContentsCounter
->stopWorker();
421 // Only remove the items from m_finishedItems. They will be removed
422 // from the other sets later on.
423 QSet
<KFileItem
>::iterator it
= m_finishedItems
.begin();
424 while (it
!= m_finishedItems
.end()) {
425 if (m_model
->index(*it
) < 0) {
426 it
= m_finishedItems
.erase(it
);
432 // Removed items won't have hover previews loaded anymore.
433 for (const KItemRange
&itemRange
: itemRanges
) {
434 int index
= itemRange
.index
;
435 for (int count
= itemRange
.count
; count
> 0; --count
) {
436 const KFileItem item
= m_model
->fileItem(index
);
437 m_hoverSequenceLoadedItems
.remove(item
);
442 // The visible items might have changed.
447 void KFileItemModelRolesUpdater::slotItemsMoved(KItemRange itemRange
, const QList
<int> &movedToIndexes
)
450 Q_UNUSED(movedToIndexes
)
452 // The visible items might have changed.
456 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &roles
)
460 // Find out if slotItemsChanged() has been done recently. If that is the
461 // case, resolving the roles is postponed until a timer has exceeded
462 // to prevent expensive repeated updates if files are updated frequently.
463 const bool itemsChangedRecently
= m_recentlyChangedItemsTimer
->isActive();
465 QSet
<KFileItem
> &targetSet
= itemsChangedRecently
? m_recentlyChangedItems
: m_changedItems
;
467 for (const KItemRange
&itemRange
: itemRanges
) {
468 int index
= itemRange
.index
;
469 for (int count
= itemRange
.count
; count
> 0; --count
) {
470 const KFileItem item
= m_model
->fileItem(index
);
471 targetSet
.insert(item
);
476 m_recentlyChangedItemsTimer
->start();
478 if (!itemsChangedRecently
) {
479 updateChangedItems();
483 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
)
488 if (m_resolvableRoles
.contains(current
)) {
489 m_pendingSortRoleItems
.clear();
490 m_finishedItems
.clear();
492 const int count
= m_model
->count();
496 // Determine the sort role synchronously for as many items as possible.
497 for (int index
= 0; index
< count
; ++index
) {
498 if (timer
.elapsed() < MaxBlockTimeout
) {
499 applySortRole(index
);
501 m_pendingSortRoleItems
.insert(m_model
->fileItem(index
));
505 applySortProgressToModel();
507 if (!m_pendingSortRoleItems
.isEmpty()) {
508 // Trigger the asynchronous determination of the sort role.
510 m_state
= ResolvingSortRole
;
511 resolveNextSortRole();
515 m_pendingSortRoleItems
.clear();
516 applySortProgressToModel();
520 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
522 if (m_state
!= PreviewJobRunning
) {
526 m_changedItems
.remove(item
);
528 const int index
= m_model
->index(item
);
533 QPixmap scaledPixmap
= transformPreviewPixmap(pixmap
);
535 QHash
<QByteArray
, QVariant
> data
= rolesData(item
, index
);
537 const QStringList overlays
= data
["iconOverlays"].toStringList();
538 // Strangely KFileItem::overlays() returns empty string-values, so
539 // we need to check first whether an overlay must be drawn at all.
540 // It is more efficient to do it here, as KIconLoader::drawOverlays()
541 // assumes that an overlay will be drawn and has some additional
543 if (!scaledPixmap
.isNull()) {
544 for (const QString
&overlay
: overlays
) {
545 if (!overlay
.isEmpty()) {
546 // There is at least one overlay, draw all overlays above m_pixmap
547 // and cancel the check
548 KIconLoader::global()->drawOverlays(overlays
, scaledPixmap
, KIconLoader::Desktop
);
554 data
.insert("iconPixmap", scaledPixmap
);
556 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
557 m_model
->setData(index
, data
);
558 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
560 m_finishedItems
.insert(item
);
563 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
&item
)
565 if (m_state
!= PreviewJobRunning
) {
569 m_changedItems
.remove(item
);
571 const int index
= m_model
->index(item
);
573 QHash
<QByteArray
, QVariant
> data
;
574 data
.insert("iconPixmap", QPixmap());
576 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
577 m_model
->setData(index
, data
);
578 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
580 applyResolvedRoles(index
, ResolveAll
);
581 m_finishedItems
.insert(item
);
585 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
587 m_previewJob
= nullptr;
589 if (m_state
!= PreviewJobRunning
) {
595 if (!m_pendingPreviewItems
.isEmpty()) {
598 if (!m_changedItems
.isEmpty()) {
599 updateChangedItems();
604 void KFileItemModelRolesUpdater::slotHoverSequenceGotPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
606 const int index
= m_model
->index(item
);
611 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
612 QVector
<QPixmap
> pixmaps
= data
["hoverSequencePixmaps"].value
<QVector
<QPixmap
>>();
613 const int loadedIndex
= pixmaps
.size();
615 float wap
= m_hoverSequencePreviewJob
->sequenceIndexWraparoundPoint();
616 if (!m_hoverSequencePreviewJob
->handlesSequences()) {
620 data
["hoverSequenceWraparoundPoint"] = wap
;
621 m_model
->setData(index
, data
);
624 // For hover sequence previews we never load index 0, because that's just the regular preview
625 // in "iconPixmap". But that means we'll load index 1 even for thumbnailers that don't support
626 // sequences, in which case we can just throw away the preview because it's the same as for
627 // index 0. Unfortunately we can't find it out earlier :(
628 if (wap
< 0.0f
|| loadedIndex
< static_cast<int>(wap
)) {
629 // Add the preview to the model data
631 const QPixmap scaledPixmap
= transformPreviewPixmap(pixmap
);
633 pixmaps
.append(scaledPixmap
);
634 data
["hoverSequencePixmaps"] = QVariant::fromValue(pixmaps
);
636 m_model
->setData(index
, data
);
638 const auto loadedIt
= std::find(m_hoverSequenceLoadedItems
.begin(), m_hoverSequenceLoadedItems
.end(), item
);
639 if (loadedIt
== m_hoverSequenceLoadedItems
.end()) {
640 m_hoverSequenceLoadedItems
.push_back(item
);
641 trimHoverSequenceLoadedItems();
645 m_hoverSequenceNumSuccessiveFailures
= 0;
648 void KFileItemModelRolesUpdater::slotHoverSequencePreviewFailed(const KFileItem
&item
)
650 const int index
= m_model
->index(item
);
655 static const int numRetries
= 2;
657 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
658 QVector
<QPixmap
> pixmaps
= data
["hoverSequencePixmaps"].value
<QVector
<QPixmap
>>();
660 qCDebug(DolphinDebug
).nospace() << "Failed to generate hover sequence preview #" << pixmaps
.size() << " for file " << item
.url().toString() << " (attempt "
661 << (m_hoverSequenceNumSuccessiveFailures
+ 1) << "/" << (numRetries
+ 1) << ")";
663 if (m_hoverSequenceNumSuccessiveFailures
>= numRetries
) {
664 // Give up and simply duplicate the previous sequence image (if any)
666 pixmaps
.append(pixmaps
.empty() ? QPixmap() : pixmaps
.last());
667 data
["hoverSequencePixmaps"] = QVariant::fromValue(pixmaps
);
669 if (!data
.contains("hoverSequenceWraparoundPoint")) {
670 // hoverSequenceWraparoundPoint is only available when PreviewJob succeeds, so unless
671 // it has previously succeeded, it's best to assume that it just doesn't handle
672 // sequences instead of trying to load the next image indefinitely.
673 data
["hoverSequenceWraparoundPoint"] = 1.0f
;
676 m_model
->setData(index
, data
);
678 m_hoverSequenceNumSuccessiveFailures
= 0;
682 m_hoverSequenceNumSuccessiveFailures
++;
685 // Next image in the sequence (or same one if the retry limit wasn't reached yet) will be
686 // loaded automatically, because slotHoverSequencePreviewJobFinished() will be triggered
687 // even when PreviewJob fails.
690 void KFileItemModelRolesUpdater::slotHoverSequencePreviewJobFinished()
692 const int index
= m_model
->index(m_hoverSequenceItem
);
694 m_hoverSequencePreviewJob
= nullptr;
698 // Since a PreviewJob can only have one associated sequence index, we can only generate
699 // one sequence image per job, so we have to start another one for the next index.
701 // Load the next image in the sequence
702 m_hoverSequencePreviewJob
= nullptr;
703 loadNextHoverSequencePreview();
706 void KFileItemModelRolesUpdater::resolveNextSortRole()
708 if (m_state
!= ResolvingSortRole
) {
712 QSet
<KFileItem
>::iterator it
= m_pendingSortRoleItems
.begin();
713 while (it
!= m_pendingSortRoleItems
.end()) {
714 const KFileItem item
= *it
;
715 const int index
= m_model
->index(item
);
717 // Continue if the sort role has already been determined for the
718 // item, and the item has not been changed recently.
719 if (!m_changedItems
.contains(item
) && m_model
->data(index
).contains(m_model
->sortRole())) {
720 it
= m_pendingSortRoleItems
.erase(it
);
724 applySortRole(index
);
725 m_pendingSortRoleItems
.erase(it
);
729 if (!m_pendingSortRoleItems
.isEmpty()) {
730 applySortProgressToModel();
731 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextSortRole
);
735 // Prevent that we try to update the items twice.
736 disconnect(m_model
, &KFileItemModel::itemsMoved
, this, &KFileItemModelRolesUpdater::slotItemsMoved
);
737 applySortProgressToModel();
738 connect(m_model
, &KFileItemModel::itemsMoved
, this, &KFileItemModelRolesUpdater::slotItemsMoved
);
743 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
745 if (m_state
!= ResolvingAllRoles
) {
749 while (!m_pendingIndexes
.isEmpty()) {
750 const int index
= m_pendingIndexes
.takeFirst();
751 const KFileItem item
= m_model
->fileItem(index
);
753 if (m_finishedItems
.contains(item
)) {
757 applyResolvedRoles(index
, ResolveAll
);
758 m_finishedItems
.insert(item
);
759 m_changedItems
.remove(item
);
763 if (!m_pendingIndexes
.isEmpty()) {
764 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles
);
768 if (m_clearPreviews
) {
769 // Only go through the list if there are items which might still have previews.
770 if (m_finishedItems
.count() != m_model
->count()) {
771 QHash
<QByteArray
, QVariant
> data
;
772 data
.insert("iconPixmap", QPixmap());
773 data
.insert("hoverSequencePixmaps", QVariant::fromValue(QVector
<QPixmap
>()));
775 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
776 for (int index
= 0; index
<= m_model
->count(); ++index
) {
777 if (m_model
->data(index
).contains("iconPixmap") || m_model
->data(index
).contains("hoverSequencePixmaps")) {
778 m_model
->setData(index
, data
);
781 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
783 m_clearPreviews
= false;
786 if (!m_changedItems
.isEmpty()) {
787 updateChangedItems();
792 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
794 m_changedItems
+= m_recentlyChangedItems
;
795 m_recentlyChangedItems
.clear();
796 updateChangedItems();
799 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString
&file
)
802 const KFileItem item
= m_model
->fileItem(QUrl::fromLocalFile(file
));
805 // itemUrl is not in the model anymore, probably because
806 // the corresponding file has been deleted in the meantime.
809 applyChangedBalooRolesForItem(item
);
815 void KFileItemModelRolesUpdater::applyChangedBalooRolesForItem(const KFileItem
&item
)
818 Baloo::File
file(item
.localPath());
821 const KBalooRolesProvider
&rolesProvider
= KBalooRolesProvider::instance();
822 QHash
<QByteArray
, QVariant
> data
;
824 const auto roles
= rolesProvider
.roles();
825 for (const QByteArray
&role
: roles
) {
826 // Overwrite all the role values with an empty QVariant, because the roles
827 // provider doesn't overwrite it when the property value list is empty.
829 data
.insert(role
, QVariant());
832 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(file
, m_roles
));
833 while (it
.hasNext()) {
835 data
.insert(it
.key(), it
.value());
838 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
839 const int index
= m_model
->index(item
);
840 m_model
->setData(index
, data
);
841 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
849 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString
&path
, int count
, long long size
)
851 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
852 const bool getSizeRole
= m_roles
.contains("size");
854 if (getSizeRole
|| getIsExpandableRole
) {
855 const int index
= m_model
->index(QUrl::fromLocalFile(path
));
857 QHash
<QByteArray
, QVariant
> data
;
860 data
.insert("count", count
);
861 data
.insert("size", QVariant::fromValue(size
));
863 if (getIsExpandableRole
) {
864 data
.insert("isExpandable", count
> 0);
867 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
868 m_model
->setData(index
, data
);
869 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
874 void KFileItemModelRolesUpdater::startUpdating()
876 if (m_state
== Paused
) {
880 if (m_finishedItems
.count() == m_model
->count()) {
881 // All roles have been resolved already.
886 // Terminate all updates that are currently active.
888 m_pendingIndexes
.clear();
893 // Determine the icons for the visible items synchronously.
894 updateVisibleIcons();
896 // A detailed update of the items in and near the visible area
897 // only makes sense if sorting is finished.
898 if (m_state
== ResolvingSortRole
) {
902 // Start the preview job or the asynchronous resolving of all roles.
903 QList
<int> indexes
= indexesToResolve();
905 if (m_previewShown
) {
906 m_pendingPreviewItems
.clear();
907 m_pendingPreviewItems
.reserve(indexes
.count());
909 for (int index
: qAsConst(indexes
)) {
910 const KFileItem item
= m_model
->fileItem(index
);
911 if (!m_finishedItems
.contains(item
)) {
912 m_pendingPreviewItems
.append(item
);
918 m_pendingIndexes
= indexes
;
919 // Trigger the asynchronous resolving of all roles.
920 m_state
= ResolvingAllRoles
;
921 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles
);
925 void KFileItemModelRolesUpdater::updateVisibleIcons()
927 int lastVisibleIndex
= m_lastVisibleIndex
;
928 if (lastVisibleIndex
<= 0) {
929 // Guess a reasonable value for the last visible index if the view
930 // has not told us about the real value yet.
931 lastVisibleIndex
= qMin(m_firstVisibleIndex
+ m_maximumVisibleItems
, m_model
->count() - 1);
932 if (lastVisibleIndex
<= 0) {
933 lastVisibleIndex
= qMin(200, m_model
->count() - 1);
940 // Try to determine the final icons for all visible items.
942 for (index
= m_firstVisibleIndex
; index
<= lastVisibleIndex
&& timer
.elapsed() < MaxBlockTimeout
; ++index
) {
943 applyResolvedRoles(index
, ResolveFast
);
946 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
947 // preliminary icons (i.e., without mime type determination) for the
951 void KFileItemModelRolesUpdater::startPreviewJob()
953 m_state
= PreviewJobRunning
;
955 if (m_pendingPreviewItems
.isEmpty()) {
956 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
960 // PreviewJob internally caches items always with the size of
961 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
962 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
963 // do a downscaling anyhow because of the frame, so in this case only the provided
964 // cache sizes are requested.
965 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128) ? QSize(256, 256) : QSize(128, 128);
967 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
968 // worst case) might block the application for several seconds. To prevent such
969 // a blocking, we only pass items with known mime type to the preview job.
970 const int count
= m_pendingPreviewItems
.count();
971 KFileItemList itemSubSet
;
972 itemSubSet
.reserve(count
);
974 if (m_pendingPreviewItems
.first().isMimeTypeKnown()) {
975 // Some mime types are known already, probably because they were
976 // determined when loading the icons for the visible items. Start
977 // a preview job for all items at the beginning of the list which
978 // have a known mime type.
980 itemSubSet
.append(m_pendingPreviewItems
.takeFirst());
981 } while (!m_pendingPreviewItems
.isEmpty() && m_pendingPreviewItems
.first().isMimeTypeKnown());
983 // Determine mime types for MaxBlockTimeout ms, and start a preview
984 // job for the corresponding items.
989 const KFileItem item
= m_pendingPreviewItems
.takeFirst();
990 item
.determineMimeType();
991 itemSubSet
.append(item
);
992 } while (!m_pendingPreviewItems
.isEmpty() && timer
.elapsed() < MaxBlockTimeout
);
995 KIO::PreviewJob
*job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
997 job
->setIgnoreMaximumSize(itemSubSet
.first().isLocalFile() && !itemSubSet
.first().isSlow() && m_localFileSizePreviewLimit
<= 0);
998 if (job
->uiDelegate()) {
999 KJobWidgets::setWindow(job
, qApp
->activeWindow());
1002 connect(job
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotGotPreview
);
1003 connect(job
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotPreviewFailed
);
1004 connect(job
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
1009 QPixmap
KFileItemModelRolesUpdater::transformPreviewPixmap(const QPixmap
&pixmap
)
1011 QPixmap scaledPixmap
= pixmap
;
1013 if (!pixmap
.hasAlpha() && !pixmap
.isNull() && m_iconSize
.width() > KIconLoader::SizeSmallMedium
&& m_iconSize
.height() > KIconLoader::SizeSmallMedium
) {
1014 if (m_enlargeSmallPreviews
) {
1015 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
1017 // Assure that small previews don't get enlarged. Instead they
1018 // should be shown centered within the frame.
1019 const QSize contentSize
= KPixmapModifier::sizeInsideFrame(m_iconSize
);
1020 const bool enlargingRequired
= scaledPixmap
.width() < contentSize
.width() && scaledPixmap
.height() < contentSize
.height();
1021 if (enlargingRequired
) {
1022 QSize frameSize
= scaledPixmap
.size() / scaledPixmap
.devicePixelRatio();
1023 frameSize
.scale(m_iconSize
, Qt::KeepAspectRatio
);
1025 QPixmap
largeFrame(frameSize
);
1026 largeFrame
.fill(Qt::transparent
);
1028 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
1030 QPainter
painter(&largeFrame
);
1031 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width() / scaledPixmap
.devicePixelRatio()) / 2,
1032 (largeFrame
.height() - scaledPixmap
.height() / scaledPixmap
.devicePixelRatio()) / 2,
1034 scaledPixmap
= largeFrame
;
1036 // The image must be shrunk as it is too large to fit into
1037 // the available icon size
1038 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
1041 } else if (!pixmap
.isNull()) {
1042 KPixmapModifier::scale(scaledPixmap
, m_iconSize
* qApp
->devicePixelRatio());
1043 scaledPixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1046 return scaledPixmap
;
1049 void KFileItemModelRolesUpdater::loadNextHoverSequencePreview()
1051 if (m_hoverSequenceItem
.isNull() || m_hoverSequencePreviewJob
) {
1055 const int index
= m_model
->index(m_hoverSequenceItem
);
1060 // We generate the next few sequence indices in advance (buffering)
1061 const int maxSeqIdx
= m_hoverSequenceIndex
+ 5;
1063 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1065 if (!data
.contains("hoverSequencePixmaps")) {
1066 // The pixmap at index 0 isn't used ("iconPixmap" will be used instead)
1067 data
.insert("hoverSequencePixmaps", QVariant::fromValue(QVector
<QPixmap
>() << QPixmap()));
1068 m_model
->setData(index
, data
);
1071 const QVector
<QPixmap
> pixmaps
= data
["hoverSequencePixmaps"].value
<QVector
<QPixmap
>>();
1073 const int loadSeqIdx
= pixmaps
.size();
1076 if (data
.contains("hoverSequenceWraparoundPoint")) {
1077 wap
= data
["hoverSequenceWraparoundPoint"].toFloat();
1079 if (wap
>= 1.0f
&& loadSeqIdx
>= static_cast<int>(wap
)) {
1080 // Reached the wraparound point -> no more previews to load.
1084 if (loadSeqIdx
> maxSeqIdx
) {
1085 // Wait until setHoverSequenceState() is called with a higher sequence index.
1089 // PreviewJob internally caches items always with the size of
1090 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
1091 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
1092 // do a downscaling anyhow because of the frame, so in this case only the provided
1093 // cache sizes are requested.
1094 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128) ? QSize(256, 256) : QSize(128, 128);
1096 KIO::PreviewJob
*job
= new KIO::PreviewJob({m_hoverSequenceItem
}, cacheSize
, &m_enabledPlugins
);
1098 job
->setSequenceIndex(loadSeqIdx
);
1099 job
->setIgnoreMaximumSize(m_hoverSequenceItem
.isLocalFile() && !m_hoverSequenceItem
.isSlow() && m_localFileSizePreviewLimit
<= 0);
1100 if (job
->uiDelegate()) {
1101 KJobWidgets::setWindow(job
, qApp
->activeWindow());
1104 connect(job
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotHoverSequenceGotPreview
);
1105 connect(job
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewFailed
);
1106 connect(job
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewJobFinished
);
1108 m_hoverSequencePreviewJob
= job
;
1111 void KFileItemModelRolesUpdater::killHoverSequencePreviewJob()
1113 if (m_hoverSequencePreviewJob
) {
1114 disconnect(m_hoverSequencePreviewJob
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotHoverSequenceGotPreview
);
1115 disconnect(m_hoverSequencePreviewJob
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewFailed
);
1116 disconnect(m_hoverSequencePreviewJob
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewJobFinished
);
1117 m_hoverSequencePreviewJob
->kill();
1118 m_hoverSequencePreviewJob
= nullptr;
1122 void KFileItemModelRolesUpdater::updateChangedItems()
1124 if (m_state
== Paused
) {
1128 if (m_changedItems
.isEmpty()) {
1132 m_finishedItems
-= m_changedItems
;
1134 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
1135 m_pendingSortRoleItems
+= m_changedItems
;
1137 if (m_state
!= ResolvingSortRole
) {
1138 // Stop the preview job if necessary, and trigger the
1139 // asynchronous determination of the sort role.
1141 m_state
= ResolvingSortRole
;
1142 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextSortRole
);
1148 QList
<int> visibleChangedIndexes
;
1149 QList
<int> invisibleChangedIndexes
;
1150 visibleChangedIndexes
.reserve(m_changedItems
.size());
1151 invisibleChangedIndexes
.reserve(m_changedItems
.size());
1153 auto changedItemsIt
= m_changedItems
.begin();
1154 while (changedItemsIt
!= m_changedItems
.end()) {
1155 const auto &item
= *changedItemsIt
;
1156 const int index
= m_model
->index(item
);
1159 changedItemsIt
= m_changedItems
.erase(changedItemsIt
);
1164 if (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
) {
1165 visibleChangedIndexes
.append(index
);
1167 invisibleChangedIndexes
.append(index
);
1171 std::sort(visibleChangedIndexes
.begin(), visibleChangedIndexes
.end());
1173 if (m_previewShown
) {
1174 for (int index
: qAsConst(visibleChangedIndexes
)) {
1175 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
1178 for (int index
: qAsConst(invisibleChangedIndexes
)) {
1179 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
1182 if (!m_previewJob
) {
1186 const bool resolvingInProgress
= !m_pendingIndexes
.isEmpty();
1187 m_pendingIndexes
= visibleChangedIndexes
+ m_pendingIndexes
+ invisibleChangedIndexes
;
1188 if (!resolvingInProgress
) {
1189 // Trigger the asynchronous resolving of the changed roles.
1190 m_state
= ResolvingAllRoles
;
1191 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles
);
1196 void KFileItemModelRolesUpdater::applySortRole(int index
)
1198 QHash
<QByteArray
, QVariant
> data
;
1199 const KFileItem item
= m_model
->fileItem(index
);
1201 if (m_model
->sortRole() == "type") {
1202 if (!item
.isMimeTypeKnown()) {
1203 item
.determineMimeType();
1206 data
.insert("type", item
.mimeComment());
1207 } else if (m_model
->sortRole() == "size" && item
.isLocalFile() && item
.isDir()) {
1208 startDirectorySizeCounting(item
, index
);
1211 // Probably the sort role is a baloo role - just determine all roles.
1212 data
= rolesData(item
, index
);
1215 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1216 m_model
->setData(index
, data
);
1217 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1220 void KFileItemModelRolesUpdater::applySortProgressToModel()
1222 // Inform the model about the progress of the resolved items,
1223 // so that it can give an indication when the sorting has been finished.
1224 const int resolvedCount
= m_model
->count() - m_pendingSortRoleItems
.count();
1225 m_model
->emitSortProgress(resolvedCount
);
1228 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index
, ResolveHint hint
)
1230 const KFileItem item
= m_model
->fileItem(index
);
1231 const bool resolveAll
= (hint
== ResolveAll
);
1233 bool iconChanged
= false;
1234 if (!item
.isMimeTypeKnown() || !item
.isFinalIconKnown()) {
1235 item
.determineMimeType();
1237 } else if (!m_model
->data(index
).contains("iconName")) {
1241 if (iconChanged
|| resolveAll
|| m_clearPreviews
) {
1246 QHash
<QByteArray
, QVariant
> data
;
1248 data
= rolesData(item
, index
);
1251 if (!item
.iconName().isEmpty()) {
1252 data
.insert("iconName", item
.iconName());
1255 if (m_clearPreviews
) {
1256 data
.insert("iconPixmap", QPixmap());
1257 data
.insert("hoverSequencePixmaps", QVariant::fromValue(QVector
<QPixmap
>()));
1260 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1261 m_model
->setData(index
, data
);
1262 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1269 void KFileItemModelRolesUpdater::startDirectorySizeCounting(const KFileItem
&item
, int index
)
1271 if (ContentDisplaySettings::directorySizeCount() || item
.isSlow() || !item
.isLocalFile()) {
1272 // fastpath no recursion necessary
1274 auto data
= m_model
->data(index
);
1275 if (data
.value("size") == -2) {
1276 // means job already started
1280 auto url
= item
.url();
1281 if (!item
.localPath().isEmpty()) {
1282 // optimization for desktop:/, trash:/
1283 url
= QUrl::fromLocalFile(item
.localPath());
1286 data
.insert("isExpandable", false);
1287 data
.insert("count", 0);
1288 data
.insert("size", -2); // invalid size, -1 means size unknown
1290 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1291 m_model
->setData(index
, data
);
1292 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1294 auto listJob
= KIO::listDir(url
);
1295 QObject::connect(listJob
, &KIO::ListJob::entries
, this, [this, index
](const KJob
* /*job*/, const KIO::UDSEntryList
&list
) {
1296 auto data
= m_model
->data(index
);
1297 int origCount
= data
.value("count").toInt();
1298 int entryCount
= origCount
;
1300 for (const KIO::UDSEntry
&entry
: list
) {
1301 const auto name
= entry
.stringValue(KIO::UDSEntry::UDS_NAME
);
1303 if (name
== QStringLiteral("..") || name
== QStringLiteral(".")) {
1306 if (!m_model
->showHiddenFiles() && name
.startsWith(QLatin1Char('.'))) {
1309 if (m_model
->showDirectoriesOnly() && !entry
.isDir()) {
1315 // count has changed
1316 if (origCount
< entryCount
) {
1317 QHash
<QByteArray
, QVariant
> data
;
1318 data
.insert("isExpandable", entryCount
> 0);
1319 data
.insert("count", entryCount
);
1321 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1322 m_model
->setData(index
, data
);
1323 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1329 // Tell m_directoryContentsCounter that we want to count the items
1330 // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
1331 const QString path
= item
.localPath();
1332 const auto priority
= index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
? KDirectoryContentsCounter::PathCountPriority::High
1333 : KDirectoryContentsCounter::PathCountPriority::Normal
;
1335 m_directoryContentsCounter
->scanDirectory(path
, priority
);
1338 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
&item
, int index
)
1340 QHash
<QByteArray
, QVariant
> data
;
1342 const bool getSizeRole
= m_roles
.contains("size");
1343 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
1345 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir()) {
1346 startDirectorySizeCounting(item
, index
);
1349 if (m_roles
.contains("extension")) {
1350 // TODO KF6 use KFileItem::suffix 464722
1351 data
.insert("extension", QFileInfo(item
.name()).suffix());
1354 if (m_roles
.contains("type")) {
1355 data
.insert("type", item
.mimeComment());
1358 QStringList overlays
= item
.overlays();
1359 for (KOverlayIconPlugin
*it
: qAsConst(m_overlayIconsPlugin
)) {
1360 overlays
.append(it
->getOverlays(item
.url()));
1362 if (!overlays
.isEmpty()) {
1363 data
.insert("iconOverlays", overlays
);
1367 if (m_balooFileMonitor
) {
1368 m_balooFileMonitor
->addFile(item
.localPath());
1369 applyChangedBalooRolesForItem(item
);
1375 void KFileItemModelRolesUpdater::slotOverlaysChanged(const QUrl
&url
, const QStringList
&)
1377 const KFileItem item
= m_model
->fileItem(url
);
1378 if (item
.isNull()) {
1381 const int index
= m_model
->index(item
);
1382 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1383 QStringList overlays
= item
.overlays();
1384 for (KOverlayIconPlugin
*it
: qAsConst(m_overlayIconsPlugin
)) {
1385 overlays
.append(it
->getOverlays(url
));
1387 data
.insert("iconOverlays", overlays
);
1388 m_model
->setData(index
, data
);
1391 void KFileItemModelRolesUpdater::updateAllPreviews()
1393 if (m_state
== Paused
) {
1394 m_previewChangedDuringPausing
= true;
1396 m_finishedItems
.clear();
1401 void KFileItemModelRolesUpdater::killPreviewJob()
1404 disconnect(m_previewJob
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotGotPreview
);
1405 disconnect(m_previewJob
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotPreviewFailed
);
1406 disconnect(m_previewJob
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
1407 m_previewJob
->kill();
1408 m_previewJob
= nullptr;
1409 m_pendingPreviewItems
.clear();
1413 QList
<int> KFileItemModelRolesUpdater::indexesToResolve() const
1415 const int count
= m_model
->count();
1418 result
.reserve(qMin(count
, (m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) + ResolveAllItemsLimit
+ (2 * m_maximumVisibleItems
)));
1420 // Add visible items.
1421 // Resolve files first, their previews are quicker.
1422 QList
<int> visibleDirs
;
1423 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
1424 const KFileItem item
= m_model
->fileItem(i
);
1426 visibleDirs
.append(i
);
1432 result
.append(visibleDirs
);
1434 // We need a reasonable upper limit for number of items to resolve after
1435 // and before the visible range. m_maximumVisibleItems can be quite large
1436 // when using Compact View.
1437 const int readAheadItems
= qMin(ReadAheadPages
* m_maximumVisibleItems
, ResolveAllItemsLimit
/ 2);
1439 // Add items after the visible range.
1440 const int endExtendedVisibleRange
= qMin(m_lastVisibleIndex
+ readAheadItems
, count
- 1);
1441 for (int i
= m_lastVisibleIndex
+ 1; i
<= endExtendedVisibleRange
; ++i
) {
1445 // Add items before the visible range in reverse order.
1446 const int beginExtendedVisibleRange
= qMax(0, m_firstVisibleIndex
- readAheadItems
);
1447 for (int i
= m_firstVisibleIndex
- 1; i
>= beginExtendedVisibleRange
; --i
) {
1451 // Add items on the last page.
1452 const int beginLastPage
= qMax(endExtendedVisibleRange
+ 1, count
- m_maximumVisibleItems
);
1453 for (int i
= beginLastPage
; i
< count
; ++i
) {
1457 // Add items on the first page.
1458 const int endFirstPage
= qMin(beginExtendedVisibleRange
, m_maximumVisibleItems
);
1459 for (int i
= 0; i
< endFirstPage
; ++i
) {
1463 // Continue adding items until ResolveAllItemsLimit is reached.
1464 int remainingItems
= ResolveAllItemsLimit
- result
.count();
1466 for (int i
= endExtendedVisibleRange
+ 1; i
< beginLastPage
&& remainingItems
> 0; ++i
) {
1471 for (int i
= beginExtendedVisibleRange
- 1; i
>= endFirstPage
&& remainingItems
> 0; --i
) {
1479 void KFileItemModelRolesUpdater::trimHoverSequenceLoadedItems()
1481 static const size_t maxLoadedItems
= 20;
1483 size_t loadedItems
= m_hoverSequenceLoadedItems
.size();
1484 while (loadedItems
> maxLoadedItems
) {
1485 const KFileItem item
= m_hoverSequenceLoadedItems
.front();
1487 m_hoverSequenceLoadedItems
.pop_front();
1490 const int index
= m_model
->index(item
);
1492 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1493 data
["hoverSequencePixmaps"] = QVariant::fromValue(QVector
<QPixmap
>() << QPixmap());
1494 m_model
->setData(index
, data
);