From de6f8246ecabc3a4102c7c29ce3a7f6c4987cfec Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Wed, 10 Oct 2018 12:04:28 +0200 Subject: [PATCH] [Background Model] Use own QCache for thumbnails Previously it would use an on-disk KImageCache which would never be invalidated. Moreover, it just uses KIO::PreviewJob which does its own more clever (compares mtime and other properties) on-disk caching already. It is using its own QCache rather than QPixmapCache to not jeopardize other caching for the potentially large wallpaper thumbnails that are only relevant while the dialog is open. BUG: 395447 FIXED-IN: 5.15.0 Differential Revision: https://phabricator.kde.org/D16095 --- wallpapers/image/CMakeLists.txt | 1 - wallpapers/image/backgroundlistmodel.cpp | 28 +++++++++++------------- wallpapers/image/backgroundlistmodel.h | 5 ++--- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/wallpapers/image/CMakeLists.txt b/wallpapers/image/CMakeLists.txt index 4c541d8cd..12a8a9c98 100644 --- a/wallpapers/image/CMakeLists.txt +++ b/wallpapers/image/CMakeLists.txt @@ -24,7 +24,6 @@ target_link_libraries(plasma_wallpaper_imageplugin KF5::KIOCore KF5::KIOWidgets # KFileDialog KF5::NewStuff - KF5::GuiAddons ) if(BUILD_TESTING) diff --git a/wallpapers/image/backgroundlistmodel.cpp b/wallpapers/image/backgroundlistmodel.cpp index b93e23c9c..a37de09ce 100644 --- a/wallpapers/image/backgroundlistmodel.cpp +++ b/wallpapers/image/backgroundlistmodel.cpp @@ -39,7 +39,6 @@ #include #include #include -#include #include #include @@ -70,19 +69,16 @@ BackgroundListModel::BackgroundListModel(Image *wallpaper, QObject *parent) : QAbstractListModel(parent), m_wallpaper(wallpaper) { + m_imageCache.setMaxCost(10 * 1024 * 1024); // 10 MiB + connect(&m_dirwatch, &KDirWatch::deleted, this, &BackgroundListModel::removeBackground); //TODO: on Qt 4.4 use the ui scale factor QFontMetrics fm(QGuiApplication::font()); m_screenshotSize = fm.width('M') * 15; - - m_imageCache = new KImageCache(QStringLiteral("plasma_wallpaper_preview"), 10485760); } -BackgroundListModel::~BackgroundListModel() -{ - delete m_imageCache; -} +BackgroundListModel::~BackgroundListModel() = default; QHash BackgroundListModel::BackgroundListModel::roleNames() const { @@ -343,14 +339,14 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const } case ScreenshotRole: { - QPixmap preview = QPixmap(QSize(m_screenshotSize*1.6, - m_screenshotSize)); - if (m_imageCache->findPixmap(b.filePath("preferred"), &preview)) { - return preview; + const QString path = b.filePath("preferred"); + + QPixmap *cachedPreview = m_imageCache.object(path); + if (cachedPreview) { + return *cachedPreview; } -// qCDebug(IMAGEWALLPAPER) << "WP preferred: " << b.filePath("preferred"); -// qCDebug(IMAGEWALLPAPER) << "WP screenshot: " << b.filePath("screenshot"); - QUrl file = QUrl::fromLocalFile(b.filePath("preferred")); + + const QUrl file = QUrl::fromLocalFile(path); if (!m_previewJobs.contains(file) && file.isValid()) { KFileItemList list; @@ -451,7 +447,9 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev return; } - m_imageCache->insertPixmap(b.filePath("preferred"), preview); + const int cost = preview.width() * preview.height() * preview.depth() / 8; + m_imageCache.insert(b.filePath("preferred"), new QPixmap(preview), cost); + //qCDebug(IMAGEWALLPAPER) << "WP preview size:" << preview.size(); emit dataChanged(index, index); } diff --git a/wallpapers/image/backgroundlistmodel.h b/wallpapers/image/backgroundlistmodel.h index 05198bdc1..a61a02e8f 100644 --- a/wallpapers/image/backgroundlistmodel.h +++ b/wallpapers/image/backgroundlistmodel.h @@ -23,6 +23,7 @@ #include "image.h" #include +#include #include #include #include @@ -32,8 +33,6 @@ #include #include -#include - #include class QEventLoop; @@ -115,7 +114,7 @@ private: QHash m_sizeCache; QHash m_previewJobs; KDirWatch m_dirwatch; - KImageCache* m_imageCache; + QCache m_imageCache; QString m_findToken; int m_screenshotSize;