Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickpainteditem.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickpainteditem.h"
5#include <private/qquickpainteditem_p.h>
6
7#include <QtQuick/private/qsgdefaultpainternode_p.h>
8#include <QtQuick/private/qsgcontext_p.h>
9#include <private/qsgadaptationlayer_p.h>
10#include <qsgtextureprovider.h>
11#include <rhi/qrhi.h>
12
13#include <qmath.h>
14
16
18{
19public:
21 QSGTexture *texture() const override { return node ? node->texture() : nullptr; }
23};
24
86 , contentsScale(1.0)
87 , fillColor(Qt::transparent)
88 , renderTarget(QQuickPaintedItem::Image)
89 , opaquePainting(false)
90 , mipmap(false)
91 , textureProvider(nullptr)
92 , node(nullptr)
93{
94}
95
104
113
118{
120 if (d->textureProvider)
122}
123
135{
137
138 if (rect.isNull() && !d->dirtyRect.isNull())
139 d->dirtyRect = contentsBoundingRect().toAlignedRect();
140 else
141 d->dirtyRect |= (contentsBoundingRect() & rect).toAlignedRect();
143}
144
153{
154 Q_D(const QQuickPaintedItem);
155 return d->opaquePainting;
156}
157
169{
171
172 if (d->opaquePainting == opaque)
173 return;
174
175 d->opaquePainting = opaque;
177}
178
179//### Qt7: remove the aa functions; they shadow the QQuickItem property
191
203
212{
213 Q_D(const QQuickPaintedItem);
214 return d->mipmap;
215}
216
228{
230
231 if (d->mipmap == enable)
232 return;
233
234 d->mipmap = enable;
235 update();
236}
237
245QQuickPaintedItem::PerformanceHints QQuickPaintedItem::performanceHints() const
246{
247 Q_D(const QQuickPaintedItem);
248 return d->performanceHints;
249}
250
260{
262 PerformanceHints oldHints = d->performanceHints;
263 if (enabled)
264 d->performanceHints |= hint;
265 else
266 d->performanceHints &= ~hint;
267 if (oldHints != d->performanceHints)
268 update();
269}
270
278void QQuickPaintedItem::setPerformanceHints(QQuickPaintedItem::PerformanceHints hints)
279{
281 if (d->performanceHints == hints)
282 return;
283 d->performanceHints = hints;
284 update();
285}
286
288{
289 Q_D(const QQuickPaintedItem);
290 return d->textureSize;
291}
292
309{
311 if (d->textureSize == size)
312 return;
313 d->textureSize = size;
315}
316
326{
327 Q_D(const QQuickPaintedItem);
328
329 qreal w = d->width;
330 QSizeF sz = d->contentsSize * d->contentsScale;
331 if (w < sz.width())
332 w = sz.width();
333 qreal h = d->height;
334 if (h < sz.height())
335 h = sz.height();
336
337 return QRectF(0, 0, w, h);
338}
339
351{
352 Q_D(const QQuickPaintedItem);
353 return d->contentsSize;
354}
355
357{
359
360 if (d->contentsSize == size)
361 return;
362
363 d->contentsSize = size;
364 update();
365
367}
368
377
389{
390 Q_D(const QQuickPaintedItem);
391 return d->contentsScale;
392}
393
395{
397
398 if (d->contentsScale == scale)
399 return;
400
401 d->contentsScale = scale;
402 update();
403
405}
406
418{
419 Q_D(const QQuickPaintedItem);
420 return d->fillColor;
421}
422
424{
426
427 if (d->fillColor == c)
428 return;
429
430 d->fillColor = c;
431 update();
432
434}
435
453{
454 Q_D(const QQuickPaintedItem);
455 return d->renderTarget;
456}
457
459{
461
462 if (d->renderTarget == target)
463 return;
464
465 d->renderTarget = target;
466 update();
467
469}
470
503{
504 Q_UNUSED(data);
506
507 if (width() <= 0 || height() <= 0) {
508 delete oldNode;
509 if (d->textureProvider) {
510 d->textureProvider->node = nullptr;
511 d->textureProvider->fireTextureChanged();
512 }
513 return nullptr;
514 }
515
516 QSGPainterNode *node = static_cast<QSGPainterNode *>(oldNode);
517 if (!node) {
518 node = d->sceneGraphContext()->createPainterNode(this);
519 d->node = node;
520 }
521
522 bool hasTextureSize = d->textureSize.width() > 0 && d->textureSize.height() > 0;
523
524 // Use the compat mode if any of the compat things are set and
525 // textureSize is 0x0.
526 if (!hasTextureSize
527 && (d->contentsScale != 1
528 || (d->contentsSize.width() > 0 && d->contentsSize.height() > 0))) {
530 node->setContentsScale(d->contentsScale);
531 QSize size = QSize(qRound(br.width()), qRound(br.height()));
532 node->setSize(size);
533 node->setTextureSize(size);
534 } else {
535 // The default, use textureSize or item's size * device pixel ratio
536 node->setContentsScale(1);
537 QSize itemSize(qRound(width()), qRound(height()));
538 node->setSize(itemSize);
539 QSize textureSize = (hasTextureSize ? d->textureSize : itemSize)
540 * window()->effectiveDevicePixelRatio();
542 }
543
544 node->setPreferredRenderTarget(d->renderTarget);
545 node->setFastFBOResizing(d->performanceHints & FastFBOResizing);
547 node->setLinearFiltering(d->smooth);
548 node->setMipmapping(d->mipmap);
549 node->setOpaquePainting(d->opaquePainting);
550 node->setFillColor(d->fillColor);
551 node->setDirty(d->dirtyRect);
552 node->update();
553
554 d->dirtyRect = QRect();
555
556 if (d->textureProvider) {
557 d->textureProvider->node = node;
558 d->textureProvider->fireTextureChanged();
559 }
560
561 return node;
562}
563
568{
570 if (d->textureProvider) {
572 d->textureProvider = nullptr;
573 }
574 d->node = nullptr; // Managed by the scene graph, just clear the pointer.
575}
576
577void QQuickPaintedItem::invalidateSceneGraph()
578{
580 delete d->textureProvider;
581 d->textureProvider = nullptr;
582 d->node = nullptr; // Managed by the scene graph, just clear the pointer
583}
584
589{
590 return true;
591}
592
597{
598 // When Item::layer::enabled == true, QQuickItem will be a texture
599 // provider. In this case we should prefer to return the layer rather
600 // than the image itself. The layer will include any children and any
601 // the image's wrap and fill mode.
604
605 Q_D(const QQuickPaintedItem);
606 QQuickWindow *w = window();
607 if (!w || !w->isSceneGraphInitialized() || QThread::currentThread() != d->sceneGraphContext()->thread()) {
608 qWarning("QQuickPaintedItem::textureProvider: can only be queried on the rendering thread of an exposed window");
609 return nullptr;
610 }
611 if (!d->textureProvider)
612 d->textureProvider = new QQuickPaintedItemTextureProvider();
613 d->textureProvider->node = d->node;
614 return d->textureProvider;
615}
616
617
627
629
630#include "moc_qquickpainteditem.cpp"
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void setAntialiasing(bool)
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
virtual QSGTextureProvider * textureProvider() const
Returns the texture provider for an item.
QSizeF size() const
QQuickWindow * window() const
Returns the window in which this item is rendered.
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
virtual void itemChange(ItemChange, const ItemChangeData &)
Called when change occurs for this item.
bool antialiasing
\qmlproperty bool QtQuick::Item::antialiasing
Definition qquickitem.h:113
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
qreal scale
\qmlproperty real QtQuick::Item::scale This property holds the scale factor for this item.
Definition qquickitem.h:107
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
Definition qquickitem.h:144
@ ItemDevicePixelRatioHasChanged
Definition qquickitem.h:154
void update()
Schedules a call to updatePaintNode() for this item.
virtual bool isTextureProvider() const
Returns true if this item is a texture provider.
QSGTexture * texture() const override
Returns a pointer to the texture object.
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
QSize contentsSize
Obsolete method for setting the contents size.
void contentsScaleChanged()
void setMipmap(bool enable)
If enable is true, mipmapping is enabled on the associated texture.
void setPerformanceHints(PerformanceHints hints)
Sets the performance hints to hints.
void setTextureSize(const QSize &size)
QColor fillColor
The item's background fill color.
void itemChange(ItemChange, const ItemChangeData &) override
\reimp
QSGNode * updatePaintNode(QSGNode *, UpdatePaintNodeData *) override
\reimp
void setRenderTarget(RenderTarget target)
void setFillColor(const QColor &)
RenderTarget renderTarget
The item's render target.
PerformanceHint
This enum describes flags that you can enable to improve rendering performance in QQuickPaintedItem.
QSGTextureProvider * textureProvider() const override
\reimp
bool mipmap() const
Returns true if mipmaps are enabled; otherwise, false is returned.
QQuickPaintedItem(QQuickItem *parent=nullptr)
Constructs a QQuickPaintedItem with the given parent item.
bool isTextureProvider() const override
\reimp
void releaseResources() override
\reimp
QRectF contentsBoundingRect() const
PerformanceHints performanceHints() const
Returns the performance hints.
void setPerformanceHint(PerformanceHint hint, bool enabled=true)
Sets the given performance hint on the item if enabled is true; otherwise clears the performance hint...
QSize textureSize
Defines the size of the texture.
void setContentsSize(const QSize &)
~QQuickPaintedItem() override
Destroys the QQuickPaintedItem.
void setAntialiasing(bool enable)
If enable is true, antialiased painting is enabled.
void renderTargetChanged()
bool opaquePainting() const
Returns true if this item is opaque; otherwise, false is returned.
bool antialiasing() const
Returns true if antialiased painting is enabled; otherwise, false is returned.
void setOpaquePainting(bool opaque)
If opaque is true, the item is opaque; otherwise, it is considered as translucent.
void textureSizeChanged()
RenderTarget
This enum describes QQuickPaintedItem's render targets.
void contentsSizeChanged()
qreal contentsScale
Obsolete method for scaling the contents.
static void schedule(QQuickWindow *window, QObject *object)
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtCore\reentrant
Definition qrect.h:484
QRect toAlignedRect() const noexcept
Definition qrect.cpp:2338
constexpr qreal height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:732
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:729
\inmodule QtCore\reentrant
Definition qrect.h:30
\group qtquick-scenegraph-nodes \title Qt Quick Scene Graph Node classes
Definition qsgnode.h:37
virtual QSGTexture * texture() const =0
virtual void setTextureSize(const QSize &size)=0
virtual void setSmoothPainting(bool s)=0
virtual void update()=0
virtual void setPreferredRenderTarget(QQuickPaintedItem::RenderTarget target)=0
virtual void setLinearFiltering(bool linearFiltering)=0
virtual void setFastFBOResizing(bool dynamic)=0
virtual void setDirty(const QRect &dirtyRect=QRect())=0
virtual void setFillColor(const QColor &c)=0
virtual void setSize(const QSize &size)=0
virtual void setContentsScale(qreal s)=0
virtual void setOpaquePainting(bool opaque)=0
virtual void setMipmapping(bool mipmapping)=0
The QSGTextureProvider class encapsulates texture based entities in QML.
void textureChanged()
This signal is emitted when the texture changes.
\inmodule QtQuick
Definition qsgtexture.h:20
\inmodule QtCore
Definition qsize.h:208
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:332
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:335
\inmodule QtCore
Definition qsize.h:25
static QThread * currentThread()
Definition qthread.cpp:1039
rect
[4]
Combined button and popup list for selecting options.
Definition qcompare.h:63
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
#define qWarning
Definition qlogging.h:166
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLenum target
GLboolean enable
GLfloat GLfloat GLfloat GLfloat h
const GLubyte * c
GLenum GLenum GLenum GLenum GLenum scale
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define emit
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QObject::connect nullptr
\inmodule QtQuick
Definition qquickitem.h:159