summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/graphicsview
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/widgets/graphicsview')
-rw-r--r--tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp124
-rw-r--r--tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp40
-rw-r--r--tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp73
3 files changed, 216 insertions, 21 deletions
diff --git a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp
index e9aae1ec59..c89b05616d 100644
--- a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -389,6 +389,8 @@ private slots:
void itemClipsChildrenToShape5();
void itemClipsTextChildToShape();
void itemClippingDiscovery();
+ void itemContainsChildrenInShape();
+ void itemContainsChildrenInShape2();
void ancestorFlags();
void untransformable();
void contextMenuEventPropagation();
@@ -5855,6 +5857,102 @@ void tst_QGraphicsItem::itemClippingDiscovery()
QCOMPARE(scene.itemAt(90, 90), (QGraphicsItem *)0);
}
+class ItemCountsBoundingRectCalls : public QGraphicsRectItem
+{
+public:
+ ItemCountsBoundingRectCalls(const QRectF & rect, QGraphicsItem *parent = 0)
+ : QGraphicsRectItem(rect, parent), boundingRectCalls(0) {}
+ QRectF boundingRect () const {
+ ++boundingRectCalls;
+ return QGraphicsRectItem::boundingRect();
+ }
+ mutable int boundingRectCalls;
+};
+
+void tst_QGraphicsItem::itemContainsChildrenInShape()
+{
+ ItemCountsBoundingRectCalls *parent = new ItemCountsBoundingRectCalls(QRectF(0,0, 10, 10));
+ ItemCountsBoundingRectCalls *childOutsideShape = new ItemCountsBoundingRectCalls(QRectF(0,0, 10, 10), parent);
+ childOutsideShape->setPos(20,0);
+
+ QGraphicsScene scene;
+ scene.setItemIndexMethod(QGraphicsScene::NoIndex);
+ scene.addItem(parent);
+
+ QVERIFY(parent->boundingRectCalls == childOutsideShape->boundingRectCalls);
+
+ int oldParentBoundingRectCalls = parent->boundingRectCalls;
+ int oldChildBoundingRectCalls = childOutsideShape->boundingRectCalls;
+
+ // First test that both items are searched if no optimization flags are set
+ QGraphicsItem* item = scene.itemAt(25,5);
+
+ QVERIFY(item == childOutsideShape);
+ QVERIFY(parent->boundingRectCalls > oldParentBoundingRectCalls);
+ QVERIFY(childOutsideShape->boundingRectCalls > oldChildBoundingRectCalls);
+ QVERIFY(parent->boundingRectCalls == childOutsideShape->boundingRectCalls);
+
+ oldParentBoundingRectCalls = parent->boundingRectCalls;
+ oldChildBoundingRectCalls = childOutsideShape->boundingRectCalls;
+
+ // Repeat the test to make sure that no caching/indexing is in effect
+ item = scene.itemAt(25,5);
+
+ QVERIFY(item == childOutsideShape);
+ QVERIFY(parent->boundingRectCalls > oldParentBoundingRectCalls);
+ QVERIFY(childOutsideShape->boundingRectCalls > oldChildBoundingRectCalls);
+ QVERIFY(parent->boundingRectCalls == childOutsideShape->boundingRectCalls);
+
+ oldParentBoundingRectCalls = parent->boundingRectCalls;
+ oldChildBoundingRectCalls = childOutsideShape->boundingRectCalls;
+
+ // Set the optimization flag and make sure that the child is not returned
+ // and that the child's boundingRect() method is never called.
+ parent->setFlag(QGraphicsItem::ItemContainsChildrenInShape);
+ item = scene.itemAt(25,5);
+
+ QVERIFY(!(item));
+ QVERIFY(parent->boundingRectCalls > oldParentBoundingRectCalls);
+ QVERIFY(childOutsideShape->boundingRectCalls == oldChildBoundingRectCalls);
+ QVERIFY(parent->boundingRectCalls > childOutsideShape->boundingRectCalls);
+}
+
+void tst_QGraphicsItem::itemContainsChildrenInShape2()
+{
+ //The tested flag behaves almost identically to ItemClipsChildrenToShape
+ //in terms of optimizations but does not enforce the clip.
+ //This test makes sure there is no clip.
+ QGraphicsScene scene;
+ QGraphicsItem *rect = scene.addRect(0, 0, 50, 50, QPen(Qt::NoPen), QBrush(Qt::yellow));
+
+ QGraphicsItem *ellipse = scene.addEllipse(0, 0, 100, 100, QPen(Qt::NoPen), QBrush(Qt::green));
+ ellipse->setParentItem(rect);
+
+ QGraphicsItem *clippedEllipse = scene.addEllipse(0, 0, 50, 50, QPen(Qt::NoPen), QBrush(Qt::blue));
+ clippedEllipse->setParentItem(ellipse);
+
+ QGraphicsItem *clippedEllipse2 = scene.addEllipse(0, 0, 25, 25, QPen(Qt::NoPen), QBrush(Qt::red));
+ clippedEllipse2->setParentItem(clippedEllipse);
+
+ QVERIFY(!(ellipse->flags() & QGraphicsItem::ItemClipsChildrenToShape));
+ QVERIFY(!(ellipse->flags() & QGraphicsItem::ItemContainsChildrenInShape));
+ ellipse->setFlags(QGraphicsItem::ItemContainsChildrenInShape);
+ QVERIFY(!(ellipse->flags() & QGraphicsItem::ItemClipsChildrenToShape));
+ QVERIFY((ellipse->flags() & QGraphicsItem::ItemContainsChildrenInShape));
+
+ QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
+ image.fill(0);
+ QPainter painter(&image);
+ painter.setRenderHint(QPainter::Antialiasing);
+ scene.render(&painter);
+ painter.end();
+
+ QCOMPARE(image.pixel(2, 2), QColor(Qt::yellow).rgba());
+ QCOMPARE(image.pixel(12, 12), QColor(Qt::red).rgba());
+ QCOMPARE(image.pixel(2, 25), QColor(Qt::blue).rgba());
+ QCOMPARE(image.pixel(2, 50), QColor(Qt::green).rgba());
+}
+
void tst_QGraphicsItem::ancestorFlags()
{
QGraphicsItem *level1 = new QGraphicsRectItem;
@@ -5975,11 +6073,27 @@ void tst_QGraphicsItem::ancestorFlags()
// Nobody handles child events
level21->setHandlesChildEvents(false);
- for (int i = 0; i < 2; ++i) {
- QGraphicsItem::GraphicsItemFlag flag = !i ? QGraphicsItem::ItemClipsChildrenToShape
- : QGraphicsItem::ItemIgnoresTransformations;
- int ancestorFlag = !i ? QGraphicsItemPrivate::AncestorClipsChildren
- : QGraphicsItemPrivate::AncestorIgnoresTransformations;
+ for (int i = 0; i < 3; ++i) {
+ QGraphicsItem::GraphicsItemFlag flag;
+ int ancestorFlag;
+
+ switch (i) {
+ case(0):
+ flag = QGraphicsItem::ItemClipsChildrenToShape;
+ ancestorFlag = QGraphicsItemPrivate::AncestorClipsChildren;
+ break;
+ case(1):
+ flag = QGraphicsItem::ItemIgnoresTransformations;
+ ancestorFlag = QGraphicsItemPrivate::AncestorIgnoresTransformations;
+ break;
+ case(2):
+ flag = QGraphicsItem::ItemContainsChildrenInShape;
+ ancestorFlag = QGraphicsItemPrivate::AncestorContainsChildren;
+ break;
+ default:
+ qFatal("Unknown ancestor flag, please fix!");
+ break;
+ }
QCOMPARE(int(level1->d_ptr->ancestorFlags), 0);
QCOMPARE(int(level21->d_ptr->ancestorFlags), 0);
diff --git a/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp b/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
index 8f57eca0a7..5bb8634b82 100644
--- a/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
+++ b/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
@@ -282,6 +282,9 @@ void tst_QGraphicsProxyWidget::initTestCase()
#ifdef Q_OS_WINCE //disable magic for WindowsCE
qApp->setAutoMaximizeThreshold(-1);
#endif
+ // Disable menu animations to prevent the alpha widget from getting in the way
+ // in actionsContextMenu().
+ QApplication::setEffectEnabled(Qt::UI_AnimateMenu, false);
}
// This will be called after the last test function is executed.
@@ -298,6 +301,7 @@ void tst_QGraphicsProxyWidget::init()
// This will be called after every test function.
void tst_QGraphicsProxyWidget::cleanup()
{
+ QVERIFY(QApplication::topLevelWidgets().isEmpty());
}
void tst_QGraphicsProxyWidget::qgraphicsproxywidget_data()
@@ -840,10 +844,11 @@ void tst_QGraphicsProxyWidget::focusOutEvent()
QTRY_VERIFY(view.isVisible());
QTRY_COMPARE(QApplication::activeWindow(), (QWidget*)&view);
- QWidget *widget = new QWidget;
+ QScopedPointer<QWidget> widgetGuard(new QWidget);
+ QWidget *widget = widgetGuard.data();
widget->setFocusPolicy(Qt::WheelFocus);
if (hasWidget)
- proxy->setWidget(widget);
+ proxy->setWidget(widgetGuard.take());
proxy->show();
proxy->setFocus();
QVERIFY(proxy->hasFocus());
@@ -970,13 +975,14 @@ void tst_QGraphicsProxyWidget::hoverEnterLeaveEvent()
QVERIFY(QTest::qWaitForWindowActive(&view));
SubQGraphicsProxyWidget *proxy = new SubQGraphicsProxyWidget;
- EventLogger *widget = new EventLogger;
+ QScopedPointer<EventLogger> widgetGuard(new EventLogger);
+ EventLogger *widget = widgetGuard.data();
widget->resize(50, 50);
widget->setAttribute(Qt::WA_Hover, hoverEnabled);
widget->setMouseTracking(true);
view.resize(100, 100);
if (hasWidget)
- proxy->setWidget(widget);
+ proxy->setWidget(widgetGuard.take());
proxy->setPos(50, 0);
scene.addItem(proxy);
QTest::qWait(30);
@@ -1001,9 +1007,6 @@ void tst_QGraphicsProxyWidget::hoverEnterLeaveEvent()
QTRY_COMPARE(widget->hoverLeave, (hasWidget && hoverEnabled) ? 1 : 0);
// does not work on all platforms
//QCOMPARE(widget->moveCount, 0);
-
- if (!hasWidget)
- delete widget;
}
#endif
@@ -2447,6 +2450,13 @@ void tst_QGraphicsProxyWidget::setFocus_complexTwoWidgets()
void tst_QGraphicsProxyWidget::popup_basic()
{
+ QScopedPointer<QComboBox> box(new QComboBox);
+ QStyleOptionComboBox opt;
+ opt.initFrom(box.data());
+ opt.editable = box->isEditable();
+ if (box->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt))
+ QSKIP("Does not work due to SH_Combobox_Popup");
+
// ProxyWidget should automatically create proxy's when the widget creates a child
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsView view(scene);
@@ -2455,12 +2465,11 @@ void tst_QGraphicsProxyWidget::popup_basic()
view.show();
SubQGraphicsProxyWidget *proxy = new SubQGraphicsProxyWidget;
- QComboBox *box = new QComboBox;
box->setGeometry(0, 0, 320, 40);
box->addItems(QStringList() << "monday" << "tuesday" << "wednesday"
<< "thursday" << "saturday" << "sunday");
QCOMPARE(proxy->childItems().count(), 0);
- proxy->setWidget(box);
+ proxy->setWidget(box.data());
proxy->show();
scene->addItem(proxy);
@@ -2480,12 +2489,7 @@ void tst_QGraphicsProxyWidget::popup_basic()
QGraphicsProxyWidget *child = (QGraphicsProxyWidget*)(proxy->childItems())[0];
QVERIFY(child->isWidget());
QVERIFY(child->widget());
- QStyleOptionComboBox opt;
- opt.initFrom(box);
- opt.editable = box->isEditable();
- if (box->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt))
- QSKIP("Does not work due to SH_Combobox_Popup");
- QCOMPARE(child->widget()->parent(), static_cast<QObject*>(box));
+ QCOMPARE(child->widget()->parent(), static_cast<QObject*>(box.data()));
QTRY_COMPARE(proxy->pos(), QPointF(box->pos()));
QCOMPARE(child->x(), qreal(box->x()));
@@ -2975,6 +2979,8 @@ void tst_QGraphicsProxyWidget::dontCrashWhenDie()
QApplication::processEvents();
delete w;
+ // This leaves an invisible proxy widget behind.
+ qDeleteAll(QApplication::topLevelWidgets());
}
void tst_QGraphicsProxyWidget::createProxyForChildWidget()
@@ -3472,7 +3478,8 @@ void tst_QGraphicsProxyWidget::clickFocus()
{
QGraphicsScene scene;
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
- QGraphicsProxyWidget *proxy = scene.addWidget(new QLineEdit);
+ QLineEdit *le1 = new QLineEdit;
+ QGraphicsProxyWidget *proxy = scene.addWidget(le1);
QGraphicsView view(&scene);
@@ -3524,6 +3531,7 @@ void tst_QGraphicsProxyWidget::clickFocus()
scene.setFocusItem(0);
proxy->setWidget(new QLineEdit); // resets focusWidget
+ delete le1;
{
QPointF lineEditCenter = proxy->mapToScene(proxy->boundingRect().center());
diff --git a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp
index dfc8465210..a4752126bc 100644
--- a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp
+++ b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp
@@ -267,6 +267,7 @@ private slots:
void removeFullyTransparentItem();
void zeroScale();
void focusItemChangedSignal();
+ void minimumRenderSize();
// task specific tests below me
void task139710_bspTreeCrash();
@@ -4678,6 +4679,78 @@ void tst_QGraphicsScene::focusItemChangedSignal()
}
+class ItemCountsPaintCalls : public QGraphicsRectItem
+{
+public:
+ ItemCountsPaintCalls(const QRectF & rect, QGraphicsItem *parent = 0)
+ : QGraphicsRectItem(rect, parent), repaints(0) {}
+ void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )
+ {
+ QGraphicsRectItem::paint(painter, option, widget);
+ ++repaints;
+ }
+ int repaints;
+};
+
+void tst_QGraphicsScene::minimumRenderSize()
+{
+ Q_CHECK_PAINTEVENTS
+
+ ItemCountsPaintCalls *bigParent = new ItemCountsPaintCalls(QRectF(0,0,100,100));
+ ItemCountsPaintCalls *smallChild = new ItemCountsPaintCalls(QRectF(0,0,10,10), bigParent);
+ ItemCountsPaintCalls *smallerGrandChild = new ItemCountsPaintCalls(QRectF(0,0,1,1), smallChild);
+ QGraphicsScene scene;
+ scene.addItem(bigParent);
+
+ CustomView view;
+ view.setScene(&scene);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&view));
+ qApp->processEvents();
+
+ // Initially, everything should be repainted the same number of times
+ int viewRepaints = 0;
+ QTRY_VERIFY(view.repaints > viewRepaints);
+ viewRepaints = view.repaints;
+
+ QVERIFY(viewRepaints == bigParent->repaints);
+ QVERIFY(viewRepaints == smallChild->repaints);
+ QVERIFY(viewRepaints == smallerGrandChild->repaints);
+
+ // Setting a minimum render size should cause a repaint
+ scene.setMinimumRenderSize(0.5);
+ qApp->processEvents();
+
+ QTRY_VERIFY(view.repaints > viewRepaints);
+ viewRepaints = view.repaints;
+
+ QVERIFY(viewRepaints == bigParent->repaints);
+ QVERIFY(viewRepaints == smallChild->repaints);
+ QVERIFY(viewRepaints == smallerGrandChild->repaints);
+
+ // Scaling should cause a repaint of big items only.
+ view.scale(0.1, 0.1);
+ qApp->processEvents();
+
+ QTRY_VERIFY(view.repaints > viewRepaints);
+ viewRepaints = view.repaints;
+
+ QVERIFY(viewRepaints == bigParent->repaints);
+ QVERIFY(viewRepaints == smallChild->repaints);
+ QVERIFY(smallChild->repaints > smallerGrandChild->repaints);
+
+ // Scaling further should cause even fewer items to be repainted
+ view.scale(0.1, 0.1); // Stacks with previous scale
+ qApp->processEvents();
+
+ QTRY_VERIFY(view.repaints > viewRepaints);
+ viewRepaints = view.repaints;
+
+ QVERIFY(viewRepaints == bigParent->repaints);
+ QVERIFY(bigParent->repaints > smallChild->repaints);
+ QVERIFY(smallChild->repaints > smallerGrandChild->repaints);
+}
+
void tst_QGraphicsScene::taskQTBUG_15977_renderWithDeviceCoordinateCache()
{
QGraphicsScene scene;