diff options
Diffstat (limited to '')
| -rw-r--r-- | plugin/src/Caelestia/cachingimagemanager.cpp | 98 |
1 files changed, 54 insertions, 44 deletions
diff --git a/plugin/src/Caelestia/cachingimagemanager.cpp b/plugin/src/Caelestia/cachingimagemanager.cpp index a5f71ba..8f2867a 100644 --- a/plugin/src/Caelestia/cachingimagemanager.cpp +++ b/plugin/src/Caelestia/cachingimagemanager.cpp @@ -1,14 +1,14 @@ #include "cachingimagemanager.hpp" -#include <QObject> -#include <QtQuick/QQuickItem> -#include <QtQuick/QQuickWindow> #include <QCryptographicHash> -#include <QThreadPool> -#include <QFile> #include <QDir> -#include <QPainter> +#include <QFile> #include <QImageReader> +#include <QObject> +#include <QPainter> +#include <QThreadPool> +#include <QtQuick/QQuickItem> +#include <QtQuick/QQuickWindow> qreal CachingImageManager::effectiveScale() const { if (m_item && m_item->window()) { @@ -49,8 +49,12 @@ void CachingImageManager::setItem(QQuickItem* item) { emit itemChanged(); if (item) { - m_widthConn = connect(item, &QQuickItem::widthChanged, this, [this]() { updateSource(); }); - m_heightConn = connect(item, &QQuickItem::heightChanged, this, [this]() { updateSource(); }); + m_widthConn = connect(item, &QQuickItem::widthChanged, this, [this]() { + updateSource(); + }); + m_heightConn = connect(item, &QQuickItem::heightChanged, this, [this]() { + updateSource(); + }); updateSource(); } } @@ -104,49 +108,54 @@ void CachingImageManager::updateSource(const QString& path) { QThreadPool::globalInstance()->start([path, self] { const QString sha = self->sha256sum(path); - QMetaObject::invokeMethod(self, [path, sha, self]() { - if (!self || self->m_path != path) { - // Object is destroyed or path has changed, ignore - return; - } + QMetaObject::invokeMethod( + self, + [path, sha, self]() { + if (!self || self->m_path != path) { + // Object is destroyed or path has changed, ignore + return; + } - const QSize size = self->effectiveSize(); + const QSize size = self->effectiveSize(); - if (!self->m_item || !size.width() || !size.height()) { - return; - } + if (!self->m_item || !size.width() || !size.height()) { + return; + } - const QString fillMode = self->m_item->property("fillMode").toString(); - const QString filename = QString("%1@%2x%3-%4.png") - .arg(sha).arg(size.width()).arg(size.height()) - .arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch"); + const QString fillMode = self->m_item->property("fillMode").toString(); + // clang-format off + const QString filename = QString("%1@%2x%3-%4.png") + .arg(sha).arg(size.width()).arg(size.height()) + .arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch"); + // clang-format on - const QUrl cache = self->m_cacheDir.resolved(QUrl(filename)); - if (self->m_cachePath == cache) { - return; - } + const QUrl cache = self->m_cacheDir.resolved(QUrl(filename)); + if (self->m_cachePath == cache) { + return; + } - self->m_cachePath = cache; - emit self->cachePathChanged(); + self->m_cachePath = cache; + emit self->cachePathChanged(); - if (!cache.isLocalFile()) { - qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file"; - return; - } + if (!cache.isLocalFile()) { + qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file"; + return; + } - const QImageReader reader(cache.toLocalFile()); - if (reader.canRead()) { - self->m_item->setProperty("source", cache); - } else { - self->m_item->setProperty("source", QUrl::fromLocalFile(path)); - self->createCache(path, cache.toLocalFile(), fillMode, size); - } + const QImageReader reader(cache.toLocalFile()); + if (reader.canRead()) { + self->m_item->setProperty("source", cache); + } else { + self->m_item->setProperty("source", QUrl::fromLocalFile(path)); + self->createCache(path, cache.toLocalFile(), fillMode, size); + } - // Clear current running sha if same - if (self->m_shaPath == path) { - self->m_shaPath = QString(); - } - }, Qt::QueuedConnection); + // Clear current running sha if same + if (self->m_shaPath == path) { + self->m_shaPath = QString(); + } + }, + Qt::QueuedConnection); }); } @@ -154,7 +163,8 @@ QUrl CachingImageManager::cachePath() const { return m_cachePath; } -void CachingImageManager::createCache(const QString& path, const QString& cache, const QString& fillMode, const QSize& size) const { +void CachingImageManager::createCache( + const QString& path, const QString& cache, const QString& fillMode, const QSize& size) const { QThreadPool::globalInstance()->start([path, cache, fillMode, size] { QImage image(path); |