PathSlider.qml 5.5 KB

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