Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/items/qquickitemgrabresult.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com> | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include <private/qtquickglobal_p.h> | - | ||||||||||||
41 | #include "qquickitemgrabresult.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #include "qquickwindow.h" | - | ||||||||||||
44 | #include "qquickitem.h" | - | ||||||||||||
45 | #if QT_CONFIG(quick_shadereffect) | - | ||||||||||||
46 | #include "qquickshadereffectsource_p.h" | - | ||||||||||||
47 | #endif | - | ||||||||||||
48 | - | |||||||||||||
49 | #include <QtQml/QQmlEngine> | - | ||||||||||||
50 | #include <QtQml/QQmlInfo> | - | ||||||||||||
51 | - | |||||||||||||
52 | #include <private/qquickpixmapcache_p.h> | - | ||||||||||||
53 | #include <private/qquickitem_p.h> | - | ||||||||||||
54 | #include <private/qsgcontext_p.h> | - | ||||||||||||
55 | #include <private/qsgadaptationlayer_p.h> | - | ||||||||||||
56 | - | |||||||||||||
57 | QT_BEGIN_NAMESPACE | - | ||||||||||||
58 | - | |||||||||||||
59 | const QEvent::Type Event_Grab_Completed = static_cast<QEvent::Type>(QEvent::User + 1); | - | ||||||||||||
60 | - | |||||||||||||
61 | class QQuickItemGrabResultPrivate : public QObjectPrivate | - | ||||||||||||
62 | { | - | ||||||||||||
63 | public: | - | ||||||||||||
64 | QQuickItemGrabResultPrivate() | - | ||||||||||||
65 | : cacheEntry(nullptr) | - | ||||||||||||
66 | , qmlEngine(nullptr) | - | ||||||||||||
67 | , texture(nullptr) | - | ||||||||||||
68 | { | - | ||||||||||||
69 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||
70 | - | |||||||||||||
71 | ~QQuickItemGrabResultPrivate() | - | ||||||||||||
72 | { | - | ||||||||||||
73 | delete cacheEntry; | - | ||||||||||||
74 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||
75 | - | |||||||||||||
76 | void ensureImageInCache() const { | - | ||||||||||||
77 | if (url.isEmpty() && !image.isNull()) {
| 0-4 | ||||||||||||
78 | url.setScheme(QQuickPixmap::itemGrabberScheme); | - | ||||||||||||
79 | url.setPath(QVariant::fromValue(item.data()).toString()); | - | ||||||||||||
80 | static uint counter = 0; | - | ||||||||||||
81 | url.setFragment(QString::number(++counter)); | - | ||||||||||||
82 | cacheEntry = new QQuickPixmap(url, image); | - | ||||||||||||
83 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
84 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
85 | - | |||||||||||||
86 | static QQuickItemGrabResult *create(QQuickItem *item, const QSize &size); | - | ||||||||||||
87 | - | |||||||||||||
88 | QImage image; | - | ||||||||||||
89 | - | |||||||||||||
90 | mutable QUrl url; | - | ||||||||||||
91 | mutable QQuickPixmap *cacheEntry; | - | ||||||||||||
92 | - | |||||||||||||
93 | QQmlEngine *qmlEngine; | - | ||||||||||||
94 | QJSValue callback; | - | ||||||||||||
95 | - | |||||||||||||
96 | QPointer<QQuickItem> item; | - | ||||||||||||
97 | QPointer<QQuickWindow> window; | - | ||||||||||||
98 | QSGLayer *texture; | - | ||||||||||||
99 | QSizeF itemSize; | - | ||||||||||||
100 | QSize textureSize; | - | ||||||||||||
101 | }; | - | ||||||||||||
102 | - | |||||||||||||
103 | /*! | - | ||||||||||||
104 | * \qmlproperty url QtQuick::ItemGrabResult::url | - | ||||||||||||
105 | * | - | ||||||||||||
106 | * This property holds a URL which can be used in conjunction with | - | ||||||||||||
107 | * URL based image consumers, such as the QtQuick::Image type. | - | ||||||||||||
108 | * | - | ||||||||||||
109 | * The URL is valid while there is a reference in QML or JavaScript | - | ||||||||||||
110 | * to the ItemGrabResult or while the image the URL references is | - | ||||||||||||
111 | * actively used. | - | ||||||||||||
112 | * | - | ||||||||||||
113 | * The URL does not represent a valid file or location to read it from, it | - | ||||||||||||
114 | * is primarily a key to access images through Qt Quick's image-based types. | - | ||||||||||||
115 | */ | - | ||||||||||||
116 | - | |||||||||||||
117 | /*! | - | ||||||||||||
118 | * \property QQuickItemGrabResult::url | - | ||||||||||||
119 | * | - | ||||||||||||
120 | * This property holds a URL which can be used in conjunction with | - | ||||||||||||
121 | * URL based image consumers, such as the QtQuick::Image type. | - | ||||||||||||
122 | * | - | ||||||||||||
123 | * The URL is valid until the QQuickItemGrabResult object is deleted. | - | ||||||||||||
124 | * | - | ||||||||||||
125 | * The URL does not represent a valid file or location to read it from, it | - | ||||||||||||
126 | * is primarily a key to access images through Qt Quick's image-based types. | - | ||||||||||||
127 | */ | - | ||||||||||||
128 | - | |||||||||||||
129 | /*! | - | ||||||||||||
130 | * \qmlproperty variant QtQuick::ItemGrabResult::image | - | ||||||||||||
131 | * | - | ||||||||||||
132 | * This property holds the pixel results from a grab in the | - | ||||||||||||
133 | * form of a QImage. | - | ||||||||||||
134 | */ | - | ||||||||||||
135 | - | |||||||||||||
136 | /*! | - | ||||||||||||
137 | * \property QQuickItemGrabResult::image | - | ||||||||||||
138 | * | - | ||||||||||||
139 | * This property holds the pixel results from a grab. | - | ||||||||||||
140 | * | - | ||||||||||||
141 | * If the grab is not yet complete or if it failed, | - | ||||||||||||
142 | * a null image is returned (\c {image.isNull()} will return \c true). | - | ||||||||||||
143 | */ | - | ||||||||||||
144 | - | |||||||||||||
145 | /*! | - | ||||||||||||
146 | \class QQuickItemGrabResult | - | ||||||||||||
147 | \inmodule QtQuick | - | ||||||||||||
148 | \brief The QQuickItemGrabResult contains the result from QQuickItem::grabToImage(). | - | ||||||||||||
149 | - | |||||||||||||
150 | \sa QQuickItem::grabToImage() | - | ||||||||||||
151 | */ | - | ||||||||||||
152 | - | |||||||||||||
153 | /*! | - | ||||||||||||
154 | * \fn void QQuickItemGrabResult::ready() | - | ||||||||||||
155 | * | - | ||||||||||||
156 | * This signal is emitted when the grab has completed. | - | ||||||||||||
157 | */ | - | ||||||||||||
158 | - | |||||||||||||
159 | /*! | - | ||||||||||||
160 | * \qmltype ItemGrabResult | - | ||||||||||||
161 | * \instantiates QQuickItemGrabResult | - | ||||||||||||
162 | * \inherits QtObject | - | ||||||||||||
163 | * \inqmlmodule QtQuick | - | ||||||||||||
164 | * \ingroup qtquick-visual | - | ||||||||||||
165 | * \brief Contains the results from a call to Item::grabToImage(). | - | ||||||||||||
166 | * | - | ||||||||||||
167 | * The ItemGrabResult is a small container used to encapsulate | - | ||||||||||||
168 | * the results from Item::grabToImage(). | - | ||||||||||||
169 | * | - | ||||||||||||
170 | * \sa Item::grabToImage() | - | ||||||||||||
171 | */ | - | ||||||||||||
172 | - | |||||||||||||
173 | QQuickItemGrabResult::QQuickItemGrabResult(QObject *parent) | - | ||||||||||||
174 | : QObject(*new QQuickItemGrabResultPrivate, parent) | - | ||||||||||||
175 | { | - | ||||||||||||
176 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||
177 | - | |||||||||||||
178 | /*! | - | ||||||||||||
179 | * \qmlmethod bool QtQuick::ItemGrabResult::saveToFile(fileName) | - | ||||||||||||
180 | * | - | ||||||||||||
181 | * Saves the grab result as an image to \a fileName. Returns true | - | ||||||||||||
182 | * if successful; otherwise returns false. | - | ||||||||||||
183 | */ | - | ||||||||||||
184 | - | |||||||||||||
185 | /*! | - | ||||||||||||
186 | * Saves the grab result as an image to \a fileName. Returns true | - | ||||||||||||
187 | * if successful; otherwise returns false. | - | ||||||||||||
188 | * | - | ||||||||||||
189 | * \note In Qt versions prior to 5.9, this function is marked as non-\c{const}. | - | ||||||||||||
190 | */ | - | ||||||||||||
191 | bool QQuickItemGrabResult::saveToFile(const QString &fileName) const | - | ||||||||||||
192 | { | - | ||||||||||||
193 | Q_D(const QQuickItemGrabResult); | - | ||||||||||||
194 | return d->image.save(fileName); never executed: return d->image.save(fileName); | 0 | ||||||||||||
195 | } | - | ||||||||||||
196 | - | |||||||||||||
197 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) | - | ||||||||||||
198 | /*! | - | ||||||||||||
199 | * \overload | - | ||||||||||||
200 | * \internal | - | ||||||||||||
201 | */ | - | ||||||||||||
202 | bool QQuickItemGrabResult::saveToFile(const QString &fileName) | - | ||||||||||||
203 | { | - | ||||||||||||
204 | return qAsConst(*this).saveToFile(fileName); never executed: return qAsConst(*this).saveToFile(fileName); | 0 | ||||||||||||
205 | } | - | ||||||||||||
206 | #endif // < Qt 6 | - | ||||||||||||
207 | - | |||||||||||||
208 | QUrl QQuickItemGrabResult::url() const | - | ||||||||||||
209 | { | - | ||||||||||||
210 | Q_D(const QQuickItemGrabResult); | - | ||||||||||||
211 | d->ensureImageInCache(); | - | ||||||||||||
212 | return d->url; executed 4 times by 1 test: return d->url; Executed by:
| 4 | ||||||||||||
213 | } | - | ||||||||||||
214 | - | |||||||||||||
215 | QImage QQuickItemGrabResult::image() const | - | ||||||||||||
216 | { | - | ||||||||||||
217 | Q_D(const QQuickItemGrabResult); | - | ||||||||||||
218 | return d->image; executed 28 times by 2 tests: return d->image; Executed by:
| 28 | ||||||||||||
219 | } | - | ||||||||||||
220 | - | |||||||||||||
221 | /*! | - | ||||||||||||
222 | * \internal | - | ||||||||||||
223 | */ | - | ||||||||||||
224 | bool QQuickItemGrabResult::event(QEvent *e) | - | ||||||||||||
225 | { | - | ||||||||||||
226 | Q_D(QQuickItemGrabResult); | - | ||||||||||||
227 | if (e->type() == Event_Grab_Completed) {
| 0-6 | ||||||||||||
228 | // JS callback | - | ||||||||||||
229 | if (d->qmlEngine && d->callback.isCallable())
| 0-6 | ||||||||||||
230 | d->callback.call(QJSValueList() << d->qmlEngine->newQObject(this)); never executed: d->callback.call(QJSValueList() << d->qmlEngine->newQObject(this)); | 0 | ||||||||||||
231 | else | - | ||||||||||||
232 | Q_EMIT ready(); executed 6 times by 2 tests: ready(); Executed by:
| 6 | ||||||||||||
233 | return true; executed 6 times by 2 tests: return true; Executed by:
| 6 | ||||||||||||
234 | } | - | ||||||||||||
235 | return QObject::event(e); never executed: return QObject::event(e); | 0 | ||||||||||||
236 | } | - | ||||||||||||
237 | - | |||||||||||||
238 | void QQuickItemGrabResult::setup() | - | ||||||||||||
239 | { | - | ||||||||||||
240 | Q_D(QQuickItemGrabResult); | - | ||||||||||||
241 | if (!d->item) {
| 0-8 | ||||||||||||
242 | disconnect(d->window.data(), &QQuickWindow::beforeSynchronizing, this, &QQuickItemGrabResult::setup); | - | ||||||||||||
243 | disconnect(d->window.data(), &QQuickWindow::afterRendering, this, &QQuickItemGrabResult::render); | - | ||||||||||||
244 | QCoreApplication::postEvent(this, new QEvent(Event_Grab_Completed)); | - | ||||||||||||
245 | return; never executed: return; | 0 | ||||||||||||
246 | } | - | ||||||||||||
247 | - | |||||||||||||
248 | QSGRenderContext *rc = QQuickWindowPrivate::get(d->window.data())->context; | - | ||||||||||||
249 | d->texture = rc->sceneGraphContext()->createLayer(rc); | - | ||||||||||||
250 | d->texture->setItem(QQuickItemPrivate::get(d->item)->itemNode()); | - | ||||||||||||
251 | d->itemSize = QSizeF(d->item->width(), d->item->height()); | - | ||||||||||||
252 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||
253 | - | |||||||||||||
254 | void QQuickItemGrabResult::render() | - | ||||||||||||
255 | { | - | ||||||||||||
256 | Q_D(QQuickItemGrabResult); | - | ||||||||||||
257 | if (!d->texture)
| 0-8 | ||||||||||||
258 | return; never executed: return; | 0 | ||||||||||||
259 | - | |||||||||||||
260 | d->texture->setRect(QRectF(0, d->itemSize.height(), d->itemSize.width(), -d->itemSize.height())); | - | ||||||||||||
261 | const QSize minSize = QQuickWindowPrivate::get(d->window.data())->context->sceneGraphContext()->minimumFBOSize(); | - | ||||||||||||
262 | d->texture->setSize(QSize(qMax(minSize.width(), d->textureSize.width()), | - | ||||||||||||
263 | qMax(minSize.height(), d->textureSize.height()))); | - | ||||||||||||
264 | d->texture->scheduleUpdate(); | - | ||||||||||||
265 | d->texture->updateTexture(); | - | ||||||||||||
266 | d->image = d->texture->toImage(); | - | ||||||||||||
267 | - | |||||||||||||
268 | delete d->texture; | - | ||||||||||||
269 | d->texture = nullptr; | - | ||||||||||||
270 | - | |||||||||||||
271 | disconnect(d->window.data(), &QQuickWindow::beforeSynchronizing, this, &QQuickItemGrabResult::setup); | - | ||||||||||||
272 | disconnect(d->window.data(), &QQuickWindow::afterRendering, this, &QQuickItemGrabResult::render); | - | ||||||||||||
273 | QCoreApplication::postEvent(this, new QEvent(Event_Grab_Completed)); | - | ||||||||||||
274 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||
275 | - | |||||||||||||
276 | QQuickItemGrabResult *QQuickItemGrabResultPrivate::create(QQuickItem *item, const QSize &targetSize) | - | ||||||||||||
277 | { | - | ||||||||||||
278 | QSize size = targetSize; | - | ||||||||||||
279 | if (size.isEmpty())
| 2-6 | ||||||||||||
280 | size = QSize(item->width(), item->height()); executed 6 times by 2 tests: size = QSize(item->width(), item->height()); Executed by:
| 6 | ||||||||||||
281 | - | |||||||||||||
282 | if (size.width() < 1 || size.height() < 1) {
| 0-8 | ||||||||||||
283 | qmlWarning(item) << "grabToImage: item has invalid dimensions"; | - | ||||||||||||
284 | return nullptr; never executed: return nullptr; | 0 | ||||||||||||
285 | } | - | ||||||||||||
286 | - | |||||||||||||
287 | if (!item->window()) {
| 0-8 | ||||||||||||
288 | qmlWarning(item) << "grabToImage: item is not attached to a window"; | - | ||||||||||||
289 | return nullptr; never executed: return nullptr; | 0 | ||||||||||||
290 | } | - | ||||||||||||
291 | - | |||||||||||||
292 | if (!item->window()->isVisible()) {
| 0-8 | ||||||||||||
293 | qmlWarning(item) << "grabToImage: item's window is not visible"; | - | ||||||||||||
294 | return nullptr; never executed: return nullptr; | 0 | ||||||||||||
295 | } | - | ||||||||||||
296 | - | |||||||||||||
297 | QQuickItemGrabResult *result = new QQuickItemGrabResult(); | - | ||||||||||||
298 | QQuickItemGrabResultPrivate *d = result->d_func(); | - | ||||||||||||
299 | d->item = item; | - | ||||||||||||
300 | d->window = item->window(); | - | ||||||||||||
301 | d->textureSize = size; | - | ||||||||||||
302 | - | |||||||||||||
303 | QQuickItemPrivate::get(item)->refFromEffectItem(false); | - | ||||||||||||
304 | - | |||||||||||||
305 | // trigger sync & render | - | ||||||||||||
306 | item->window()->update(); | - | ||||||||||||
307 | - | |||||||||||||
308 | return result; executed 8 times by 2 tests: return result; Executed by:
| 8 | ||||||||||||
309 | } | - | ||||||||||||
310 | - | |||||||||||||
311 | /*! | - | ||||||||||||
312 | * Grabs the item into an in-memory image. | - | ||||||||||||
313 | * | - | ||||||||||||
314 | * The grab happens asynchronously and the signal QQuickItemGrabResult::ready() | - | ||||||||||||
315 | * is emitted when the grab has been completed. | - | ||||||||||||
316 | * | - | ||||||||||||
317 | * Use \a targetSize to specify the size of the target image. By default, the | - | ||||||||||||
318 | * result will have the same size as item. | - | ||||||||||||
319 | * | - | ||||||||||||
320 | * If the grab could not be initiated, the function returns \c null. | - | ||||||||||||
321 | * | - | ||||||||||||
322 | * \note This function will render the item to an offscreen surface and | - | ||||||||||||
323 | * copy that surface from the GPU's memory into the CPU's memory, which can | - | ||||||||||||
324 | * be quite costly. For "live" preview, use \l {QtQuick::Item::layer.enabled} {layers} | - | ||||||||||||
325 | * or ShaderEffectSource. | - | ||||||||||||
326 | * | - | ||||||||||||
327 | * \sa QQuickWindow::grabWindow() | - | ||||||||||||
328 | */ | - | ||||||||||||
329 | QSharedPointer<QQuickItemGrabResult> QQuickItem::grabToImage(const QSize &targetSize) | - | ||||||||||||
330 | { | - | ||||||||||||
331 | QQuickItemGrabResult *result = QQuickItemGrabResultPrivate::create(this, targetSize); | - | ||||||||||||
332 | if (!result)
| 0-8 | ||||||||||||
333 | return QSharedPointer<QQuickItemGrabResult>(); never executed: return QSharedPointer<QQuickItemGrabResult>(); | 0 | ||||||||||||
334 | - | |||||||||||||
335 | connect(window(), &QQuickWindow::beforeSynchronizing, result, &QQuickItemGrabResult::setup, Qt::DirectConnection); | - | ||||||||||||
336 | connect(window(), &QQuickWindow::afterRendering, result, &QQuickItemGrabResult::render, Qt::DirectConnection); | - | ||||||||||||
337 | - | |||||||||||||
338 | return QSharedPointer<QQuickItemGrabResult>(result); executed 8 times by 2 tests: return QSharedPointer<QQuickItemGrabResult>(result); Executed by:
| 8 | ||||||||||||
339 | } | - | ||||||||||||
340 | - | |||||||||||||
341 | /*! | - | ||||||||||||
342 | * \qmlmethod bool QtQuick::Item::grabToImage(callback, targetSize) | - | ||||||||||||
343 | * | - | ||||||||||||
344 | * Grabs the item into an in-memory image. | - | ||||||||||||
345 | * | - | ||||||||||||
346 | * The grab happens asynchronously and the JavaScript function \a callback is | - | ||||||||||||
347 | * invoked when the grab is completed. The callback takes one argument, which | - | ||||||||||||
348 | * is the result of the grab operation; an \l ItemGrabResult object. | - | ||||||||||||
349 | * | - | ||||||||||||
350 | * Use \a targetSize to specify the size of the target image. By default, the result | - | ||||||||||||
351 | * will have the same size as the item. | - | ||||||||||||
352 | * | - | ||||||||||||
353 | * If the grab could not be initiated, the function returns \c false. | - | ||||||||||||
354 | * | - | ||||||||||||
355 | * The following snippet shows how to grab an item and store the results to | - | ||||||||||||
356 | * a file. | - | ||||||||||||
357 | * | - | ||||||||||||
358 | * \snippet qml/itemGrab.qml grab-source | - | ||||||||||||
359 | * \snippet qml/itemGrab.qml grab-to-file | - | ||||||||||||
360 | * | - | ||||||||||||
361 | * The following snippet shows how to grab an item and use the results in | - | ||||||||||||
362 | * another image element. | - | ||||||||||||
363 | * | - | ||||||||||||
364 | * \snippet qml/itemGrab.qml grab-image-target | - | ||||||||||||
365 | * \snippet qml/itemGrab.qml grab-to-cache | - | ||||||||||||
366 | * | - | ||||||||||||
367 | * \note This function will render the item to an offscreen surface and | - | ||||||||||||
368 | * copy that surface from the GPU's memory into the CPU's memory, which can | - | ||||||||||||
369 | * be quite costly. For "live" preview, use \l {QtQuick::Item::layer.enabled} {layers} | - | ||||||||||||
370 | * or ShaderEffectSource. | - | ||||||||||||
371 | */ | - | ||||||||||||
372 | - | |||||||||||||
373 | /*! | - | ||||||||||||
374 | * \internal | - | ||||||||||||
375 | * Only visible from QML. | - | ||||||||||||
376 | */ | - | ||||||||||||
377 | bool QQuickItem::grabToImage(const QJSValue &callback, const QSize &targetSize) | - | ||||||||||||
378 | { | - | ||||||||||||
379 | QQmlEngine *engine = qmlEngine(this); | - | ||||||||||||
380 | if (!engine) {
| 0 | ||||||||||||
381 | qmlWarning(this) << "grabToImage: item has no QML engine"; | - | ||||||||||||
382 | return false; never executed: return false; | 0 | ||||||||||||
383 | } | - | ||||||||||||
384 | - | |||||||||||||
385 | if (!callback.isCallable()) {
| 0 | ||||||||||||
386 | qmlWarning(this) << "grabToImage: 'callback' is not a function"; | - | ||||||||||||
387 | return false; never executed: return false; | 0 | ||||||||||||
388 | } | - | ||||||||||||
389 | - | |||||||||||||
390 | QSize size = targetSize; | - | ||||||||||||
391 | if (size.isEmpty())
| 0 | ||||||||||||
392 | size = QSize(width(), height()); never executed: size = QSize(width(), height()); | 0 | ||||||||||||
393 | - | |||||||||||||
394 | if (size.width() < 1 || size.height() < 1) {
| 0 | ||||||||||||
395 | qmlWarning(this) << "grabToImage: item has invalid dimensions"; | - | ||||||||||||
396 | return false; never executed: return false; | 0 | ||||||||||||
397 | } | - | ||||||||||||
398 | - | |||||||||||||
399 | if (!window()) {
| 0 | ||||||||||||
400 | qmlWarning(this) << "grabToImage: item is not attached to a window"; | - | ||||||||||||
401 | return false; never executed: return false; | 0 | ||||||||||||
402 | } | - | ||||||||||||
403 | - | |||||||||||||
404 | QQuickItemGrabResult *result = QQuickItemGrabResultPrivate::create(this, size); | - | ||||||||||||
405 | if (!result)
| 0 | ||||||||||||
406 | return false; never executed: return false; | 0 | ||||||||||||
407 | - | |||||||||||||
408 | connect(window(), &QQuickWindow::beforeSynchronizing, result, &QQuickItemGrabResult::setup, Qt::DirectConnection); | - | ||||||||||||
409 | connect(window(), &QQuickWindow::afterRendering, result, &QQuickItemGrabResult::render, Qt::DirectConnection); | - | ||||||||||||
410 | - | |||||||||||||
411 | QQuickItemGrabResultPrivate *d = result->d_func(); | - | ||||||||||||
412 | d->qmlEngine = engine; | - | ||||||||||||
413 | d->callback = callback; | - | ||||||||||||
414 | return true; never executed: return true; | 0 | ||||||||||||
415 | } | - | ||||||||||||
416 | - | |||||||||||||
417 | QT_END_NAMESPACE | - | ||||||||||||
418 | - | |||||||||||||
419 | #include "moc_qquickitemgrabresult.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |