diff options
| author | Filippo Cucchetto <filippocucchetto@gmail.com> | 2015-01-18 17:35:09 +0100 |
|---|---|---|
| committer | Filippo Cucchetto <filippocucchetto@gmail.com> | 2015-02-16 07:21:57 +0000 |
| commit | 4d40351035d744a6021a7930cd46fe021474ba1d (patch) | |
| tree | 77d8de3c323345ca92907aecb216b07d38f1ca92 | |
| parent | f442e9569fe4baf92ecc71d701484d32153c7d80 (diff) | |
Added remove function to the SplitView
Change-Id: Ie30e76356716a546f7eaa3e4c2eccbc15ebf9a5c
Task-number: QTBUG-42509
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
| -rw-r--r-- | src/controls/SplitView.qml | 95 | ||||
| -rw-r--r-- | tests/auto/controls/data/tst_splitview.qml | 83 |
2 files changed, 164 insertions, 14 deletions
diff --git a/src/controls/SplitView.qml b/src/controls/SplitView.qml index 38b7f8391..be1a9e923 100644 --- a/src/controls/SplitView.qml +++ b/src/controls/SplitView.qml @@ -181,14 +181,27 @@ Item { \since QtQuick.Controls 1.3 */ function addItem(item) { d.updateLayoutGuard = true - d.addItem_impl(item) - d.calculateImplicitSize() d.updateLayoutGuard = false d.updateFillIndex() } + /*! Remove \a item from the view. + \since QtQuick.Controls 1.4 */ + function removeItem(item) { + d.updateLayoutGuard = true + var result = d.removeItem_impl(item) + if (result !== null) { + d.calculateImplicitSize() + d.updateLayoutGuard = false + d.updateFillIndex() + } + else { + d.updateLayoutGuard = false + } + } + SystemPalette { id: pal } QtObject { @@ -230,8 +243,12 @@ Item { handleLoader.createObject(splitterHandles, {"__handleIndex":splitterItems.children.length - 1}) item.parent = splitterItems + d.initItemConnections(item) + } - // should match disconnections in Component.onDestruction + function initItemConnections(item) + { + // should match disconnections in terminateItemConnections item.widthChanged.connect(d.updateLayout) item.heightChanged.connect(d.updateLayout) item.Layout.maximumWidthChanged.connect(d.updateLayout) @@ -247,6 +264,66 @@ Item { item.Layout.fillHeightChanged.connect(d.updateFillIndex) } + function terminateItemConnections(item) + { + // should match connections in initItemConnections + item.widthChanged.disconnect(d.updateLayout) + item.heightChanged.disconnect(d.updateLayout) + item.Layout.maximumWidthChanged.disconnect(d.updateLayout) + item.Layout.minimumWidthChanged.disconnect(d.updateLayout) + item.Layout.maximumHeightChanged.disconnect(d.updateLayout) + item.Layout.minimumHeightChanged.disconnect(d.updateLayout) + item.visibleChanged.disconnect(d.updateFillIndex) + item.Layout.fillWidthChanged.disconnect(d.updateFillIndex) + item.Layout.fillHeightChanged.disconnect(d.updateFillIndex) + } + + function removeItem_impl(item) + { + var pos = itemPos(item) + + // Check pos range + if (pos < 0 || pos >= __items.length) + return null + + // Temporary unset the fillIndex + fillIndex = __items.length - 1 + + // Remove the handle at the left/right of the item that + // is going to be removed + var handlePos = -1 + var hasPrevious = pos > 0 + var hasNext = (pos + 1) < __items.length + + if (hasPrevious) + handlePos = pos-1 + else if (hasNext) + handlePos = pos + if (handlePos >= 0) { + var handle = __handles[handlePos] + handle.visible = false + handle.parent = null + handle.destroy() + for (var i = handlePos; i < __handles.length; ++i) + __handles[i].__handleIndex = i + } + + // Remove the item. + // Disconnect the item to be removed + terminateItemConnections(item) + item.parent = null + + return item + } + + function itemPos(item) + { + for (var i = 0; i < __items.length; ++i) + if (item === __items[i]) + return i + return -1 + } + function init() { for (var i=0; i<__contents.length; ++i) { @@ -540,17 +617,7 @@ Item { Component.onDestruction: { for (var i=0; i<splitterItems.children.length; ++i) { var item = splitterItems.children[i]; - - // should match connections in init() - item.widthChanged.disconnect(d.updateLayout) - item.heightChanged.disconnect(d.updateLayout) - item.Layout.maximumWidthChanged.disconnect(d.updateLayout) - item.Layout.minimumWidthChanged.disconnect(d.updateLayout) - item.Layout.maximumHeightChanged.disconnect(d.updateLayout) - item.Layout.minimumHeightChanged.disconnect(d.updateLayout) - item.visibleChanged.disconnect(d.updateFillIndex) - item.Layout.fillWidthChanged.disconnect(d.updateFillIndex) - item.Layout.fillHeightChanged.disconnect(d.updateFillIndex) + d.terminateItemConnections(item) } } } diff --git a/tests/auto/controls/data/tst_splitview.qml b/tests/auto/controls/data/tst_splitview.qml index 15e8ce621..e24469216 100644 --- a/tests/auto/controls/data/tst_splitview.qml +++ b/tests/auto/controls/data/tst_splitview.qml @@ -327,4 +327,87 @@ TestCase { view.destroy() } + + Component + { + id: splitView_dynamic_item_remove + SplitView + { + anchors.fill: parent + property alias item1: item4 + property alias item2: item5 + property alias item3: item6 + handleDelegate: Rectangle { width: handleWidth; height: handleHeight; color: "black" } + Rectangle { + id: item4 + color: "yellow" + Layout.minimumWidth: 100 + } + Rectangle { + id: item5 + color: "green" + Layout.fillWidth: true + } + Rectangle { + id: item6 + color: "blue" + Layout.minimumWidth: 100 + } + } + } + + function test_dynamic_item_remove() { + var view = splitView_dynamic_item_remove.createObject(testCase); + verify (view !== null, "splitview created is null") + waitForRendering(view) + compare (view.__items.length, 3) + + // verify initial positions + compare (view.item1.x, 0) + compare (view.item1.y, 0) + compare (view.item1.width, 100) + compare (view.item1.height, 500) + + compare (view.item2.x, 100 + handleWidth) + compare (view.item2.y, 0) + compare (view.item2.width, view.item3.x - view.item2.x - handleWidth) + compare (view.item2.height, 500) + + compare (view.item3.x, 300) + compare (view.item3.y, 0) + compare (view.item3.width, 100) + compare (view.item3.height, 500) + + // remove center item and verify the position of the other two + // the last should fill width since there's no other item with fillwidth=true + view.removeItem(view.item2) + waitForRendering(view) + compare (view.__items.length, 2) + + compare (view.item1.x, 0) + compare (view.item1.y, 0) + compare (view.item1.width, 100) + compare (view.item1.height, 500) + + compare (view.item3.x, 100 + handleWidth) + compare (view.item3.y, 0) + compare (view.item3.width, view.width - view.item1.width - handleWidth) + compare (view.item3.height, 500) + + // remove first item the last should fill width since there's no other + // item with fillwidth=true + view.removeItem(view.item1) + waitForRendering(view) + compare (view.__items.length, 1) + + compare (view.item3.x, 0) + compare (view.item3.y, 0) + compare (view.item3.width, 400) + compare (view.item3.height, 500) + + // remove the last item + view.removeItem(view.item3) + waitForRendering(view) + compare (view.__items.length, 0) + } } |
