Commit a6deb27b authored by J-P Nurmi's avatar J-P Nurmi Committed by The Qt Project
Browse files

Fix TabView not to insert duplicate tabs


There are three ways to add tabs:
- declare 'static' Tab { } child elements
- call addTab() / insertTab()
- Component::createObject(tabView)

=> Do not rely on the calling order of Component.onCompleted()
and make sure that the same tabs are not accidentally re-inserted.

Task-number: QTBUG-30873
Change-Id: Ib30cfb676debbb302c5e9f7d757f66aab6fcc684
Reviewed-by: default avatarCaroline Chao <caroline.chao@digia.com>
parent bb7e55b7
6.2 5.10 5.11 5.12 5.12.1 5.12.10 5.12.11 5.12.12 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.12.7 5.12.8 5.12.9 5.13 5.13.0 5.13.1 5.13.2 5.14 5.14.0 5.14.1 5.14.2 5.15 5.15.0 5.15.1 5.15.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.9.8 dev old/5.1 old/5.2 wip/calendar wip/tizen v5.15.0-alpha1 v5.14.1 v5.14.0 v5.14.0-rc2 v5.14.0-rc1 v5.14.0-beta3 v5.14.0-beta2 v5.14.0-beta1 v5.14.0-alpha1 v5.13.2 v5.13.1 v5.13.0 v5.13.0-rc3 v5.13.0-rc2 v5.13.0-rc1 v5.13.0-beta4 v5.13.0-beta3 v5.13.0-beta2 v5.13.0-beta1 v5.13.0-alpha1 v5.12.7 v5.12.6 v5.12.5 v5.12.4 v5.12.3 v5.12.2 v5.12.1 v5.12.0 v5.12.0-rc2 v5.12.0-rc1 v5.12.0-beta4 v5.12.0-beta3 v5.12.0-beta2 v5.12.0-beta1 v5.12.0-alpha1 v5.11.3 v5.11.2 v5.11.1 v5.11.0 v5.11.0-rc2 v5.11.0-rc1 v5.11.0-beta4 v5.11.0-beta3 v5.11.0-beta2 v5.11.0-beta1 v5.11.0-alpha1 v5.10.1 v5.10.0 v5.10.0-rc3 v5.10.0-rc2 v5.10.0-rc1 v5.10.0-beta4 v5.10.0-beta3 v5.10.0-beta2 v5.10.0-beta1 v5.10.0-alpha1 v5.9.9 v5.9.8 v5.9.7 v5.9.6 v5.9.5 v5.9.4 v5.9.3 v5.9.2 v5.9.1 v5.9.0 v5.9.0-rc2 v5.9.0-rc1 v5.9.0-beta4 v5.9.0-beta3 v5.9.0-beta2 v5.9.0-beta1 v5.9.0-alpha1 v5.8.0 v5.8.0-rc1 v5.8.0-beta1 v5.8.0-alpha1 v5.7.1 v5.7.0 v5.7.0-rc1 v5.7.0-beta1 v5.7.0-alpha1 v5.6.3 v5.6.2 v5.6.1 v5.6.1-1 v5.6.0 v5.6.0-rc1 v5.6.0-beta1 v5.6.0-alpha1 v5.5.1 v5.5.0 v5.5.0-rc1 v5.5.0-beta1 v5.5.0-alpha1 v5.4.2 v5.4.1 v5.4.0 v5.4.0-rc1 v5.4.0-beta1 v5.4.0-alpha1 v5.3.2 v5.3.1 v5.3.0 v5.3.0-rc1 v5.3.0-beta1 v5.3.0-alpha1 v5.2.1 v5.2.0 v5.2.0-rc1 v5.2.0-beta1 v5.2.0-alpha1 v5.1.1 v5.1.0 v5.1.0-rc2 v5.1.0-rc1 v5.1.0-beta1
No related merge requests found
Showing with 54 additions and 37 deletions
...@@ -57,6 +57,10 @@ Loader { ...@@ -57,6 +57,10 @@ Loader {
/*! This property holds the title of the tab. */ /*! This property holds the title of the tab. */
property string title property string title
/*! \internal */
property bool __inserted: false
Accessible.role: Accessible.LayeredPane Accessible.role: Accessible.LayeredPane
active: false active: false
visible: false visible: false
......
...@@ -85,13 +85,7 @@ FocusScope { ...@@ -85,13 +85,7 @@ FocusScope {
Returns the newly added tab. Returns the newly added tab.
*/ */
function addTab(title, component) { function addTab(title, component) {
var tab = tabcomp.createObject(stack) return insertTab(__tabs.count, title, component)
tab.sourceComponent = component
__tabs.append({tab: tab})
tab.parent = stack
tab.title = title
__setOpacities()
return tab
} }
/*! Inserts a new tab with title at index, with an optional Component. /*! Inserts a new tab with title at index, with an optional Component.
...@@ -102,6 +96,7 @@ FocusScope { ...@@ -102,6 +96,7 @@ FocusScope {
tab.sourceComponent = component tab.sourceComponent = component
tab.parent = stack tab.parent = stack
tab.title = title tab.title = title
tab.__inserted = true
__tabs.insert(index, {tab: tab}) __tabs.insert(index, {tab: tab})
__setOpacities() __setOpacities()
return tab return tab
...@@ -206,12 +201,26 @@ FocusScope { ...@@ -206,12 +201,26 @@ FocusScope {
property string style property string style
property int baseOverlap property int baseOverlap
Component.onCompleted: { Component.onCompleted: addTabs(stack.children)
for (var i = 0 ; i < stack.children.length ; ++i) {
if (stack.children[i].Accessible.role === Accessible.LayeredPane) function addTabs(tabs) {
__tabs.append({tab: stack.children[i]}) var tabAdded = false
for (var i = 0 ; i < tabs.length ; ++i) {
var tab = tabs[i]
if (!tab.__inserted && tab.Accessible.role === Accessible.LayeredPane) {
tab.__inserted = true
if (tab.parent === root) {
tab.parent = stack
// a tab added dynamically by Component::createObject() and passing the
// tab view as a parent should also get automatically removed when destructed
tab.Component.onDestruction.connect(stack.onDynamicTabDestroyed.bind(tab))
}
__tabs.append({tab: tab})
tabAdded = true
}
} }
__setOpacities() if (tabAdded)
__setOpacities()
} }
function onDynamicTabDestroyed() { function onDynamicTabDestroyed() {
...@@ -226,20 +235,7 @@ FocusScope { ...@@ -226,20 +235,7 @@ FocusScope {
onLoaded: { item.z = -1 } onLoaded: { item.z = -1 }
} }
onChildrenChanged: { onChildrenChanged: stack.addTabs(root.children)
var tabAdded = false
for (var i = 0; i < children.length; ++i) {
var child = children[i]
if (child.Accessible.role === Accessible.LayeredPane) {
__tabs.append({tab: child})
child.parent = stack
child.Component.onDestruction.connect(stack.onDynamicTabDestroyed.bind(child))
tabAdded = true
}
}
if (tabAdded)
__setOpacities()
}
states: [ states: [
State { State {
......
...@@ -192,18 +192,35 @@ TestCase { ...@@ -192,18 +192,35 @@ TestCase {
} }
function test_dynamicTabs() { function test_dynamicTabs() {
var tabView = Qt.createQmlObject('import QtQuick 2.1; import QtQuick.Controls 1.0; TabView { property Component tabComponent: Component { Tab { } } }', testCase, ''); var test_tabView = ' \
compare(tabView.count, 0) import QtQuick 2.1; \
var tab1 = tabView.tabComponent.createObject(tabView) import QtQuick.Controls 1.0; \
compare(tabView.count, 1) TabView { \
var tab2 = tabView.tabComponent.createObject(tabView) id: tabView; \
compare(tabView.count, 2) Tab { title: "static" } \
tab1.destroy() property Component tabComponent: Component { \
wait(0) id: tabComponent; \
compare(tabView.count, 1) Tab { title: "dynamic" } \
tab2.destroy() } \
Component.onCompleted: { \
addTab("added", tabComponent); \
insertTab(0, "inserted", tabComponent); \
tabComponent.createObject(tabView); \
} \
} '
var tabView = Qt.createQmlObject(test_tabView, testCase, '')
// insertTab(), addTab(), createObject() and static Tab {}
compare(tabView.count, 4)
compare(tabView.tabAt(0).title, "inserted")
var tab = tabView.tabComponent.createObject(tabView)
compare(tabView.count, 5)
compare(tabView.tabAt(4).title, "dynamic")
tab.destroy()
wait(0) wait(0)
compare(tabView.count, 0) compare(tabView.count, 4)
tabView.destroy()
} }
function test_mousePressOnTabBar() { function test_mousePressOnTabBar() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment