diff options
Diffstat (limited to 'tests/auto/qgraphicslinearlayout')
-rw-r--r-- | tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp | 116 |
1 files changed, 72 insertions, 44 deletions
diff --git a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index 2c59044..fd0885b 100644 --- a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -76,6 +76,7 @@ private slots: void invalidate(); void itemAt_data(); void itemAt(); + void itemAt_visualOrder(); void orientation_data(); void orientation(); void removeAt_data(); @@ -163,19 +164,19 @@ class RectWidget : public QGraphicsWidget public: RectWidget(QGraphicsItem *parent = 0, const QBrush &brush = QBrush()) : QGraphicsWidget(parent){ m_brush = brush;} - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(m_brush); painter->drawRoundRect(rect()); } - + void setSizeHint(Qt::SizeHint which, const QSizeF &size) { m_sizeHints[which] = size; updateGeometry(); } - + virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const { if (m_sizeHints[which].isValid()) { return m_sizeHints[which]; @@ -210,17 +211,16 @@ void tst_QGraphicsLinearLayout::qgraphicslinearlayout() layout.setOrientation(Qt::Vertical); layout.orientation(); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert null item"); - //QCOMPARE(layout.count(), 0); + QCOMPARE(layout.count(), 0); layout.addItem(0); - //QCOMPARE(layout.count(), 0); + QCOMPARE(layout.count(), 0); layout.addStretch(0); - //QCOMPARE(layout.count(), 1); + QCOMPARE(layout.count(), 0); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert null item"); layout.insertItem(0, 0); layout.insertStretch(0, 0); layout.removeItem(0); - layout.removeAt(0); - //QCOMPARE(layout.count(), 1); + QCOMPARE(layout.count(), 0); layout.setSpacing(0); layout.spacing(); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::setStretchFactor: cannot assign a stretch factor to a null item"); @@ -231,8 +231,7 @@ void tst_QGraphicsLinearLayout::qgraphicslinearlayout() QCOMPARE(layout.alignment(0), 0); layout.setGeometry(QRectF()); layout.geometry(); - //QCOMPARE(layout.count(), 1); - layout.itemAt(0); + QCOMPARE(layout.count(), 0); layout.invalidate(); layout.sizeHint(Qt::MinimumSize, QSizeF()); } @@ -285,7 +284,7 @@ void tst_QGraphicsLinearLayout::alignment() int i; bool addWidget = true; for (i = 0; i < sizeof(alignmentsToTest)/sizeof(Qt::Alignment); ++i) { - QGraphicsLayoutItem *loutItem; + QGraphicsLayoutItem *loutItem; Qt::Alignment align = alignmentsToTest[i]; if (!align && i > 0) addWidget = false; @@ -537,7 +536,10 @@ void tst_QGraphicsLinearLayout::insertItem() QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); layout.insertItem(insertItemAt, item); QCOMPARE(layout.count(), itemCount + layoutCount + 1); - QCOMPARE(layout.itemAt(insertItemAt), insertItemAt == -1 ? (QGraphicsLayoutItem*)0 : item); + + if (insertItemAt >= 0 && (itemCount + layoutCount >= 0)) { + QCOMPARE(layout.itemAt(insertItemAt), item); + } layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -599,8 +601,7 @@ void tst_QGraphicsLinearLayout::insertStretch() } widget->setLayout(layout); layout->insertStretch(insertItemAt, stretch); - QCOMPARE(layout->itemAt(insertItemAt), (QGraphicsLayoutItem*)0); - QCOMPARE(layout->count(), itemCount + layoutCount + 1); + QCOMPARE(layout->count(), itemCount + layoutCount); layout->activate(); view.show(); @@ -669,7 +670,6 @@ void tst_QGraphicsLinearLayout::invalidate() void tst_QGraphicsLinearLayout::itemAt_data() { QTest::addColumn<int>("index"); - QTest::newRow("-1") << -1; QTest::newRow("0") << 0; QTest::newRow("1") << 1; QTest::newRow("2") << 2; @@ -681,7 +681,32 @@ void tst_QGraphicsLinearLayout::itemAt() // see also the insertItem() etc tests QFETCH(int, index); SubQGraphicsLinearLayout layout; - QCOMPARE(layout.itemAt(index), (QGraphicsLayoutItem*)0); + for (int i = 0; i < 3; ++i) + layout.addItem(new QGraphicsWidget); + + QVERIFY(layout.itemAt(index) != 0); +} + +void tst_QGraphicsLinearLayout::itemAt_visualOrder() +{ + QGraphicsLinearLayout *l = new QGraphicsLinearLayout; + + QGraphicsWidget *w1 = new QGraphicsWidget; + l->addItem(w1); + + QGraphicsWidget *w3 = new QGraphicsWidget; + l->addItem(w3); + + QGraphicsWidget *w0 = new QGraphicsWidget; + l->insertItem(0, w0); + + QGraphicsWidget *w2 = new QGraphicsWidget; + l->insertItem(2, w2); + + QCOMPARE(l->itemAt(0), w0); + QCOMPARE(l->itemAt(1), w1); + QCOMPARE(l->itemAt(2), w2); + QCOMPARE(l->itemAt(3), w3); } void tst_QGraphicsLinearLayout::orientation_data() @@ -695,7 +720,7 @@ void tst_QGraphicsLinearLayout::orientation_data() void tst_QGraphicsLinearLayout::orientation() { QFETCH(Qt::Orientation, orientation); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -721,7 +746,7 @@ void tst_QGraphicsLinearLayout::orientation() qreal pos; if (initialOrientation == Qt::Horizontal) pos = item->pos().x(); - else + else pos = item->pos().y(); positions.append(pos); @@ -736,7 +761,7 @@ void tst_QGraphicsLinearLayout::orientation() QGraphicsWidget *item = static_cast<QGraphicsWidget*>(layout.itemAt(i)); if (initialOrientation == Qt::Horizontal) QCOMPARE(item->pos().y(), positions.at(i)); - else + else QCOMPARE(item->pos().x(), positions.at(i)); } @@ -779,19 +804,18 @@ void tst_QGraphicsLinearLayout::removeAt() layout.addItem(new SubQGraphicsLinearLayout); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - QGraphicsLayoutItem *w = layout.itemAt(removeItemAt); - QGraphicsLayoutItem *wParent = 0; + QGraphicsLayoutItem *w = 0; + if (removeItemAt >= 0 && removeItemAt < layout.count()) + w = layout.itemAt(removeItemAt); if (w) { - wParent = w->parentLayoutItem(); + QGraphicsLayoutItem *wParent = w->parentLayoutItem(); QCOMPARE(wParent, static_cast<QGraphicsLayoutItem *>(&layout)); - } - layout.removeAt(removeItemAt); - if (w) { + layout.removeAt(removeItemAt); wParent = w->parentLayoutItem(); QCOMPARE(wParent, static_cast<QGraphicsLayoutItem *>(0)); + delete w; } - delete w; - QCOMPARE(layout.count(), itemCount + layoutCount - ((removeItemAt == -1) ? 0 : 1)); + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -829,11 +853,15 @@ void tst_QGraphicsLinearLayout::removeItem() for (int i = 0; i < layoutCount; ++i) layout.addItem(new SubQGraphicsLinearLayout); - QGraphicsLayoutItem *w = layout.itemAt(removeItemAt); + QGraphicsLayoutItem *w = 0; + if (removeItemAt >= 0 && removeItemAt < layout.count()) + w = layout.itemAt(removeItemAt); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - layout.removeItem(w); - delete w; - QCOMPARE(layout.count(), itemCount + layoutCount - ((removeItemAt == -1) ? 0 : 1)); + if (w) { + layout.removeItem(w); + delete w; + } + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -882,7 +910,7 @@ void tst_QGraphicsLinearLayout::setSpacing_data() void tst_QGraphicsLinearLayout::setSpacing() { QFETCH(qreal, spacing); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -994,7 +1022,7 @@ void tst_QGraphicsLinearLayout::itemSpacing() /** * The stretch factors are not applied linearly, but they are used together with both the preferred size, maximum size to form the * internal effective stretch factor. - * There is only need to apply stretch factors if the size of the layout is different than the layouts preferred size. + * There is only need to apply stretch factors if the size of the layout is different than the layouts preferred size. * (If the size of the layout is the preferred size, then all items should get their preferred sizes. * However, imagine this use case: * Layout @@ -1007,7 +1035,7 @@ void tst_QGraphicsLinearLayout::itemSpacing() * layout->resize(120, h) * * In QLayout, C would become 50, B would become 50 and A would get 20. When scaling a layout this would give a jerky feeling, since - * the item with the highest stretch factor will first resize. When that has reached its maximum the next candidate for stretch will + * the item with the highest stretch factor will first resize. When that has reached its maximum the next candidate for stretch will * resize, and finally, item with the lowest stretch factor will resize. * In QGraphicsLinearLayout we try to scale all items so that they all reach their maximum at the same time. This means that * their relative sizes are not proportional to their stretch factors. @@ -1059,7 +1087,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() view.show(); widget->show(); - + qreal firstStretch = -1; qreal firstExtent = -1.; qreal sumExtent = 0; @@ -1073,7 +1101,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() QCOMPARE(extent, firstExtent); else if (stretch > firstStretch) QVERIFY(extent > firstExtent); - else + else QVERIFY(extent < firstExtent); } else { firstStretch = (qreal)stretch; @@ -1082,7 +1110,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() sumExtent+= extent; } QCOMPARE(sumExtent, totalSize); - + delete widget; } @@ -1167,7 +1195,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() QSizePolicy sp = item->sizePolicy(); if (orientation == Qt::Horizontal) sp.setHorizontalPolicy(QSizePolicy::Policy(sp.horizontalPolicy() | QSizePolicy::IgnoreFlag)); - else + else sp.setVerticalPolicy(QSizePolicy::Policy(sp.verticalPolicy() | QSizePolicy::IgnoreFlag)); item->setSizePolicy(sp); } @@ -1180,7 +1208,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() view.resize(400,300); if (newSize.isValid()) widget->resize(newSize); - + QApplication::processEvents(); for (i = 0; i < count; ++i) { QSizeF itemSize = layout->itemAt(i)->geometry().size(); @@ -1189,7 +1217,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() if (i < expectedSizes.count()) QCOMPARE(itemSize.width(), qreal(expectedSizes.at(i))); } - + delete widget; } @@ -1254,7 +1282,7 @@ void tst_QGraphicsLinearLayout::updateGeometry() layout->setContentsMargins(0, 0, 0, 0); QCOMPARE(w1->parentLayoutItem(), static_cast<QGraphicsLayoutItem*>(layout)); QCOMPARE(layout->parentLayoutItem(), static_cast<QGraphicsLayoutItem*>(window)); - + view.show(); QApplication::processEvents(); QCOMPARE(window->size().toSize(), QSize(100, 50)); @@ -1283,7 +1311,7 @@ void tst_QGraphicsLinearLayout::updateGeometry() QApplication::processEvents(); w1->setMinimumSize(120, 60); QApplication::processEvents(); - QCOMPARE(window->size().toSize(), QSize(124, 64)); + QCOMPARE(window->size().toSize(), QSize(124, 64)); } { @@ -1341,7 +1369,7 @@ void tst_QGraphicsLinearLayout::layoutDirection() w2->setPreferredSize(20, 20); w2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); layout->addItem(w2); - + scene.addItem(window); window->setLayout(layout); view.show(); @@ -1359,7 +1387,7 @@ void tst_QGraphicsLinearLayout::layoutDirection() QCOMPARE(w1->geometry().left(), 29.0); QCOMPARE(w2->geometry().right(), 23.0); QCOMPARE(w2->geometry().left(), 3.0); - + delete window; } |