PathSlider.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) 2017 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.2
  4. import QtQuick.Controls 1.2
  5. import QtQuick.Layouts 1.1
  6. import QtQuick.Controls.Styles 1.1
  7. import UM 1.0 as UM
  8. import Cura 1.0 as Cura
  9. Item {
  10. id: sliderRoot
  11. // handle properties
  12. property real handleSize: 10
  13. property real handleRadius: handleSize / 2
  14. property color handleColor: "black"
  15. property color handleActiveColor: "white"
  16. property color rangeColor: "black"
  17. property real handleLabelWidth: width
  18. // track properties
  19. property real trackThickness: 4 // width of the slider track
  20. property real trackRadius: trackThickness / 2
  21. property color trackColor: "white"
  22. property real trackBorderWidth: 1 // width of the slider track border
  23. property color trackBorderColor: "black"
  24. // value properties
  25. property real maximumValue: 100
  26. property bool roundValues: true
  27. property real handleValue: maximumValue
  28. property bool pathsVisible: true
  29. function getHandleValueFromSliderHandle () {
  30. return handle.getValue()
  31. }
  32. function setHandleValue (value) {
  33. handle.setValue(value)
  34. updateRangeHandle()
  35. }
  36. function updateRangeHandle () {
  37. rangeHandle.width = handle.x - sliderRoot.handleSize
  38. }
  39. // slider track
  40. Rectangle {
  41. id: track
  42. width: sliderRoot.width - sliderRoot.handleSize
  43. height: sliderRoot.trackThickness
  44. radius: sliderRoot.trackRadius
  45. anchors.centerIn: sliderRoot
  46. color: sliderRoot.trackColor
  47. border.width: sliderRoot.trackBorderWidth
  48. border.color: sliderRoot.trackBorderColor
  49. visible: sliderRoot.pathsVisible
  50. }
  51. // Progress indicator
  52. Item {
  53. id: rangeHandle
  54. x: handle.width
  55. height: sliderRoot.handleSize
  56. width: handle.x - sliderRoot.handleSize
  57. anchors.verticalCenter: sliderRoot.verticalCenter
  58. visible: sliderRoot.pathsVisible
  59. Rectangle {
  60. height: sliderRoot.trackThickness - 2 * sliderRoot.trackBorderWidth
  61. width: parent.width + sliderRoot.handleSize
  62. anchors.centerIn: parent
  63. color: sliderRoot.rangeColor
  64. }
  65. }
  66. // Handle
  67. Rectangle {
  68. id: handle
  69. x: sliderRoot.handleSize
  70. width: sliderRoot.handleSize
  71. height: sliderRoot.handleSize
  72. anchors.verticalCenter: sliderRoot.verticalCenter
  73. radius: sliderRoot.handleRadius
  74. color: handleLabel.activeFocus ? sliderRoot.handleActiveColor : sliderRoot.handleColor
  75. visible: sliderRoot.pathsVisible
  76. function onHandleDragged () {
  77. // update the range handle
  78. sliderRoot.updateRangeHandle()
  79. // set the new value after moving the handle position
  80. UM.SimulationView.setCurrentPath(getValue())
  81. }
  82. // get the value based on the slider position
  83. function getValue () {
  84. var result = x / (sliderRoot.width - sliderRoot.handleSize)
  85. result = result * sliderRoot.maximumValue
  86. result = sliderRoot.roundValues ? Math.round(result) : result
  87. return result
  88. }
  89. // set the slider position based on the value
  90. function setValue (value) {
  91. UM.SimulationView.setCurrentPath(value)
  92. var diff = value / sliderRoot.maximumValue
  93. var newXPosition = Math.round(diff * (sliderRoot.width - sliderRoot.handleSize))
  94. x = newXPosition
  95. // update the range handle
  96. sliderRoot.updateRangeHandle()
  97. }
  98. Keys.onRightPressed: handleLabel.setValue(handleLabel.value + ((event.modifiers & Qt.ShiftModifier) ? 10 : 1))
  99. Keys.onLeftPressed: handleLabel.setValue(handleLabel.value - ((event.modifiers & Qt.ShiftModifier) ? 10 : 1))
  100. // dragging
  101. MouseArea {
  102. anchors.fill: parent
  103. drag {
  104. target: parent
  105. axis: Drag.XAxis
  106. minimumX: 0
  107. maximumX: sliderRoot.width - sliderRoot.handleSize
  108. }
  109. onPressed: {
  110. handleLabel.forceActiveFocus()
  111. }
  112. onPositionChanged: parent.onHandleDragged()
  113. }
  114. SimulationSliderLabel {
  115. id: handleLabel
  116. height: sliderRoot.handleSize + UM.Theme.getSize("default_margin").height
  117. y: parent.y + sliderRoot.handleSize + UM.Theme.getSize("default_margin").height
  118. anchors.horizontalCenter: parent.horizontalCenter
  119. target: Qt.point(x + width / 2, sliderRoot.height)
  120. visible: false
  121. startFrom: 0
  122. // custom properties
  123. maximumValue: sliderRoot.maximumValue
  124. value: sliderRoot.handleValue
  125. busy: UM.SimulationView.busy
  126. setValue: handle.setValue // connect callback functions
  127. }
  128. }
  129. }