Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickclipnode.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// Qt-Security score:significant reason:default
4
5
7
8#include <QtGui/qvector2d.h>
9#include <QtCore/qmath.h>
10
12
13QQuickDefaultClipNode::QQuickDefaultClipNode(const QRectF &rect)
14 : m_rect(rect)
15 , m_radius(0)
16 , m_dirty_geometry(true)
17 , m_geometry(QSGGeometry::defaultAttributes_Point2D(), 0)
18{
19 Q_UNUSED(m_reserved);
20 setGeometry(&m_geometry);
21 setIsRectangular(true);
22}
23
24void QQuickDefaultClipNode::setRect(const QRectF &rect)
25{
26 m_rect = rect;
27 m_dirty_geometry = true;
28}
29
30void QQuickDefaultClipNode::setRadius(qreal radius)
31{
32 m_radius = radius;
33 m_dirty_geometry = true;
34 setIsRectangular(radius == 0);
35}
36
37void QQuickDefaultClipNode::update()
38{
39 if (m_dirty_geometry) {
40 updateGeometry();
41 m_dirty_geometry = false;
42 }
43}
44
45void QQuickDefaultClipNode::updateGeometry()
46{
47 QSGGeometry *g = geometry();
48
49 if (qFuzzyIsNull(m_radius)) {
50 g->allocate(4);
51 QSGGeometry::updateRectGeometry(g, m_rect);
52
53 } else {
54 int vertexCount = 0;
55
56 // Radius should never exceeds half of the width or half of the height
57 qreal radius = qMin(qMin(m_rect.width() / 2, m_rect.height() / 2), m_radius);
58 QRectF rect = m_rect;
59 rect.adjust(radius, radius, -radius, -radius);
60
61 int segments = qMin(30, qCeil(radius)); // Number of segments per corner.
62
63 g->allocate((segments + 1) * 4);
64
65 QVector2D *vertices = (QVector2D *)g->vertexData();
66
67 for (int part = 0; part < 2; ++part) {
68 for (int i = 0; i <= segments; ++i) {
69 //### Should change to calculate sin/cos only once.
70 qreal angle = qreal(0.5 * M_PI) * (part + i / qreal(segments));
71 qreal s = qFastSin(angle);
72 qreal c = qFastCos(angle);
73 qreal y = (part ? rect.bottom() : rect.top()) - radius * c; // current inner y-coordinate.
74 qreal lx = rect.left() - radius * s; // current inner left x-coordinate.
75 qreal rx = rect.right() + radius * s; // current inner right x-coordinate.
76
77 vertices[vertexCount++] = QVector2D(rx, y);
78 vertices[vertexCount++] = QVector2D(lx, y);
79 }
80 }
81
82 }
83#ifdef QSG_RUNTIME_DESCRIPTION
84#ifndef QT_NO_DEBUG_STREAM
85 QString desc;
86 {
87 QDebug dbg(&desc);
88 dbg << m_rect;
89 if (!qFuzzyIsNull(m_radius))
90 dbg << "radius" << m_radius;
91 }
92 qsgnode_set_description(this, desc);
93#endif
94#endif
95 setClipRect(m_rect);
96 markDirty(DirtyGeometry);
97}
98
99QT_END_NAMESPACE