1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
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 | |
41 | #include "qquickclipnode_p.h" |
42 | |
43 | #include <QtGui/qvector2d.h> |
44 | #include <QtCore/qmath.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | QQuickDefaultClipNode::QQuickDefaultClipNode(const QRectF &rect) |
49 | : m_rect(rect) |
50 | , m_radius(0) |
51 | , m_dirty_geometry(true) |
52 | , m_geometry(QSGGeometry::defaultAttributes_Point2D(), 0) |
53 | { |
54 | Q_UNUSED(m_reserved); |
55 | setGeometry(&m_geometry); |
56 | setIsRectangular(true); |
57 | } |
58 | |
59 | void QQuickDefaultClipNode::setRect(const QRectF &rect) |
60 | { |
61 | m_rect = rect; |
62 | m_dirty_geometry = true; |
63 | } |
64 | |
65 | void QQuickDefaultClipNode::setRadius(qreal radius) |
66 | { |
67 | m_radius = radius; |
68 | m_dirty_geometry = true; |
69 | setIsRectangular(radius == 0); |
70 | } |
71 | |
72 | void QQuickDefaultClipNode::update() |
73 | { |
74 | if (m_dirty_geometry) { |
75 | updateGeometry(); |
76 | m_dirty_geometry = false; |
77 | } |
78 | } |
79 | |
80 | void QQuickDefaultClipNode::updateGeometry() |
81 | { |
82 | QSGGeometry *g = geometry(); |
83 | |
84 | if (qFuzzyIsNull(d: m_radius)) { |
85 | g->allocate(vertexCount: 4); |
86 | QSGGeometry::updateRectGeometry(g, rect: m_rect); |
87 | |
88 | } else { |
89 | int vertexCount = 0; |
90 | |
91 | // Radius should never exceeds half of the width or half of the height |
92 | qreal radius = qMin(a: qMin(a: m_rect.width() / 2, b: m_rect.height() / 2), b: m_radius); |
93 | QRectF rect = m_rect; |
94 | rect.adjust(xp1: radius, yp1: radius, xp2: -radius, yp2: -radius); |
95 | |
96 | int segments = qMin(a: 30, b: qCeil(v: radius)); // Number of segments per corner. |
97 | |
98 | g->allocate(vertexCount: (segments + 1) * 4); |
99 | |
100 | QVector2D *vertices = (QVector2D *)g->vertexData(); |
101 | |
102 | for (int part = 0; part < 2; ++part) { |
103 | for (int i = 0; i <= segments; ++i) { |
104 | //### Should change to calculate sin/cos only once. |
105 | qreal angle = qreal(0.5 * M_PI) * (part + i / qreal(segments)); |
106 | qreal s = qFastSin(x: angle); |
107 | qreal c = qFastCos(x: angle); |
108 | qreal y = (part ? rect.bottom() : rect.top()) - radius * c; // current inner y-coordinate. |
109 | qreal lx = rect.left() - radius * s; // current inner left x-coordinate. |
110 | qreal rx = rect.right() + radius * s; // current inner right x-coordinate. |
111 | |
112 | vertices[vertexCount++] = QVector2D(rx, y); |
113 | vertices[vertexCount++] = QVector2D(lx, y); |
114 | } |
115 | } |
116 | |
117 | } |
118 | setClipRect(m_rect); |
119 | markDirty(bits: DirtyGeometry); |
120 | } |
121 | |
122 | QT_END_NAMESPACE |
123 | |