diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-08-27 17:17:07 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-08-27 17:17:07 +1000 |
| commit | 800b5e6515b2d68e589bf2307cd78ce8e059fcba (patch) | |
| tree | 54a0ae79460a440b93d8bd21e0ec6793cc6d2e2a /plugin/src/Caelestia/cutils.cpp | |
| parent | picker: better client detection (diff) | |
| download | caelestia-shell-800b5e6515b2d68e589bf2307cd78ce8e059fcba.tar.gz caelestia-shell-800b5e6515b2d68e589bf2307cd78ce8e059fcba.tar.bz2 caelestia-shell-800b5e6515b2d68e589bf2307cd78ce8e059fcba.zip | |
plugin: saveItem ensure parent dir
Also const everything and format
Diffstat (limited to 'plugin/src/Caelestia/cutils.cpp')
| -rw-r--r-- | plugin/src/Caelestia/cutils.cpp | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp index 1285c55..fd7fcdd 100644 --- a/plugin/src/Caelestia/cutils.cpp +++ b/plugin/src/Caelestia/cutils.cpp @@ -5,61 +5,66 @@ #include <QtQuick/QQuickItemGrabResult> #include <QThreadPool> #include <QQmlEngine> +#include <QDir> -void CUtils::saveItem(QQuickItem* target, const QUrl& path) { +void CUtils::saveItem(QQuickItem* target, const QUrl& path) const { this->saveItem(target, path, QRect(), QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) { - this->saveItem(target, path, rect, QJSValue(), QJSValue()); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const { + this->saveItem(target, path, rect, QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) { - this->saveItem(target, path, QRect(), onSaved, QJSValue()); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const { + this->saveItem(target, path, QRect(), onSaved, QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) { - this->saveItem(target, path, QRect(), onSaved, onFailed); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const { + this->saveItem(target, path, QRect(), onSaved, onFailed); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) { - this->saveItem(target, path, rect, onSaved, QJSValue()); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const { + this->saveItem(target, path, rect, onSaved, QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) { - if (!target) { - qWarning() << "CUtils::saveItem: a target is required"; - return; - } +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const { + if (!target) { + qWarning() << "CUtils::saveItem: a target is required"; + return; + } - if (!path.isLocalFile()) { - qWarning() << "CUtils::saveItem:" << path << "is not a local file"; - return; - } + if (!path.isLocalFile()) { + qWarning() << "CUtils::saveItem:" << path << "is not a local file"; + return; + } - QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage(); + QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage(); - QObject::connect( - grabResult.data(), - &QQuickItemGrabResult::ready, - this, - [grabResult, rect, path, onSaved, onFailed, this]() { - QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] { - QImage image = grabResult->image(); + QObject::connect( + grabResult.data(), + &QQuickItemGrabResult::ready, + this, + [grabResult, rect, path, onSaved, onFailed, this]() { + QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] { + QImage image = grabResult->image(); - if (!rect.isEmpty()) { - image = image.copy(rect); - } + if (!rect.isEmpty()) { + image = image.copy(rect); + } - const QString file = path.toLocalFile(); - if (image.save(file)) { - if (onSaved.isCallable()) { - onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - } else if (onFailed.isCallable()) { - onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - }); - } - ); + const QString file = path.toLocalFile(); + const QString parent = QFileInfo(file).absolutePath(); + if (QDir().mkpath(parent) && image.save(file)) { + 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)) }); + } + } + }); + } + ); } |