Skip to content

Commit 7531266

Browse files
committed
core:itemimagegrab: add crop and grab method
Quickshot coming soon™?
1 parent 3f34303 commit 7531266

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/core/itemimagegrab.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@ void ItemImageGrab::grab(QQuickItem* target, const QUrl& path) {
1212
}
1313

1414
void ItemImageGrab::grab(QQuickItem* target, const QUrl& path, const QSize& targetSize) {
15+
this->cropAndGrab(target, path, QRect(), targetSize);
16+
}
17+
18+
void ItemImageGrab::cropAndGrab(QQuickItem* target, const QUrl& path, const QRect& rect) {
19+
this->cropAndGrab(target, path, rect, QSize());
20+
}
21+
22+
void ItemImageGrab::cropAndGrab(
23+
QQuickItem* target,
24+
const QUrl& path,
25+
const QRect& rect,
26+
const QSize& targetSize
27+
) {
1528
if (!target) {
16-
qWarning() << "ItemImageGrab::grab: a target is required";
29+
qWarning() << "ItemImageGrab: a target is required";
30+
return;
31+
}
32+
33+
if (!path.isLocalFile()) {
34+
qWarning() << "ItemImageGrab: can only save to a file on the local filesystem";
1735
return;
1836
}
1937

@@ -28,10 +46,18 @@ void ItemImageGrab::grab(QQuickItem* target, const QUrl& path, const QSize& targ
2846
grabResult.data(),
2947
&QQuickItemGrabResult::ready,
3048
this,
31-
[grabResult, path, this]() {
32-
QThreadPool::globalInstance()->start([grabResult, path, this] {
33-
if (grabResult->saveToFile(path)) {
34-
emit this->saved(path);
49+
[grabResult, rect, path, this]() {
50+
QThreadPool::globalInstance()->start([grabResult, rect, path, this] {
51+
QImage image = grabResult->image();
52+
53+
if (!rect.isEmpty()) {
54+
image = image.copy(rect);
55+
}
56+
57+
const QString localFile = path.toLocalFile();
58+
59+
if (image.save(localFile)) {
60+
emit this->saved(localFile);
3561
} else {
3662
emit this->failed(path);
3763
}

src/core/itemimagegrab.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class ItemImageGrab: public QObject {
1515
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path);
1616
Q_INVOKABLE void grab(QQuickItem* target, const QUrl& path, const QSize& targetSize);
1717

18+
Q_INVOKABLE void cropAndGrab(QQuickItem* target, const QUrl& path, const QRect& rect);
19+
Q_INVOKABLE void
20+
cropAndGrab(QQuickItem* target, const QUrl& path, const QRect& rect, const QSize& targetSize);
21+
1822
signals:
19-
void saved(const QUrl& path);
23+
void saved(const QString& path);
2024
void failed(const QUrl& path);
2125
};

0 commit comments

Comments
 (0)