SimulationViewMainComponent.qml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (c) 2018 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.4
  4. import QtQuick.Controls 1.2
  5. import QtQuick.Layouts 1.1
  6. import QtQuick.Controls.Styles 1.1
  7. import UM 1.4 as UM
  8. import Cura 1.0 as Cura
  9. Item
  10. {
  11. property bool is_simulation_playing: false
  12. visible: UM.SimulationView.layerActivity && CuraApplication.platformActivity
  13. PathSlider
  14. {
  15. id: pathSlider
  16. height: UM.Theme.getSize("slider_handle").width
  17. width: UM.Theme.getSize("slider_layerview_size").height
  18. anchors.bottom: parent.bottom
  19. anchors.bottomMargin: UM.Theme.getSize("default_margin").height
  20. anchors.horizontalCenter: parent.horizontalCenter
  21. visible: !UM.SimulationView.compatibilityMode
  22. // Custom properties
  23. handleValue: UM.SimulationView.currentPath
  24. maximumValue: UM.SimulationView.numPaths
  25. // Update values when layer data changes.
  26. Connections
  27. {
  28. target: UM.SimulationView
  29. onMaxPathsChanged: pathSlider.setHandleValue(UM.SimulationView.currentPath)
  30. onCurrentPathChanged:
  31. {
  32. // Only pause the simulation when the layer was changed manually, not when the simulation is running
  33. if (pathSlider.manuallyChanged)
  34. {
  35. playButton.pauseSimulation()
  36. }
  37. pathSlider.setHandleValue(UM.SimulationView.currentPath)
  38. }
  39. }
  40. // Ensure that the slider handlers show the correct value after switching views.
  41. Component.onCompleted:
  42. {
  43. pathSlider.setHandleValue(UM.SimulationView.currentPath)
  44. }
  45. }
  46. UM.SimpleButton
  47. {
  48. id: playButton
  49. iconSource: !is_simulation_playing ? "./resources/simulation_resume.svg": "./resources/simulation_pause.svg"
  50. width: UM.Theme.getSize("small_button").width
  51. height: UM.Theme.getSize("small_button").height
  52. hoverColor: UM.Theme.getColor("slider_handle_active")
  53. color: UM.Theme.getColor("slider_handle")
  54. iconMargin: UM.Theme.getSize("thick_lining").width
  55. visible: !UM.SimulationView.compatibilityMode
  56. Connections
  57. {
  58. target: UM.Preferences
  59. onPreferenceChanged:
  60. {
  61. if (preference !== "view/only_show_top_layers" && preference !== "view/top_layer_count" && ! preference.match("layerview/"))
  62. {
  63. return;
  64. }
  65. playButton.pauseSimulation()
  66. }
  67. }
  68. anchors
  69. {
  70. right: pathSlider.left
  71. verticalCenter: pathSlider.verticalCenter
  72. }
  73. onClicked:
  74. {
  75. if(is_simulation_playing)
  76. {
  77. pauseSimulation()
  78. }
  79. else
  80. {
  81. resumeSimulation()
  82. }
  83. }
  84. function pauseSimulation()
  85. {
  86. UM.SimulationView.setSimulationRunning(false)
  87. simulationTimer.stop()
  88. is_simulation_playing = false
  89. layerSlider.manuallyChanged = true
  90. pathSlider.manuallyChanged = true
  91. }
  92. function resumeSimulation()
  93. {
  94. UM.SimulationView.setSimulationRunning(true)
  95. simulationTimer.start()
  96. layerSlider.manuallyChanged = false
  97. pathSlider.manuallyChanged = false
  98. }
  99. }
  100. Timer
  101. {
  102. id: simulationTimer
  103. interval: 100
  104. running: false
  105. repeat: true
  106. onTriggered:
  107. {
  108. var currentPath = UM.SimulationView.currentPath
  109. var numPaths = UM.SimulationView.numPaths
  110. var currentLayer = UM.SimulationView.currentLayer
  111. var numLayers = UM.SimulationView.numLayers
  112. // When the user plays the simulation, if the path slider is at the end of this layer, we start
  113. // the simulation at the beginning of the current layer.
  114. if (!is_simulation_playing)
  115. {
  116. if (currentPath >= numPaths)
  117. {
  118. UM.SimulationView.setCurrentPath(0)
  119. }
  120. else
  121. {
  122. UM.SimulationView.setCurrentPath(currentPath + 1)
  123. }
  124. }
  125. // If the simulation is already playing and we reach the end of a layer, then it automatically
  126. // starts at the beginning of the next layer.
  127. else
  128. {
  129. if (currentPath >= numPaths)
  130. {
  131. // At the end of the model, the simulation stops
  132. if (currentLayer >= numLayers)
  133. {
  134. playButton.pauseSimulation()
  135. }
  136. else
  137. {
  138. UM.SimulationView.setCurrentLayer(currentLayer + 1)
  139. UM.SimulationView.setCurrentPath(0)
  140. }
  141. }
  142. else
  143. {
  144. UM.SimulationView.setCurrentPath(currentPath + 1)
  145. }
  146. }
  147. // The status must be set here instead of in the resumeSimulation function otherwise it won't work
  148. // correctly, because part of the logic is in this trigger function.
  149. is_simulation_playing = true
  150. }
  151. }
  152. LayerSlider
  153. {
  154. id: layerSlider
  155. width: UM.Theme.getSize("slider_handle").width
  156. height: UM.Theme.getSize("slider_layerview_size").height
  157. anchors
  158. {
  159. right: parent.right
  160. verticalCenter: parent.verticalCenter
  161. rightMargin: UM.Theme.getSize("default_margin").width
  162. }
  163. // Custom properties
  164. upperValue: UM.SimulationView.currentLayer
  165. lowerValue: UM.SimulationView.minimumLayer
  166. maximumValue: UM.SimulationView.numLayers
  167. // Update values when layer data changes
  168. Connections
  169. {
  170. target: UM.SimulationView
  171. onMaxLayersChanged: layerSlider.setUpperValue(UM.SimulationView.currentLayer)
  172. onMinimumLayerChanged: layerSlider.setLowerValue(UM.SimulationView.minimumLayer)
  173. onCurrentLayerChanged:
  174. {
  175. // Only pause the simulation when the layer was changed manually, not when the simulation is running
  176. if (layerSlider.manuallyChanged)
  177. {
  178. playButton.pauseSimulation()
  179. }
  180. layerSlider.setUpperValue(UM.SimulationView.currentLayer)
  181. }
  182. }
  183. // Make sure the slider handlers show the correct value after switching views
  184. Component.onCompleted:
  185. {
  186. layerSlider.setLowerValue(UM.SimulationView.minimumLayer)
  187. layerSlider.setUpperValue(UM.SimulationView.currentLayer)
  188. }
  189. }
  190. }