|
7 | 7 | #include <qthreadpool.h>
|
8 | 8 | #include <qtmetamacros.h>
|
9 | 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(); |
| 10 | +void ItemImageGrab::grab(QQuickItem* target, const QUrl& path) { |
| 11 | + this->grab(target, path, QSize()); |
17 | 12 | }
|
18 | 13 |
|
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 | 14 | 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 | + ); |
43 | 41 | }
|
0 commit comments