summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJocelyn Turcotte <jocelyn.turcotte@digia.com>2014-05-06 13:45:59 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-05-07 14:02:20 +0200
commit7c7e510b6b4fb84d248059b9c55f58490fccc339 (patch)
tree7e5b0b78880c82eb42bfd6354eaafda1d12f0d8b
parenta84bb8aa5c24083c7ef54951a98bc62e7526da20 (diff)
downloadqtquickcontrols-7c7e510b6b4fb84d248059b9c55f58490fccc339.tar.gz
Fix the update of TabView.currentIndex when inserting/removing tabs
- Update currentIndex when adding or inserting tabs. - Make sure that we update the current index before updating the array to avoid a state of currentIndex > count -1 when removing the last tab, where a property binding on TabView.count could try fetching a tab past the array limit through TabView.getTab. - Only decrease currentIndex when removedIndex <= currentIndex to keep the current tab active when possible. Activate the next tab instead of the previous one when the current tab is removed to make it more instinctive to remove multiple tabs successively. - Keep the current behavior of leaving currentIndex to 0 when removing the last tab. Change-Id: I030b86a03ce73f1177b1f04857abbdc2f7ec5835 Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
-rw-r--r--src/controls/TabView.qml19
-rw-r--r--tests/auto/controls/data/tst_tabview.qml35
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()
}