From 8b1f2be27be18459ba2c8f4d675b5bf36f7d2307 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Mon, 1 Sep 2025 17:41:26 +1000 Subject: internal: format cpp --- plugin/src/Caelestia/cachingimagemanager.cpp | 114 +++++++++++++++------------ plugin/src/Caelestia/cachingimagemanager.hpp | 5 +- plugin/src/Caelestia/cutils.cpp | 90 ++++++++++++--------- plugin/src/Caelestia/cutils.hpp | 5 +- plugin/src/Caelestia/filesystemmodel.cpp | 8 +- plugin/src/Caelestia/filesystemmodel.hpp | 23 +++--- 6 files changed, 138 insertions(+), 107 deletions(-) (limited to 'plugin/src') 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 -#include -#include #include -#include -#include #include -#include +#include #include +#include +#include +#include +#include +#include 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; - } - - const QSize size = self->effectiveSize(); - - 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 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); + 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); }); } @@ -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); diff --git a/plugin/src/Caelestia/cachingimagemanager.hpp b/plugin/src/Caelestia/cachingimagemanager.hpp index 4d4a5ae..b0e5551 100644 --- a/plugin/src/Caelestia/cachingimagemanager.hpp +++ b/plugin/src/Caelestia/cachingimagemanager.hpp @@ -1,8 +1,8 @@ #pragma once +#include #include #include -#include class CachingImageManager : public QObject { Q_OBJECT @@ -15,7 +15,8 @@ class CachingImageManager : public QObject { Q_PROPERTY(QUrl cachePath READ cachePath NOTIFY cachePathChanged) public: - explicit CachingImageManager(QObject* parent = nullptr): QObject(parent) {} + explicit CachingImageManager(QObject* parent = nullptr) + : QObject(parent) {} [[nodiscard]] QQuickItem* item() const; void setItem(QQuickItem* item); diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp index 6ba4807..29cf7ce 100644 --- a/plugin/src/Caelestia/cutils.cpp +++ b/plugin/src/Caelestia/cutils.cpp @@ -1,12 +1,12 @@ #include "cutils.hpp" +#include #include -#include +#include +#include #include #include -#include -#include -#include +#include void CUtils::saveItem(QQuickItem* target, const QUrl& path) { this->saveItem(target, path, QRect(), QJSValue(), QJSValue()); @@ -47,7 +47,8 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q auto scaledRect = rect; const qreal scale = target->window()->devicePixelRatio(); if (rect.isValid() && scale != 1.0) { - scaledRect = QRectF(rect.left() * scale, rect.top() * scale, rect.width() * scale, rect.height() * scale).toRect(); + scaledRect = + QRectF(rect.left() * scale, rect.top() * scale, rect.width() * scale, rect.height() * scale).toRect(); } const QSharedPointer grabResult = target->grabToImage(); @@ -65,21 +66,24 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q const QString parent = QFileInfo(file).absolutePath(); const bool success = QDir().mkpath(parent) && image.save(file); - QMetaObject::invokeMethod(this, [file, success, path, onSaved, onFailed, this]() { - if (success) { - if (onSaved.isCallable()) { - onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - } else { - qWarning() << "CUtils::saveItem: failed to save" << path; - if (onFailed.isCallable()) { - onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + QMetaObject::invokeMethod( + this, + [file, success, path, onSaved, onFailed, this]() { + if (success) { + if (onSaved.isCallable()) { + onSaved.call( + { QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + } + } else { + qWarning() << "CUtils::saveItem: failed to save" << path; + if (onFailed.isCallable()) { + onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + } } - } - }, Qt::QueuedConnection); + }, + Qt::QueuedConnection); }); - } - ); + }); } bool CUtils::copyFile(const QUrl& source, const QUrl& target) const { @@ -120,21 +124,23 @@ void CUtils::getDominantColour(QQuickItem* item, int rescaleSize, QJSValue callb const QSharedPointer grabResult = item->grabToImage(); - QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this, - [grabResult, rescaleSize, callback, this]() { + QObject::connect( + grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rescaleSize, callback, this]() { const QImage image = grabResult->image(); QThreadPool::globalInstance()->start([grabResult, image, rescaleSize, callback, this]() { const QColor color = this->findDominantColour(image, rescaleSize); if (callback.isCallable()) { - QMetaObject::invokeMethod(this, [color, callback, this]() { - callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(color)) }); - }, Qt::QueuedConnection); + QMetaObject::invokeMethod( + this, + [color, callback, this]() { + callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(color)) }); + }, + Qt::QueuedConnection); } }); - } - ); + }); } void CUtils::getDominantColour(const QString& path, QJSValue callback) { @@ -158,9 +164,12 @@ void CUtils::getDominantColour(const QString& path, int rescaleSize, QJSValue ca const QColor color = this->findDominantColour(image, rescaleSize); if (callback.isCallable()) { - QMetaObject::invokeMethod(this, [color, callback, this]() { - callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(color)) }); - }, Qt::QueuedConnection); + QMetaObject::invokeMethod( + this, + [color, callback, this]() { + callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(color)) }); + }, + Qt::QueuedConnection); } }); } @@ -234,21 +243,23 @@ void CUtils::getAverageLuminance(QQuickItem* item, int rescaleSize, QJSValue cal const QSharedPointer grabResult = item->grabToImage(); - QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this, - [grabResult, rescaleSize, callback, this]() { + QObject::connect( + grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rescaleSize, callback, this]() { const QImage image = grabResult->image(); QThreadPool::globalInstance()->start([grabResult, image, rescaleSize, callback, this]() { const qreal luminance = this->findAverageLuminance(image, rescaleSize); if (callback.isCallable()) { - QMetaObject::invokeMethod(this, [luminance, callback]() { - callback.call({ QJSValue(luminance) }); - }, Qt::QueuedConnection); + QMetaObject::invokeMethod( + this, + [luminance, callback]() { + callback.call({ QJSValue(luminance) }); + }, + Qt::QueuedConnection); } }); - } - ); + }); } void CUtils::getAverageLuminance(const QString& path, QJSValue callback) { @@ -272,9 +283,12 @@ void CUtils::getAverageLuminance(const QString& path, int rescaleSize, QJSValue const qreal luminance = this->findAverageLuminance(image, rescaleSize); if (callback.isCallable()) { - QMetaObject::invokeMethod(this, [luminance, callback]() { - callback.call({ QJSValue(luminance) }); - }, Qt::QueuedConnection); + QMetaObject::invokeMethod( + this, + [luminance, callback]() { + callback.call({ QJSValue(luminance) }); + }, + Qt::QueuedConnection); } }); } diff --git a/plugin/src/Caelestia/cutils.hpp b/plugin/src/Caelestia/cutils.hpp index ef758f2..cac5d7c 100644 --- a/plugin/src/Caelestia/cutils.hpp +++ b/plugin/src/Caelestia/cutils.hpp @@ -1,8 +1,8 @@ #pragma once +#include #include #include -#include class CUtils : public QObject { Q_OBJECT @@ -15,7 +15,8 @@ public: Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved); Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed); Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved); - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed); + Q_INVOKABLE void saveItem( + QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed); Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target) const; Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const; diff --git a/plugin/src/Caelestia/filesystemmodel.cpp b/plugin/src/Caelestia/filesystemmodel.cpp index dc6027c..0a3d31a 100644 --- a/plugin/src/Caelestia/filesystemmodel.cpp +++ b/plugin/src/Caelestia/filesystemmodel.cpp @@ -1,12 +1,12 @@ #include "filesystemmodel.hpp" -#include -#include #include -#include #include #include +#include #include +#include +#include int FileSystemModel::rowCount(const QModelIndex& parent) const { if (parent != QModelIndex()) { @@ -23,7 +23,7 @@ QVariant FileSystemModel::data(const QModelIndex& index, int role) const { } QHash FileSystemModel::roleNames() const { - return {{ Qt::UserRole, "modelData"}}; + return { { Qt::UserRole, "modelData" } }; } QString FileSystemModel::path() const { diff --git a/plugin/src/Caelestia/filesystemmodel.hpp b/plugin/src/Caelestia/filesystemmodel.hpp index 77a5826..e12b26a 100644 --- a/plugin/src/Caelestia/filesystemmodel.hpp +++ b/plugin/src/Caelestia/filesystemmodel.hpp @@ -1,12 +1,12 @@ #pragma once -#include -#include #include -#include -#include #include +#include +#include #include +#include +#include class FileSystemEntry : public QObject { Q_OBJECT @@ -23,7 +23,10 @@ class FileSystemEntry : public QObject { public: explicit FileSystemEntry(const QString& path, const QString& relativePath, QObject* parent = nullptr) - : QObject(parent), m_fileInfo(QFileInfo(path)), m_path(path), m_relativePath(relativePath) {} + : QObject(parent) + , m_fileInfo(QFileInfo(path)) + , m_path(path) + , m_relativePath(relativePath) {} QString path() const { return m_path; }; QString relativePath() const { return m_relativePath; }; @@ -71,10 +74,12 @@ public: Q_ENUM(Filter) explicit FileSystemModel(QObject* parent = nullptr) - : QAbstractListModel(parent), m_recursive(true), m_filter(NoFilter) { - connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::watchDirIfRecursive); - connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::updateEntries); - } + : QAbstractListModel(parent) + , m_recursive(true) + , m_filter(NoFilter) { + connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::watchDirIfRecursive); + connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::updateEntries); + } int rowCount(const QModelIndex& parent = QModelIndex()) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; -- cgit v1.2.3-freya