Skip to content

Commit

Permalink
Say hello to QQuickItem::grabToImage().
Browse files Browse the repository at this point in the history
[ChangeLog][QtQuick][Item] Added functions QQuickItem::grabToImage()
and Item::grabToImage() to allow grabbing of items into
system-memory images.

Change-Id: I76cd73bb62f7440569c6fce63d63528559845721
Reviewed-by: Michael Brasser <michael.brasser@live.com>
  • Loading branch information
sletta authored and The Qt Project committed May 1, 2014
1 parent ef5a68d commit 0cb12e9
Show file tree
Hide file tree
Showing 11 changed files with 851 additions and 2 deletions.
87 changes: 87 additions & 0 deletions src/quick/doc/snippets/qml/itemGrab.qml
@@ -0,0 +1,87 @@
/****************************************************************************
**
** Copyright (C) 2014 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.4

Item {
width: 320
height: 480

//! [grab-source]
Rectangle {
id: source
width: 100
height: 100
gradient: Gradient {
GradientStop { position: 0; color: "steelblue" }
GradientStop { position: 1; color: "black" }
}
}
//! [grab-source]

//! [grab-image-target]
Image {
id: image
}
//! [grab-image-target]
Timer {
repeat: false
running: true
interval: 1000
onTriggered: {
//! [grab-to-file]

// ...
source.grabToImage(function(result) {
result.save("something.png");
});
//! [grab-to-file]

//! [grab-to-cache]

// ...
source.grabToImage(function(result) {
image.source = result.url;
},
Qt.size(50, 50));
//! [grab-to-cache]
}
}
}
6 changes: 4 additions & 2 deletions src/quick/items/items.pri
Expand Up @@ -75,7 +75,8 @@ HEADERS += \
$$PWD/qquickitemviewtransition_p.h \
$$PWD/qquickscreen_p.h \
$$PWD/qquickwindowmodule_p.h \
$$PWD/qquickframebufferobject.h
$$PWD/qquickframebufferobject.h \
$$PWD/qquickitemgrabresult.h

SOURCES += \
$$PWD/qquickevents.cpp \
Expand Down Expand Up @@ -128,7 +129,8 @@ SOURCES += \
$$PWD/qquickitemviewtransition.cpp \
$$PWD/qquickwindowmodule.cpp \
$$PWD/qquickscreen.cpp \
$$PWD/qquickframebufferobject.cpp
$$PWD/qquickframebufferobject.cpp \
$$PWD/qquickitemgrabresult.cpp

SOURCES += \
$$PWD/qquickshadereffect.cpp \
Expand Down
7 changes: 7 additions & 0 deletions src/quick/items/qquickitem.h
Expand Up @@ -43,6 +43,7 @@
#define QQUICKITEM_H

#include <QtQuick/qtquickglobal.h>
#include <QtQuick/qquickitemgrabresult.h>
#include <QtQml/qqml.h>
#include <QtQml/qqmlcomponent.h>

Expand All @@ -52,6 +53,7 @@
#include <QtGui/qfont.h>
#include <QtGui/qaccessible.h>


QT_BEGIN_NAMESPACE

class QQuickItem;
Expand Down Expand Up @@ -92,6 +94,7 @@ class QTouchEvent;
class QSGNode;
class QSGTransformNode;
class QSGTextureProvider;
class QQuickItemGrabResult;

class Q_QUICK_EXPORT QQuickItem : public QObject, public QQmlParserStatus
{
Expand Down Expand Up @@ -310,6 +313,10 @@ class Q_QUICK_EXPORT QQuickItem : public QObject, public QQmlParserStatus
bool keepTouchGrab() const;
void setKeepTouchGrab(bool);

// implemented in qquickitemgrabresult.cpp
Q_REVISION(4) Q_INVOKABLE bool grabToImage(const QJSValue &callback, const QSize &targetSize = QSize());
QSharedPointer<QQuickItemGrabResult> grabToImage(const QSize &targetSize = QSize());

Q_INVOKABLE virtual bool contains(const QPointF &point) const;

QTransform itemTransform(QQuickItem *, bool *) const;
Expand Down

0 comments on commit 0cb12e9

Please sign in to comment.