RangeSlider.qml 5.7 KB

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