44 #include <QtGui/qgraphicsview.h> |
44 #include <QtGui/qgraphicsview.h> |
45 #include <QtGui/qgraphicsscene.h> |
45 #include <QtGui/qgraphicsscene.h> |
46 #include <QtGui/qgraphicsitem.h> |
46 #include <QtGui/qgraphicsitem.h> |
47 #include <QtGui/qstyleoption.h> |
47 #include <QtGui/qstyleoption.h> |
48 |
48 |
|
49 #include <private/qgraphicseffect_p.h> |
|
50 |
49 //TESTED_CLASS= |
51 //TESTED_CLASS= |
50 //TESTED_FILES= |
52 //TESTED_FILES= |
51 |
53 |
52 class CustomItem : public QGraphicsRectItem |
54 class CustomItem : public QGraphicsRectItem |
53 { |
55 { |
54 public: |
56 public: |
55 CustomItem(qreal x, qreal y, qreal width, qreal height) |
57 CustomItem(qreal x, qreal y, qreal width, qreal height) |
56 : QGraphicsRectItem(x, y, width, height), numRepaints(0), |
58 : QGraphicsRectItem(x, y, width, height), numRepaints(0), |
57 m_painter(0), m_styleOption(0) |
59 m_painter(0) |
58 {} |
60 {} |
59 |
61 |
60 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
62 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
61 { |
63 { |
62 m_painter = painter; |
64 m_painter = painter; |
63 m_styleOption = option; |
|
64 ++numRepaints; |
65 ++numRepaints; |
65 QGraphicsRectItem::paint(painter, option, widget); |
66 QGraphicsRectItem::paint(painter, option, widget); |
66 } |
67 } |
67 |
68 |
68 void reset() |
69 void reset() |
69 { |
70 { |
70 numRepaints = 0; |
71 numRepaints = 0; |
71 m_painter = 0; |
72 m_painter = 0; |
72 m_styleOption = 0; |
|
73 } |
73 } |
74 |
74 |
75 int numRepaints; |
75 int numRepaints; |
76 QPainter *m_painter; |
76 QPainter *m_painter; |
77 const QStyleOption *m_styleOption; |
|
78 }; |
77 }; |
79 |
78 |
80 class CustomEffect : public QGraphicsEffect |
79 class CustomEffect : public QGraphicsEffect |
81 { |
80 { |
82 public: |
81 public: |
83 CustomEffect() |
82 CustomEffect() |
84 : QGraphicsEffect(), numRepaints(0), m_margin(10), m_sourceChanged(false), |
83 : QGraphicsEffect(), numRepaints(0), m_margin(10), m_sourceChanged(false), |
85 m_sourceBoundingRectChanged(false), doNothingInDraw(false), |
84 m_sourceBoundingRectChanged(false), doNothingInDraw(false), |
86 storeDeviceDependentStuff(false), |
85 storeDeviceDependentStuff(false), |
87 m_painter(0), m_styleOption(0), m_source(0) |
86 m_painter(0), m_source(0) |
88 {} |
87 {} |
89 |
88 |
90 QRectF boundingRectFor(const QRectF &rect) const |
89 QRectF boundingRectFor(const QRectF &rect) const |
91 { return rect.adjusted(-m_margin, -m_margin, m_margin, m_margin); } |
90 { return rect.adjusted(-m_margin, -m_margin, m_margin, m_margin); } |
92 |
91 |
110 } |
108 } |
111 |
109 |
112 int margin() const |
110 int margin() const |
113 { return m_margin; } |
111 { return m_margin; } |
114 |
112 |
115 void draw(QPainter *painter, QGraphicsEffectSource *source) |
113 void draw(QPainter *painter) |
116 { |
114 { |
117 ++numRepaints; |
115 ++numRepaints; |
118 if (storeDeviceDependentStuff) { |
116 if (storeDeviceDependentStuff) { |
119 deviceCoordinatesPixmap = source->pixmap(Qt::DeviceCoordinates); |
117 deviceCoordinatesPixmap = source()->pixmap(Qt::DeviceCoordinates); |
120 deviceRect = source->deviceRect(); |
118 deviceRect = QRect(0, 0, painter->device()->width(), painter->device()->height()); |
121 sourceDeviceBoundingRect = source->boundingRect(Qt::DeviceCoordinates); |
119 sourceDeviceBoundingRect = source()->boundingRect(Qt::DeviceCoordinates); |
122 } |
120 } |
123 if (doNothingInDraw) |
121 if (doNothingInDraw) |
124 return; |
122 return; |
125 m_source = source; |
123 m_source = source(); |
126 m_painter = painter; |
124 m_painter = painter; |
127 m_styleOption = source->styleOption(); |
125 source()->draw(painter); |
128 source->draw(painter); |
|
129 } |
126 } |
130 |
127 |
131 void sourceChanged() |
128 void sourceChanged() |
132 { m_sourceChanged = true; } |
129 { m_sourceChanged = true; } |
133 |
130 |
286 QCOMPARE(effect->source()->boundingRect(), itemBoundingRect); |
283 QCOMPARE(effect->source()->boundingRect(), itemBoundingRect); |
287 } |
284 } |
288 |
285 |
289 void tst_QGraphicsEffectSource::deviceRect() |
286 void tst_QGraphicsEffectSource::deviceRect() |
290 { |
287 { |
291 QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::deviceRect: Not yet implemented, lacking device context"); |
|
292 QCOMPARE(effect->source()->deviceRect(), QRect()); |
|
293 |
|
294 // We can at least check that the rect was correct in QGraphicsEffect::draw. |
|
295 effect->storeDeviceDependentStuff = true; |
288 effect->storeDeviceDependentStuff = true; |
296 effect->source()->update(); |
289 effect->source()->update(); |
297 QTest::qWait(50); |
290 QTest::qWait(50); |
298 QCOMPARE(effect->deviceRect, view->viewport()->rect()); |
291 QCOMPARE(effect->deviceRect, view->viewport()->rect()); |
299 } |
292 } |
316 // Make sure default value is Qt::LogicalCoordinates. |
309 // Make sure default value is Qt::LogicalCoordinates. |
317 QPixmap pixmap2 = effect->source()->pixmap(); |
310 QPixmap pixmap2 = effect->source()->pixmap(); |
318 QCOMPARE(pixmap1, pixmap2); |
311 QCOMPARE(pixmap1, pixmap2); |
319 } |
312 } |
320 |
313 |
|
314 class PaddingEffect : public QGraphicsEffect |
|
315 { |
|
316 public: |
|
317 PaddingEffect(QObject *parent) : QGraphicsEffect(parent) |
|
318 { |
|
319 } |
|
320 |
|
321 QRectF boundingRectFor(const QRectF &src) const { |
|
322 return src.adjusted(-10, -10, 10, 10); |
|
323 } |
|
324 |
|
325 void draw(QPainter *) { |
|
326 pix = source()->pixmap(coordinateMode, &offset, padMode); |
|
327 } |
|
328 |
|
329 QPixmap pix; |
|
330 QPoint offset; |
|
331 QGraphicsEffect::PixmapPadMode padMode; |
|
332 Qt::CoordinateSystem coordinateMode; |
|
333 }; |
|
334 |
|
335 void tst_QGraphicsEffectSource::pixmapPadding_data() |
|
336 { |
|
337 QTest::addColumn<int>("coordinateMode"); |
|
338 QTest::addColumn<int>("padMode"); |
|
339 QTest::addColumn<QSize>("size"); |
|
340 QTest::addColumn<QPoint>("offset"); |
|
341 QTest::addColumn<uint>("ulPixel"); |
|
342 |
|
343 QTest::newRow("log,nopad") << int(Qt::LogicalCoordinates) |
|
344 << int(QGraphicsEffect::NoPad) |
|
345 << QSize(10, 10) << QPoint(0, 0) |
|
346 << 0xffff0000u; |
|
347 |
|
348 QTest::newRow("log,transparent") << int(Qt::LogicalCoordinates) |
|
349 << int(QGraphicsEffect::PadToTransparentBorder) |
|
350 << QSize(14, 14) << QPoint(-2, -2) |
|
351 << 0x00000000u; |
|
352 |
|
353 QTest::newRow("log,effectrect") << int(Qt::LogicalCoordinates) |
|
354 << int(QGraphicsEffect::PadToEffectiveBoundingRect) |
|
355 << QSize(20, 20) << QPoint(-5, -5) |
|
356 << 0x00000000u; |
|
357 |
|
358 QTest::newRow("dev,nopad") << int(Qt::DeviceCoordinates) |
|
359 << int(QGraphicsEffect::NoPad) |
|
360 << QSize(20, 20) << QPoint(40, 40) |
|
361 << 0xffff0000u; |
|
362 |
|
363 QTest::newRow("dev,transparent") << int(Qt::DeviceCoordinates) |
|
364 << int(QGraphicsEffect::PadToTransparentBorder) |
|
365 << QSize(24, 24) << QPoint(38, 38) |
|
366 << 0x00000000u; |
|
367 |
|
368 QTest::newRow("dev,effectrect") << int(Qt::DeviceCoordinates) |
|
369 << int(QGraphicsEffect::PadToEffectiveBoundingRect) |
|
370 << QSize(40, 40) << QPoint(30, 30) |
|
371 << 0x00000000u; |
|
372 |
|
373 } |
|
374 |
|
375 void tst_QGraphicsEffectSource::pixmapPadding() |
|
376 { |
|
377 QPixmap dummyTarget(100, 100); |
|
378 QPainter dummyPainter(&dummyTarget); |
|
379 dummyPainter.translate(40, 40); |
|
380 dummyPainter.scale(2, 2); |
|
381 |
|
382 QPixmap pm(10, 10); |
|
383 pm.fill(Qt::red); |
|
384 |
|
385 QGraphicsScene *scene = new QGraphicsScene(); |
|
386 PaddingEffect *effect = new PaddingEffect(scene); |
|
387 QGraphicsPixmapItem *pmItem = new QGraphicsPixmapItem(pm); |
|
388 scene->addItem(pmItem); |
|
389 pmItem->setGraphicsEffect(effect); |
|
390 |
|
391 QFETCH(int, coordinateMode); |
|
392 QFETCH(int, padMode); |
|
393 QFETCH(QPoint, offset); |
|
394 QFETCH(QSize, size); |
|
395 QFETCH(uint, ulPixel); |
|
396 |
|
397 effect->padMode = (QGraphicsEffect::PixmapPadMode) padMode; |
|
398 effect->coordinateMode = (Qt::CoordinateSystem) coordinateMode; |
|
399 |
|
400 scene->render(&dummyPainter, scene->itemsBoundingRect(), scene->itemsBoundingRect()); |
|
401 |
|
402 QCOMPARE(effect->pix.size(), size); |
|
403 QCOMPARE(effect->offset, offset); |
|
404 QCOMPARE(effect->pix.toImage().pixel(0, 0), ulPixel); |
|
405 |
|
406 // ### Fix corruption in scene destruction, then enable... |
|
407 // delete scene; |
|
408 } |
|
409 |
321 QTEST_MAIN(tst_QGraphicsEffectSource) |
410 QTEST_MAIN(tst_QGraphicsEffectSource) |
322 #include "tst_qgraphicseffectsource.moc" |
411 #include "tst_qgraphicseffectsource.moc" |
323 |
412 |