aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2021-08-20 15:22:53 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2022-06-05 13:39:22 +0200
commitdda364c1489b0716bec0572d121361b3b3198c5c (patch)
treec1eab8053e2f1200f10130f75786fc77cadb6116 /src
parent37db56d8d427f0c18d4593ac4d77f8125029b16b (diff)
Canvas: Add support for specifying a size when saving to an image
This will make it possible for the image to be scaled into the given size, subsequently if this is set then it will make the DPR for the image be 1.0. [ChangeLog][QtQuick][Canvas] Canvas.save() now takes an optional size argument, which sets the size of the image to save, and sets DPR=1. Change-Id: I25f2d65a528282a26f46ef7dd1c62894307360cc Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quick/items/context2d/qquickcanvasitem.cpp21
-rw-r--r--src/quick/items/context2d/qquickcanvasitem_p.h2
2 files changed, 16 insertions, 7 deletions
diff --git a/src/quick/items/context2d/qquickcanvasitem.cpp b/src/quick/items/context2d/qquickcanvasitem.cpp
index 0e5698f47f..9b6faffaa0 100644
--- a/src/quick/items/context2d/qquickcanvasitem.cpp
+++ b/src/quick/items/context2d/qquickcanvasitem.cpp
@@ -972,22 +972,25 @@ void QQuickCanvasItem::checkAnimationCallbacks()
}
/*!
- \qmlmethod bool QtQuick::Canvas::save(string filename)
+ \qmlmethod bool QtQuick::Canvas::save(string filename, size imageSize = undefined)
Saves the current canvas content into an image file \a filename.
- The saved image format is automatically decided by the \a filename's
- suffix. Returns \c true on success.
+ The saved image format is automatically decided by the \a filename's suffix.
+ Returns \c true on success. If \a imageSize is specified, the resulting
+ image will have this size, and will have a devicePixelRatio of \c 1.0.
+ Otherwise, the \l {QQuickWindow::}{devicePixelRatio()} of the window in
+ which the canvas is displayed is applied to the saved image.
\note Calling this method will force painting the whole canvas, not just the
current canvas visible window.
\sa canvasWindow, canvasSize, toDataURL()
*/
-bool QQuickCanvasItem::save(const QString &filename) const
+bool QQuickCanvasItem::save(const QString &filename, const QSizeF &imageSize) const
{
Q_D(const QQuickCanvasItem);
QUrl url = d->baseUrl.resolved(QUrl::fromLocalFile(filename));
- return toImage().save(url.toLocalFile());
+ return toImage(QRectF(QPointF(0, 0), imageSize)).save(url.toLocalFile());
}
QQmlRefPointer<QQuickCanvasPixmap> QQuickCanvasItem::loadedPixmap(const QUrl& url)
@@ -1097,6 +1100,12 @@ bool QQuickCanvasItem::isImageLoaded(const QUrl& url) const
&& d->pixmaps.value(fullPathUrl)->pixmap()->isReady();
}
+/*!
+ \internal
+ Returns a QImage representing the requested \a rect which is in device independent pixels of the item.
+ If \a rect is empty, then it will use the whole item's rect by default.
+*/
+
QImage QQuickCanvasItem::toImage(const QRectF& rect) const
{
Q_D(const QQuickCanvasItem);
@@ -1105,7 +1114,7 @@ QImage QQuickCanvasItem::toImage(const QRectF& rect) const
return QImage();
const QRectF &rectSource = rect.isEmpty() ? canvasWindow() : rect;
- const qreal dpr = window() ? window()->effectiveDevicePixelRatio() : qreal(1);
+ const qreal dpr = window() && rect.isEmpty() ? window()->effectiveDevicePixelRatio() : qreal(1);
const QRectF rectScaled(rectSource.topLeft() * dpr, rectSource.size() * dpr);
QImage image = d->context->toImage(rectScaled);
diff --git a/src/quick/items/context2d/qquickcanvasitem_p.h b/src/quick/items/context2d/qquickcanvasitem_p.h
index 11e1ec3fb2..829fe657a1 100644
--- a/src/quick/items/context2d/qquickcanvasitem_p.h
+++ b/src/quick/items/context2d/qquickcanvasitem_p.h
@@ -154,7 +154,7 @@ public:
Q_INVOKABLE void requestPaint();
Q_INVOKABLE void markDirty(const QRectF& dirtyRect = QRectF());
- Q_INVOKABLE bool save(const QString &filename) const;
+ Q_INVOKABLE bool save(const QString &filename, const QSizeF &imageSize = QSizeF()) const;
Q_INVOKABLE QString toDataURL(const QString& type = QLatin1String("image/png")) const;
QQmlRefPointer<QQuickCanvasPixmap> loadedPixmap(const QUrl& url);