summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugin/src/Caelestia/cachingimagemanager.cpp94
-rw-r--r--plugin/src/Caelestia/cachingimagemanager.hpp5
-rw-r--r--plugin/src/Caelestia/filesystemmodel.hpp2
3 files changed, 54 insertions, 47 deletions
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 <QCryptographicHash>
#include <QDir>
#include <QFile>
+#include <QFutureWatcher>
#include <QImageReader>
#include <QObject>
#include <QPainter>
#include <QThreadPool>
+#include <QtConcurrent>
#include <QtQuick/QQuickItem>
#include <QtQuick/QQuickWindow>
@@ -104,59 +106,63 @@ void CachingImageManager::updateSource(const QString& path) {
m_shaPath = path;
- const QPointer<CachingImageManager> self(this);
- QThreadPool::globalInstance()->start([path, self] {
- const QString sha = self->sha256sum(path);
+ const auto future = QtConcurrent::run(&CachingImageManager::sha256sum, path);
- QMetaObject::invokeMethod(
- self,
- [path, sha, self]() {
- if (!self || self->m_path != path) {
- // Object is destroyed or path has changed, ignore
- return;
- }
+ const auto watcher = new QFutureWatcher<QString>(this);
- const QSize size = self->effectiveSize();
+ connect(watcher, &QFutureWatcher<QString>::finished, this, [watcher, path, this]() {
+ if (m_path != path) {
+ // Object is destroyed or path has changed, ignore
+ watcher->deleteLater();
+ return;
+ }
- if (!self->m_item || !size.width() || !size.height()) {
- return;
- }
+ const QSize size = effectiveSize();
- 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
+ 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;
+ }
- const QUrl cache = self->m_cacheDir.resolved(QUrl(filename));
- if (self->m_cachePath == cache) {
- return;
- }
+ m_cachePath = cache;
+ emit cachePathChanged();
- self->m_cachePath = cache;
- emit self->cachePathChanged();
+ if (!cache.isLocalFile()) {
+ qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file";
+ watcher->deleteLater();
+ return;
+ }
- if (!cache.isLocalFile()) {
- qWarning() << "CachingImageManager::updateSource: cachePath" << cache << "is not a local file";
- 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);
+ }
- 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 (m_shaPath == path) {
+ m_shaPath = QString();
+ }
- // Clear current running sha if same
- if (self->m_shaPath == path) {
- self->m_shaPath = QString();
- }
- },
- Qt::QueuedConnection);
+ 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 <QFuture>
+#include <QObject>
#include <QtQuick/QQuickItem>
-#include <qobject.h>
#include <qqmlintegration.h>
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<QString>& removedPaths, const QSet<QString>& addedPaths);
- static bool compareEntries(const FileSystemEntry* a, const FileSystemEntry* b);
+ [[nodiscard]] static bool compareEntries(const FileSystemEntry* a, const FileSystemEntry* b);
};