author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <QtGui/qdesktopwidget.h> |
|
45 |
#include <QtGui/qgraphicseffect.h> |
|
46 |
#include <QtGui/qgraphicsview.h> |
|
47 |
#include <QtGui/qgraphicsscene.h> |
|
48 |
#include <QtGui/qgraphicsitem.h> |
|
49 |
#include <QtGui/qstyleoption.h> |
|
50 |
||
51 |
#include "../../shared/util.h" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
#include <private/qgraphicseffect_p.h> |
0 | 53 |
|
54 |
//TESTED_CLASS= |
|
55 |
//TESTED_FILES= |
|
56 |
||
57 |
class tst_QGraphicsEffect : public QObject |
|
58 |
{ |
|
59 |
Q_OBJECT |
|
60 |
public slots: |
|
61 |
void initTestCase(); |
|
62 |
||
63 |
private slots: |
|
64 |
void setEnabled(); |
|
65 |
void source(); |
|
66 |
void boundingRectFor(); |
|
67 |
void boundingRect(); |
|
68 |
void draw(); |
|
69 |
void opacity(); |
|
70 |
void grayscale(); |
|
71 |
void colorize(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
72 |
void drawPixmapItem(); |
0 | 73 |
}; |
74 |
||
75 |
void tst_QGraphicsEffect::initTestCase() |
|
76 |
{} |
|
77 |
||
78 |
class CustomItem : public QGraphicsRectItem |
|
79 |
{ |
|
80 |
public: |
|
81 |
CustomItem(qreal x, qreal y, qreal width, qreal height) |
|
82 |
: QGraphicsRectItem(x, y, width, height), numRepaints(0), |
|
83 |
m_painter(0), m_styleOption(0) |
|
84 |
{} |
|
85 |
||
86 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
87 |
{ |
|
88 |
m_painter = painter; |
|
89 |
m_styleOption = option; |
|
90 |
++numRepaints; |
|
91 |
QGraphicsRectItem::paint(painter, option, widget); |
|
92 |
} |
|
93 |
||
94 |
void reset() |
|
95 |
{ |
|
96 |
numRepaints = 0; |
|
97 |
m_painter = 0; |
|
98 |
m_styleOption = 0; |
|
99 |
} |
|
100 |
||
101 |
int numRepaints; |
|
102 |
QPainter *m_painter; |
|
103 |
const QStyleOption *m_styleOption; |
|
104 |
}; |
|
105 |
||
106 |
class CustomEffect : public QGraphicsEffect |
|
107 |
{ |
|
108 |
public: |
|
109 |
CustomEffect() |
|
110 |
: QGraphicsEffect(), numRepaints(0), m_margin(10), |
|
111 |
doNothingInDraw(false), m_painter(0), m_styleOption(0), m_source(0), m_opacity(1.0) |
|
112 |
{} |
|
113 |
||
114 |
QRectF boundingRectFor(const QRectF &rect) const |
|
115 |
{ return rect.adjusted(-m_margin, -m_margin, m_margin, m_margin); } |
|
116 |
||
117 |
void reset() |
|
118 |
{ |
|
119 |
numRepaints = 0; |
|
120 |
m_sourceChangedFlags = QGraphicsEffect::ChangeFlags(); |
|
121 |
m_painter = 0; |
|
122 |
m_styleOption = 0; |
|
123 |
m_source = 0; |
|
124 |
m_opacity = 1.0; |
|
125 |
} |
|
126 |
||
127 |
void setMargin(int margin) |
|
128 |
{ |
|
129 |
m_margin = margin; |
|
130 |
updateBoundingRect(); |
|
131 |
} |
|
132 |
||
133 |
int margin() const |
|
134 |
{ return m_margin; } |
|
135 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
void draw(QPainter *painter) |
0 | 137 |
{ |
138 |
++numRepaints; |
|
139 |
if (doNothingInDraw) |
|
140 |
return; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
m_source = source(); |
0 | 142 |
m_painter = painter; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
m_styleOption = source()->styleOption(); |
0 | 144 |
m_opacity = painter->opacity(); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
drawSource(painter); |
0 | 146 |
} |
147 |
||
148 |
void sourceChanged(QGraphicsEffect::ChangeFlags flags) |
|
149 |
{ m_sourceChangedFlags |= flags; } |
|
150 |
||
151 |
int numRepaints; |
|
152 |
int m_margin; |
|
153 |
QGraphicsEffect::ChangeFlags m_sourceChangedFlags; |
|
154 |
bool doNothingInDraw; |
|
155 |
QPainter *m_painter; |
|
156 |
const QStyleOption *m_styleOption; |
|
157 |
QGraphicsEffectSource *m_source; |
|
158 |
qreal m_opacity; |
|
159 |
}; |
|
160 |
||
161 |
void tst_QGraphicsEffect::setEnabled() |
|
162 |
{ |
|
163 |
CustomEffect effect; |
|
164 |
QVERIFY(effect.isEnabled()); |
|
165 |
||
166 |
effect.setEnabled(false); |
|
167 |
QVERIFY(!effect.isEnabled()); |
|
168 |
} |
|
169 |
||
170 |
void tst_QGraphicsEffect::source() |
|
171 |
{ |
|
172 |
QPointer<CustomEffect> effect = new CustomEffect; |
|
173 |
QVERIFY(!effect->source()); |
|
174 |
QVERIFY(!effect->m_sourceChangedFlags); |
|
175 |
||
176 |
// Install effect on QGraphicsItem. |
|
177 |
QGraphicsItem *item = new QGraphicsRectItem(0, 0, 10, 10); |
|
178 |
item->setGraphicsEffect(effect); |
|
179 |
QVERIFY(effect->source()); |
|
180 |
QCOMPARE(effect->source()->graphicsItem(), item); |
|
181 |
QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceAttached); |
|
182 |
effect->reset(); |
|
183 |
||
184 |
// Make sure disabling/enabling the effect doesn't change the source. |
|
185 |
effect->setEnabled(false); |
|
186 |
QVERIFY(effect->source()); |
|
187 |
QCOMPARE(effect->source()->graphicsItem(), item); |
|
188 |
QVERIFY(!effect->m_sourceChangedFlags); |
|
189 |
effect->reset(); |
|
190 |
||
191 |
effect->setEnabled(true); |
|
192 |
QVERIFY(effect->source()); |
|
193 |
QCOMPARE(effect->source()->graphicsItem(), item); |
|
194 |
QVERIFY(!effect->m_sourceChangedFlags); |
|
195 |
effect->reset(); |
|
196 |
||
197 |
// Uninstall effect on QGraphicsItem. |
|
198 |
effect->reset(); |
|
199 |
item->setGraphicsEffect(0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
QVERIFY(!effect); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
201 |
effect = new CustomEffect; |
0 | 202 |
|
203 |
// The item takes ownership and should delete the effect when destroyed. |
|
204 |
item->setGraphicsEffect(effect); |
|
205 |
QPointer<QGraphicsEffectSource> source = effect->source(); |
|
206 |
QVERIFY(source); |
|
207 |
QCOMPARE(source->graphicsItem(), item); |
|
208 |
delete item; |
|
209 |
QVERIFY(!effect); |
|
210 |
QVERIFY(!source); |
|
211 |
} |
|
212 |
||
213 |
void tst_QGraphicsEffect::boundingRectFor() |
|
214 |
{ |
|
215 |
CustomEffect effect; |
|
216 |
int margin = effect.margin(); |
|
217 |
const QRectF source(0, 0, 100, 100); |
|
218 |
QCOMPARE(effect.boundingRectFor(source), source.adjusted(-margin, -margin, margin, margin)); |
|
219 |
||
220 |
effect.setMargin(margin = 20); |
|
221 |
QCOMPARE(effect.boundingRectFor(source), source.adjusted(-margin, -margin, margin, margin)); |
|
222 |
} |
|
223 |
||
224 |
void tst_QGraphicsEffect::boundingRect() |
|
225 |
{ |
|
226 |
// No source; empty bounding rect. |
|
227 |
CustomEffect *effect = new CustomEffect; |
|
228 |
QCOMPARE(effect->boundingRect(), QRectF()); |
|
229 |
||
230 |
// Install effect on QGraphicsItem. |
|
231 |
QRectF itemRect(0, 0, 100, 100); |
|
232 |
QGraphicsRectItem *item = new QGraphicsRectItem; |
|
233 |
item->setRect(itemRect); |
|
234 |
item->setGraphicsEffect(effect); |
|
235 |
int margin = effect->margin(); |
|
236 |
QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); |
|
237 |
QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); |
|
238 |
||
239 |
// Make sure disabling/enabling the effect doesn't change the bounding rect. |
|
240 |
effect->setEnabled(false); |
|
241 |
QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); |
|
242 |
QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); |
|
243 |
effect->setEnabled(true); |
|
244 |
QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); |
|
245 |
QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); |
|
246 |
||
247 |
// Change effect margins. |
|
248 |
effect->setMargin(margin = 20); |
|
249 |
QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); |
|
250 |
QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); |
|
251 |
||
252 |
// Uninstall effect on QGraphicsItem. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
QPointer<CustomEffect> ptr = effect; |
0 | 254 |
item->setGraphicsEffect(0); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
255 |
QVERIFY(!ptr); |
0 | 256 |
|
257 |
delete item; |
|
258 |
} |
|
259 |
||
260 |
void tst_QGraphicsEffect::draw() |
|
261 |
{ |
|
262 |
QGraphicsScene scene; |
|
263 |
CustomItem *item = new CustomItem(0, 0, 100, 100); |
|
264 |
scene.addItem(item); |
|
265 |
||
266 |
QGraphicsView view(&scene); |
|
267 |
view.show(); |
|
268 |
QTest::qWaitForWindowShown(&view); |
|
269 |
QTRY_VERIFY(item->numRepaints > 0); |
|
270 |
item->reset(); |
|
271 |
||
272 |
// Make sure installing the effect triggers a repaint. |
|
273 |
CustomEffect *effect = new CustomEffect; |
|
274 |
item->setGraphicsEffect(effect); |
|
275 |
QTest::qWait(50); |
|
276 |
QCOMPARE(effect->numRepaints, 1); |
|
277 |
QCOMPARE(item->numRepaints, 1); |
|
278 |
||
279 |
// Make sure QPainter* and QStyleOptionGraphicsItem* stays persistent |
|
280 |
// during QGraphicsEffect::draw/QGraphicsItem::paint. |
|
281 |
QVERIFY(effect->m_painter); |
|
282 |
QCOMPARE(effect->m_painter, item->m_painter); |
|
283 |
QCOMPARE(effect->m_styleOption, item->m_styleOption); |
|
284 |
// Make sure QGraphicsEffect::source is persistent. |
|
285 |
QCOMPARE(effect->m_source, effect->source()); |
|
286 |
effect->reset(); |
|
287 |
item->reset(); |
|
288 |
||
289 |
// Make sure updating the source triggers a repaint. |
|
290 |
item->update(); |
|
291 |
QTest::qWait(50); |
|
292 |
QCOMPARE(effect->numRepaints, 1); |
|
293 |
QCOMPARE(item->numRepaints, 1); |
|
294 |
QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceInvalidated); |
|
295 |
effect->reset(); |
|
296 |
item->reset(); |
|
297 |
||
298 |
// Make sure changing the effect's bounding rect triggers a repaint. |
|
299 |
effect->setMargin(20); |
|
300 |
QTest::qWait(50); |
|
301 |
QCOMPARE(effect->numRepaints, 1); |
|
302 |
QCOMPARE(item->numRepaints, 1); |
|
303 |
effect->reset(); |
|
304 |
item->reset(); |
|
305 |
||
306 |
// Make sure change the item's bounding rect triggers a repaint. |
|
307 |
item->setRect(0, 0, 50, 50); |
|
308 |
QTest::qWait(50); |
|
309 |
QCOMPARE(effect->numRepaints, 1); |
|
310 |
QCOMPARE(item->numRepaints, 1); |
|
311 |
QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceBoundingRectChanged); |
|
312 |
effect->reset(); |
|
313 |
item->reset(); |
|
314 |
||
315 |
// Make sure the effect is the one to issue a repaint of the item. |
|
316 |
effect->doNothingInDraw = true; |
|
317 |
item->update(); |
|
318 |
QTest::qWait(50); |
|
319 |
QCOMPARE(effect->numRepaints, 1); |
|
320 |
QCOMPARE(item->numRepaints, 0); |
|
321 |
effect->doNothingInDraw = false; |
|
322 |
effect->reset(); |
|
323 |
item->reset(); |
|
324 |
||
325 |
// Make sure we update the source when disabling/enabling the effect. |
|
326 |
effect->setEnabled(false); |
|
327 |
QTest::qWait(50); |
|
328 |
QCOMPARE(effect->numRepaints, 0); |
|
329 |
QCOMPARE(item->numRepaints, 1); |
|
330 |
effect->reset(); |
|
331 |
item->reset(); |
|
332 |
||
333 |
effect->setEnabled(true); |
|
334 |
QTest::qWait(50); |
|
335 |
QCOMPARE(effect->numRepaints, 1); |
|
336 |
QCOMPARE(item->numRepaints, 1); |
|
337 |
effect->reset(); |
|
338 |
item->reset(); |
|
339 |
||
340 |
// Effect is already enabled; nothing should happen. |
|
341 |
effect->setEnabled(true); |
|
342 |
QTest::qWait(50); |
|
343 |
QCOMPARE(effect->numRepaints, 0); |
|
344 |
QCOMPARE(item->numRepaints, 0); |
|
345 |
||
346 |
// Make sure uninstalling an effect triggers a repaint. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
347 |
QPointer<CustomEffect> ptr = effect; |
0 | 348 |
item->setGraphicsEffect(0); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
QVERIFY(!ptr); |
0 | 350 |
QTest::qWait(50); |
351 |
QCOMPARE(item->numRepaints, 1); |
|
352 |
} |
|
353 |
||
354 |
void tst_QGraphicsEffect::opacity() |
|
355 |
{ |
|
356 |
// Make sure the painter's opacity is correct in QGraphicsEffect::draw. |
|
357 |
QGraphicsScene scene; |
|
358 |
CustomItem *item = new CustomItem(0, 0, 100, 100); |
|
359 |
item->setOpacity(0.5); |
|
360 |
CustomEffect *effect = new CustomEffect; |
|
361 |
item->setGraphicsEffect(effect); |
|
362 |
scene.addItem(item); |
|
363 |
||
364 |
QGraphicsView view(&scene); |
|
365 |
view.show(); |
|
366 |
QTest::qWaitForWindowShown(&view); |
|
367 |
QTRY_VERIFY(effect->numRepaints > 0); |
|
368 |
QCOMPARE(effect->m_opacity, qreal(0.5)); |
|
369 |
} |
|
370 |
||
371 |
void tst_QGraphicsEffect::grayscale() |
|
372 |
{ |
|
373 |
if (qApp->desktop()->depth() < 24) { |
|
374 |
QSKIP("Test only works on 32 bit displays", SkipAll); |
|
375 |
return; |
|
376 |
} |
|
377 |
||
378 |
QGraphicsScene scene(0, 0, 100, 100); |
|
379 |
||
380 |
QGraphicsRectItem *item = scene.addRect(0, 0, 50, 50); |
|
381 |
item->setPen(Qt::NoPen); |
|
382 |
item->setBrush(QColor(122, 193, 66)); // Qt light green |
|
383 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
384 |
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
385 |
effect->setColor(Qt::black); |
0 | 386 |
item->setGraphicsEffect(effect); |
387 |
||
388 |
QPainter painter; |
|
389 |
QImage image(100, 100, QImage::Format_ARGB32_Premultiplied); |
|
390 |
||
391 |
image.fill(0); |
|
392 |
painter.begin(&image); |
|
393 |
painter.setRenderHint(QPainter::Antialiasing); |
|
394 |
scene.render(&painter); |
|
395 |
painter.end(); |
|
396 |
||
397 |
QCOMPARE(image.pixel(10, 10), qRgb(148, 148, 148)); |
|
398 |
||
399 |
effect->setStrength(0.5); |
|
400 |
||
401 |
image.fill(0); |
|
402 |
painter.begin(&image); |
|
403 |
painter.setRenderHint(QPainter::Antialiasing); |
|
404 |
scene.render(&painter); |
|
405 |
painter.end(); |
|
406 |
||
407 |
QCOMPARE(image.pixel(10, 10), qRgb(135, 171, 107)); |
|
408 |
||
409 |
effect->setStrength(0.0); |
|
410 |
||
411 |
image.fill(0); |
|
412 |
painter.begin(&image); |
|
413 |
painter.setRenderHint(QPainter::Antialiasing); |
|
414 |
scene.render(&painter); |
|
415 |
painter.end(); |
|
416 |
||
417 |
QCOMPARE(image.pixel(10, 10), qRgb(122, 193, 66)); |
|
418 |
} |
|
419 |
||
420 |
void tst_QGraphicsEffect::colorize() |
|
421 |
{ |
|
422 |
if (qApp->desktop()->depth() < 24) { |
|
423 |
QSKIP("Test only works on 32 bit displays", SkipAll); |
|
424 |
return; |
|
425 |
} |
|
426 |
||
427 |
QGraphicsScene scene(0, 0, 100, 100); |
|
428 |
||
429 |
QGraphicsRectItem *item = scene.addRect(0, 0, 50, 50); |
|
430 |
item->setPen(Qt::NoPen); |
|
431 |
item->setBrush(QColor(122, 193, 66)); // Qt light green |
|
432 |
||
433 |
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect; |
|
434 |
effect->setColor(QColor(102, 153, 51)); // Qt dark green |
|
435 |
item->setGraphicsEffect(effect); |
|
436 |
||
437 |
QPainter painter; |
|
438 |
QImage image(100, 100, QImage::Format_ARGB32_Premultiplied); |
|
439 |
||
440 |
image.fill(0); |
|
441 |
painter.begin(&image); |
|
442 |
painter.setRenderHint(QPainter::Antialiasing); |
|
443 |
scene.render(&painter); |
|
444 |
painter.end(); |
|
445 |
||
446 |
QCOMPARE(image.pixel(10, 10), qRgb(191, 212, 169)); |
|
447 |
||
448 |
effect->setStrength(0.5); |
|
449 |
||
450 |
image.fill(0); |
|
451 |
painter.begin(&image); |
|
452 |
painter.setRenderHint(QPainter::Antialiasing); |
|
453 |
scene.render(&painter); |
|
454 |
painter.end(); |
|
455 |
||
456 |
QCOMPARE(image.pixel(10, 10), qRgb(156, 203, 117)); |
|
457 |
||
458 |
effect->setStrength(0.0); |
|
459 |
||
460 |
image.fill(0); |
|
461 |
painter.begin(&image); |
|
462 |
painter.setRenderHint(QPainter::Antialiasing); |
|
463 |
scene.render(&painter); |
|
464 |
painter.end(); |
|
465 |
||
466 |
QCOMPARE(image.pixel(10, 10), qRgb(122, 193, 66)); |
|
467 |
} |
|
468 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
469 |
class PixmapItemEffect : public QGraphicsEffect |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
470 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
471 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
472 |
PixmapItemEffect(const QPixmap &source) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
473 |
: QGraphicsEffect() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
474 |
, pixmap(source) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
475 |
, repaints(0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
476 |
{} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
477 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
478 |
QRectF boundingRectFor(const QRectF &rect) const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
479 |
{ return rect; } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
480 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
481 |
void draw(QPainter *painter) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
482 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
483 |
QVERIFY(sourcePixmap(Qt::LogicalCoordinates).pixmapData() == pixmap.pixmapData()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
484 |
QVERIFY((painter->worldTransform().type() <= QTransform::TxTranslate) == (sourcePixmap(Qt::DeviceCoordinates).pixmapData() == pixmap.pixmapData())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
485 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
486 |
++repaints; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
487 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
488 |
QPixmap pixmap; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
489 |
int repaints; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
490 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
491 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
492 |
void tst_QGraphicsEffect::drawPixmapItem() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
493 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
494 |
QImage image(32, 32, QImage::Format_RGB32); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
495 |
QPainter p(&image); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
496 |
p.fillRect(0, 0, 32, 16, Qt::blue); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
497 |
p.fillRect(0, 16, 32, 16, Qt::red); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
498 |
p.end(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
499 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
500 |
QGraphicsScene scene; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
501 |
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
502 |
scene.addItem(item); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
503 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
504 |
PixmapItemEffect *effect = new PixmapItemEffect(item->pixmap()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
505 |
item->setGraphicsEffect(effect); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
506 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
QGraphicsView view(&scene); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
view.show(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
509 |
QTest::qWaitForWindowShown(&view); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
510 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
511 |
item->rotate(180); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
QTest::qWait(50); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
QTRY_VERIFY(effect->repaints >= 2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
515 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
516 |
|
0 | 517 |
QTEST_MAIN(tst_QGraphicsEffect) |
518 |
#include "tst_qgraphicseffect.moc" |
|
519 |