Slider.qml 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import QtQuick 2.0
  2. Item {
  3. id: slider
  4. property int value: 1
  5. property real maximum: 1
  6. property real minimum: 0
  7. property string caption: ""
  8. property bool pressed: mouseArea.pressed
  9. width: parent.width
  10. height: 80
  11. function updatePos() {
  12. if (maximum > minimum) {
  13. var pos = (track.width - 10) * (value - minimum) / (maximum - minimum) - 5;
  14. return Math.min(Math.max(pos, 5), track.width - 5) - 10;
  15. } else {
  16. return 5;
  17. }
  18. }
  19. Text {
  20. id: captionText
  21. x: 20
  22. text: slider.caption
  23. font.pixelSize: slider.width/15
  24. color: "#404040"
  25. }
  26. Item {
  27. id: track
  28. height: 20
  29. anchors.bottom: parent.bottom
  30. anchors.left: parent.left
  31. anchors.leftMargin: 22
  32. anchors.right: parent.right
  33. anchors.rightMargin: 22
  34. BorderImage {
  35. source: "images/slider_track.png"
  36. anchors.left: parent.left
  37. anchors.right: parent.right
  38. border.right: 2
  39. }
  40. BorderImage {
  41. id: trackFilled
  42. anchors.left: minimum == -maximum ? (value < 0 ? handle.horizontalCenter : parent.horizontalCenter) : parent.left
  43. anchors.right: minimum == -maximum && value < 0 ? parent.horizontalCenter : handle.horizontalCenter
  44. source: "images/slider_track_filled.png"
  45. border.left: 3
  46. border.right: 3
  47. }
  48. Image {
  49. id: handle
  50. anchors.verticalCenter: parent.verticalCenter
  51. anchors.verticalCenterOffset: -4
  52. width: 32
  53. height: 48
  54. smooth: true
  55. source: "images/slider_thumb2.png"
  56. x: updatePos()
  57. }
  58. MouseArea {
  59. id: mouseArea
  60. anchors {
  61. left: parent.left
  62. right: parent.right
  63. verticalCenter: parent.verticalCenter
  64. }
  65. height: 100
  66. preventStealing: true
  67. onPressed: {
  68. var handleX = Math.max(0, Math.min(mouseX, mouseArea.width))
  69. var realValue = (maximum - minimum) * handleX / mouseArea.width + minimum;
  70. value = realValue;
  71. }
  72. onPositionChanged: {
  73. if (pressed) {
  74. var handleX = Math.max(0, Math.min(mouseX, mouseArea.width))
  75. var realValue = (maximum - minimum) * handleX / mouseArea.width + minimum;
  76. value = realValue;
  77. }
  78. }
  79. }
  80. }
  81. }