diff options
author | Martin Jones <martin.jones@nokia.com> | 2011-09-23 16:13:51 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-09-26 03:24:42 +0200 |
commit | 14228a41471a327f6e600220a795a1c6cb23c2b2 (patch) | |
tree | c7c9f127b8dfc9f9e608ab421a692be7896d0c18 /tests | |
parent | 2790f9b1d5488f7f29903408eaa5d2a6118f55ee (diff) |
Support margins in Flickable.
It is sometimes desireable to leave a margin/add decoration around the
content of a Flickable. This adds topMargin, leftMargin, bottomMargin
rightMargin, xOrigin and yOrigin properties to Flickable.
Task-number: QTBUG-21362
Change-Id: Ia24ea4c63e7a8de683b68100baac782c6f3a66bb
Reviewed-on: http://codereview.qt-project.org/5445
Reviewed-by: Bea Lam <bea.lam@nokia.com>
Diffstat (limited to 'tests')
6 files changed, 375 insertions, 0 deletions
diff --git a/tests/auto/declarative/qsgflickable/data/margins.qml b/tests/auto/declarative/qsgflickable/data/margins.qml new file mode 100644 index 0000000000..4866bd8301 --- /dev/null +++ b/tests/auto/declarative/qsgflickable/data/margins.qml @@ -0,0 +1,19 @@ +import QtQuick 2.0 + +Flickable { + width: 200; height: 200 + contentWidth: row.width; contentHeight: row.height + + topMargin: 20 + bottomMargin: 30 + leftMargin: 40 + rightMargin: 50 + + Row { + id: row + Repeater { + model: 4 + Rectangle { width: 400; height: 600; color: "blue" } + } + } +} diff --git a/tests/auto/declarative/qsgflickable/tst_qsgflickable.cpp b/tests/auto/declarative/qsgflickable/tst_qsgflickable.cpp index e75914a746..554be2346a 100644 --- a/tests/auto/declarative/qsgflickable/tst_qsgflickable.cpp +++ b/tests/auto/declarative/qsgflickable/tst_qsgflickable.cpp @@ -80,6 +80,7 @@ private slots: void movingAndDragging(); void disabled(); void flickVelocity(); + void margins(); private: QDeclarativeEngine engine; @@ -563,6 +564,65 @@ void tst_qsgflickable::flickVelocity() delete canvas; } +void tst_qsgflickable::margins() +{ + QDeclarativeEngine engine; + QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/margins.qml")); + QSGItem *root = qobject_cast<QSGItem*>(c.create()); + QSGFlickable *obj = qobject_cast<QSGFlickable*>(root); + QVERIFY(obj != 0); + + // starting state + QCOMPARE(obj->contentX(), -40.); + QCOMPARE(obj->contentY(), -20.); + QCOMPARE(obj->contentWidth(), 1600.); + QCOMPARE(obj->contentHeight(), 600.); + QCOMPARE(obj->xOrigin(), 0.); + QCOMPARE(obj->yOrigin(), 0.); + + // Reduce left margin + obj->setLeftMargin(30); + QTRY_COMPARE(obj->contentX(), -30.); + + // Reduce top margin + obj->setTopMargin(20); + QTRY_COMPARE(obj->contentY(), -20.); + + // position to the far right, including margin + obj->setContentX(1600 + 50 - obj->width()); + obj->returnToBounds(); + QTest::qWait(200); + QCOMPARE(obj->contentX(), 1600. + 50. - obj->width()); + + // position beyond the far right, including margin + obj->setContentX(1600 + 50 - obj->width() + 1.); + obj->returnToBounds(); + QTRY_COMPARE(obj->contentX(), 1600. + 50. - obj->width()); + + // Reduce right margin + obj->setRightMargin(40); + QTRY_COMPARE(obj->contentX(), 1600. + 40. - obj->width()); + QCOMPARE(obj->contentWidth(), 1600.); + + // position to the far bottom, including margin + obj->setContentY(600 + 30 - obj->height()); + obj->returnToBounds(); + QTest::qWait(200); + QCOMPARE(obj->contentY(), 600. + 30. - obj->height()); + + // position beyond the far bottom, including margin + obj->setContentY(600 + 30 - obj->height() + 1.); + obj->returnToBounds(); + QTRY_COMPARE(obj->contentY(), 600. + 30. - obj->height()); + + // Reduce bottom margin + obj->setBottomMargin(20); + QTRY_COMPARE(obj->contentY(), 600. + 20. - obj->height()); + QCOMPARE(obj->contentHeight(), 600.); + + delete root; +} + void tst_qsgflickable::flick(QSGView *canvas, const QPoint &from, const QPoint &to, int duration) { const int pointCount = 5; diff --git a/tests/auto/declarative/qsggridview/data/margins.qml b/tests/auto/declarative/qsggridview/data/margins.qml new file mode 100644 index 0000000000..d369658a91 --- /dev/null +++ b/tests/auto/declarative/qsggridview/data/margins.qml @@ -0,0 +1,55 @@ +import QtQuick 2.0 + +Rectangle { + id: root + + width: 240 + height: 320 + color: "#ffffff" + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 100 + height: 80 + border.color: "blue" + property string name: model.name + Text { + text: index + } + Text { + x: 40 + text: wrapper.x + ", " + wrapper.y + } + Text { + y: 20 + id: textName + objectName: "textName" + text: name + } + Text { + y: 40 + id: textNumber + objectName: "textNumber" + text: number + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + GridView { + id: grid + objectName: "grid" + width: 240 + height: 320 + cellWidth: 100 + cellHeight: 80 + leftMargin: 30 + rightMargin: 50 + flow: GridView.TopToBottom + layoutDirection: (testRightToLeft == true) ? Qt.RightToLeft : Qt.LeftToRight + model: testModel + delegate: myDelegate + } + Text { anchors.bottom: parent.bottom; text: grid.contentX } +} diff --git a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp index 5d35ffc542..f5cda2bbd9 100644 --- a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp +++ b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp @@ -110,6 +110,7 @@ private slots: void testQtQuick11Attributes(); void testQtQuick11Attributes_data(); void columnCount(); + void margins(); private: QSGView *createView(); @@ -2803,6 +2804,136 @@ void tst_QSGGridView::columnCount() QCOMPARE(items.at(9)->y(), qreal(100)); } +void tst_QSGGridView::margins() +{ + { + QSGView *canvas = createView(); + canvas->show(); + + TestModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/margins.qml")); + qApp->processEvents(); + + QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QSGItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QCOMPARE(gridview->contentX(), -30.); + QCOMPARE(gridview->xOrigin(), 0.); + + // check end bound + gridview->positionViewAtEnd(); + qreal pos = gridview->contentX(); + gridview->setContentX(pos + 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos + 50); + + // remove item before visible and check that left margin is maintained + // and xOrigin is updated + gridview->setContentX(200); + model.removeItems(0, 4); + QTest::qWait(100); + gridview->setContentX(-50); + gridview->returnToBounds(); + QCOMPARE(gridview->xOrigin(), 100.); + QTRY_COMPARE(gridview->contentX(), 70.); + + // reduce left margin + gridview->setLeftMargin(20); + QCOMPARE(gridview->xOrigin(), 100.); + QTRY_COMPARE(gridview->contentX(), 80.); + + // check end bound + gridview->positionViewAtEnd(); + QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin + pos = gridview->contentX(); + gridview->setContentX(pos + 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos + 50); + + // reduce right margin + pos = gridview->contentX(); + gridview->setRightMargin(40); + QCOMPARE(gridview->xOrigin(), 0.); + QTRY_COMPARE(gridview->contentX(), pos-10); + + delete canvas; + } + { + //RTL + QSGView *canvas = createView(); + canvas->show(); + + TestModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/margins.qml")); + qApp->processEvents(); + + QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QSGItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QCOMPARE(gridview->contentX(), -240+30.); + QCOMPARE(gridview->xOrigin(), 0.); + + // check end bound + gridview->positionViewAtEnd(); + qreal pos = gridview->contentX(); + gridview->setContentX(pos - 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos - 50); + + // remove item before visible and check that left margin is maintained + // and xOrigin is updated + gridview->setContentX(-400); + model.removeItems(0, 4); + QTest::qWait(100); + gridview->setContentX(-240+50); + gridview->returnToBounds(); + QCOMPARE(gridview->xOrigin(), -100.); + QTRY_COMPARE(gridview->contentX(), -240-70.); + + // reduce left margin (i.e. right side due to RTL) + pos = gridview->contentX(); + gridview->setLeftMargin(20); + QCOMPARE(gridview->xOrigin(), -100.); + QTRY_COMPARE(gridview->contentX(), -240-80.); + + // check end bound + gridview->positionViewAtEnd(); + QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin + pos = gridview->contentX(); + gridview->setContentX(pos - 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos - 50); + + // reduce right margin (i.e. left side due to RTL) + pos = gridview->contentX(); + gridview->setRightMargin(40); + QCOMPARE(gridview->xOrigin(), 0.); + QTRY_COMPARE(gridview->contentX(), pos+10); + + delete canvas; + } +} + QSGView *tst_QSGGridView::createView() { QSGView *canvas = new QSGView(0); diff --git a/tests/auto/declarative/qsglistview/data/margins.qml b/tests/auto/declarative/qsglistview/data/margins.qml new file mode 100644 index 0000000000..19bbef500f --- /dev/null +++ b/tests/auto/declarative/qsglistview/data/margins.qml @@ -0,0 +1,47 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 240 + height: 320 + color: "#ffffff" + + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + height: 20 + width: 240 + Text { + text: index + } + Text { + x: 30 + id: textName + objectName: "textName" + text: name + } + Text { + x: 120 + id: textNumber + objectName: "textNumber" + text: number + } + Text { + x: 200 + text: wrapper.y + } + color: ListView.isCurrentItem ? "lightsteelblue" : "white" + } + } + ListView { + id: list + objectName: "list" + anchors.fill: parent + topMargin: 30 + bottomMargin: 50 + model: testModel + delegate: myDelegate + } +} diff --git a/tests/auto/declarative/qsglistview/tst_qsglistview.cpp b/tests/auto/declarative/qsglistview/tst_qsglistview.cpp index ce1587db88..291877cba6 100644 --- a/tests/auto/declarative/qsglistview/tst_qsglistview.cpp +++ b/tests/auto/declarative/qsglistview/tst_qsglistview.cpp @@ -136,6 +136,7 @@ private slots: void onRemove_data(); void rightToLeft(); void test_mirroring(); + void margins(); private: template <class T> void items(); @@ -3542,6 +3543,68 @@ void tst_QSGListView::test_mirroring() delete canvasB; } +void tst_QSGListView::margins() +{ + QSGView *canvas = createView(); + + TestModel2 model; + for (int i = 0; i < 50; i++) + model.addItem("Item" + QString::number(i), ""); + + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/margins.qml")); + canvas->show(); + qApp->processEvents(); + + QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list"); + QTRY_VERIFY(listview != 0); + + QSGItem *contentItem = listview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QCOMPARE(listview->contentY(), -30.); + QCOMPARE(listview->yOrigin(), 0.); + + // check end bound + listview->positionViewAtEnd(); + qreal pos = listview->contentY(); + listview->setContentY(pos + 80); + listview->returnToBounds(); + QTRY_COMPARE(listview->contentY(), pos + 50); + + // remove item before visible and check that top margin is maintained + // and yOrigin is updated + listview->setContentY(100); + model.removeItem(1); + QTest::qWait(100); + listview->setContentY(-50); + listview->returnToBounds(); + QCOMPARE(listview->yOrigin(), 20.); + QTRY_COMPARE(listview->contentY(), -10.); + + // reduce top margin + listview->setTopMargin(20); + QCOMPARE(listview->yOrigin(), 20.); + QTRY_COMPARE(listview->contentY(), 0.); + + // check end bound + listview->positionViewAtEnd(); + pos = listview->contentY(); + listview->setContentY(pos + 80); + listview->returnToBounds(); + QTRY_COMPARE(listview->contentY(), pos + 50); + + // reduce bottom margin + pos = listview->contentY(); + listview->setBottomMargin(40); + QCOMPARE(listview->yOrigin(), 20.); + QTRY_COMPARE(listview->contentY(), pos-10); + + delete canvas; +} + void tst_QSGListView::qListModelInterface_items() { items<TestModel>(); |