diff options
-rw-r--r-- | src/controls/TabView.qml | 19 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_tabview.qml | 35 |
2 files changed, 52 insertions, 2 deletions
diff --git a/src/controls/TabView.qml b/src/controls/TabView.qml index 5be224ce..440606e0 100644 --- a/src/controls/TabView.qml +++ b/src/controls/TabView.qml @@ -103,6 +103,7 @@ FocusScope { __tabs.insert(index, {tab: tab}) tab.__inserted = true tab.parent = stack + __didInsertIndex(index) __setOpacities() return tab } @@ -110,10 +111,9 @@ FocusScope { /*! Removes and destroys a tab at the given \a index. */ function removeTab(index) { var tab = __tabs.get(index).tab + __willRemoveIndex(index) __tabs.remove(index, 1) tab.destroy() - if (currentIndex > 0) - currentIndex-- __setOpacities() } @@ -153,6 +153,19 @@ FocusScope { onCurrentIndexChanged: __setOpacities() /*! \internal */ + function __willRemoveIndex(index) { + // Make sure currentIndex will points to the same tab after the removal. + // Also activate the next index if the current index is being removed, + // except when it's both the current and last index. + if (count > 1 && (currentIndex > index || currentIndex == count -1)) + --currentIndex + } + function __didInsertIndex(index) { + // Make sure currentIndex points to the same tab as before the insertion. + if (count > 1 && currentIndex >= index) + currentIndex++ + } + function __setOpacities() { for (var i = 0; i < __tabs.count; ++i) { var child = __tabs.get(i).tab @@ -229,6 +242,7 @@ FocusScope { if (completed) tab.Component.onDestruction.connect(stack.onDynamicTabDestroyed.bind(tab)) __tabs.append({tab: tab}) + __didInsertIndex(__tabs.count - 1) tabAdded = true } } @@ -239,6 +253,7 @@ FocusScope { function onDynamicTabDestroyed() { for (var i = 0; i < __tabs.count; ++i) { if (__tabs.get(i).tab === this) { + __willRemoveIndex(i) __tabs.remove(i, 1) __setOpacities() break diff --git a/tests/auto/controls/data/tst_tabview.qml b/tests/auto/controls/data/tst_tabview.qml index b7de67ba..9e556dd1 100644 --- a/tests/auto/controls/data/tst_tabview.qml +++ b/tests/auto/controls/data/tst_tabview.qml @@ -84,22 +84,53 @@ TestCase { function test_addRemoveTab() { var tabView = Qt.createQmlObject('import QtQuick 2.2; import QtQuick.Controls 1.2; TabView { }', testCase, ''); + + function verifyCurrentIndexCountDiff() { + verify(!tabView.currentIndex || tabView.count > tabView.currentIndex) + } + tabView.currentIndexChanged.connect(verifyCurrentIndexCountDiff) + tabView.countChanged.connect(verifyCurrentIndexCountDiff) + compare(tabView.count, 0) + compare(tabView.currentIndex, 0) tabView.addTab("title 1", newTab) compare(tabView.count, 1) + compare(tabView.currentIndex, 0) tabView.addTab("title 2", newTab) compare(tabView.count, 2) + compare(tabView.currentIndex, 0) compare(tabView.getTab(0).title, "title 1") compare(tabView.getTab(1).title, "title 2") + tabView.currentIndex = 1 + tabView.insertTab(1, "title 3") compare(tabView.count, 3) + compare(tabView.currentIndex, 2) compare(tabView.getTab(0).title, "title 1") compare(tabView.getTab(1).title, "title 3") compare(tabView.getTab(2).title, "title 2") tabView.insertTab(0, "title 4") compare(tabView.count, 4) + compare(tabView.currentIndex, 3) + compare(tabView.getTab(0).title, "title 4") + compare(tabView.getTab(1).title, "title 1") + compare(tabView.getTab(2).title, "title 3") + compare(tabView.getTab(3).title, "title 2") + + tabView.insertTab(tabView.count, "title 5") + compare(tabView.count, 5) + compare(tabView.currentIndex, 3) + compare(tabView.getTab(0).title, "title 4") + compare(tabView.getTab(1).title, "title 1") + compare(tabView.getTab(2).title, "title 3") + compare(tabView.getTab(3).title, "title 2") + compare(tabView.getTab(4).title, "title 5") + + tabView.removeTab(tabView.count - 1) + compare(tabView.count, 4) + compare(tabView.currentIndex, 3) compare(tabView.getTab(0).title, "title 4") compare(tabView.getTab(1).title, "title 1") compare(tabView.getTab(2).title, "title 3") @@ -107,21 +138,25 @@ TestCase { tabView.removeTab(0) compare(tabView.count, 3) + compare(tabView.currentIndex, 2) compare(tabView.getTab(0).title, "title 1") compare(tabView.getTab(1).title, "title 3") compare(tabView.getTab(2).title, "title 2") tabView.removeTab(1) compare(tabView.count, 2) + compare(tabView.currentIndex, 1) compare(tabView.getTab(0).title, "title 1") compare(tabView.getTab(1).title, "title 2") tabView.removeTab(1) compare(tabView.count, 1) + compare(tabView.currentIndex, 0) compare(tabView.getTab(0).title, "title 1") tabView.removeTab(0) compare(tabView.count, 0) + compare(tabView.currentIndex, 0) tabView.destroy() } |