2 /****************************************************************************
4 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
41 ****************************************************************************/
44 #include "qsgclipnode_p.h"
46 #include <QtGui/qvector2d.h>
47 #include <QtCore/qmath.h>
49 QSGDefaultClipNode::QSGDefaultClipNode(const QRectF &rect)
52 , m_dirty_geometry(true)
53 , m_geometry(QSGGeometry::defaultAttributes_Point2D(), 0)
55 setGeometry(&m_geometry);
56 setIsRectangular(true);
59 void QSGDefaultClipNode::setRect(const QRectF &rect)
62 m_dirty_geometry = true;
65 void QSGDefaultClipNode::setRadius(qreal radius)
68 m_dirty_geometry = true;
69 setIsRectangular(radius == 0);
72 void QSGDefaultClipNode::update()
74 if (m_dirty_geometry) {
76 m_dirty_geometry = false;
80 void QSGDefaultClipNode::updateGeometry()
82 QSGGeometry *g = geometry();
84 if (qFuzzyIsNull(m_radius)) {
86 QSGGeometry::updateRectGeometry(g, m_rect);
91 // Radius should never exceeds half of the width or half of the height
92 qreal radius = qMin(qMin(m_rect.width() / 2, m_rect.height() / 2), m_radius);
94 rect.adjust(radius, radius, -radius, -radius);
96 int segments = qMin(30, qCeil(radius)); // Number of segments per corner.
98 g->allocate((segments + 1) * 2);
100 QVector2D *vertices = (QVector2D *)g->vertexData();
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(angle);
107 qreal c = qFastCos(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.
112 vertices[vertexCount++] = QVector2D(rx, y);
113 vertices[vertexCount++] = QVector2D(lx, y);
117 markDirty(DirtyGeometry);