Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgclipnode.cpp
1
2 /****************************************************************************
3 **
4 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 **
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 **
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
15 ** this package.
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 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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43
44 #include "qsgclipnode_p.h"
45
46 #include <QtGui/qvector2d.h>
47 #include <QtCore/qmath.h>
48
49 QSGDefaultClipNode::QSGDefaultClipNode(const QRectF &rect)
50     : m_rect(rect)
51     , m_radius(0)
52     , m_dirty_geometry(true)
53     , m_geometry(QSGGeometry::defaultAttributes_Point2D(), 0)
54 {
55     setGeometry(&m_geometry);
56     setIsRectangular(true);
57 }
58
59 void QSGDefaultClipNode::setRect(const QRectF &rect)
60 {
61     m_rect = rect;
62     m_dirty_geometry = true;
63 }
64
65 void QSGDefaultClipNode::setRadius(qreal radius)
66 {
67     m_radius = radius;
68     m_dirty_geometry = true;
69     setIsRectangular(radius == 0);
70 }
71
72 void QSGDefaultClipNode::update()
73 {
74     if (m_dirty_geometry) {
75         updateGeometry();
76         m_dirty_geometry = false;
77     }
78 }
79
80 void QSGDefaultClipNode::updateGeometry()
81 {
82     QSGGeometry *g = geometry();
83
84     if (qFuzzyIsNull(m_radius)) {
85         g->allocate(4);
86         QSGGeometry::updateRectGeometry(g, 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(qMin(m_rect.width() / 2, m_rect.height() / 2), m_radius);
93         QRectF rect = m_rect;
94         rect.adjust(radius, radius, -radius, -radius);
95
96         int segments = qMin(30, qCeil(radius)); // Number of segments per corner.
97
98         g->allocate((segments + 1) * 2);
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(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.
111
112                 vertices[vertexCount++] = QVector2D(rx, y);
113                 vertices[vertexCount++] = QVector2D(lx, y);
114             }
115         }
116
117         markDirty(DirtyGeometry);
118     }
119     setClipRect(m_rect);
120 }
121