Qt 6.x
The Qt SDK
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 , antialiasing(false)
91 , mipmap(false)
92 , textureProvider(nullptr)
93 , node(nullptr)
94{
95}
96
102{
104}
105
110 : QQuickItem(dd, parent)
111{
113}
114
119{
121 if (d->textureProvider)
123}
124
136{
138
139 if (rect.isNull() && !d->dirtyRect.isNull())
140 d->dirtyRect = contentsBoundingRect().toAlignedRect();
141 else
142 d->dirtyRect |= (contentsBoundingRect() & rect).toAlignedRect();
144}
145
154{
155 Q_D(const QQuickPaintedItem);
156 return d->opaquePainting;
157}
158
170{
172
173 if (d->opaquePainting == opaque)
174 return;
175
176 d->opaquePainting = opaque;
178}
179
188{
189 Q_D(const QQuickPaintedItem);
190 return d->antialiasing;
191}
192
201{
203
204 if (d->antialiasing == enable)
205 return;
206
207 d->antialiasing = enable;
208 update();
209}
210
219{
220 Q_D(const QQuickPaintedItem);
221 return d->mipmap;
222}
223
235{
237
238 if (d->mipmap == enable)
239 return;
240
241 d->mipmap = enable;
242 update();
243}
244
252QQuickPaintedItem::PerformanceHints QQuickPaintedItem::performanceHints() const
253{
254 Q_D(const QQuickPaintedItem);
255 return d->performanceHints;
256}
257
267{
269 PerformanceHints oldHints = d->performanceHints;
270 if (enabled)
271 d->performanceHints |= hint;
272 else
273 d->performanceHints &= ~hint;
274 if (oldHints != d->performanceHints)
275 update();
276}
277
285void QQuickPaintedItem::setPerformanceHints(QQuickPaintedItem::PerformanceHints hints)
286{
288 if (d->performanceHints == hints)
289 return;
290 d->performanceHints = hints;
291 update();
292}
293
295{
296 Q_D(const QQuickPaintedItem);
297 return d->textureSize;
298}
299
316{
318 if (d->textureSize == size)
319 return;
320 d->textureSize = size;
322}
323
333{
334 Q_D(const QQuickPaintedItem);
335
336 qreal w = d->width;
337 QSizeF sz = d->contentsSize * d->contentsScale;
338 if (w < sz.width())
339 w = sz.width();
340 qreal h = d->height;
341 if (h < sz.height())
342 h = sz.height();
343
344 return QRectF(0, 0, w, h);
345}
346
358{
359 Q_D(const QQuickPaintedItem);
360 return d->contentsSize;
361}
362
364{
366
367 if (d->contentsSize == size)
368 return;
369
370 d->contentsSize = size;
371 update();
372
374}
375
381{
383}
384
396{
397 Q_D(const QQuickPaintedItem);
398 return d->contentsScale;
399}
400
402{
404
405 if (d->contentsScale == scale)
406 return;
407
408 d->contentsScale = scale;
409 update();
410
412}
413
425{
426 Q_D(const QQuickPaintedItem);
427 return d->fillColor;
428}
429
431{
433
434 if (d->fillColor == c)
435 return;
436
437 d->fillColor = c;
438 update();
439
441}
442
460{
461 Q_D(const QQuickPaintedItem);
462 return d->renderTarget;
463}
464
466{
468
469 if (d->renderTarget == target)
470 return;
471
472 d->renderTarget = target;
473 update();
474
476}
477
510{
511 Q_UNUSED(data);
513
514 if (width() <= 0 || height() <= 0) {
515 delete oldNode;
516 if (d->textureProvider) {
517 d->textureProvider->node = nullptr;
518 d->textureProvider->fireTextureChanged();
519 }
520 return nullptr;
521 }
522
523 QSGPainterNode *node = static_cast<QSGPainterNode *>(oldNode);
524 if (!node) {
525 node = d->sceneGraphContext()->createPainterNode(this);
526 d->node = node;
527 }
528
529 bool hasTextureSize = d->textureSize.width() > 0 && d->textureSize.height() > 0;
530
531 // Use the compat mode if any of the compat things are set and
532 // textureSize is 0x0.
533 if (!hasTextureSize
534 && (d->contentsScale != 1
535 || (d->contentsSize.width() > 0 && d->contentsSize.height() > 0))) {
537 node->setContentsScale(d->contentsScale);
538 QSize size = QSize(qRound(br.width()), qRound(br.height()));
539 node->setSize(size);
540 node->setTextureSize(size);
541 } else {
542 // The default, use textureSize or item's size * device pixel ratio
543 node->setContentsScale(1);
544 QSize itemSize(qRound(width()), qRound(height()));
545 node->setSize(itemSize);
546 QSize textureSize = (hasTextureSize ? d->textureSize : itemSize)
547 * window()->effectiveDevicePixelRatio();
549 }
550
551 node->setPreferredRenderTarget(d->renderTarget);
552 node->setFastFBOResizing(d->performanceHints & FastFBOResizing);
553 node->setSmoothPainting(d->antialiasing);
554 node->setLinearFiltering(d->smooth);
555 node->setMipmapping(d->mipmap);
556 node->setOpaquePainting(d->opaquePainting);
557 node->setFillColor(d->fillColor);
558 node->setDirty(d->dirtyRect);
559 node->update();
560
561 d->dirtyRect = QRect();
562
563 if (d->textureProvider) {
564 d->textureProvider->node = node;
565 d->textureProvider->fireTextureChanged();
566 }
567
568 return node;
569}
570
575{
577 if (d->textureProvider) {
579 d->textureProvider = nullptr;
580 }
581 d->node = nullptr; // Managed by the scene graph, just clear the pointer.
582}
583
584void QQuickPaintedItem::invalidateSceneGraph()
585{
587 delete d->textureProvider;
588 d->textureProvider = nullptr;
589 d->node = nullptr; // Managed by the scene graph, just clear the pointer
590}
591
596{
597 return true;
598}
599
604{
605 // When Item::layer::enabled == true, QQuickItem will be a texture
606 // provider. In this case we should prefer to return the layer rather
607 // than the image itself. The layer will include any children and any
608 // the image's wrap and fill mode.
611
612 Q_D(const QQuickPaintedItem);
613 QQuickWindow *w = window();
614 if (!w || !w->isSceneGraphInitialized() || QThread::currentThread() != d->sceneGraphContext()->thread()) {
615 qWarning("QQuickPaintedItem::textureProvider: can only be queried on the rendering thread of an exposed window");
616 return nullptr;
617 }
618 if (!d->textureProvider)
619 d->textureProvider = new QQuickPaintedItemTextureProvider();
620 d->textureProvider->node = d->node;
621 return d->textureProvider;
622}
623
624
629{
630 if (change == ItemDevicePixelRatioHasChanged)
631 update();
633}
634
636
637#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:64
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:76
virtual void itemChange(ItemChange, const ItemChangeData &)
Called when change occurs for this item.
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
qreal scale
\qmlproperty real QtQuick::Item::scale This property holds the scale factor for this item.
Definition qquickitem.h:106
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
Definition qquickitem.h:143
@ ItemDevicePixelRatioHasChanged
Definition qquickitem.h:153
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:483
QRect toAlignedRect() const noexcept
Definition qrect.cpp:2330
constexpr qreal height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:718
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:715
\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:207
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:321
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
\inmodule QtCore
Definition qsize.h:25
static QThread * currentThread()
Definition qthread.cpp:966
rect
[4]
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
#define qWarning
Definition qlogging.h:162
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLenum target
GLboolean enable
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
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:92
QObject::connect nullptr
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent
\inmodule QtQuick
Definition qquickitem.h:158