PathSlider.qml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. {
  11. id: sliderRoot
  12. // handle properties
  13. property real handleSize: UM.Theme.getSize("slider_handle").width
  14. property real handleRadius: handleSize / 2
  15. property color handleColor: UM.Theme.getColor("slider_handle")
  16. property color handleActiveColor: UM.Theme.getColor("slider_handle_active")
  17. property color rangeColor: UM.Theme.getColor("slider_groove_fill")
  18. property real handleLabelWidth: width
  19. // track properties
  20. property real trackThickness: UM.Theme.getSize("slider_groove").width
  21. property real trackRadius: UM.Theme.getSize("slider_groove_radius").width
  22. property color trackColor: UM.Theme.getColor("slider_groove")
  23. // value properties
  24. property real maximumValue: 100
  25. property real minimumValue: 0
  26. property bool roundValues: true
  27. property real handleValue: maximumValue
  28. property bool pathsVisible: true
  29. property bool manuallyChanged: true // Indicates whether the value was changed manually or during simulation
  30. function getHandleValueFromSliderHandle()
  31. {
  32. return handle.getValue()
  33. }
  34. function setHandleValue(value)
  35. {
  36. handle.setValue(value)
  37. updateRangeHandle()
  38. }
  39. function updateRangeHandle()
  40. {
  41. rangeHandle.width = handle.x - sliderRoot.handleSize
  42. }
  43. function normalizeValue(value)
  44. {
  45. return Math.min(Math.max(value, sliderRoot.minimumValue), sliderRoot.maximumValue)
  46. }
  47. // slider track
  48. Rectangle
  49. {
  50. id: track
  51. width: sliderRoot.width - sliderRoot.handleSize
  52. height: sliderRoot.trackThickness
  53. radius: sliderRoot.trackRadius
  54. anchors.centerIn: sliderRoot
  55. color: sliderRoot.trackColor
  56. visible: sliderRoot.pathsVisible
  57. }
  58. // Progress indicator
  59. Item
  60. {
  61. id: rangeHandle
  62. x: handle.width
  63. height: sliderRoot.handleSize
  64. width: handle.x - sliderRoot.handleSize
  65. anchors.verticalCenter: sliderRoot.verticalCenter
  66. visible: sliderRoot.pathsVisible
  67. Rectangle
  68. {
  69. height: sliderRoot.trackThickness
  70. width: parent.width + sliderRoot.handleSize
  71. anchors.centerIn: parent
  72. radius: sliderRoot.trackRadius
  73. color: sliderRoot.rangeColor
  74. }
  75. }
  76. // Handle
  77. Rectangle
  78. {
  79. id: handle
  80. x: sliderRoot.handleSize
  81. width: sliderRoot.handleSize
  82. height: sliderRoot.handleSize
  83. anchors.verticalCenter: sliderRoot.verticalCenter
  84. radius: sliderRoot.handleRadius
  85. color: handleLabel.activeFocus ? sliderRoot.handleActiveColor : sliderRoot.handleColor
  86. visible: sliderRoot.pathsVisible
  87. function onHandleDragged()
  88. {
  89. sliderRoot.manuallyChanged = true
  90. // update the range handle
  91. sliderRoot.updateRangeHandle()
  92. // set the new value after moving the handle position
  93. UM.SimulationView.setCurrentPath(getValue())
  94. }
  95. // get the value based on the slider position
  96. function getValue()
  97. {
  98. var result = x / (sliderRoot.width - sliderRoot.handleSize)
  99. result = result * sliderRoot.maximumValue
  100. result = sliderRoot.roundValues ? Math.round(result) : result
  101. return result
  102. }
  103. function setValueManually(value)
  104. {
  105. sliderRoot.manuallyChanged = true
  106. handle.setValue(value)
  107. }
  108. // set the slider position based on the value
  109. function setValue(value)
  110. {
  111. // Normalize values between range, since using arrow keys will create out-of-the-range values
  112. value = sliderRoot.normalizeValue(value)
  113. UM.SimulationView.setCurrentPath(value)
  114. var diff = value / sliderRoot.maximumValue
  115. var newXPosition = Math.round(diff * (sliderRoot.width - sliderRoot.handleSize))
  116. x = newXPosition
  117. // update the range handle
  118. sliderRoot.updateRangeHandle()
  119. }
  120. Keys.onRightPressed: handleLabel.setValue(handleLabel.value + ((event.modifiers & Qt.ShiftModifier) ? 10 : 1))
  121. Keys.onLeftPressed: handleLabel.setValue(handleLabel.value - ((event.modifiers & Qt.ShiftModifier) ? 10 : 1))
  122. // dragging
  123. MouseArea
  124. {
  125. anchors.fill: parent
  126. drag
  127. {
  128. target: parent
  129. axis: Drag.XAxis
  130. minimumX: 0
  131. maximumX: sliderRoot.width - sliderRoot.handleSize
  132. }
  133. onPressed: handleLabel.forceActiveFocus()
  134. onPositionChanged: parent.onHandleDragged()
  135. }
  136. SimulationSliderLabel
  137. {
  138. id: handleLabel
  139. height: sliderRoot.handleSize + UM.Theme.getSize("default_margin").height
  140. y: parent.y + sliderRoot.handleSize + UM.Theme.getSize("default_margin").height
  141. anchors.horizontalCenter: parent.horizontalCenter
  142. target: Qt.point(x + width / 2, sliderRoot.height)
  143. visible: false
  144. startFrom: 0
  145. // custom properties
  146. maximumValue: sliderRoot.maximumValue
  147. value: sliderRoot.handleValue
  148. busy: UM.SimulationView.busy
  149. setValue: handle.setValueManually // connect callback functions
  150. }
  151. }
  152. }