From bcba9f6d73dcc7ec45eb6eaa0c5ad33fe7f50cf4 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Wed, 3 Sep 2025 23:16:58 +1000 Subject: plugin/cim: use QtConcurrent --- plugin/src/Caelestia/cachingimagemanager.cpp | 112 ++++++++++++++------------- plugin/src/Caelestia/cachingimagemanager.hpp | 5 +- plugin/src/Caelestia/filesystemmodel.hpp | 2 +- 3 files changed, 63 insertions(+), 56 deletions(-) (limited to 'plugin/src') diff --git a/plugin/src/Caelestia/cachingimagemanager.cpp b/plugin/src/Caelestia/cachingimagemanager.cpp index 8f2867a..93e1972 100644 --- a/plugin/src/Caelestia/cachingimagemanager.cpp +++ b/plugin/src/Caelestia/cachingimagemanager.cpp @@ -3,10 +3,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -104,59 +106,63 @@ void CachingImageManager::updateSource(const QString& path) { m_shaPath = path; - const QPointer self(this); - 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; - } - - const QSize size = self->effectiveSize(); - - if (!self->m_item || !size.width() || !size.height()) { - return; - } - - 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; - } - - self->m_cachePath = cache; - emit self->cachePathChanged(); - - 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); - } - - // Clear current running sha if same - if (self->m_shaPath == path) { - self->m_shaPath = QString(); - } - }, - Qt::QueuedConnection); + const auto future = QtConcurrent::run(&CachingImageManager::sha256sum, path); + + const auto watcher = new QFutureWatcher(this); + + connect(watcher, &QFutureWatcher::finished, this, [watcher, path, this]() { + if (m_path != path) { + // Object is destroyed or path has changed, ignore + watcher->deleteLater(); + return; + } + + const QSize size = effectiveSize(); + + if (!m_item || !size.width() || !size.height()) { + watcher->deleteLater(); + return; + } + + const QString fillMode = m_item->property("fillMode").toString(); + // clang-format off + const QString filename = QString("%1@%2x%3-%4.png") + .arg(watcher->result()).arg(size.width()).arg(size.height()) + .arg(fillMode == "PreserveAspectCrop" ? "crop" : fillMode == "PreserveAspectFit" ? "fit" : "stretch"); + // clang-format on + + const QUrl cache = m_cacheDir.resolved(QUrl(filename)); + if (m_cachePath == cache) { + watcher->deleteLater(); + return; + } + + m_cachePath = cache; + emit cachePathChanged(); + + if (!cache.isLocalFile()) { + qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file"; + watcher->deleteLater(); + return; + } + + const QImageReader reader(cache.toLocalFile()); + if (reader.canRead()) { + m_item->setProperty("source", cache); + } else { + m_item->setProperty("source", QUrl::fromLocalFile(path)); + createCache(path, cache.toLocalFile(), fillMode, size); + } + + // Clear current running sha if same + if (m_shaPath == path) { + m_shaPath = QString(); + } + + watcher->deleteLater(); }); + + watcher->setFuture(future); } QUrl CachingImageManager::cachePath() const { @@ -201,7 +207,7 @@ void CachingImageManager::createCache( }); } -QString CachingImageManager::sha256sum(const QString& path) const { +QString CachingImageManager::sha256sum(const QString& path) { QFile file(path); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "CachingImageManager::sha256sum: failed to open" << path; diff --git a/plugin/src/Caelestia/cachingimagemanager.hpp b/plugin/src/Caelestia/cachingimagemanager.hpp index b0e5551..0c22661 100644 --- a/plugin/src/Caelestia/cachingimagemanager.hpp +++ b/plugin/src/Caelestia/cachingimagemanager.hpp @@ -1,7 +1,8 @@ #pragma once +#include +#include #include -#include #include class CachingImageManager : public QObject { @@ -56,5 +57,5 @@ private: [[nodiscard]] QSize effectiveSize() const; void createCache(const QString& path, const QString& cache, const QString& fillMode, const QSize& size) const; - [[nodiscard]] QString sha256sum(const QString& path) const; + [[nodiscard]] static QString sha256sum(const QString& path); }; diff --git a/plugin/src/Caelestia/filesystemmodel.hpp b/plugin/src/Caelestia/filesystemmodel.hpp index 6d4c41c..89b03e6 100644 --- a/plugin/src/Caelestia/filesystemmodel.hpp +++ b/plugin/src/Caelestia/filesystemmodel.hpp @@ -155,5 +155,5 @@ private: void updateEntries(); void updateEntriesForDir(const QString& dir); void applyChanges(const QSet& removedPaths, const QSet& addedPaths); - static bool compareEntries(const FileSystemEntry* a, const FileSystemEntry* b); + [[nodiscard]] static bool compareEntries(const FileSystemEntry* a, const FileSystemEntry* b); }; -- cgit v1.2.3-freya