Commit de6f8246 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[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
parent dfbb2039
...@@ -24,7 +24,6 @@ target_link_libraries(plasma_wallpaper_imageplugin ...@@ -24,7 +24,6 @@ target_link_libraries(plasma_wallpaper_imageplugin
KF5::KIOCore KF5::KIOCore
KF5::KIOWidgets # KFileDialog KF5::KIOWidgets # KFileDialog
KF5::NewStuff KF5::NewStuff
KF5::GuiAddons
) )
if(BUILD_TESTING) if(BUILD_TESTING)
......
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
#include <QDebug> #include <QDebug>
#include <KIO/PreviewJob> #include <KIO/PreviewJob>
#include <KLocalizedString> #include <KLocalizedString>
#include <kimagecache.h>
#include <kaboutdata.h> #include <kaboutdata.h>
#include <KPackage/Package> #include <KPackage/Package>
...@@ -70,19 +69,16 @@ BackgroundListModel::BackgroundListModel(Image *wallpaper, QObject *parent) ...@@ -70,19 +69,16 @@ BackgroundListModel::BackgroundListModel(Image *wallpaper, QObject *parent)
: QAbstractListModel(parent), : QAbstractListModel(parent),
m_wallpaper(wallpaper) m_wallpaper(wallpaper)
{ {
m_imageCache.setMaxCost(10 * 1024 * 1024); // 10 MiB
connect(&m_dirwatch, &KDirWatch::deleted, this, &BackgroundListModel::removeBackground); connect(&m_dirwatch, &KDirWatch::deleted, this, &BackgroundListModel::removeBackground);
//TODO: on Qt 4.4 use the ui scale factor //TODO: on Qt 4.4 use the ui scale factor
QFontMetrics fm(QGuiApplication::font()); QFontMetrics fm(QGuiApplication::font());
m_screenshotSize = fm.width('M') * 15; m_screenshotSize = fm.width('M') * 15;
m_imageCache = new KImageCache(QStringLiteral("plasma_wallpaper_preview"), 10485760);
} }
BackgroundListModel::~BackgroundListModel() BackgroundListModel::~BackgroundListModel() = default;
{
delete m_imageCache;
}
QHash<int, QByteArray> BackgroundListModel::BackgroundListModel::roleNames() const QHash<int, QByteArray> BackgroundListModel::BackgroundListModel::roleNames() const
{ {
...@@ -343,14 +339,14 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const ...@@ -343,14 +339,14 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const
} }
case ScreenshotRole: { case ScreenshotRole: {
QPixmap preview = QPixmap(QSize(m_screenshotSize*1.6, const QString path = b.filePath("preferred");
m_screenshotSize));
if (m_imageCache->findPixmap(b.filePath("preferred"), &preview)) { QPixmap *cachedPreview = m_imageCache.object(path);
return preview; if (cachedPreview) {
return *cachedPreview;
} }
// qCDebug(IMAGEWALLPAPER) << "WP preferred: " << b.filePath("preferred");
// qCDebug(IMAGEWALLPAPER) << "WP screenshot: " << b.filePath("screenshot"); const QUrl file = QUrl::fromLocalFile(path);
QUrl file = QUrl::fromLocalFile(b.filePath("preferred"));
if (!m_previewJobs.contains(file) && file.isValid()) { if (!m_previewJobs.contains(file) && file.isValid()) {
KFileItemList list; KFileItemList list;
...@@ -451,7 +447,9 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev ...@@ -451,7 +447,9 @@ void BackgroundListModel::showPreview(const KFileItem &item, const QPixmap &prev
return; 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(); //qCDebug(IMAGEWALLPAPER) << "WP preview size:" << preview.size();
emit dataChanged(index, index); emit dataChanged(index, index);
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "image.h" #include "image.h"
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QCache>
#include <QPixmap> #include <QPixmap>
#include <QRunnable> #include <QRunnable>
#include <QThread> #include <QThread>
...@@ -32,8 +33,6 @@ ...@@ -32,8 +33,6 @@
#include <KDirWatch> #include <KDirWatch>
#include <KFileItem> #include <KFileItem>
#include <kimagecache.h>
#include <KPackage/PackageStructure> #include <KPackage/PackageStructure>
class QEventLoop; class QEventLoop;
...@@ -115,7 +114,7 @@ private: ...@@ -115,7 +114,7 @@ private:
QHash<QString, QSize> m_sizeCache; QHash<QString, QSize> m_sizeCache;
QHash<QUrl, QPersistentModelIndex> m_previewJobs; QHash<QUrl, QPersistentModelIndex> m_previewJobs;
KDirWatch m_dirwatch; KDirWatch m_dirwatch;
KImageCache* m_imageCache; QCache<QString, QPixmap> m_imageCache;
QString m_findToken; QString m_findToken;
int m_screenshotSize; int m_screenshotSize;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment