diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-08-26 20:39:26 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-08-26 20:39:26 +1000 |
| commit | d65fb180ad5ee41049b9d0f9a962901524636b6a (patch) | |
| tree | df7be08c2ac182e592405883df81e6e054973638 /plugin | |
| parent | background: add visualiser (diff) | |
| download | caelestia-shell-d65fb180ad5ee41049b9d0f9a962901524636b6a.tar.gz caelestia-shell-d65fb180ad5ee41049b9d0f9a962901524636b6a.tar.bz2 caelestia-shell-d65fb180ad5ee41049b9d0f9a962901524636b6a.zip | |
plugin: add saveItem
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | plugin/src/Caelestia/CMakeLists.txt | 25 | ||||
| -rw-r--r-- | plugin/src/Caelestia/cutils.cpp | 65 | ||||
| -rw-r--r-- | plugin/src/Caelestia/cutils.hpp | 18 |
4 files changed, 127 insertions, 0 deletions
diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt new file mode 100644 index 0000000..29612d8 --- /dev/null +++ b/plugin/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.19) +project(Caelestia VERSION "0.0.1") + +set(QT_MIN_VERSION "6.9.0") +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_compile_options(-Wall -Wextra) + +find_package(Qt6 REQUIRED COMPONENTS Core Qml) + +if(QT_KNOWN_POLICY_QTP0001) + qt_policy(SET QTP0001 NEW) +endif() + +set(INSTALL_QMLDIR "" CACHE STRING "QML install dir") +set(QT_QML_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/qml") +qt_standard_project_setup() +add_subdirectory(src/Caelestia) diff --git a/plugin/src/Caelestia/CMakeLists.txt b/plugin/src/Caelestia/CMakeLists.txt new file mode 100644 index 0000000..61d7f7e --- /dev/null +++ b/plugin/src/Caelestia/CMakeLists.txt @@ -0,0 +1,25 @@ +qt_add_qml_module(caelestia + URI Caelestia + VERSION 0.1 + SOURCES + cutils.hpp cutils.cpp +) + +qt_query_qml_module(caelestia + URI module_uri + VERSION module_version + PLUGIN_TARGET module_plugin_target + TARGET_PATH module_target_path + QMLDIR module_qmldir + TYPEINFO module_typeinfo + QML_FILES module_qml_files + RESOURCES module_resources +) + +set(module_dir "${INSTALL_QMLDIR}/${module_target_path}") +install(TARGETS caelestia LIBRARY DESTINATION "${module_dir}" RUNTIME DESTINATION "${module_dir}") +install(TARGETS "${module_plugin_target}" LIBRARY DESTINATION "${module_dir}" RUNTIME DESTINATION "${module_dir}") +install(FILES "${module_qmldir}" DESTINATION "${module_dir}") +install(FILES "${module_typeinfo}" DESTINATION "${module_dir}") + +target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml) 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 <qobject.h> +#include <QtQuick/QQuickItem> +#include <QtQuick/QQuickItemGrabResult> +#include <QThreadPool> +#include <QQmlEngine> + +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<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(); + + 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)) }); + } + }); + } + ); +} diff --git a/plugin/src/Caelestia/cutils.hpp b/plugin/src/Caelestia/cutils.hpp new file mode 100644 index 0000000..bf7cb22 --- /dev/null +++ b/plugin/src/Caelestia/cutils.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <qobject.h> +#include <QtQuick/QQuickItem> + +class CUtils : public QObject { + Q_OBJECT; + QML_NAMED_ELEMENT(CUtils); + QML_SINGLETON; + +public: + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path); + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect); + 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); +}; |