0054f92c3d2f2205b5dcc429eadca84732bd4946
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / util / qsgsimpletexturenode.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include "qsgsimpletexturenode.h"
44
45 QT_BEGIN_NAMESPACE
46
47 static void qsgsimpletexturenode_update(QSGGeometry *g,
48                                         QSGTexture *texture,
49                                         const QRectF &rect)
50 {
51     if (!texture)
52         return;
53
54     QSize ts = texture->textureSize();
55     QRectF sourceRect(0, 0, ts.width(), ts.height());
56     QSGGeometry::updateTexturedRectGeometry(g, rect, texture->convertToNormalizedSourceRect(sourceRect));
57 }
58
59 /*!
60   \class QSGSimpleTextureNode
61   \brief The QSGSimpleTextureNode class is provided for convenience to easily draw
62   textured content using the QML scene graph.
63
64   \inmodule QtQuick
65
66   \warning The simple texture node class must have a texture before being
67   added to the scene graph to be rendered.
68 */
69
70 /*!
71     Constructs a new simple texture node
72  */
73 QSGSimpleTextureNode::QSGSimpleTextureNode()
74     : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
75 {
76     setGeometry(&m_geometry);
77     setMaterial(&m_material);
78     setOpaqueMaterial(&m_opaque_material);
79 }
80
81 /*!
82     Sets the filtering to be used for this texture node to \a filtering.
83
84     For smooth scaling, use QSGTexture::Linear; for normal scaling, use
85     QSGTexture::Nearest.
86  */
87 void QSGSimpleTextureNode::setFiltering(QSGTexture::Filtering filtering)
88 {
89     if (m_material.filtering() == filtering)
90         return;
91
92     m_material.setFiltering(filtering);
93     m_opaque_material.setFiltering(filtering);
94     markDirty(DirtyMaterial);
95 }
96
97
98 /*!
99     Returns the filtering currently set on this texture node
100  */
101 QSGTexture::Filtering QSGSimpleTextureNode::filtering() const
102 {
103     return m_material.filtering();
104 }
105
106
107 /*!
108     Sets the target rect of this texture node to \a r
109  */
110 void QSGSimpleTextureNode::setRect(const QRectF &r)
111 {
112     if (m_rect == r)
113         return;
114     m_rect = r;
115     qsgsimpletexturenode_update(&m_geometry, texture(), m_rect);
116     markDirty(DirtyGeometry);
117 }
118
119
120 /*!
121     Returns the target rect of this texture node.
122  */
123 QRectF QSGSimpleTextureNode::rect() const
124 {
125     return m_rect;
126 }
127
128 /*!
129     Sets the texture of this texture node to \a texture.
130
131     \warning A texture node must have a texture before being added
132     to the scenegraph to be rendered.
133  */
134 void QSGSimpleTextureNode::setTexture(QSGTexture *texture)
135 {
136     if (m_material.texture() == texture)
137         return;
138     m_material.setTexture(texture);
139     m_opaque_material.setTexture(texture);
140     qsgsimpletexturenode_update(&m_geometry, texture, m_rect);
141     markDirty(DirtyMaterial);
142 }
143
144
145
146 /*!
147     Returns the texture for this texture node
148  */
149 QSGTexture *QSGSimpleTextureNode::texture() const
150 {
151     return m_material.texture();
152 }
153
154 QT_END_NAMESPACE