Slider.qml 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import QtQuick 2.0
  2. Item {
  3. id: root
  4. width: 200
  5. height: 112
  6. property real value: 0
  7. property real maximum: 1
  8. property real minimum: 0
  9. property int xMax: width - handle.width - 4
  10. property alias text: sliderText.text
  11. onValueChanged: updatePos();
  12. onXMaxChanged: updatePos();
  13. onMinimumChanged: updatePos();
  14. function updatePos() {
  15. if (maximum > minimum) {
  16. var pos = 2 + (value - minimum) * root.xMax / (maximum - minimum);
  17. pos = Math.min(pos, width - handle.width - 2);
  18. pos = Math.max(pos, 2);
  19. handle.x = pos;
  20. } else {
  21. handle.x = 2;
  22. }
  23. }
  24. Rectangle {
  25. id: backgroundBar
  26. anchors.fill: parent
  27. anchors.topMargin: 40
  28. anchors.bottomMargin: 40
  29. border.width: 1
  30. border.color: "white"
  31. opacity: 0.2
  32. gradient: Gradient {
  33. GradientStop { position: 0.0; color: "lightgray" }
  34. GradientStop { position: 1.0; color: "gray" }
  35. }
  36. }
  37. Rectangle {
  38. id: filler
  39. anchors.left: parent.left
  40. anchors.right: handle.horizontalCenter
  41. anchors.top: backgroundBar.top
  42. anchors.bottom: backgroundBar.bottom
  43. border.width: 1
  44. border.color: "#202020"
  45. gradient: Gradient {
  46. GradientStop { position: 0.0; color: "#f0f0f0" }
  47. GradientStop { position: 1.0; color: "#404040" }
  48. }
  49. }
  50. Item {
  51. id: handle
  52. width: 80
  53. height: parent.height
  54. Image {
  55. anchors.centerIn: parent
  56. source: "images/handle.png"
  57. }
  58. Image {
  59. id: handleHighlight
  60. anchors.centerIn: parent
  61. source: "images/toggle.png"
  62. opacity: mouseArea.pressed ? 0.6 : 0
  63. Behavior on opacity {
  64. NumberAnimation { duration: 200 }
  65. }
  66. }
  67. MouseArea {
  68. id: mouseArea
  69. anchors.fill: parent
  70. anchors.margins: -20
  71. drag.target: parent
  72. drag.axis: Drag.XAxis
  73. drag.minimumX: 2
  74. drag.maximumX: root.xMax+2
  75. onPositionChanged: {
  76. value = (maximum - minimum) * (handle.x-2) / root.xMax + minimum;
  77. }
  78. }
  79. }
  80. Text {
  81. id: sliderText
  82. anchors.centerIn: parent
  83. font.pixelSize: 28
  84. style: Text.Outline
  85. styleColor: "white"
  86. color: "black"
  87. opacity: 0.4
  88. }
  89. }