diff options
author | Christian Manning <cmanning999@gmail.com> | 2014-02-12 02:01:08 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-05-12 22:52:27 +0200 |
commit | 5988cfda014ffbe8ab8640f84c4fc704335b205d (patch) | |
tree | c40ab5b70dee1f2de8621aba4e2d87dab6c5c431 | |
parent | 2b7e54ef5cf8e357493a54d977fcb2bf8288dc7a (diff) | |
download | qtquickcontrols-5988cfda014ffbe8ab8640f84c4fc704335b205d.tar.gz |
Add ability to dynamically add items to SplitView
Factor out the loop body of init() into a pushItem() function to
make the ability user-accessible.
Can only append items due to the list property limitation.
Takes account of item's fill property
Task-number: QTBUG-35281
Change-Id: Ibb2a5e3cf945bede544cf6bf8eebe13ffd2f79e5
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
-rw-r--r-- | src/controls/SplitView.qml | 48 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_splitview.qml | 73 |
2 files changed, 106 insertions, 15 deletions
diff --git a/src/controls/SplitView.qml b/src/controls/SplitView.qml index dadff049..f5bb16d7 100644 --- a/src/controls/SplitView.qml +++ b/src/controls/SplitView.qml @@ -166,6 +166,17 @@ Item { onHeightChanged: d.updateLayout() onOrientationChanged: d.changeOrientation() + /*! Add an item to the end of the view. */ + function addItem(item) { + d.updateLayoutGuard = true + + d.addItem_impl(item) + + d.calculateImplicitSize() + d.updateLayoutGuard = false + d.updateFillIndex() + } + SystemPalette { id: pal } QtObject { @@ -185,28 +196,35 @@ Item { property int fillIndex: -1 property bool updateLayoutGuard: true + function addItem_impl(item) + { + // temporarily set fillIndex to new item + fillIndex = __items.length + if (splitterItems.children.length > 0) + handleLoader.createObject(splitterHandles, {"__handleIndex":splitterItems.children.length - 1}) + + item.parent = splitterItems + + // should match disconnections in Component.onDestruction + item.widthChanged.connect(d.updateLayout) + item.heightChanged.connect(d.updateLayout) + item.Layout.maximumWidthChanged.connect(d.updateLayout) + item.Layout.minimumWidthChanged.connect(d.updateLayout) + item.Layout.maximumHeightChanged.connect(d.updateLayout) + item.Layout.minimumHeightChanged.connect(d.updateLayout) + item.visibleChanged.connect(d.updateFillIndex) + item.Layout.fillWidthChanged.connect(d.updateFillIndex) + item.Layout.fillHeightChanged.connect(d.updateFillIndex) + } + function init() { for (var i=0; i<__contents.length; ++i) { var item = __contents[i]; if (!item.hasOwnProperty("x")) continue - - if (splitterItems.children.length > 0) - handleLoader.createObject(splitterHandles, {"__handleIndex":splitterItems.children.length - 1}) - item.parent = splitterItems + addItem_impl(item) i-- // item was removed from list - - // should match disconnections in Component.onDestruction - item.widthChanged.connect(d.updateLayout) - item.heightChanged.connect(d.updateLayout) - item.Layout.maximumWidthChanged.connect(d.updateLayout) - item.Layout.minimumWidthChanged.connect(d.updateLayout) - item.Layout.maximumHeightChanged.connect(d.updateLayout) - item.Layout.minimumHeightChanged.connect(d.updateLayout) - item.visibleChanged.connect(d.updateFillIndex) - item.Layout.fillWidthChanged.connect(d.updateFillIndex) - item.Layout.fillHeightChanged.connect(d.updateFillIndex) } d.calculateImplicitSize() diff --git a/tests/auto/controls/data/tst_splitview.qml b/tests/auto/controls/data/tst_splitview.qml index 64436c46..dfc55c94 100644 --- a/tests/auto/controls/data/tst_splitview.qml +++ b/tests/auto/controls/data/tst_splitview.qml @@ -213,4 +213,77 @@ TestCase { compare (view.item3.x, view.width - view.item3.width) view.destroy() } + + Component { + id: item_to_add_dynamically + Rectangle { + width: 50 + height: 100 + color: "yellow" + } + } + + function test_dynamic_item_add() { + // QTBUG-35281 + var view = splitView.createObject(testCase); + verify (view !== null, "splitview created is null") + verify (view.orientation === Qt.Horizontal) + waitForRendering(view) + + var item3 = item_to_add_dynamically.createObject() + view.addItem(item3) + // reset item2 width + view.item2.width = 200 + waitForRendering(view) + + compare (view.__items.length, 3) + + compare (view.item1.x, 0) + compare (view.item1.y, 0) + compare (view.item1.width, 100) + compare (view.item1.height, 500) + + compare (view.item2.x, view.item1.x + view.item1.width + handleWidth) + compare (view.item2.y, 0) + compare (view.item2.width, 200) + compare (view.item2.height, 500) + + compare (item3.x, view.item2.x + view.item2.width + handleWidth) + compare (item3.y, 0) + compare (item3.width, testCase.width - view.item2.width - view.item1.width - (handleWidth*2)) + compare (item3.height, 500) + + view.destroy() + } + + function test_dynamic_item_add_fillWidth() { + var view = splitView.createObject(testCase); + verify (view !== null, "splitview created is null") + verify (view.orientation === Qt.Horizontal) + view.item2.Layout.fillWidth = true + waitForRendering(view) + + var item3 = item_to_add_dynamically.createObject() + view.addItem(item3) + waitForRendering(view) + + compare (view.__items.length, 3) + + compare (view.item1.x, 0) + compare (view.item1.y, 0) + compare (view.item1.width, 100) + compare (view.item1.height, 500) + + compare (view.item2.x, view.item1.x + view.item1.width + handleWidth) + compare (view.item2.y, 0) + compare (view.item2.width, testCase.width - view.item1.width - item3.width - (handleWidth*2)) + compare (view.item2.height, 500) + + compare (item3.x, view.item2.x + view.item2.width + handleWidth) + compare (item3.y, 0) + compare (item3.width, 50) + compare (item3.height, 500) + + view.destroy() + } } |