plugin: add saveItem
This commit is contained in:
parent
783057ab0d
commit
d65fb180ad
5 changed files with 128 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
||||||
.direnv
|
.direnv
|
||||||
/result
|
/result
|
||||||
/.qmlls.ini
|
/.qmlls.ini
|
||||||
|
build/
|
||||||
|
|
|
||||||
19
plugin/CMakeLists.txt
Normal file
19
plugin/CMakeLists.txt
Normal file
|
|
@ -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)
|
||||||
25
plugin/src/Caelestia/CMakeLists.txt
Normal file
25
plugin/src/Caelestia/CMakeLists.txt
Normal file
|
|
@ -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)
|
||||||
65
plugin/src/Caelestia/cutils.cpp
Normal file
65
plugin/src/Caelestia/cutils.cpp
Normal file
|
|
@ -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)) });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
18
plugin/src/Caelestia/cutils.hpp
Normal file
18
plugin/src/Caelestia/cutils.hpp
Normal file
|
|
@ -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);
|
||||||
|
};
|
||||||
Loading…
Add table
Reference in a new issue