Commit b5ef5601 authored by Fushan Wen's avatar Fushan Wen
Browse files

wallpapers/image: fix side-by-side previews sometimes not showing

QCache will remove a cached item when it reaches the maximum allowed
total cost of the cache, so it's not stable to use QCache as a transfer
station to store side-by-side previews that are still under
construction, or there will only be dark previews for wallpapers with
both light and dark variants.


(cherry picked from commit 7a618142)
parent a4de7bf6
Pipeline #243345 canceled with stage
in 26 seconds
......@@ -81,14 +81,15 @@ void AbstractImageListModel::slotHandlePreview(const KFileItem &item, const QPix
it->removeOne(urlString);
const QStringList paths = job->property("paths").toStringList();
QPixmap *const cachedPreview = m_imageCache.object(paths);
auto cachedPreviewIt = m_imageTempCache.find(paths);
if (!cachedPreview && !it->empty()) {
m_imageCache.insert(paths, new QPixmap(preview), 0);
if (cachedPreviewIt == m_imageTempCache.end() && !it->empty()) {
m_imageTempCache.insert(paths, preview);
// it->empty() is handled in the end
} else if (cachedPreview) {
return;
} else if (cachedPreviewIt != m_imageTempCache.end()) {
// Show multiple images side by side
QPainter p(cachedPreview);
QPainter p(&*cachedPreviewIt);
const int i = paths.indexOf(urlString);
const double start = i / static_cast<double>(paths.size());
......@@ -97,8 +98,8 @@ void AbstractImageListModel::slotHandlePreview(const KFileItem &item, const QPix
const QPoint topLeft(start * preview.width(), 0);
const QPoint bottomRight(end * preview.width(), preview.height());
// Inserted area
const QPoint topLeft2(start * cachedPreview->width(), 0);
const QPoint bottomRight2(end * cachedPreview->width(), cachedPreview->height());
const QPoint topLeft2(start * cachedPreviewIt->width(), 0);
const QPoint bottomRight2(end * cachedPreviewIt->width(), cachedPreviewIt->height());
p.drawPixmap(QRect(topLeft2, bottomRight2), preview.copy(QRect(topLeft, bottomRight)));
}
......@@ -108,10 +109,13 @@ void AbstractImageListModel::slotHandlePreview(const KFileItem &item, const QPix
m_previewJobsUrls.erase(it);
QPixmap *finalPreview = nullptr;
if (!cachedPreview) {
if (cachedPreviewIt == m_imageTempCache.end()) {
// Single image
finalPreview = new QPixmap(preview);
} else {
finalPreview = new QPixmap(*cachedPreview);
// Side-by-side image
finalPreview = new QPixmap(*cachedPreviewIt);
m_imageTempCache.erase(cachedPreviewIt);
}
if (m_imageCache.insert(paths, finalPreview, 1)) {
......
......@@ -79,6 +79,8 @@ protected:
QSize m_targetSize;
QCache<QStringList, QPixmap> m_imageCache;
// Store side-by-side images
QHash<QStringList, QPixmap> m_imageTempCache;
QCache<QString, QString /* title */> m_backgroundTitleCache;
QCache<QString, QString /* author */> m_backgroundAuthorCache;
QCache<QString, QSize> m_imageSizeCache;
......
Supports Markdown
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