Skip to content

Commit 3f506f7

Browse files
committed
core/itemimagegrab: create
Create ItemImageGrab qml component
1 parent 13e8736 commit 3f506f7

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ qt_add_library(quickshell-core STATIC
3939
scriptmodel.cpp
4040
colorquantizer.cpp
4141
toolsupport.cpp
42+
itemimagegrab.cpp
4243
)
4344

4445
qt_add_qml_module(quickshell-core

src/core/itemimagegrab.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "itemimagegrab.hpp"
2+
3+
#include <qobject.h>
4+
#include <qquickitem.h>
5+
#include <qquickitemgrabresult.h>
6+
#include <qsize.h>
7+
#include <qthreadpool.h>
8+
#include <qtmetamacros.h>
9+
10+
QQuickItem* ItemImageGrab::target() const { return this->mTarget; }
11+
12+
void ItemImageGrab::setTarget(QQuickItem* target) {
13+
if (target == this->mTarget) return;
14+
15+
this->mTarget = target;
16+
emit this->targetChanged();
17+
}
18+
19+
void ItemImageGrab::grab(const QUrl& path) { this->grab(this->mTarget, path); }
20+
21+
void ItemImageGrab::grab(const QUrl& path, const QSize& targetSize) { this->grab(this->mTarget, path, targetSize); }
22+
23+
void ItemImageGrab::grab(QQuickItem* target, const QUrl& path) { this->grab(target, path, QSize()); }
24+
25+
void ItemImageGrab::grab(QQuickItem* target, const QUrl& path, const QSize& targetSize) {
26+
if (!target) return;
27+
28+
QSharedPointer<QQuickItemGrabResult> grabResult;
29+
if (targetSize.isEmpty()){
30+
grabResult = target->grabToImage();
31+
}else{
32+
grabResult = target->grabToImage(targetSize);}
33+
34+
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, path, this]() {
35+
QThreadPool::globalInstance()->start([grabResult, path, this] {
36+
if (grabResult->saveToFile(path)) {
37+
emit this->saved(path);
38+
} else {
39+
emit this->failed(path);
40+
}
41+
});
42+
});
43+
}

src/core/itemimagegrab.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <qobject.h>
4+
#include <qquickitem.h>
5+
#include <qsize.h>
6+
7+
/// Allows for saving an item grab to an file asynchronously.
8+
class ItemImageGrab: public QObject {
9+
Q_OBJECT;
10+
11+
Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged);
12+
QML_ELEMENT;
13+
14+
public:
15+
explicit ItemImageGrab(QObject* parent = nullptr): QObject(parent) {};
16+
17+
[[nodiscard]] QQuickItem* target() const;
18+
void setTarget(QQuickItem* target);
19+
20+
Q_INVOKABLE void grab(const QUrl& path);
21+
Q_INVOKABLE void grab(const QUrl& path, const QSize& targetSize);
22+
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path);
23+
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path, const QSize& targetSize);
24+
25+
signals:
26+
void saved(const QUrl& path);
27+
void failed(const QUrl& path);
28+
29+
void targetChanged();
30+
31+
private:
32+
QPointer<QQuickItem> mTarget;
33+
};

src/core/module.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ headers = [
3030
"clock.hpp",
3131
"scriptmodel.hpp",
3232
"colorquantizer.hpp",
33+
"itemimagegrab.hpp",
3334
]
3435
-----

0 commit comments

Comments
 (0)