1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qfxblendedimage.h"
#include <QtDeclarative/qmlcontext.h>
#if defined(QFX_RENDER_OPENGL2)
#include <glbasicshaders.h>
#endif
QT_BEGIN_NAMESPACE
/*!
\qmlclass BlendedImage
\brief The BlendedImage elements blends two different images depending on a blend ratio.
This element can be used to simulate blur on slow devices by setting secondaryUrl with
a pre-rendered blurred version of primaryUrl.
Note that this class will only work under OpenGL. On the software canvas it will display
only the primary image unless the blend is > 0.75, in which case it will display only the
secondary image.
*/
/*!
\internal
\class QFxBlendedImage
\brief The QFxBlendedImage blends two different images depending on a blend ratio.
This class can be used to simulate blur on slow devices by setting secondaryUrl with
a pre-rendered blurred version of primaryUrl.
Note that this class will only work under OpenGL. On the software canvas it will display
only the primary image unless the blend is > 0.75, in which case it will display only the
secondary image.
*/
QFxBlendedImage::QFxBlendedImage(QFxItem *parent)
: QFxItem(parent), _blend(0), _smooth(false), dirty(false)
{
#if defined(QFX_RENDER_OPENGL2)
setOptions(HasContents);
#endif
}
/*!
\qmlproperty string BlendedImage::primaryUrl
The URL of the first image to be displayed in this item.
*/
QString QFxBlendedImage::primaryUrl() const
{
return primSrc;
}
void QFxBlendedImage::primaryLoaded()
{
primPix = QFxPixmap(primUrl);
dirty = true;
update();
}
void QFxBlendedImage::setPrimaryUrl(const QString &url)
{
if (primSrc == url)
return;
if (!primSrc.isEmpty())
QFxPixmap::cancelGet(primUrl,this,SLOT(primaryLoaded()));
primSrc = url;
primUrl = qmlContext(this)->resolvedUrl(url);
if (!primSrc.isEmpty())
QFxPixmap::get(qmlEngine(this), primUrl,this,SLOT(primaryLoaded()));
}
/*!
\qmlproperty string BlendedImage::secondaryUrl
The URL of the second image to be displayed in this item.
*/
QString QFxBlendedImage::secondaryUrl() const
{
return secSrc;
}
void QFxBlendedImage::secondaryLoaded()
{
secPix = QFxPixmap(secUrl);
dirty = true;
update();
}
void QFxBlendedImage::setSecondaryUrl(const QString &url)
{
if (secSrc == url)
return;
if (!secSrc.isEmpty())
QFxPixmap::cancelGet(secUrl,this,SLOT(secondaryLoaded()));
secSrc = url;
secUrl = qmlContext(this)->resolvedUrl(url);
if (!secSrc.isEmpty())
QFxPixmap::get(qmlEngine(this), secUrl,this,SLOT(secondaryLoaded()));
}
/*!
\qmlproperty real BlendedImage::blend
The ratio used to blend the two images.
If blend has a value of 0, only the first image will be displayed.
If blend has a value of 1, only the second image will be displayed.
*/
qreal QFxBlendedImage::blend() const
{
return _blend;
}
void QFxBlendedImage::setBlend(qreal b)
{
_blend = b;
update();
}
/*!
\qmlproperty bool BlendedImage::smooth
Set this property if you want the image to be smoothly filtered when scaled or
transformed. Smooth filtering gives better visual quality, but is slower. If
the BlendedImage is displayed at its natural size, this property has no visual or
performance effect.
\note Generally scaling artifacts are only visible if the image is stationary on
the screen. A common pattern when animating an image is to disable smooth
filtering at the beginning of the animation and reenable it at the conclusion.
*/
bool QFxBlendedImage::smoothTransform() const
{
return _smooth;
}
void QFxBlendedImage::setSmoothTransform(bool s)
{
if (_smooth == s)
return;
_smooth = s;
update();
}
#if defined(QFX_RENDER_QPAINTER)
void QFxBlendedImage::paintContents(QPainter &p)
{
if (primSrc.isNull() && secSrc.isNull())
return;
if (_smooth) {
p.save();
p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, _smooth);
}
if (_blend < 0.75)
p.drawImage(0, 0, primPix);
else
p.drawImage(0, 0, secPix);
if (_smooth) {
p.restore();
}
}
#elif defined(QFX_RENDER_OPENGL2)
void QFxBlendedImage::paintGLContents(GLPainter &p)
{
static DualTextureBlendShader *shader = 0;
if (!shader)
shader = new DualTextureBlendShader();
if (dirty) {
prim.clear();
sec.clear();
prim.setImage(primPix);
sec.setImage(secPix);
dirty = false;
}
if (prim.isNull() || sec.isNull()) {
return;
}
GLfloat vertices[8];
GLfloat texVertices[8];
float widthV = width();
float heightV = height();
if (!widthV)
widthV = qMax(primPix.width(), secPix.width());
if (!heightV)
heightV = qMax(primPix.height(), secPix.height());
vertices[0] = 0; vertices[1] = heightV;
vertices[2] = widthV; vertices[3] = heightV;
vertices[4] = 0; vertices[5] = 0;
vertices[6] = widthV; vertices[7] = 0;
texVertices[0] = 0; texVertices[1] = 0;
texVertices[2] = 1; texVertices[3] = 0;
texVertices[4] = 0; texVertices[5] = 1;
texVertices[6] = 1; texVertices[7] = 1;
if (_blend == 0 || _blend == 1) {
QGLShaderProgram *tshader = p.useTextureShader();
GLTexture *tex = 0;
if (_blend == 0)
tex = &prim;
else
tex = &sec;
tshader->setAttributeArray(SingleTextureShader::Vertices, vertices, 2);
tshader->setAttributeArray(SingleTextureShader::TextureCoords, texVertices, 2);
glBindTexture(GL_TEXTURE_2D, tex->texture());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
tshader->disableAttributeArray(SingleTextureShader::Vertices);
tshader->disableAttributeArray(SingleTextureShader::TextureCoords);
} else {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, prim.texture());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, sec.texture());
shader->enable();
shader->setOpacity(1);
qreal b = _blend;
if (b > 1) b = 1;
else if (b < 0) b = 0;
shader->setBlend(b);
shader->setTransform(p.activeTransform);
shader->setAttributeArray(DualTextureBlendShader::Vertices, vertices, 2);
shader->setAttributeArray(DualTextureBlendShader::TextureCoords, texVertices, 2);
shader->setAttributeArray(DualTextureBlendShader::BlendTextureCoords, texVertices, 2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
shader->disableAttributeArray(DualTextureBlendShader::Vertices);
shader->disableAttributeArray(DualTextureBlendShader::TextureCoords);
shader->disableAttributeArray(DualTextureBlendShader::BlendTextureCoords);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
}
}
#endif
QML_DEFINE_TYPE(QFxBlendedImage,BlendedImage);
QT_END_NAMESPACE
|