Slider.qml 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import QtQuick.Templates as T
  6. import QtQuick.Controls.impl
  7. import QtQuick.Controls.Material
  8. import QtQuick.Controls.Material.impl
  9. T.Slider {
  10. id: control
  11. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  12. implicitHandleWidth + leftPadding + rightPadding)
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. implicitHandleHeight + topPadding + bottomPadding)
  15. padding: 6
  16. // The Slider is discrete if all of the following requirements are met:
  17. // * stepSize is positive
  18. // * snapMode is set to SnapAlways
  19. // * the difference between to and from is cleanly divisible by the stepSize
  20. // * the number of tick marks intended to be rendered is less than the width to height ratio, or vice versa for vertical sliders.
  21. readonly property real __steps: Math.abs(to - from) / stepSize
  22. readonly property bool __isDiscrete: stepSize >= Number.EPSILON
  23. && snapMode === Slider.SnapAlways
  24. && Math.abs(Math.round(__steps) - __steps) < Number.EPSILON
  25. && Math.floor(__steps) < (horizontal ? background.width / background.height : background.height / background.width)
  26. handle: SliderHandle {
  27. x: control.leftPadding + (control.horizontal ? control.visualPosition * (control.availableWidth - width) : (control.availableWidth - width) / 2)
  28. y: control.topPadding + (control.horizontal ? (control.availableHeight - height) / 2 : control.visualPosition * (control.availableHeight - height))
  29. value: control.value
  30. handleHasFocus: control.visualFocus
  31. handlePressed: control.pressed
  32. handleHovered: control.hovered
  33. }
  34. background: Item {
  35. x: control.leftPadding + (control.horizontal ? 0 : (control.availableWidth - width) / 2)
  36. y: control.topPadding + (control.horizontal ? (control.availableHeight - height) / 2 : 0)
  37. implicitWidth: control.horizontal ? 200 : 48
  38. implicitHeight: control.horizontal ? 48 : 200
  39. width: control.horizontal ? control.availableWidth : 4
  40. height: control.horizontal ? 4 : control.availableHeight
  41. Rectangle {
  42. x: (control.horizontal ? (control.implicitHandleWidth / 2) - (control.__isDiscrete ? 2 : 0) : 0)
  43. y: (control.horizontal ? 0 : (control.implicitHandleHeight / 2) - (control.__isDiscrete ? 2 : 0))
  44. width: parent.width - (control.horizontal ? (control.implicitHandleWidth - (control.__isDiscrete ? 4 : 0)) : 0)
  45. height: parent.height - (control.horizontal ? 0 : (control.implicitHandleHeight - (control.__isDiscrete ? 4 : 0)))
  46. scale: control.horizontal && control.mirrored ? -1 : 1
  47. radius: Math.min(width, height) / 2
  48. color: control.enabled ? Color.transparent(control.Material.accentColor, 0.33) : control.Material.sliderDisabledColor
  49. Rectangle {
  50. x: control.horizontal ? 0 : (parent.width - width) / 2
  51. y: control.horizontal ? (parent.height - height) / 2 : control.visualPosition * parent.height
  52. width: control.horizontal ? control.position * parent.width : 4
  53. height: control.horizontal ? 4 : control.position * parent.height
  54. radius: Math.min(width, height) / 2
  55. color: control.enabled ? control.Material.accentColor : control.Material.sliderDisabledColor
  56. }
  57. // Declaring this as a property (in combination with the parent binding below) avoids ids,
  58. // which prevent deferred execution.
  59. property Repeater repeater: Repeater {
  60. parent: control.background.children[0]
  61. model: control.__isDiscrete ? Math.floor(control.__steps) + 1 : 0
  62. delegate: Rectangle {
  63. width: 2
  64. height: 2
  65. radius: 2
  66. x: control.horizontal ? (parent.width - width * 2) * currentPosition + (width / 2) : (parent.width - width) / 2
  67. y: control.horizontal ? (parent.height - height) / 2 : (parent.height - height * 2) * currentPosition + (height / 2)
  68. color: (control.horizontal && control.visualPosition > currentPosition)
  69. || (!control.horizontal && control.visualPosition <= currentPosition)
  70. ? control.Material.primaryHighlightedTextColor : control.Material.accentColor
  71. required property int index
  72. readonly property real currentPosition: index / (parent.repeater.count - 1)
  73. }
  74. }
  75. }
  76. }
  77. }