From 492301406a4656fbc6c9a1be0e77e68c5535bf93 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Tue, 31 Mar 2020 13:35:22 +0200 Subject: [PATCH] [Image Wallpaper] Fix thumbnail generation when model is reloaded in-flight GHNS Quick Button signals changedEntriesChanged initially even if nothing changed. While this is a bug that should be fixed in GHNS, the wallpaper model didn't handle the case well, when the model was reloaded while a preview was being generated. It looks up whether a preview exists by QUrl and stores a QPersistentModelIndex. However, when the model is reloaded and it accesses data for the same QUrl, the job might still be in-flight, but the QPersistentModelIndex is invalid as the model has been reset. This fixes it by performing all lookups exclusively on QPersistentModelIndex. The hash lookup is optimized for data() (loads of accesses) with the slower key() lookup being done only when the less frequently occurring thumbnail generation finishes/fails. It will now do two preview jobs initially, but at least it shows something again... BUG: 419234 FIXED-IN: 5.18.4 Differential Revision: https://phabricator.kde.org/D28420 --- wallpapers/image/backgroundlistmodel.cpp | 16 ++++++++-------- wallpapers/image/backgroundlistmodel.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/wallpapers/image/backgroundlistmodel.cpp b/wallpapers/image/backgroundlistmodel.cpp index d7efaef3b..551966b62 100644 --- a/wallpapers/image/backgroundlistmodel.cpp +++ b/wallpapers/image/backgroundlistmodel.cpp @@ -347,11 +347,11 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const return *cachedPreview; } - const QUrl file = QUrl::fromLocalFile(path); - if (!m_previewJobs.contains(file) && file.isValid()) { - + const QUrl url = QUrl::fromLocalFile(path); + const QPersistentModelIndex persistentIndex(index); + if (!m_previewJobsUrls.contains(persistentIndex) && url.isValid()) { KFileItemList list; - list.append(KFileItem(file, QString(), 0)); + list.append(KFileItem(url, QString(), 0)); QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); KIO::PreviewJob* job = KIO::filePreview(list, QSize(m_screenshotSize*1.6, @@ -361,7 +361,7 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const this, &BackgroundListModel::showPreview); connect(job, &KIO::PreviewJob::failed, this, &BackgroundListModel::previewFailed); - const_cast(this)->m_previewJobs.insert(file, QPersistentModelIndex(index)); + const_cast(this)->m_previewJobsUrls.insert(persistentIndex, url); } return QVariant(); @@ -436,8 +436,8 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev return; } - QPersistentModelIndex index = m_previewJobs.value(item.url()); - m_previewJobs.remove(item.url()); + QPersistentModelIndex index = m_previewJobsUrls.key(item.url()); + m_previewJobsUrls.remove(index); if (!index.isValid()) { return; @@ -457,7 +457,7 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev void BackgroundListModel::previewFailed(const KFileItem &item) { - m_previewJobs.remove(item.url()); + m_previewJobsUrls.remove(m_previewJobsUrls.key(item.url())); } KPackage::Package BackgroundListModel::package(int index) const diff --git a/wallpapers/image/backgroundlistmodel.h b/wallpapers/image/backgroundlistmodel.h index 9ae5a41b5..f78e024d4 100644 --- a/wallpapers/image/backgroundlistmodel.h +++ b/wallpapers/image/backgroundlistmodel.h @@ -114,7 +114,7 @@ private: QSet m_removableWallpapers; QHash m_sizeCache; - QHash m_previewJobs; + QHash m_previewJobsUrls; KDirWatch m_dirwatch; QCache m_imageCache;