Skip to content

Commit 3f34303

Browse files
committed
core/itemimagegrab: remove target prop
Also format
1 parent 8507d4a commit 3f34303

File tree

2 files changed

+33
-47
lines changed

2 files changed

+33
-47
lines changed

src/core/itemimagegrab.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,35 @@
77
#include <qthreadpool.h>
88
#include <qtmetamacros.h>
99

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();
10+
void ItemImageGrab::grab(QQuickItem* target, const QUrl& path) {
11+
this->grab(target, path, QSize());
1712
}
1813

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-
2514
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-
});
15+
if (!target) {
16+
qWarning() << "ItemImageGrab::grab: a target is required";
17+
return;
18+
}
19+
20+
QSharedPointer<QQuickItemGrabResult> grabResult;
21+
if (targetSize.isEmpty()) {
22+
grabResult = target->grabToImage();
23+
} else {
24+
grabResult = target->grabToImage(targetSize);
25+
}
26+
27+
QObject::connect(
28+
grabResult.data(),
29+
&QQuickItemGrabResult::ready,
30+
this,
31+
[grabResult, path, this]() {
32+
QThreadPool::globalInstance()->start([grabResult, path, this] {
33+
if (grabResult->saveToFile(path)) {
34+
emit this->saved(path);
35+
} else {
36+
emit this->failed(path);
37+
}
38+
});
39+
}
40+
);
4341
}

src/core/itemimagegrab.hpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,15 @@
77
/// Allows for saving an item grab to an file asynchronously.
88
class ItemImageGrab: public QObject {
99
Q_OBJECT;
10-
11-
Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged);
12-
QML_ELEMENT;
10+
QML_ELEMENT;
1311

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

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);
15+
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path);
16+
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path, const QSize& targetSize);
2417

2518
signals:
26-
void saved(const QUrl& path);
27-
void failed(const QUrl& path);
28-
29-
void targetChanged();
30-
31-
private:
32-
QPointer<QQuickItem> mTarget;
19+
void saved(const QUrl& path);
20+
void failed(const QUrl& path);
3321
};

0 commit comments

Comments
 (0)