From d22576ee8273c0537116e69a3134af62db5589f1 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Sat, 30 Aug 2025 14:45:42 +1000 Subject: plugin/cim: better cache impl --- components/images/CachingImage.qml | 5 --- plugin/CMakeLists.txt | 2 +- plugin/src/Caelestia/CMakeLists.txt | 2 +- plugin/src/Caelestia/cachingimagemanager.cpp | 48 ++++++++++++++++++++++------ plugin/src/Caelestia/cachingimagemanager.hpp | 4 +-- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/components/images/CachingImage.qml b/components/images/CachingImage.qml index 2aa0558..b515622 100644 --- a/components/images/CachingImage.qml +++ b/components/images/CachingImage.qml @@ -16,11 +16,6 @@ Image { sourceSize.width: sourceWidth sourceSize.height: sourceHeight - onStatusChanged: { - if (!manager.usingCache && status === Image.Ready) - CUtils.saveItem(this, manager.cachePath); - } - Connections { target: QsWindow.window diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index a493c32..a38d3c4 100644 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -1,4 +1,4 @@ -find_package(Qt6 REQUIRED COMPONENTS Core Qml) +find_package(Qt6 REQUIRED COMPONENTS Core Qml Gui) if(QT_KNOWN_POLICY_QTP0001) qt_policy(SET QTP0001 NEW) diff --git a/plugin/src/Caelestia/CMakeLists.txt b/plugin/src/Caelestia/CMakeLists.txt index 3ea0225..dfc7f78 100644 --- a/plugin/src/Caelestia/CMakeLists.txt +++ b/plugin/src/Caelestia/CMakeLists.txt @@ -23,4 +23,4 @@ install(TARGETS "${module_plugin_target}" LIBRARY DESTINATION "${module_dir}" RU install(FILES "${module_qmldir}" DESTINATION "${module_dir}") install(FILES "${module_typeinfo}" DESTINATION "${module_dir}") -target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml) +target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml Qt::Gui) diff --git a/plugin/src/Caelestia/cachingimagemanager.cpp b/plugin/src/Caelestia/cachingimagemanager.cpp index 325a1bd..01d518f 100644 --- a/plugin/src/Caelestia/cachingimagemanager.cpp +++ b/plugin/src/Caelestia/cachingimagemanager.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include qreal CachingImageManager::effectiveScale() const { if (m_item->window()) { @@ -99,10 +101,9 @@ void CachingImageManager::updateSource(const QString& path) { const QString sha = sha256sum(path); QMetaObject::invokeMethod(this, [path, sha, this]() { - const QString filename = QString("%1@%2x%3.png") - .arg(sha) - .arg(effectiveWidth()) - .arg(effectiveHeight()); + int width = effectiveWidth(); + int height = effectiveHeight(); + const QString filename = QString("%1@%2x%3.png").arg(sha).arg(width).arg(height); const QUrl cache = m_cacheDir.resolved(QUrl(filename)); if (m_cachePath == cache) { @@ -123,10 +124,8 @@ void CachingImageManager::updateSource(const QString& path) { m_item->setProperty("source", cache); } else { m_item->setProperty("source", QUrl::fromLocalFile(path)); + createCache(path, cache.toLocalFile(), QSize(width, height)); } - - m_usingCache = cacheExists; - emit usingCacheChanged(); }, Qt::QueuedConnection); }); } @@ -135,8 +134,39 @@ QUrl CachingImageManager::cachePath() const { return m_cachePath; } -bool CachingImageManager::usingCache() const { - return m_usingCache; +void CachingImageManager::createCache(const QString& path, const QString& cache, const QSize& size) const { + QThreadPool::globalInstance()->start([path, cache, size] { + QImageReader reader(path); + + QSize imgSize = reader.size(); + if (!imgSize.isValid()) { + qWarning() << "CachingImageManager::createCache: unable to get size of" << path; + return; + } + + qreal scale = std::max( + qreal(size.width()) / imgSize.width(), + qreal(size.height()) / imgSize.height() + ); + QSizeF scaledSize(imgSize.width() * scale, imgSize.height() * scale); + qreal xOff = (scaledSize.width() - size.width()) / 2.0; + qreal yOff = (scaledSize.height() - size.height()) / 2.0; + + reader.setScaledSize(scaledSize.toSize()); + reader.setScaledClipRect(QRectF(xOff, yOff, size.width(), size.height()).toRect()); + + QImage image = reader.read(); + + if (image.isNull()) { + qWarning() << "CachingImageManager::createCache: failed to read" << path; + return; + } + + const QString parent = QFileInfo(cache).absolutePath(); + if (!QDir().mkpath(parent) || !image.save(cache)) { + qWarning() << "CachingImageManager::createCache: failed to save to" << cache; + } + }); } QString CachingImageManager::sha256sum(const QString& path) const { diff --git a/plugin/src/Caelestia/cachingimagemanager.hpp b/plugin/src/Caelestia/cachingimagemanager.hpp index 602d862..c78dcda 100644 --- a/plugin/src/Caelestia/cachingimagemanager.hpp +++ b/plugin/src/Caelestia/cachingimagemanager.hpp @@ -13,7 +13,6 @@ class CachingImageManager : public QObject { Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged); Q_PROPERTY(QUrl cachePath READ cachePath NOTIFY cachePathChanged); - Q_PROPERTY(bool usingCache READ usingCache NOTIFY usingCacheChanged); public: explicit CachingImageManager(QObject* parent = nullptr): QObject(parent) {}; @@ -28,7 +27,6 @@ public: void setPath(const QString& path); [[nodiscard]] QUrl cachePath() const; - [[nodiscard]] bool usingCache() const; Q_INVOKABLE void updateSource(); Q_INVOKABLE void updateSource(const QString& path); @@ -47,7 +45,6 @@ private: QString m_path; QUrl m_cachePath; - bool m_usingCache; QMetaObject::Connection m_widthConn; QMetaObject::Connection m_heightConn; @@ -56,5 +53,6 @@ private: [[nodiscard]] int effectiveWidth() const; [[nodiscard]] int effectiveHeight() const; + void createCache(const QString& path, const QString& cache, const QSize& size) const; [[nodiscard]] QString sha256sum(const QString& path) const; }; -- cgit v1.2.3-freya