From d65fb180ad5ee41049b9d0f9a962901524636b6a Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Tue, 26 Aug 2025 20:39:26 +1000 Subject: plugin: add saveItem --- plugin/src/Caelestia/cutils.cpp | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 plugin/src/Caelestia/cutils.cpp (limited to 'plugin/src/Caelestia/cutils.cpp') diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp new file mode 100644 index 0000000..1285c55 --- /dev/null +++ b/plugin/src/Caelestia/cutils.cpp @@ -0,0 +1,65 @@ +#include "cutils.hpp" + +#include +#include +#include +#include +#include + +void CUtils::saveItem(QQuickItem* target, const QUrl& path) { + 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, QJSValue onSaved) { + 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, 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, QJSValue onFailed) { + if (!target) { + qWarning() << "CUtils::saveItem: a target is required"; + return; + } + + if (!path.isLocalFile()) { + qWarning() << "CUtils::saveItem:" << path << "is not a local file"; + return; + } + + QSharedPointer 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(); + + 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)) }); + } + }); + } + ); +} -- cgit v1.2.3-freya