TabBar.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQml 2.14 as Qml
  40. import QtQuick 2.2
  41. import QtQuick.Controls 1.2
  42. import QtQuick.Controls.Private 1.0
  43. /*!
  44. \qmltype TabBar
  45. \internal
  46. \inqmlmodule QtQuick.Controls.Private
  47. */
  48. FocusScope {
  49. id: tabbar
  50. height: Math.max(tabrow.height, Math.max(leftCorner.height, rightCorner.height))
  51. width: tabView.width
  52. activeFocusOnTab: true
  53. Keys.onRightPressed: {
  54. if (tabView && tabView.currentIndex < tabView.count - 1)
  55. tabView.currentIndex = tabView.currentIndex + 1
  56. }
  57. Keys.onLeftPressed: {
  58. if (tabView && tabView.currentIndex > 0)
  59. tabView.currentIndex = tabView.currentIndex - 1
  60. }
  61. onTabViewChanged: parent = tabView
  62. visible: tabView ? tabView.tabsVisible : true
  63. property var tabView
  64. property var style
  65. property var styleItem: tabView.__styleItem ? tabView.__styleItem : null
  66. property bool tabsMovable: styleItem ? styleItem.tabsMovable : false
  67. property int tabsAlignment: styleItem ? styleItem.tabsAlignment : Qt.AlignLeft
  68. property int tabOverlap: styleItem ? styleItem.tabOverlap : 0
  69. property int elide: Text.ElideRight
  70. property real availableWidth: tabbar.width - leftCorner.width - rightCorner.width
  71. property var __selectedTabRect
  72. function tab(index) {
  73. for (var i = 0; i < tabrow.children.length; ++i) {
  74. if (tabrow.children[i].tabindex == index) {
  75. return tabrow.children[i]
  76. }
  77. }
  78. return null;
  79. }
  80. /*! \internal */
  81. function __isAncestorOf(item, child) {
  82. //TODO: maybe removed from 5.2 if the function was merged in qtdeclarative
  83. if (child === item)
  84. return false;
  85. while (child) {
  86. child = child.parent;
  87. if (child === item)
  88. return true;
  89. }
  90. return false;
  91. }
  92. Loader {
  93. id: background
  94. anchors.fill: parent
  95. sourceComponent: styleItem ? styleItem.tabBar : undefined
  96. }
  97. ListView {
  98. id: tabrow
  99. objectName: "tabrow"
  100. Accessible.role: Accessible.PageTabList
  101. LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
  102. spacing: -tabOverlap
  103. orientation: Qt.Horizontal
  104. interactive: false
  105. focus: true
  106. clip: true
  107. // Note this will silence the binding loop warnings caused by QTBUG-35038
  108. // and should be removed when this issue is resolved.
  109. property int contentWidthWorkaround: contentWidth > 0 ? contentWidth: 0
  110. width: Math.min(availableWidth, count ? contentWidthWorkaround : availableWidth)
  111. height: currentItem ? currentItem.height : 0
  112. highlightMoveDuration: 0
  113. // We cannot bind directly to the currentIndex because the actual model is
  114. // populated after the listview is completed, resulting in an invalid contentItem
  115. currentIndex: tabView.currentIndex < model.count ? tabView.currentIndex : -1
  116. onCurrentIndexChanged: tabrow.positionViewAtIndex(currentIndex, ListView.Contain)
  117. moveDisplaced: Transition {
  118. NumberAnimation {
  119. property: "x"
  120. duration: 125
  121. easing.type: Easing.OutQuad
  122. }
  123. }
  124. states: [
  125. State {
  126. name: "left"
  127. when: tabsAlignment === Qt.AlignLeft
  128. AnchorChanges { target:tabrow ; anchors.left: parent.left }
  129. PropertyChanges { target:tabrow ; anchors.leftMargin: leftCorner.width }
  130. },
  131. State {
  132. name: "center"
  133. when: tabsAlignment === Qt.AlignHCenter
  134. AnchorChanges { target:tabrow ; anchors.horizontalCenter: tabbar.horizontalCenter }
  135. },
  136. State {
  137. name: "right"
  138. when: tabsAlignment === Qt.AlignRight
  139. AnchorChanges { target:tabrow ; anchors.right: parent.right }
  140. PropertyChanges { target:tabrow ; anchors.rightMargin: rightCorner.width }
  141. }
  142. ]
  143. model: tabView.__tabs
  144. delegate: MouseArea {
  145. id: tabitem
  146. objectName: "mousearea"
  147. hoverEnabled: Settings.hoverEnabled
  148. focus: true
  149. enabled: modelData.enabled
  150. Qml.Binding {
  151. target: tabbar
  152. when: selected
  153. property: "__selectedTabRect"
  154. value: Qt.rect(x, y, width, height)
  155. restoreMode: Binding.RestoreBinding
  156. }
  157. drag.target: tabsMovable ? tabloader : null
  158. drag.axis: Drag.XAxis
  159. drag.minimumX: drag.active ? 0 : -Number.MAX_VALUE
  160. drag.maximumX: tabrow.width - tabitem.width
  161. property int tabindex: index
  162. property bool selected : tabView.currentIndex === index
  163. property string title: modelData.title
  164. property bool nextSelected: tabView.currentIndex === index + 1
  165. property bool previousSelected: tabView.currentIndex === index - 1
  166. property bool keyPressed: false
  167. property bool effectivePressed: pressed && containsMouse || keyPressed
  168. z: selected ? 1 : -index
  169. implicitWidth: tabloader.implicitWidth
  170. implicitHeight: tabloader.implicitHeight
  171. function changeTab() {
  172. tabView.currentIndex = index;
  173. var next = tabbar.nextItemInFocusChain(true);
  174. if (__isAncestorOf(tabView.getTab(currentIndex), next))
  175. next.forceActiveFocus();
  176. }
  177. onClicked: {
  178. if (tabrow.interactive) {
  179. changeTab()
  180. }
  181. }
  182. onPressed: {
  183. if (!tabrow.interactive) {
  184. changeTab()
  185. }
  186. }
  187. Keys.onPressed: {
  188. if (event.key === Qt.Key_Space && !event.isAutoRepeat && !tabitem.pressed)
  189. tabitem.keyPressed = true
  190. }
  191. Keys.onReleased: {
  192. if (event.key === Qt.Key_Space && !event.isAutoRepeat && tabitem.keyPressed)
  193. tabitem.keyPressed = false
  194. }
  195. onFocusChanged: if (!focus) tabitem.keyPressed = false
  196. Loader {
  197. id: tabloader
  198. property Item control: tabView
  199. property int index: tabindex
  200. property QtObject styleData: QtObject {
  201. readonly property alias index: tabitem.tabindex
  202. readonly property alias selected: tabitem.selected
  203. readonly property alias title: tabitem.title
  204. readonly property alias nextSelected: tabitem.nextSelected
  205. readonly property alias previousSelected: tabitem.previousSelected
  206. readonly property alias pressed: tabitem.effectivePressed
  207. readonly property alias hovered: tabitem.containsMouse
  208. readonly property alias enabled: tabitem.enabled
  209. readonly property bool activeFocus: tabitem.activeFocus
  210. readonly property real availableWidth: tabbar.availableWidth
  211. readonly property real totalWidth: tabrow.contentWidth
  212. }
  213. sourceComponent: loader.item ? loader.item.tab : null
  214. Drag.keys: "application/x-tabbartab"
  215. Drag.active: tabitem.drag.active
  216. Drag.source: tabitem
  217. property real __prevX: 0
  218. property real __dragX: 0
  219. onXChanged: {
  220. if (Drag.active) {
  221. // keep track for the snap back animation
  222. __dragX = tabitem.mapFromItem(tabrow, tabloader.x, 0).x
  223. // when moving to the left, the hot spot is the left edge and vice versa
  224. Drag.hotSpot.x = x < __prevX ? 0 : width
  225. __prevX = x
  226. }
  227. }
  228. width: tabitem.width
  229. state: Drag.active ? "drag" : ""
  230. transitions: [
  231. Transition {
  232. to: "drag"
  233. PropertyAction { target: tabloader; property: "parent"; value: tabrow }
  234. },
  235. Transition {
  236. from: "drag"
  237. SequentialAnimation {
  238. PropertyAction { target: tabloader; property: "parent"; value: tabitem }
  239. NumberAnimation {
  240. target: tabloader
  241. duration: 50
  242. easing.type: Easing.OutQuad
  243. property: "x"
  244. from: tabloader.__dragX
  245. to: 0
  246. }
  247. }
  248. }
  249. ]
  250. }
  251. Accessible.role: Accessible.PageTab
  252. Accessible.name: modelData.title
  253. }
  254. }
  255. Loader {
  256. id: leftCorner
  257. anchors.verticalCenter: parent.verticalCenter
  258. anchors.left: parent.left
  259. sourceComponent: styleItem ? styleItem.leftCorner : undefined
  260. width: item ? item.implicitWidth : 0
  261. height: item ? item.implicitHeight : 0
  262. }
  263. Loader {
  264. id: rightCorner
  265. anchors.verticalCenter: parent.verticalCenter
  266. anchors.right: parent.right
  267. sourceComponent: styleItem ? styleItem.rightCorner : undefined
  268. width: item ? item.implicitWidth : 0
  269. height: item ? item.implicitHeight : 0
  270. }
  271. DropArea {
  272. anchors.fill: tabrow
  273. keys: "application/x-tabbartab"
  274. onPositionChanged: {
  275. var source = drag.source
  276. var target = tabrow.itemAt(drag.x, drag.y)
  277. if (source && target && source !== target) {
  278. source = source.drag.target
  279. target = target.drag.target
  280. var center = target.parent.x + target.width / 2
  281. if ((source.index > target.index && source.x < center)
  282. || (source.index < target.index && source.x + source.width > center))
  283. tabView.moveTab(source.index, target.index)
  284. }
  285. }
  286. }
  287. }