Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ qt_add_library(quickshell-core STATIC
scriptmodel.cpp
colorquantizer.cpp
toolsupport.cpp
itemimagegrab.cpp
)

qt_add_qml_module(quickshell-core
Expand Down
43 changes: 43 additions & 0 deletions src/core/itemimagegrab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "itemimagegrab.hpp"

#include <qobject.h>
#include <qquickitem.h>
#include <qquickitemgrabresult.h>
#include <qsize.h>
#include <qthreadpool.h>
#include <qtmetamacros.h>

QQuickItem* ItemImageGrab::target() const { return this->mTarget; }

void ItemImageGrab::setTarget(QQuickItem* target) {
if (target == this->mTarget) return;

this->mTarget = target;
emit this->targetChanged();
}

void ItemImageGrab::grab(const QUrl& path) { this->grab(this->mTarget, path); }

void ItemImageGrab::grab(const QUrl& path, const QSize& targetSize) { this->grab(this->mTarget, path, targetSize); }

void ItemImageGrab::grab(QQuickItem* target, const QUrl& path) { this->grab(target, path, QSize()); }

void ItemImageGrab::grab(QQuickItem* target, const QUrl& path, const QSize& targetSize) {
if (!target) return;

QSharedPointer<QQuickItemGrabResult> grabResult;
if (targetSize.isEmpty()){
grabResult = target->grabToImage();
}else{
grabResult = target->grabToImage(targetSize);}

QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, path, this]() {
QThreadPool::globalInstance()->start([grabResult, path, this] {
if (grabResult->saveToFile(path)) {
emit this->saved(path);
} else {
emit this->failed(path);
}
});
});
}
33 changes: 33 additions & 0 deletions src/core/itemimagegrab.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <qobject.h>
#include <qquickitem.h>
#include <qsize.h>

/// Allows for saving an item grab to an file asynchronously.
class ItemImageGrab: public QObject {
Q_OBJECT;

Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged);
QML_ELEMENT;

public:
explicit ItemImageGrab(QObject* parent = nullptr): QObject(parent) {};

[[nodiscard]] QQuickItem* target() const;
void setTarget(QQuickItem* target);

Q_INVOKABLE void grab(const QUrl& path);
Q_INVOKABLE void grab(const QUrl& path, const QSize& targetSize);
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path);
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path, const QSize& targetSize);

signals:
void saved(const QUrl& path);
void failed(const QUrl& path);

void targetChanged();

private:
QPointer<QQuickItem> mTarget;
};
1 change: 1 addition & 0 deletions src/core/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ headers = [
"clock.hpp",
"scriptmodel.hpp",
"colorquantizer.hpp",
"itemimagegrab.hpp",
]
-----