tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
   281     void task176178_itemIndexMethodBreaksSceneRect();
   281     void task176178_itemIndexMethodBreaksSceneRect();
   282     void task160653_selectionChanged();
   282     void task160653_selectionChanged();
   283     void task250680_childClip();
   283     void task250680_childClip();
   284     void taskQTBUG_5904_crashWithDeviceCoordinateCache();
   284     void taskQTBUG_5904_crashWithDeviceCoordinateCache();
   285     void taskQT657_paintIntoCacheWithTransparentParts();
   285     void taskQT657_paintIntoCacheWithTransparentParts();
       
   286     void taskQTBUG_7863_paintIntoCacheWithTransparentParts();
   286 };
   287 };
   287 
   288 
   288 void tst_QGraphicsScene::initTestCase()
   289 void tst_QGraphicsScene::initTestCase()
   289 {
   290 {
   290 #ifdef Q_OS_WINCE //disable magic for WindowsCE
   291 #ifdef Q_OS_WINCE //disable magic for WindowsCE
  4346     // No crash, then it passed!
  4347     // No crash, then it passed!
  4347 }
  4348 }
  4348 
  4349 
  4349 void tst_QGraphicsScene::taskQT657_paintIntoCacheWithTransparentParts()
  4350 void tst_QGraphicsScene::taskQT657_paintIntoCacheWithTransparentParts()
  4350 {
  4351 {
       
  4352     // Test using DeviceCoordinateCache and opaque item
  4351     QWidget *w = new QWidget();
  4353     QWidget *w = new QWidget();
  4352     w->setPalette(Qt::blue);
  4354     w->setPalette(QColor(0, 0, 255));
  4353     w->setGeometry(0, 0, 50, 50);
  4355     w->setGeometry(0, 0, 50, 50);
  4354 
  4356 
  4355     QGraphicsScene *scene = new QGraphicsScene();
  4357     QGraphicsScene *scene = new QGraphicsScene();
  4356     QGraphicsView *view = new QGraphicsView(scene);
  4358     CustomView *view = new CustomView;
       
  4359     view->setScene(scene);
  4357 
  4360 
  4358     QGraphicsProxyWidget *proxy = scene->addWidget(w);
  4361     QGraphicsProxyWidget *proxy = scene->addWidget(w);
  4359     proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
  4362     proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
  4360     proxy->rotate(15);
  4363     proxy->rotate(15);
  4361 
  4364 
  4362     view->show();
  4365     view->show();
  4363     QTest::qWaitForWindowShown(view);
  4366     QTest::qWaitForWindowShown(view);
  4364     w->update(10,10,10,10);
  4367     view->repaints = 0;
       
  4368     proxy->update(10, 10, 10, 10);
  4365     QTest::qWait(50);
  4369     QTest::qWait(50);
       
  4370     QTRY_VERIFY(view->repaints > 0);
  4366 
  4371 
  4367     QPixmap pix;
  4372     QPixmap pix;
  4368     QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(proxy);
  4373     QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(proxy);
  4369     QPixmapCache::Key key = itemp->extraItemCache()->deviceData.value(view->viewport()).key;
  4374     QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->deviceData.value(view->viewport()).key, &pix));
  4370     QVERIFY(QPixmapCache::find(key, &pix));
       
  4371 
  4375 
  4372     QTransform t = proxy->sceneTransform();
  4376     QTransform t = proxy->sceneTransform();
  4373     // Map from scene coordinates to pixmap coordinates.
  4377     // Map from scene coordinates to pixmap coordinates.
  4374     // X origin in the pixmap is the most-left point
  4378     // X origin in the pixmap is the most-left point
  4375     // of the item's boundingRect in the scene.
  4379     // of the item's boundingRect in the scene.
  4380     QImage im = subpix.toImage();
  4384     QImage im = subpix.toImage();
  4381     for(int i = 0; i < im.width(); i++) {
  4385     for(int i = 0; i < im.width(); i++) {
  4382         for(int j = 0; j < im.height(); j++)
  4386         for(int j = 0; j < im.height(); j++)
  4383             QCOMPARE(qAlpha(im.pixel(i, j)), 255);
  4387             QCOMPARE(qAlpha(im.pixel(i, j)), 255);
  4384     }
  4388     }
       
  4389 
       
  4390     delete w;
       
  4391 }
       
  4392 
       
  4393 void tst_QGraphicsScene::taskQTBUG_7863_paintIntoCacheWithTransparentParts()
       
  4394 {
       
  4395     // Test using DeviceCoordinateCache and semi-transparent item
       
  4396     {
       
  4397         QGraphicsRectItem *backItem = new QGraphicsRectItem(0, 0, 100, 100);
       
  4398         backItem->setBrush(QColor(255, 255, 0));
       
  4399         QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
       
  4400         rectItem->setBrush(QColor(0, 0, 255, 125));
       
  4401         rectItem->setParentItem(backItem);
       
  4402 
       
  4403         QGraphicsScene *scene = new QGraphicsScene();
       
  4404         CustomView *view = new CustomView;
       
  4405         view->setScene(scene);
       
  4406 
       
  4407         scene->addItem(backItem);
       
  4408         rectItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
       
  4409         backItem->rotate(15);
       
  4410 
       
  4411         view->show();
       
  4412         QTest::qWaitForWindowShown(view);
       
  4413         view->repaints = 0;
       
  4414         rectItem->update(10, 10, 10, 10);
       
  4415         QTest::qWait(50);
       
  4416         QTRY_VERIFY(view->repaints > 0);
       
  4417 
       
  4418         QPixmap pix;
       
  4419         QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
       
  4420         QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->deviceData.value(view->viewport()).key, &pix));
       
  4421 
       
  4422         QTransform t = rectItem->sceneTransform();
       
  4423         // Map from scene coordinates to pixmap coordinates.
       
  4424         // X origin in the pixmap is the most-left point
       
  4425         // of the item's boundingRect in the scene.
       
  4426         qreal adjust = t.mapRect(rectItem->boundingRect().toRect()).left();
       
  4427         QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
       
  4428         QPixmap subpix = pix.copy(rect);
       
  4429 
       
  4430         QImage im = subpix.toImage();
       
  4431         for(int i = 0; i < im.width(); i++) {
       
  4432             for(int j = 0; j < im.height(); j++) {
       
  4433                 QCOMPARE(qAlpha(im.pixel(i, j)), 125);
       
  4434             }
       
  4435         }
       
  4436 
       
  4437         delete view;
       
  4438     }
       
  4439 
       
  4440     // Test using ItemCoordinateCache and opaque item
       
  4441     {
       
  4442         QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
       
  4443         rectItem->setBrush(QColor(0, 0, 255));
       
  4444 
       
  4445         QGraphicsScene *scene = new QGraphicsScene();
       
  4446         CustomView *view = new CustomView;
       
  4447         view->setScene(scene);
       
  4448 
       
  4449         scene->addItem(rectItem);
       
  4450         rectItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
       
  4451         rectItem->rotate(15);
       
  4452 
       
  4453         view->show();
       
  4454         QTest::qWaitForWindowShown(view);
       
  4455         view->repaints = 0;
       
  4456         rectItem->update(10, 10, 10, 10);
       
  4457         QTest::qWait(50);
       
  4458         QTRY_VERIFY(view->repaints > 0);
       
  4459 
       
  4460         QPixmap pix;
       
  4461         QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
       
  4462         QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->key, &pix));
       
  4463 
       
  4464         QTransform t = rectItem->sceneTransform();
       
  4465         // Map from scene coordinates to pixmap coordinates.
       
  4466         // X origin in the pixmap is the most-left point
       
  4467         // of the item's boundingRect in the scene.
       
  4468         qreal adjust = t.mapRect(rectItem->boundingRect().toRect()).left();
       
  4469         QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
       
  4470         QPixmap subpix = pix.copy(rect);
       
  4471 
       
  4472         QImage im = subpix.toImage();
       
  4473         for(int i = 0; i < im.width(); i++) {
       
  4474             for(int j = 0; j < im.height(); j++)
       
  4475                 QCOMPARE(qAlpha(im.pixel(i, j)), 255);
       
  4476         }
       
  4477 
       
  4478         delete view;
       
  4479     }
       
  4480 
       
  4481     // Test using ItemCoordinateCache and semi-transparent item
       
  4482     {
       
  4483         QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 50, 50);
       
  4484         rectItem->setBrush(QColor(0, 0, 255, 125));
       
  4485 
       
  4486         QGraphicsScene *scene = new QGraphicsScene();
       
  4487         CustomView *view = new CustomView;
       
  4488         view->setScene(scene);
       
  4489 
       
  4490         scene->addItem(rectItem);
       
  4491         rectItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
       
  4492         rectItem->rotate(15);
       
  4493 
       
  4494         view->show();
       
  4495         QTest::qWaitForWindowShown(view);
       
  4496         view->repaints = 0;
       
  4497         rectItem->update(10, 10, 10, 10);
       
  4498         QTest::qWait(50);
       
  4499         QTRY_VERIFY(view->repaints > 0);
       
  4500 
       
  4501         QPixmap pix;
       
  4502         QGraphicsItemPrivate* itemp = QGraphicsItemPrivate::get(rectItem);
       
  4503         QTRY_VERIFY(QPixmapCache::find(itemp->extraItemCache()->key, &pix));
       
  4504 
       
  4505         QTransform t = rectItem->sceneTransform();
       
  4506         // Map from scene coordinates to pixmap coordinates.
       
  4507         // X origin in the pixmap is the most-left point
       
  4508         // of the item's boundingRect in the scene.
       
  4509         qreal adjust = t.mapRect(rectItem->boundingRect().toRect()).left();
       
  4510         QRect rect = t.mapRect(QRect(10, 10, 10, 10)).adjusted(-adjust, 0, -adjust + 1, 1);
       
  4511         QPixmap subpix = pix.copy(rect);
       
  4512 
       
  4513         QImage im = subpix.toImage();
       
  4514         for(int i = 0; i < im.width(); i++) {
       
  4515             for(int j = 0; j < im.height(); j++)
       
  4516                 QCOMPARE(qAlpha(im.pixel(i, j)), 125);
       
  4517         }
       
  4518 
       
  4519         delete view;
       
  4520     }
  4385 }
  4521 }
  4386 
  4522 
  4387 QTEST_MAIN(tst_QGraphicsScene)
  4523 QTEST_MAIN(tst_QGraphicsScene)
  4388 #include "tst_qgraphicsscene.moc"
  4524 #include "tst_qgraphicsscene.moc"