ScrollBar.qml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 QtQuick 2.2
  40. import QtQuick.Controls 1.2
  41. import QtQuick.Controls.Private 1.0
  42. /*!
  43. \qmltype ScrollBar
  44. \internal
  45. \inqmlmodule QtQuick.Controls.Private
  46. */
  47. Item {
  48. id: scrollbar
  49. property bool isTransient: false
  50. property bool active: false
  51. property int orientation: Qt.Horizontal
  52. property alias minimumValue: slider.minimumValue
  53. property alias maximumValue: slider.maximumValue
  54. property alias value: slider.value
  55. property int singleStep: 20
  56. activeFocusOnTab: false
  57. Accessible.role: Accessible.ScrollBar
  58. implicitWidth: panelLoader.implicitWidth
  59. implicitHeight: panelLoader.implicitHeight
  60. property bool upPressed
  61. property bool downPressed
  62. property bool pageUpPressed
  63. property bool pageDownPressed
  64. property bool handlePressed
  65. property Item __panel: panelLoader.item
  66. Loader {
  67. id: panelLoader
  68. anchors.fill: parent
  69. sourceComponent: __style ? __style.__scrollbar : null
  70. onStatusChanged: if (status === Loader.Error) console.error("Failed to load Style for", root)
  71. property alias __control: scrollbar
  72. property QtObject __styleData: QtObject {
  73. readonly property alias horizontal: internal.horizontal
  74. readonly property alias upPressed: scrollbar.upPressed
  75. readonly property alias downPressed: scrollbar.downPressed
  76. readonly property alias handlePressed: scrollbar.handlePressed
  77. }
  78. }
  79. MouseArea {
  80. id: internal
  81. property bool horizontal: orientation === Qt.Horizontal
  82. property int pageStep: internal.horizontal ? width : height
  83. property bool scrollToClickposition: internal.scrollToClickPosition
  84. anchors.fill: parent
  85. cursorShape: __panel && __panel.visible ? Qt.ArrowCursor : Qt.IBeamCursor // forces a cursor change
  86. property bool autoincrement: false
  87. property bool scrollToClickPosition: __style ? __style.scrollToClickedPosition : 0
  88. // Update hover item
  89. onEntered: if (!pressed) __panel.activeControl = __panel.hitTest(mouseX, mouseY)
  90. onExited: if (!pressed) __panel.activeControl = "none"
  91. onMouseXChanged: if (!pressed) __panel.activeControl = __panel.hitTest(mouseX, mouseY)
  92. hoverEnabled: Settings.hoverEnabled
  93. preventStealing: true
  94. property var pressedX
  95. property var pressedY
  96. property int oldPosition
  97. property int grooveSize
  98. Timer {
  99. running: upPressed || downPressed || pageUpPressed || pageDownPressed
  100. interval: 350
  101. onTriggered: internal.autoincrement = true
  102. }
  103. Timer {
  104. running: internal.autoincrement
  105. interval: 60
  106. repeat: true
  107. onTriggered: {
  108. if (upPressed && internal.containsMouse)
  109. internal.decrement();
  110. else if (downPressed && internal.containsMouse)
  111. internal.increment();
  112. else if (pageUpPressed)
  113. internal.decrementPage();
  114. else if (pageDownPressed)
  115. internal.incrementPage();
  116. }
  117. }
  118. onPositionChanged: {
  119. if (handlePressed) {
  120. if (!horizontal)
  121. slider.position = oldPosition + (mouseY - pressedY)
  122. else
  123. slider.position = oldPosition + (mouseX - pressedX)
  124. }
  125. }
  126. onPressed: {
  127. if (mouse.source !== Qt.MouseEventNotSynthesized) {
  128. mouse.accepted = false
  129. return
  130. }
  131. __panel.activeControl = __panel.hitTest(mouseX, mouseY)
  132. scrollToClickposition = scrollToClickPosition
  133. var handleRect = __panel.subControlRect("handle")
  134. var grooveRect = __panel.subControlRect("groove")
  135. grooveSize = horizontal ? grooveRect.width - handleRect.width:
  136. grooveRect.height - handleRect.height;
  137. if (__panel.activeControl === "handle") {
  138. pressedX = mouseX;
  139. pressedY = mouseY;
  140. handlePressed = true;
  141. oldPosition = slider.position;
  142. } else if (__panel.activeControl === "up") {
  143. decrement();
  144. upPressed = Qt.binding(function() {return containsMouse});
  145. } else if (__panel.activeControl === "down") {
  146. increment();
  147. downPressed = Qt.binding(function() {return containsMouse});
  148. } else if (!scrollToClickposition){
  149. if (__panel.activeControl === "upPage") {
  150. decrementPage();
  151. pageUpPressed = true;
  152. } else if (__panel.activeControl === "downPage") {
  153. incrementPage();
  154. pageDownPressed = true;
  155. }
  156. } else { // scroll to click position
  157. slider.position = horizontal ? mouseX - handleRect.width/2 - grooveRect.x
  158. : mouseY - handleRect.height/2 - grooveRect.y
  159. pressedX = mouseX;
  160. pressedY = mouseY;
  161. handlePressed = true;
  162. oldPosition = slider.position;
  163. }
  164. }
  165. onReleased: {
  166. __panel.activeControl = __panel.hitTest(mouseX, mouseY);
  167. autoincrement = false;
  168. upPressed = false;
  169. downPressed = false;
  170. handlePressed = false;
  171. pageUpPressed = false;
  172. pageDownPressed = false;
  173. }
  174. onWheel: {
  175. var stepCount = -(wheel.angleDelta.x ? wheel.angleDelta.x : wheel.angleDelta.y) / 120
  176. if (stepCount != 0) {
  177. if (wheel.modifiers & Qt.ControlModifier || wheel.modifiers & Qt.ShiftModifier)
  178. incrementPage(stepCount)
  179. else
  180. increment(stepCount)
  181. }
  182. }
  183. function incrementPage(stepCount) {
  184. value = boundValue(value + getSteps(pageStep, stepCount))
  185. }
  186. function decrementPage(stepCount) {
  187. value = boundValue(value - getSteps(pageStep, stepCount))
  188. }
  189. function increment(stepCount) {
  190. value = boundValue(value + getSteps(singleStep, stepCount))
  191. }
  192. function decrement(stepCount) {
  193. value = boundValue(value - getSteps(singleStep, stepCount))
  194. }
  195. function boundValue(val) {
  196. return Math.min(Math.max(val, minimumValue), maximumValue)
  197. }
  198. function getSteps(step, count) {
  199. if (count)
  200. step *= count
  201. return step
  202. }
  203. RangeModel {
  204. id: slider
  205. minimumValue: 0.0
  206. maximumValue: 1.0
  207. value: 0
  208. stepSize: 0.0
  209. inverted: false
  210. positionAtMaximum: internal.grooveSize
  211. }
  212. }
  213. }