ScrollViewHelper.qml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 ScrollViewHeader
  45. \internal
  46. \inqmlmodule QtQuick.Controls.Private
  47. */
  48. Item {
  49. id: scrollHelper
  50. property alias horizontalScrollBar: hscrollbar
  51. property alias verticalScrollBar: vscrollbar
  52. property bool blockUpdates: false
  53. property int availableHeight
  54. property int availableWidth
  55. property int contentHeight
  56. property int contentWidth
  57. property bool active
  58. property int horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded
  59. property int verticalScrollBarPolicy: Qt.ScrollBarAsNeeded
  60. property int leftMargin: outerFrame && root.__style ? root.__style.padding.left : 0
  61. property int rightMargin: outerFrame && root.__style ? root.__style.padding.right : 0
  62. property int topMargin: outerFrame && root.__style ? root.__style.padding.top : 0
  63. property int bottomMargin: outerFrame && root.__style ? root.__style.padding.bottom : 0
  64. anchors.fill: parent
  65. Timer {
  66. id: layoutTimer
  67. interval: 0;
  68. onTriggered: {
  69. blockUpdates = true;
  70. scrollHelper.contentWidth = flickableItem !== null ? flickableItem.contentWidth : 0
  71. scrollHelper.contentHeight = flickableItem !== null ? flickableItem.contentHeight : 0
  72. scrollHelper.availableWidth = viewport.width
  73. scrollHelper.availableHeight = viewport.height
  74. blockUpdates = false;
  75. hscrollbar.valueChanged();
  76. vscrollbar.valueChanged();
  77. }
  78. }
  79. Connections {
  80. target: viewport
  81. function onWidthChanged() { layoutTimer.running = true }
  82. function onHeightChanged() { layoutTimer.running = true }
  83. }
  84. Connections {
  85. target: flickableItem
  86. function onContentWidthChanged() { layoutTimer.running = true }
  87. function onContentHeightChanged() { layoutTimer.running = true }
  88. function onContentXChanged() {
  89. hscrollbar.flash()
  90. vscrollbar.flash()
  91. }
  92. function onContentYChanged() {
  93. hscrollbar.flash()
  94. vscrollbar.flash()
  95. }
  96. }
  97. Loader {
  98. id: cornerFill
  99. z: 1
  100. sourceComponent: __style ? __style.corner : null
  101. anchors.right: parent.right
  102. anchors.bottom: parent.bottom
  103. anchors.bottomMargin: bottomMargin
  104. anchors.rightMargin: rightMargin
  105. width: visible ? vscrollbar.width : 0
  106. height: visible ? hscrollbar.height : 0
  107. visible: hscrollbar.visible && !hscrollbar.isTransient && vscrollbar.visible && !vscrollbar.isTransient
  108. }
  109. ScrollBar {
  110. id: hscrollbar
  111. readonly property int scrollAmount: contentWidth - availableWidth
  112. readonly property bool scrollable: scrollAmount > 0
  113. isTransient: !!__panel && !!__panel.isTransient
  114. active: !!__panel && (__panel.sunken || __panel.activeControl !== "none")
  115. enabled: !isTransient || __panel.visible
  116. orientation: Qt.Horizontal
  117. visible: horizontalScrollBarPolicy == Qt.ScrollBarAsNeeded ? scrollable : horizontalScrollBarPolicy == Qt.ScrollBarAlwaysOn
  118. height: visible ? implicitHeight : 0
  119. z: 1
  120. maximumValue: scrollable ? scrollAmount : 0
  121. minimumValue: 0
  122. anchors.bottom: parent.bottom
  123. anchors.left: parent.left
  124. anchors.right: cornerFill.left
  125. anchors.leftMargin: leftMargin
  126. anchors.bottomMargin: bottomMargin
  127. onScrollAmountChanged: {
  128. var scrollableAmount = scrollable ? scrollAmount : 0
  129. if (flickableItem && (flickableItem.atXBeginning || flickableItem.atXEnd)) {
  130. value = Math.min(scrollableAmount, flickableItem.contentX - flickableItem.originX);
  131. } else if (value > scrollableAmount) {
  132. value = scrollableAmount;
  133. }
  134. }
  135. onValueChanged: {
  136. if (flickableItem && !blockUpdates) {
  137. flickableItem.contentX = value + flickableItem.originX
  138. }
  139. }
  140. Qml.Binding {
  141. target: hscrollbar.__panel
  142. property: "raised"
  143. value: vscrollbar.active || scrollHelper.active
  144. when: hscrollbar.isTransient
  145. restoreMode: Binding.RestoreBinding
  146. }
  147. Qml.Binding {
  148. target: hscrollbar.__panel
  149. property: "visible"
  150. value: true
  151. when: !hscrollbar.isTransient || scrollHelper.active
  152. restoreMode: Binding.RestoreBinding
  153. }
  154. function flash() {
  155. if (hscrollbar.isTransient) {
  156. hscrollbar.__panel.on = true
  157. hscrollbar.__panel.visible = true
  158. hFlasher.start()
  159. }
  160. }
  161. Timer {
  162. id: hFlasher
  163. interval: 10
  164. onTriggered: hscrollbar.__panel.on = false
  165. }
  166. }
  167. ScrollBar {
  168. id: vscrollbar
  169. readonly property int scrollAmount: contentHeight - availableHeight
  170. readonly property bool scrollable: scrollAmount > 0
  171. isTransient: !!__panel && !!__panel.isTransient
  172. active: !!__panel && (__panel.sunken || __panel.activeControl !== "none")
  173. enabled: !isTransient || __panel.visible
  174. orientation: Qt.Vertical
  175. visible: verticalScrollBarPolicy === Qt.ScrollBarAsNeeded ? scrollable : verticalScrollBarPolicy === Qt.ScrollBarAlwaysOn
  176. width: visible ? implicitWidth : 0
  177. z: 1
  178. anchors.bottom: cornerFill.top
  179. maximumValue: scrollable ? scrollAmount + __viewTopMargin : 0
  180. minimumValue: 0
  181. anchors.right: parent.right
  182. anchors.top: parent.top
  183. anchors.topMargin: __scrollBarTopMargin + topMargin
  184. anchors.rightMargin: rightMargin
  185. onScrollAmountChanged: {
  186. if (flickableItem && (flickableItem.atYBeginning || flickableItem.atYEnd)) {
  187. value = flickableItem.contentY - flickableItem.originY
  188. }
  189. }
  190. onValueChanged: {
  191. if (flickableItem && !blockUpdates && enabled) {
  192. flickableItem.contentY = value + flickableItem.originY
  193. }
  194. }
  195. Qml.Binding {
  196. target: vscrollbar.__panel
  197. property: "raised"
  198. value: hscrollbar.active || scrollHelper.active
  199. when: vscrollbar.isTransient
  200. restoreMode: Binding.RestoreBinding
  201. }
  202. Qml.Binding {
  203. target: vscrollbar.__panel
  204. property: "visible"
  205. value: true
  206. when: !vscrollbar.isTransient || scrollHelper.active
  207. restoreMode: Binding.RestoreBinding
  208. }
  209. function flash() {
  210. if (vscrollbar.isTransient) {
  211. vscrollbar.__panel.on = true
  212. vscrollbar.__panel.visible = true
  213. vFlasher.start()
  214. }
  215. }
  216. Timer {
  217. id: vFlasher
  218. interval: 10
  219. onTriggered: vscrollbar.__panel.on = false
  220. }
  221. }
  222. }