aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/context2d/qquickcanvasitem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quick/items/context2d/qquickcanvasitem.cpp')
-rw-r--r--src/quick/items/context2d/qquickcanvasitem.cpp114
1 files changed, 47 insertions, 67 deletions
diff --git a/src/quick/items/context2d/qquickcanvasitem.cpp b/src/quick/items/context2d/qquickcanvasitem.cpp
index a5888203cd..d78bd040c2 100644
--- a/src/quick/items/context2d/qquickcanvasitem.cpp
+++ b/src/quick/items/context2d/qquickcanvasitem.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtQuick module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <private/qsgadaptationlayer_p.h>
#include "qquickcanvasitem_p.h"
@@ -45,7 +9,7 @@
#include <private/qquickcontext2dtexture_p.h>
#include <private/qsgadaptationlayer_p.h>
#include <qsgtextureprovider.h>
-#include <QtQuick/private/qquickpixmapcache_p.h>
+#include <QtQuick/private/qquickpixmap_p.h>
#include <QtGui/QGuiApplication>
#include <qsgtextureprovider.h>
@@ -288,7 +252,7 @@ QQuickCanvasItemPrivate::~QQuickCanvasItemPrivate()
QPainter instead of the more expensive and likely less performing
JavaScript and Context2D approach.
- \sa Context2D QQuickPaintedItem
+ \sa Context2D, QQuickPaintedItem, {Qt Quick Examples - Pointer Handlers}
*/
QQuickCanvasItem::QQuickCanvasItem(QQuickItem *parent)
@@ -417,7 +381,7 @@ void QQuickCanvasItem::setCanvasSize(const QSizeF & size)
By default the tileSize is the same as the canvasSize.
- \obsolete This feature is incomplete. For details, see QTBUG-33129.
+ \deprecated This feature is incomplete. For details, see QTBUG-33129.
\sa canvasSize, canvasWindow
*/
@@ -452,7 +416,7 @@ void QQuickCanvasItem::setTileSize(const QSize & size)
can display different visible areas by changing the canvas windowSize
and/or position.
- \obsolete This feature is incomplete. For details, see QTBUG-33129
+ \deprecated This feature is incomplete. For details, see QTBUG-33129.
\sa canvasSize, tileSize
*/
@@ -480,9 +444,8 @@ void QQuickCanvasItem::setCanvasWindow(const QRectF& rect)
\qmlproperty enumeration QtQuick::Canvas::renderTarget
Holds the current canvas render target.
- \list
- \li Canvas.Image - render to an in memory image buffer.
- \endlist
+ \value Canvas.Image Render to an in-memory image buffer.
+ \value Canvas.FramebufferObject As of Qt 6.0, this value is ignored.
This hint is supplied along with renderStrategy to the graphics context to
determine the method of rendering. A renderStrategy, renderTarget or a
@@ -516,11 +479,9 @@ void QQuickCanvasItem::setRenderTarget(QQuickCanvasItem::RenderTarget target)
\qmlproperty enumeration QtQuick::Canvas::renderStrategy
Holds the current canvas rendering strategy.
- \list
- \li Canvas.Immediate - context will perform graphics commands immediately in the main UI thread.
- \li Canvas.Threaded - context will defer graphics commands to a private rendering thread.
- \li Canvas.Cooperative - context will defer graphics commands to the applications global render thread.
- \endlist
+ \value Canvas.Immediate context will perform graphics commands immediately in the main UI thread.
+ \value Canvas.Threaded context will defer graphics commands to a private rendering thread.
+ \value Canvas.Cooperative context will defer graphics commands to the applications global render thread.
This hint is supplied along with renderTarget to the graphics context to
determine the method of rendering. A renderStrategy, renderTarget or a
@@ -652,6 +613,10 @@ void QQuickCanvasItem::invalidateSceneGraph()
d->textureProvider = nullptr;
delete d->nodeTexture;
d->nodeTexture = nullptr;
+
+ // As we can expect(/hope) that the SG will be "good again", we can requestPaint ( which does 'markDirty(canvasWindow);' )
+ // Otherwise this Canvas will be "blank" when SG comes back
+ requestPaint();
}
void QQuickCanvasItem::schedulePolish()
@@ -713,12 +678,12 @@ void QQuickCanvasItem::updatePolish()
QV4::ExecutionEngine *v4 = qmlEngine(this)->handle();
QV4::Scope scope(v4);
QV4::ScopedFunctionObject function(scope);
- QV4::JSCallData jsCall(scope, 1);
- *jsCall->thisObject = QV4::QObjectWrapper::wrap(v4, this);
+ QV4::JSCallArguments jsCall(scope, 1);
+ *jsCall.thisObject = QV4::QObjectWrapper::wrap(v4, this);
for (auto it = animationCallbacks.cbegin(), end = animationCallbacks.cend(); it != end; ++it) {
function = it.value().value();
- jsCall->args[0] = QV4::Value::fromUInt32(QDateTime::currentMSecsSinceEpoch());
+ jsCall.args[0] = QV4::Value::fromUInt32(QDateTime::currentMSecsSinceEpoch());
function->call(jsCall);
}
}
@@ -845,7 +810,7 @@ QSGTextureProvider *QQuickCanvasItem::textureProvider() const
*/
-void QQuickCanvasItem::getContext(QQmlV4Function *args)
+void QQuickCanvasItem::getContext(QQmlV4FunctionPtr args)
{
Q_D(QQuickCanvasItem);
@@ -889,7 +854,7 @@ void QQuickCanvasItem::getContext(QQmlV4Function *args)
scene.
*/
-void QQuickCanvasItem::requestAnimationFrame(QQmlV4Function *args)
+void QQuickCanvasItem::requestAnimationFrame(QQmlV4FunctionPtr args)
{
QV4::Scope scope(args->v4engine());
QV4::ScopedFunctionObject f(scope, (*args)[0]);
@@ -918,7 +883,7 @@ void QQuickCanvasItem::requestAnimationFrame(QQmlV4Function *args)
This function will cancel the animation callback referenced by \a handle.
*/
-void QQuickCanvasItem::cancelRequestAnimationFrame(QQmlV4Function *args)
+void QQuickCanvasItem::cancelRequestAnimationFrame(QQmlV4FunctionPtr args)
{
QV4::Scope scope(args->v4engine());
QV4::ScopedValue v(scope, (*args)[0]);
@@ -972,30 +937,33 @@ 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)
+QQmlRefPointer<QQuickCanvasPixmap> QQuickCanvasItem::loadedPixmap(const QUrl& url, QSizeF sourceSize)
{
Q_D(QQuickCanvasItem);
QUrl fullPathUrl = d->baseUrl.resolved(url);
if (!d->pixmaps.contains(fullPathUrl)) {
- loadImage(url);
+ loadImage(url, sourceSize);
}
return d->pixmaps.value(fullPathUrl);
}
@@ -1009,7 +977,7 @@ QQmlRefPointer<QQuickCanvasPixmap> QQuickCanvasItem::loadedPixmap(const QUrl& ur
*/
/*!
- \qmlmethod QtQuick::Canvas::loadImage(url image)
+ \qmlmethod QtQuick::Canvas::loadImage(url image, size sourceSize = undefined)
Loads the given \a image asynchronously.
@@ -1018,10 +986,14 @@ QQmlRefPointer<QQuickCanvasPixmap> QQuickCanvasItem::loadedPixmap(const QUrl& ur
\note Only loaded images can be painted on the Canvas item.
+ If \a sourceSize is specified, the image will be scaled to that size during loading. This is
+ useful for loading scalable (vector) images (eg. SVGs) at their intended display size. This
+ parameter was introduced in Qt 6.7.
+
\sa unloadImage(), imageLoaded(), isImageLoaded(),
Context2D::createImageData(), Context2D::drawImage()
*/
-void QQuickCanvasItem::loadImage(const QUrl& url)
+void QQuickCanvasItem::loadImage(const QUrl& url, QSizeF sourceSize)
{
Q_D(QQuickCanvasItem);
QUrl fullPathUrl = d->baseUrl.resolved(url);
@@ -1033,6 +1005,8 @@ void QQuickCanvasItem::loadImage(const QUrl& url)
pix->load(qmlEngine(this)
, fullPathUrl
+ , QRect()
+ , sourceSize.toSize()
, QQuickPixmap::Cache | QQuickPixmap::Asynchronous);
if (pix->isLoading())
pix->connectFinished(this, SIGNAL(imageLoaded()));
@@ -1097,6 +1071,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 +1085,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);
@@ -1226,7 +1206,7 @@ QRect QQuickCanvasItem::tiledRect(const QRectF &window, const QSize &tileSize)
This signal is emitted when the \a region needs to be rendered. If a context
is active it can be referenced from the context property.
- This signal can be triggered by markdirty(), requestPaint() or by changing
+ This signal can be triggered by markDirty(), requestPaint() or by changing
the current canvas window.
*/