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 auto plugins
= KPluginMetaData::findPlugins(QStringLiteral("kf6/overlayicon"), {}, KPluginMetaData::AllowEmptyMetaData
);
123 for (const KPluginMetaData
&data
: plugins
) {
124 auto instance
= QPluginLoader(data
.fileName()).instance();
125 auto plugin
= qobject_cast
<KOverlayIconPlugin
*>(instance
);
127 m_overlayIconsPlugin
.append(plugin
);
128 connect(plugin
, &KOverlayIconPlugin::overlaysChanged
, this, &KFileItemModelRolesUpdater::slotOverlaysChanged
);
130 // not our/valid plugin, so delete the created object
136 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
141 void KFileItemModelRolesUpdater::setIconSize(const QSize
&size
)
143 if (size
!= m_iconSize
) {
145 if (m_state
== Paused
) {
146 m_iconSizeChangedDuringPausing
= true;
147 } else if (m_previewShown
) {
148 // An icon size change requires the regenerating of
150 m_finishedItems
.clear();
156 QSize
KFileItemModelRolesUpdater::iconSize() const
161 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index
, int count
)
170 if (index
== m_firstVisibleIndex
&& count
== m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) {
171 // The range has not been changed
175 m_firstVisibleIndex
= index
;
176 m_lastVisibleIndex
= qMin(index
+ count
- 1, m_model
->count() - 1);
181 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count
)
183 m_maximumVisibleItems
= count
;
186 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
188 if (show
== m_previewShown
) {
192 m_previewShown
= show
;
194 m_clearPreviews
= true;
200 bool KFileItemModelRolesUpdater::previewsShown() const
202 return m_previewShown
;
205 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
207 if (enlarge
!= m_enlargeSmallPreviews
) {
208 m_enlargeSmallPreviews
= enlarge
;
209 if (m_previewShown
) {
215 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
217 return m_enlargeSmallPreviews
;
220 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
&list
)
222 if (m_enabledPlugins
!= list
) {
223 m_enabledPlugins
= list
;
224 if (m_previewShown
) {
230 void KFileItemModelRolesUpdater::setPaused(bool paused
)
232 if (paused
== (m_state
== Paused
)) {
240 const bool updatePreviews
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) || m_previewChangedDuringPausing
;
241 const bool resolveAll
= updatePreviews
|| m_rolesChangedDuringPausing
;
243 m_finishedItems
.clear();
246 m_iconSizeChangedDuringPausing
= false;
247 m_previewChangedDuringPausing
= false;
248 m_rolesChangedDuringPausing
= false;
250 if (!m_pendingSortRoleItems
.isEmpty()) {
251 m_state
= ResolvingSortRole
;
252 resolveNextSortRole();
261 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
> &roles
)
263 if (m_roles
!= roles
) {
267 // Check whether there is at least one role that must be resolved
268 // with the help of Baloo. If this is the case, a (quite expensive)
269 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
270 // the role gets watched for changes.
271 const KBalooRolesProvider
&rolesProvider
= KBalooRolesProvider::instance();
272 bool hasBalooRole
= false;
273 QSetIterator
<QByteArray
> it(roles
);
274 while (it
.hasNext()) {
275 const QByteArray
&role
= it
.next();
276 if (rolesProvider
.roles().contains(role
)) {
282 if (hasBalooRole
&& m_balooConfig
.fileIndexingEnabled() && !m_balooFileMonitor
) {
283 m_balooFileMonitor
= new Baloo::FileMonitor(this);
284 connect(m_balooFileMonitor
, &Baloo::FileMonitor::fileMetaDataChanged
, this, &KFileItemModelRolesUpdater::applyChangedBalooRoles
);
285 } else if (!hasBalooRole
&& m_balooFileMonitor
) {
286 delete m_balooFileMonitor
;
287 m_balooFileMonitor
= nullptr;
291 if (m_state
== Paused
) {
292 m_rolesChangedDuringPausing
= true;
299 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
304 bool KFileItemModelRolesUpdater::isPaused() const
306 return m_state
== Paused
;
309 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
311 return m_enabledPlugins
;
314 void KFileItemModelRolesUpdater::setLocalFileSizePreviewLimit(const qlonglong size
)
316 m_localFileSizePreviewLimit
= size
;
319 qlonglong
KFileItemModelRolesUpdater::localFileSizePreviewLimit() const
321 return m_localFileSizePreviewLimit
;
324 void KFileItemModelRolesUpdater::setHoverSequenceState(const QUrl
&itemUrl
, int seqIdx
)
326 const KFileItem item
= m_model
->fileItem(itemUrl
);
328 if (item
!= m_hoverSequenceItem
) {
329 killHoverSequencePreviewJob();
332 m_hoverSequenceItem
= item
;
333 m_hoverSequenceIndex
= seqIdx
;
335 if (!m_previewShown
) {
339 m_hoverSequenceNumSuccessiveFailures
= 0;
341 loadNextHoverSequencePreview();
344 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
&itemRanges
)
349 // Determine the sort role synchronously for as many items as possible.
350 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
351 int insertedCount
= 0;
352 for (const KItemRange
&range
: itemRanges
) {
353 const int lastIndex
= insertedCount
+ range
.index
+ range
.count
- 1;
354 for (int i
= insertedCount
+ range
.index
; i
<= lastIndex
; ++i
) {
355 if (timer
.elapsed() < MaxBlockTimeout
) {
358 m_pendingSortRoleItems
.insert(m_model
->fileItem(i
));
361 insertedCount
+= range
.count
;
364 applySortProgressToModel();
366 // If there are still items whose sort role is unknown, check if the
367 // asynchronous determination of the sort role is already in progress,
368 // and start it if that is not the case.
369 if (!m_pendingSortRoleItems
.isEmpty() && m_state
!= ResolvingSortRole
) {
371 m_state
= ResolvingSortRole
;
372 resolveNextSortRole();
379 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
&itemRanges
)
383 const bool allItemsRemoved
= (m_model
->count() == 0);
386 if (m_balooFileMonitor
) {
387 // Don't let the FileWatcher watch for removed items
388 if (allItemsRemoved
) {
389 m_balooFileMonitor
->clear();
391 QStringList newFileList
;
392 const QStringList oldFileList
= m_balooFileMonitor
->files();
393 for (const QString
&file
: oldFileList
) {
394 if (m_model
->index(QUrl::fromLocalFile(file
)) >= 0) {
395 newFileList
.append(file
);
398 m_balooFileMonitor
->setFiles(newFileList
);
403 if (allItemsRemoved
) {
406 m_finishedItems
.clear();
407 m_pendingSortRoleItems
.clear();
408 m_pendingIndexes
.clear();
409 m_pendingPreviewItems
.clear();
410 m_recentlyChangedItems
.clear();
411 m_recentlyChangedItemsTimer
->stop();
412 m_changedItems
.clear();
413 m_hoverSequenceLoadedItems
.clear();
416 if (!m_model
->showDirectoriesOnly()) {
417 m_directoryContentsCounter
->stopWorker();
420 // Only remove the items from m_finishedItems. They will be removed
421 // from the other sets later on.
422 QSet
<KFileItem
>::iterator it
= m_finishedItems
.begin();
423 while (it
!= m_finishedItems
.end()) {
424 if (m_model
->index(*it
) < 0) {
425 it
= m_finishedItems
.erase(it
);
431 // Removed items won't have hover previews loaded anymore.
432 for (const KItemRange
&itemRange
: itemRanges
) {
433 int index
= itemRange
.index
;
434 for (int count
= itemRange
.count
; count
> 0; --count
) {
435 const KFileItem item
= m_model
->fileItem(index
);
436 m_hoverSequenceLoadedItems
.remove(item
);
441 // The visible items might have changed.
446 void KFileItemModelRolesUpdater::slotItemsMoved(KItemRange itemRange
, const QList
<int> &movedToIndexes
)
449 Q_UNUSED(movedToIndexes
)
451 // The visible items might have changed.
455 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &roles
)
459 // Find out if slotItemsChanged() has been done recently. If that is the
460 // case, resolving the roles is postponed until a timer has exceeded
461 // to prevent expensive repeated updates if files are updated frequently.
462 const bool itemsChangedRecently
= m_recentlyChangedItemsTimer
->isActive();
464 QSet
<KFileItem
> &targetSet
= itemsChangedRecently
? m_recentlyChangedItems
: m_changedItems
;
466 for (const KItemRange
&itemRange
: itemRanges
) {
467 int index
= itemRange
.index
;
468 for (int count
= itemRange
.count
; count
> 0; --count
) {
469 const KFileItem item
= m_model
->fileItem(index
);
470 targetSet
.insert(item
);
475 m_recentlyChangedItemsTimer
->start();
477 if (!itemsChangedRecently
) {
478 updateChangedItems();
482 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
)
487 if (m_resolvableRoles
.contains(current
)) {
488 m_pendingSortRoleItems
.clear();
489 m_finishedItems
.clear();
491 const int count
= m_model
->count();
495 // Determine the sort role synchronously for as many items as possible.
496 for (int index
= 0; index
< count
; ++index
) {
497 if (timer
.elapsed() < MaxBlockTimeout
) {
498 applySortRole(index
);
500 m_pendingSortRoleItems
.insert(m_model
->fileItem(index
));
504 applySortProgressToModel();
506 if (!m_pendingSortRoleItems
.isEmpty()) {
507 // Trigger the asynchronous determination of the sort role.
509 m_state
= ResolvingSortRole
;
510 resolveNextSortRole();
514 m_pendingSortRoleItems
.clear();
515 applySortProgressToModel();
519 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
521 if (m_state
!= PreviewJobRunning
) {
525 m_changedItems
.remove(item
);
527 const int index
= m_model
->index(item
);
532 QPixmap scaledPixmap
= transformPreviewPixmap(pixmap
);
534 QHash
<QByteArray
, QVariant
> data
= rolesData(item
, index
);
536 const QStringList overlays
= data
["iconOverlays"].toStringList();
537 // Strangely KFileItem::overlays() returns empty string-values, so
538 // we need to check first whether an overlay must be drawn at all.
539 // It is more efficient to do it here, as KIconLoader::drawOverlays()
540 // assumes that an overlay will be drawn and has some additional
542 if (!scaledPixmap
.isNull()) {
543 for (const QString
&overlay
: overlays
) {
544 if (!overlay
.isEmpty()) {
545 // There is at least one overlay, draw all overlays above m_pixmap
546 // and cancel the check
547 KIconLoader::global()->drawOverlays(overlays
, scaledPixmap
, KIconLoader::Desktop
);
553 data
.insert("iconPixmap", scaledPixmap
);
555 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
556 m_model
->setData(index
, data
);
557 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
559 m_finishedItems
.insert(item
);
562 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
&item
)
564 if (m_state
!= PreviewJobRunning
) {
568 m_changedItems
.remove(item
);
570 const int index
= m_model
->index(item
);
572 QHash
<QByteArray
, QVariant
> data
;
573 data
.insert("iconPixmap", QPixmap());
575 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
576 m_model
->setData(index
, data
);
577 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
579 applyResolvedRoles(index
, ResolveAll
);
580 m_finishedItems
.insert(item
);
584 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
586 m_previewJob
= nullptr;
588 if (m_state
!= PreviewJobRunning
) {
594 if (!m_pendingPreviewItems
.isEmpty()) {
597 if (!m_changedItems
.isEmpty()) {
598 updateChangedItems();
603 void KFileItemModelRolesUpdater::slotHoverSequenceGotPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
605 const int index
= m_model
->index(item
);
610 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
611 QVector
<QPixmap
> pixmaps
= data
["hoverSequencePixmaps"].value
<QVector
<QPixmap
>>();
612 const int loadedIndex
= pixmaps
.size();
614 float wap
= m_hoverSequencePreviewJob
->sequenceIndexWraparoundPoint();
615 if (!m_hoverSequencePreviewJob
->handlesSequences()) {
619 data
["hoverSequenceWraparoundPoint"] = wap
;
620 m_model
->setData(index
, data
);
623 // For hover sequence previews we never load index 0, because that's just the regular preview
624 // in "iconPixmap". But that means we'll load index 1 even for thumbnailers that don't support
625 // sequences, in which case we can just throw away the preview because it's the same as for
626 // index 0. Unfortunately we can't find it out earlier :(
627 if (wap
< 0.0f
|| loadedIndex
< static_cast<int>(wap
)) {
628 // Add the preview to the model data
630 const QPixmap scaledPixmap
= transformPreviewPixmap(pixmap
);
632 pixmaps
.append(scaledPixmap
);
633 data
["hoverSequencePixmaps"] = QVariant::fromValue(pixmaps
);
635 m_model
->setData(index
, data
);
637 const auto loadedIt
= std::find(m_hoverSequenceLoadedItems
.begin(), m_hoverSequenceLoadedItems
.end(), item
);
638 if (loadedIt
== m_hoverSequenceLoadedItems
.end()) {
639 m_hoverSequenceLoadedItems
.push_back(item
);
640 trimHoverSequenceLoadedItems();
644 m_hoverSequenceNumSuccessiveFailures
= 0;
647 void KFileItemModelRolesUpdater::slotHoverSequencePreviewFailed(const KFileItem
&item
)
649 const int index
= m_model
->index(item
);
654 static const int numRetries
= 2;
656 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
657 QVector
<QPixmap
> pixmaps
= data
["hoverSequencePixmaps"].value
<QVector
<QPixmap
>>();
659 qCDebug(DolphinDebug
).nospace() << "Failed to generate hover sequence preview #" << pixmaps
.size() << " for file " << item
.url().toString() << " (attempt "
660 << (m_hoverSequenceNumSuccessiveFailures
+ 1) << "/" << (numRetries
+ 1) << ")";
662 if (m_hoverSequenceNumSuccessiveFailures
>= numRetries
) {
663 // Give up and simply duplicate the previous sequence image (if any)
665 pixmaps
.append(pixmaps
.empty() ? QPixmap() : pixmaps
.last());
666 data
["hoverSequencePixmaps"] = QVariant::fromValue(pixmaps
);
668 if (!data
.contains("hoverSequenceWraparoundPoint")) {
669 // hoverSequenceWraparoundPoint is only available when PreviewJob succeeds, so unless
670 // it has previously succeeded, it's best to assume that it just doesn't handle
671 // sequences instead of trying to load the next image indefinitely.
672 data
["hoverSequenceWraparoundPoint"] = 1.0f
;
675 m_model
->setData(index
, data
);
677 m_hoverSequenceNumSuccessiveFailures
= 0;
681 m_hoverSequenceNumSuccessiveFailures
++;
684 // Next image in the sequence (or same one if the retry limit wasn't reached yet) will be
685 // loaded automatically, because slotHoverSequencePreviewJobFinished() will be triggered
686 // even when PreviewJob fails.
689 void KFileItemModelRolesUpdater::slotHoverSequencePreviewJobFinished()
691 const int index
= m_model
->index(m_hoverSequenceItem
);
693 m_hoverSequencePreviewJob
= nullptr;
697 // Since a PreviewJob can only have one associated sequence index, we can only generate
698 // one sequence image per job, so we have to start another one for the next index.
700 // Load the next image in the sequence
701 m_hoverSequencePreviewJob
= nullptr;
702 loadNextHoverSequencePreview();
705 void KFileItemModelRolesUpdater::resolveNextSortRole()
707 if (m_state
!= ResolvingSortRole
) {
711 QSet
<KFileItem
>::iterator it
= m_pendingSortRoleItems
.begin();
712 while (it
!= m_pendingSortRoleItems
.end()) {
713 const KFileItem item
= *it
;
714 const int index
= m_model
->index(item
);
716 // Continue if the sort role has already been determined for the
717 // item, and the item has not been changed recently.
718 if (!m_changedItems
.contains(item
) && m_model
->data(index
).contains(m_model
->sortRole())) {
719 it
= m_pendingSortRoleItems
.erase(it
);
723 applySortRole(index
);
724 m_pendingSortRoleItems
.erase(it
);
728 if (!m_pendingSortRoleItems
.isEmpty()) {
729 applySortProgressToModel();
730 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextSortRole
);
734 // Prevent that we try to update the items twice.
735 disconnect(m_model
, &KFileItemModel::itemsMoved
, this, &KFileItemModelRolesUpdater::slotItemsMoved
);
736 applySortProgressToModel();
737 connect(m_model
, &KFileItemModel::itemsMoved
, this, &KFileItemModelRolesUpdater::slotItemsMoved
);
742 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
744 if (m_state
!= ResolvingAllRoles
) {
748 while (!m_pendingIndexes
.isEmpty()) {
749 const int index
= m_pendingIndexes
.takeFirst();
750 const KFileItem item
= m_model
->fileItem(index
);
752 if (m_finishedItems
.contains(item
)) {
756 applyResolvedRoles(index
, ResolveAll
);
757 m_finishedItems
.insert(item
);
758 m_changedItems
.remove(item
);
762 if (!m_pendingIndexes
.isEmpty()) {
763 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles
);
767 if (m_clearPreviews
) {
768 // Only go through the list if there are items which might still have previews.
769 if (m_finishedItems
.count() != m_model
->count()) {
770 QHash
<QByteArray
, QVariant
> data
;
771 data
.insert("iconPixmap", QPixmap());
772 data
.insert("hoverSequencePixmaps", QVariant::fromValue(QVector
<QPixmap
>()));
774 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
775 for (int index
= 0; index
<= m_model
->count(); ++index
) {
776 if (m_model
->data(index
).contains("iconPixmap") || m_model
->data(index
).contains("hoverSequencePixmaps")) {
777 m_model
->setData(index
, data
);
780 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
782 m_clearPreviews
= false;
785 if (!m_changedItems
.isEmpty()) {
786 updateChangedItems();
791 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
793 m_changedItems
+= m_recentlyChangedItems
;
794 m_recentlyChangedItems
.clear();
795 updateChangedItems();
798 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString
&file
)
801 const KFileItem item
= m_model
->fileItem(QUrl::fromLocalFile(file
));
804 // itemUrl is not in the model anymore, probably because
805 // the corresponding file has been deleted in the meantime.
808 applyChangedBalooRolesForItem(item
);
814 void KFileItemModelRolesUpdater::applyChangedBalooRolesForItem(const KFileItem
&item
)
817 Baloo::File
file(item
.localPath());
820 const KBalooRolesProvider
&rolesProvider
= KBalooRolesProvider::instance();
821 QHash
<QByteArray
, QVariant
> data
;
823 const auto roles
= rolesProvider
.roles();
824 for (const QByteArray
&role
: roles
) {
825 // Overwrite all the role values with an empty QVariant, because the roles
826 // provider doesn't overwrite it when the property value list is empty.
828 data
.insert(role
, QVariant());
831 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(file
, m_roles
));
832 while (it
.hasNext()) {
834 data
.insert(it
.key(), it
.value());
837 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
838 const int index
= m_model
->index(item
);
839 m_model
->setData(index
, data
);
840 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
848 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString
&path
, int count
, long long size
)
850 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
851 const bool getSizeRole
= m_roles
.contains("size");
853 if (getSizeRole
|| getIsExpandableRole
) {
854 const int index
= m_model
->index(QUrl::fromLocalFile(path
));
856 QHash
<QByteArray
, QVariant
> data
;
859 data
.insert("count", count
);
860 data
.insert("size", QVariant::fromValue(size
));
862 if (getIsExpandableRole
) {
863 data
.insert("isExpandable", count
> 0);
866 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
867 m_model
->setData(index
, data
);
868 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
873 void KFileItemModelRolesUpdater::startUpdating()
875 if (m_state
== Paused
) {
879 if (m_finishedItems
.count() == m_model
->count()) {
880 // All roles have been resolved already.
885 // Terminate all updates that are currently active.
887 m_pendingIndexes
.clear();
892 // Determine the icons for the visible items synchronously.
893 updateVisibleIcons();
895 // A detailed update of the items in and near the visible area
896 // only makes sense if sorting is finished.
897 if (m_state
== ResolvingSortRole
) {
901 // Start the preview job or the asynchronous resolving of all roles.
902 QList
<int> indexes
= indexesToResolve();
904 if (m_previewShown
) {
905 m_pendingPreviewItems
.clear();
906 m_pendingPreviewItems
.reserve(indexes
.count());
908 for (int index
: std::as_const(indexes
)) {
909 const KFileItem item
= m_model
->fileItem(index
);
910 if (!m_finishedItems
.contains(item
)) {
911 m_pendingPreviewItems
.append(item
);
917 m_pendingIndexes
= indexes
;
918 // Trigger the asynchronous resolving of all roles.
919 m_state
= ResolvingAllRoles
;
920 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles
);
924 void KFileItemModelRolesUpdater::updateVisibleIcons()
926 int lastVisibleIndex
= m_lastVisibleIndex
;
927 if (lastVisibleIndex
<= 0) {
928 // Guess a reasonable value for the last visible index if the view
929 // has not told us about the real value yet.
930 lastVisibleIndex
= qMin(m_firstVisibleIndex
+ m_maximumVisibleItems
, m_model
->count() - 1);
931 if (lastVisibleIndex
<= 0) {
932 lastVisibleIndex
= qMin(200, m_model
->count() - 1);
939 // Try to determine the final icons for all visible items.
941 for (index
= m_firstVisibleIndex
; index
<= lastVisibleIndex
&& timer
.elapsed() < MaxBlockTimeout
; ++index
) {
942 applyResolvedRoles(index
, ResolveFast
);
945 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
946 // preliminary icons (i.e., without mime type determination) for the
950 void KFileItemModelRolesUpdater::startPreviewJob()
952 m_state
= PreviewJobRunning
;
954 if (m_pendingPreviewItems
.isEmpty()) {
955 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
959 // PreviewJob internally caches items always with the size of
960 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
961 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
962 // do a downscaling anyhow because of the frame, so in this case only the provided
963 // cache sizes are requested.
964 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128) ? QSize(256, 256) : QSize(128, 128);
966 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
967 // worst case) might block the application for several seconds. To prevent such
968 // a blocking, we only pass items with known mime type to the preview job.
969 const int count
= m_pendingPreviewItems
.count();
970 KFileItemList itemSubSet
;
971 itemSubSet
.reserve(count
);
973 if (m_pendingPreviewItems
.first().isMimeTypeKnown()) {
974 // Some mime types are known already, probably because they were
975 // determined when loading the icons for the visible items. Start
976 // a preview job for all items at the beginning of the list which
977 // have a known mime type.
979 itemSubSet
.append(m_pendingPreviewItems
.takeFirst());
980 } while (!m_pendingPreviewItems
.isEmpty() && m_pendingPreviewItems
.first().isMimeTypeKnown());
982 // Determine mime types for MaxBlockTimeout ms, and start a preview
983 // job for the corresponding items.
988 const KFileItem item
= m_pendingPreviewItems
.takeFirst();
989 item
.determineMimeType();
990 itemSubSet
.append(item
);
991 } while (!m_pendingPreviewItems
.isEmpty() && timer
.elapsed() < MaxBlockTimeout
);
994 KIO::PreviewJob
*job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
996 job
->setIgnoreMaximumSize(itemSubSet
.first().isLocalFile() && !itemSubSet
.first().isSlow() && m_localFileSizePreviewLimit
<= 0);
997 if (job
->uiDelegate()) {
998 KJobWidgets::setWindow(job
, qApp
->activeWindow());
1001 connect(job
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotGotPreview
);
1002 connect(job
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotPreviewFailed
);
1003 connect(job
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
1008 QPixmap
KFileItemModelRolesUpdater::transformPreviewPixmap(const QPixmap
&pixmap
)
1010 QPixmap scaledPixmap
= pixmap
;
1012 if (!pixmap
.hasAlpha() && !pixmap
.isNull() && m_iconSize
.width() > KIconLoader::SizeSmallMedium
&& m_iconSize
.height() > KIconLoader::SizeSmallMedium
) {
1013 if (m_enlargeSmallPreviews
) {
1014 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
1016 // Assure that small previews don't get enlarged. Instead they
1017 // should be shown centered within the frame.
1018 const QSize contentSize
= KPixmapModifier::sizeInsideFrame(m_iconSize
);
1019 const bool enlargingRequired
= scaledPixmap
.width() < contentSize
.width() && scaledPixmap
.height() < contentSize
.height();
1020 if (enlargingRequired
) {
1021 QSize frameSize
= scaledPixmap
.size() / scaledPixmap
.devicePixelRatio();
1022 frameSize
.scale(m_iconSize
, Qt::KeepAspectRatio
);
1024 QPixmap
largeFrame(frameSize
);
1025 largeFrame
.fill(Qt::transparent
);
1027 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
1029 QPainter
painter(&largeFrame
);
1030 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width() / scaledPixmap
.devicePixelRatio()) / 2,
1031 (largeFrame
.height() - scaledPixmap
.height() / scaledPixmap
.devicePixelRatio()) / 2,
1033 scaledPixmap
= largeFrame
;
1035 // The image must be shrunk as it is too large to fit into
1036 // the available icon size
1037 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
1040 } else if (!pixmap
.isNull()) {
1041 KPixmapModifier::scale(scaledPixmap
, m_iconSize
* qApp
->devicePixelRatio());
1042 scaledPixmap
.setDevicePixelRatio(qApp
->devicePixelRatio());
1045 return scaledPixmap
;
1048 void KFileItemModelRolesUpdater::loadNextHoverSequencePreview()
1050 if (m_hoverSequenceItem
.isNull() || m_hoverSequencePreviewJob
) {
1054 const int index
= m_model
->index(m_hoverSequenceItem
);
1059 // We generate the next few sequence indices in advance (buffering)
1060 const int maxSeqIdx
= m_hoverSequenceIndex
+ 5;
1062 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1064 if (!data
.contains("hoverSequencePixmaps")) {
1065 // The pixmap at index 0 isn't used ("iconPixmap" will be used instead)
1066 data
.insert("hoverSequencePixmaps", QVariant::fromValue(QVector
<QPixmap
>() << QPixmap()));
1067 m_model
->setData(index
, data
);
1070 const QVector
<QPixmap
> pixmaps
= data
["hoverSequencePixmaps"].value
<QVector
<QPixmap
>>();
1072 const int loadSeqIdx
= pixmaps
.size();
1075 if (data
.contains("hoverSequenceWraparoundPoint")) {
1076 wap
= data
["hoverSequenceWraparoundPoint"].toFloat();
1078 if (wap
>= 1.0f
&& loadSeqIdx
>= static_cast<int>(wap
)) {
1079 // Reached the wraparound point -> no more previews to load.
1083 if (loadSeqIdx
> maxSeqIdx
) {
1084 // Wait until setHoverSequenceState() is called with a higher sequence index.
1088 // PreviewJob internally caches items always with the size of
1089 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
1090 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
1091 // do a downscaling anyhow because of the frame, so in this case only the provided
1092 // cache sizes are requested.
1093 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128) ? QSize(256, 256) : QSize(128, 128);
1095 KIO::PreviewJob
*job
= new KIO::PreviewJob({m_hoverSequenceItem
}, cacheSize
, &m_enabledPlugins
);
1097 job
->setSequenceIndex(loadSeqIdx
);
1098 job
->setIgnoreMaximumSize(m_hoverSequenceItem
.isLocalFile() && !m_hoverSequenceItem
.isSlow() && m_localFileSizePreviewLimit
<= 0);
1099 if (job
->uiDelegate()) {
1100 KJobWidgets::setWindow(job
, qApp
->activeWindow());
1103 connect(job
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotHoverSequenceGotPreview
);
1104 connect(job
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewFailed
);
1105 connect(job
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewJobFinished
);
1107 m_hoverSequencePreviewJob
= job
;
1110 void KFileItemModelRolesUpdater::killHoverSequencePreviewJob()
1112 if (m_hoverSequencePreviewJob
) {
1113 disconnect(m_hoverSequencePreviewJob
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotHoverSequenceGotPreview
);
1114 disconnect(m_hoverSequencePreviewJob
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewFailed
);
1115 disconnect(m_hoverSequencePreviewJob
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotHoverSequencePreviewJobFinished
);
1116 m_hoverSequencePreviewJob
->kill();
1117 m_hoverSequencePreviewJob
= nullptr;
1121 void KFileItemModelRolesUpdater::updateChangedItems()
1123 if (m_state
== Paused
) {
1127 if (m_changedItems
.isEmpty()) {
1131 m_finishedItems
-= m_changedItems
;
1133 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
1134 m_pendingSortRoleItems
+= m_changedItems
;
1136 if (m_state
!= ResolvingSortRole
) {
1137 // Stop the preview job if necessary, and trigger the
1138 // asynchronous determination of the sort role.
1140 m_state
= ResolvingSortRole
;
1141 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextSortRole
);
1147 QList
<int> visibleChangedIndexes
;
1148 QList
<int> invisibleChangedIndexes
;
1149 visibleChangedIndexes
.reserve(m_changedItems
.size());
1150 invisibleChangedIndexes
.reserve(m_changedItems
.size());
1152 auto changedItemsIt
= m_changedItems
.begin();
1153 while (changedItemsIt
!= m_changedItems
.end()) {
1154 const auto &item
= *changedItemsIt
;
1155 const int index
= m_model
->index(item
);
1158 changedItemsIt
= m_changedItems
.erase(changedItemsIt
);
1163 if (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
) {
1164 visibleChangedIndexes
.append(index
);
1166 invisibleChangedIndexes
.append(index
);
1170 std::sort(visibleChangedIndexes
.begin(), visibleChangedIndexes
.end());
1172 if (m_previewShown
) {
1173 for (int index
: std::as_const(visibleChangedIndexes
)) {
1174 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
1177 for (int index
: std::as_const(invisibleChangedIndexes
)) {
1178 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
1181 if (!m_previewJob
) {
1185 const bool resolvingInProgress
= !m_pendingIndexes
.isEmpty();
1186 m_pendingIndexes
= visibleChangedIndexes
+ m_pendingIndexes
+ invisibleChangedIndexes
;
1187 if (!resolvingInProgress
) {
1188 // Trigger the asynchronous resolving of the changed roles.
1189 m_state
= ResolvingAllRoles
;
1190 QTimer::singleShot(0, this, &KFileItemModelRolesUpdater::resolveNextPendingRoles
);
1195 void KFileItemModelRolesUpdater::applySortRole(int index
)
1197 QHash
<QByteArray
, QVariant
> data
;
1198 const KFileItem item
= m_model
->fileItem(index
);
1200 if (m_model
->sortRole() == "type") {
1201 if (!item
.isMimeTypeKnown()) {
1202 item
.determineMimeType();
1205 data
.insert("type", item
.mimeComment());
1206 } else if (m_model
->sortRole() == "size" && item
.isLocalFile() && item
.isDir()) {
1207 startDirectorySizeCounting(item
, index
);
1210 // Probably the sort role is a baloo role - just determine all roles.
1211 data
= rolesData(item
, index
);
1214 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1215 m_model
->setData(index
, data
);
1216 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1219 void KFileItemModelRolesUpdater::applySortProgressToModel()
1221 // Inform the model about the progress of the resolved items,
1222 // so that it can give an indication when the sorting has been finished.
1223 const int resolvedCount
= m_model
->count() - m_pendingSortRoleItems
.count();
1224 m_model
->emitSortProgress(resolvedCount
);
1227 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index
, ResolveHint hint
)
1229 const KFileItem item
= m_model
->fileItem(index
);
1230 const bool resolveAll
= (hint
== ResolveAll
);
1232 bool iconChanged
= false;
1233 if (!item
.isMimeTypeKnown() || !item
.isFinalIconKnown()) {
1234 item
.determineMimeType();
1236 } else if (!m_model
->data(index
).contains("iconName")) {
1240 if (iconChanged
|| resolveAll
|| m_clearPreviews
) {
1245 QHash
<QByteArray
, QVariant
> data
;
1247 data
= rolesData(item
, index
);
1250 if (!item
.iconName().isEmpty()) {
1251 data
.insert("iconName", item
.iconName());
1254 if (m_clearPreviews
) {
1255 data
.insert("iconPixmap", QPixmap());
1256 data
.insert("hoverSequencePixmaps", QVariant::fromValue(QVector
<QPixmap
>()));
1259 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1260 m_model
->setData(index
, data
);
1261 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1268 void KFileItemModelRolesUpdater::startDirectorySizeCounting(const KFileItem
&item
, int index
)
1270 if (!item
.isLocalFile()) {
1274 if (ContentDisplaySettings::directorySizeCount() || item
.isSlow()) {
1275 // fastpath no recursion necessary
1277 auto data
= m_model
->data(index
);
1278 if (data
.value("size") == -2) {
1279 // means job already started
1283 auto url
= item
.url();
1284 if (!item
.localPath().isEmpty()) {
1285 // optimization for desktop:/, trash:/
1286 url
= QUrl::fromLocalFile(item
.localPath());
1289 data
.insert("isExpandable", false);
1290 data
.insert("count", 0);
1291 data
.insert("size", -2); // invalid size, -1 means size unknown
1293 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1294 m_model
->setData(index
, data
);
1295 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1297 auto listJob
= KIO::listDir(url
, KIO::HideProgressInfo
);
1298 QObject::connect(listJob
, &KIO::ListJob::entries
, this, [this, index
](const KJob
* /*job*/, const KIO::UDSEntryList
&list
) {
1299 auto data
= m_model
->data(index
);
1300 int origCount
= data
.value("count").toInt();
1301 int entryCount
= origCount
;
1303 for (const KIO::UDSEntry
&entry
: list
) {
1304 const auto name
= entry
.stringValue(KIO::UDSEntry::UDS_NAME
);
1306 if (name
== QStringLiteral("..") || name
== QStringLiteral(".")) {
1309 if (!m_model
->showHiddenFiles() && name
.startsWith(QLatin1Char('.'))) {
1312 if (m_model
->showDirectoriesOnly() && !entry
.isDir()) {
1318 // count has changed
1319 if (origCount
< entryCount
) {
1320 QHash
<QByteArray
, QVariant
> data
;
1321 data
.insert("isExpandable", entryCount
> 0);
1322 data
.insert("count", entryCount
);
1324 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1325 m_model
->setData(index
, data
);
1326 connect(m_model
, &KFileItemModel::itemsChanged
, this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1332 // Tell m_directoryContentsCounter that we want to count the items
1333 // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
1334 const QString path
= item
.localPath();
1335 const auto priority
= index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
? KDirectoryContentsCounter::PathCountPriority::High
1336 : KDirectoryContentsCounter::PathCountPriority::Normal
;
1338 m_directoryContentsCounter
->scanDirectory(path
, priority
);
1341 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
&item
, int index
)
1343 QHash
<QByteArray
, QVariant
> data
;
1345 const bool getSizeRole
= m_roles
.contains("size");
1346 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
1348 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir()) {
1349 startDirectorySizeCounting(item
, index
);
1352 if (m_roles
.contains("extension")) {
1353 // TODO KF6 use KFileItem::suffix 464722
1354 data
.insert("extension", QFileInfo(item
.name()).suffix());
1357 if (m_roles
.contains("type")) {
1358 data
.insert("type", item
.mimeComment());
1361 QStringList overlays
= item
.overlays();
1362 for (KOverlayIconPlugin
*it
: std::as_const(m_overlayIconsPlugin
)) {
1363 overlays
.append(it
->getOverlays(item
.url()));
1365 if (!overlays
.isEmpty()) {
1366 data
.insert("iconOverlays", overlays
);
1370 if (m_balooFileMonitor
) {
1371 m_balooFileMonitor
->addFile(item
.localPath());
1372 applyChangedBalooRolesForItem(item
);
1378 void KFileItemModelRolesUpdater::slotOverlaysChanged(const QUrl
&url
, const QStringList
&)
1380 const KFileItem item
= m_model
->fileItem(url
);
1381 if (item
.isNull()) {
1384 const int index
= m_model
->index(item
);
1385 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1386 QStringList overlays
= item
.overlays();
1387 for (KOverlayIconPlugin
*it
: std::as_const(m_overlayIconsPlugin
)) {
1388 overlays
.append(it
->getOverlays(url
));
1390 data
.insert("iconOverlays", overlays
);
1391 m_model
->setData(index
, data
);
1394 void KFileItemModelRolesUpdater::updateAllPreviews()
1396 if (m_state
== Paused
) {
1397 m_previewChangedDuringPausing
= true;
1399 m_finishedItems
.clear();
1404 void KFileItemModelRolesUpdater::killPreviewJob()
1407 disconnect(m_previewJob
, &KIO::PreviewJob::gotPreview
, this, &KFileItemModelRolesUpdater::slotGotPreview
);
1408 disconnect(m_previewJob
, &KIO::PreviewJob::failed
, this, &KFileItemModelRolesUpdater::slotPreviewFailed
);
1409 disconnect(m_previewJob
, &KIO::PreviewJob::finished
, this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
1410 m_previewJob
->kill();
1411 m_previewJob
= nullptr;
1412 m_pendingPreviewItems
.clear();
1416 QList
<int> KFileItemModelRolesUpdater::indexesToResolve() const
1418 const int count
= m_model
->count();
1421 result
.reserve(qMin(count
, (m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) + ResolveAllItemsLimit
+ (2 * m_maximumVisibleItems
)));
1423 // Add visible items.
1424 // Resolve files first, their previews are quicker.
1425 QList
<int> visibleDirs
;
1426 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
1427 const KFileItem item
= m_model
->fileItem(i
);
1429 visibleDirs
.append(i
);
1435 result
.append(visibleDirs
);
1437 // We need a reasonable upper limit for number of items to resolve after
1438 // and before the visible range. m_maximumVisibleItems can be quite large
1439 // when using Compact View.
1440 const int readAheadItems
= qMin(ReadAheadPages
* m_maximumVisibleItems
, ResolveAllItemsLimit
/ 2);
1442 // Add items after the visible range.
1443 const int endExtendedVisibleRange
= qMin(m_lastVisibleIndex
+ readAheadItems
, count
- 1);
1444 for (int i
= m_lastVisibleIndex
+ 1; i
<= endExtendedVisibleRange
; ++i
) {
1448 // Add items before the visible range in reverse order.
1449 const int beginExtendedVisibleRange
= qMax(0, m_firstVisibleIndex
- readAheadItems
);
1450 for (int i
= m_firstVisibleIndex
- 1; i
>= beginExtendedVisibleRange
; --i
) {
1454 // Add items on the last page.
1455 const int beginLastPage
= qMax(endExtendedVisibleRange
+ 1, count
- m_maximumVisibleItems
);
1456 for (int i
= beginLastPage
; i
< count
; ++i
) {
1460 // Add items on the first page.
1461 const int endFirstPage
= qMin(beginExtendedVisibleRange
, m_maximumVisibleItems
);
1462 for (int i
= 0; i
< endFirstPage
; ++i
) {
1466 // Continue adding items until ResolveAllItemsLimit is reached.
1467 int remainingItems
= ResolveAllItemsLimit
- result
.count();
1469 for (int i
= endExtendedVisibleRange
+ 1; i
< beginLastPage
&& remainingItems
> 0; ++i
) {
1474 for (int i
= beginExtendedVisibleRange
- 1; i
>= endFirstPage
&& remainingItems
> 0; --i
) {
1482 void KFileItemModelRolesUpdater::trimHoverSequenceLoadedItems()
1484 static const size_t maxLoadedItems
= 20;
1486 size_t loadedItems
= m_hoverSequenceLoadedItems
.size();
1487 while (loadedItems
> maxLoadedItems
) {
1488 const KFileItem item
= m_hoverSequenceLoadedItems
.front();
1490 m_hoverSequenceLoadedItems
.pop_front();
1493 const int index
= m_model
->index(item
);
1495 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1496 data
["hoverSequencePixmaps"] = QVariant::fromValue(QVector
<QPixmap
>() << QPixmap());
1497 m_model
->setData(index
, data
);
1502 #include "moc_kfileitemmodelrolesupdater.cpp"