MonitorButton.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //Copyright (c) 2022 Ultimaker B.V.
  2. //Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.2
  4. import UM 1.5 as UM
  5. import Cura 1.0 as Cura
  6. Item
  7. {
  8. id: base
  9. UM.I18nCatalog { id: catalog; name: "cura"}
  10. height: childrenRect.height + UM.Theme.getSize("thick_margin").height
  11. property bool printerConnected: Cura.MachineManager.printerConnected
  12. property bool printerAcceptsCommands: printerConnected && Cura.MachineManager.printerOutputDevices[0].acceptsCommands
  13. property var activePrinter: printerConnected ? Cura.MachineManager.printerOutputDevices[0].activePrinter : null
  14. property var activePrintJob: activePrinter ? activePrinter.activePrintJob: null
  15. property real progress:
  16. {
  17. if(!printerConnected)
  18. {
  19. return 0
  20. }
  21. if(activePrinter == null)
  22. {
  23. return 0
  24. }
  25. if(activePrintJob == null)
  26. {
  27. return 0
  28. }
  29. if(activePrintJob.timeTotal == 0)
  30. {
  31. return 0 // Prevent division by 0
  32. }
  33. return activePrintJob.timeElapsed / activePrintJob.timeTotal * 100
  34. }
  35. property int backendState: UM.Backend.state
  36. property bool showProgress: {
  37. // determine if we need to show the progress bar + percentage
  38. if(activePrintJob == null)
  39. {
  40. return false;
  41. }
  42. switch(base.activePrintJob.state)
  43. {
  44. case "printing":
  45. case "paused":
  46. case "pausing":
  47. case "resuming":
  48. return true;
  49. case "pre_print": // heating, etc.
  50. case "wait_cleanup":
  51. case "offline":
  52. case "abort": // note sure if this jobState actually occurs in the wild
  53. case "error": // after clicking abort you apparently get "error"
  54. case "ready": // ready to print or getting ready
  55. case "": // ready to print or getting ready
  56. default:
  57. return false;
  58. }
  59. }
  60. property variant statusColor:
  61. {
  62. if(!printerConnected || !printerAcceptsCommands || activePrinter == null)
  63. {
  64. return UM.Theme.getColor("text");
  65. }
  66. switch(activePrinter.state)
  67. {
  68. case "maintenance":
  69. return UM.Theme.getColor("status_busy");
  70. case "error":
  71. return UM.Theme.getColor("status_stopped");
  72. }
  73. if(base.activePrintJob == null)
  74. {
  75. return UM.Theme.getColor("text");
  76. }
  77. switch(base.activePrintJob.state)
  78. {
  79. case "printing":
  80. case "pre_print":
  81. case "wait_cleanup":
  82. case "pausing":
  83. case "resuming":
  84. return UM.Theme.getColor("status_busy");
  85. case "ready":
  86. case "":
  87. return UM.Theme.getColor("status_ready");
  88. case "paused":
  89. return UM.Theme.getColor("status_paused");
  90. case "error":
  91. return UM.Theme.getColor("status_stopped");
  92. case "offline":
  93. return UM.Theme.getColor("status_offline");
  94. default:
  95. return UM.Theme.getColor("text");
  96. }
  97. }
  98. property bool activity: CuraApplication.platformActivity;
  99. property string fileBaseName
  100. property string statusText:
  101. {
  102. if(!printerConnected)
  103. {
  104. return catalog.i18nc("@label:MonitorStatus", "Not connected to a printer");
  105. }
  106. if(!printerAcceptsCommands)
  107. {
  108. return catalog.i18nc("@label:MonitorStatus", "Printer does not accept commands");
  109. }
  110. var printerOutputDevice = Cura.MachineManager.printerOutputDevices[0]
  111. if(activePrinter == null)
  112. {
  113. return "";
  114. }
  115. if(activePrinter.state == "maintenance")
  116. {
  117. return catalog.i18nc("@label:MonitorStatus", "In maintenance. Please check the printer");
  118. }
  119. if(base.activePrintJob == null)
  120. {
  121. return " "
  122. }
  123. switch(base.activePrintJob.state)
  124. {
  125. case "offline":
  126. return catalog.i18nc("@label:MonitorStatus", "Lost connection with the printer");
  127. case "printing":
  128. return catalog.i18nc("@label:MonitorStatus", "Printing...");
  129. //TODO: Add text for case "pausing".
  130. case "paused":
  131. return catalog.i18nc("@label:MonitorStatus", "Paused");
  132. //TODO: Add text for case "resuming".
  133. case "pre_print":
  134. return catalog.i18nc("@label:MonitorStatus", "Preparing...");
  135. case "wait_cleanup":
  136. return catalog.i18nc("@label:MonitorStatus", "Please remove the print");
  137. case "error":
  138. return printerOutputDevice.errorText;
  139. default:
  140. return " ";
  141. }
  142. }
  143. UM.Label
  144. {
  145. id: statusLabel
  146. width: parent.width - 2 * UM.Theme.getSize("thick_margin").width
  147. anchors.top: parent.top
  148. anchors.left: parent.left
  149. anchors.leftMargin: UM.Theme.getSize("thick_margin").width
  150. color: base.statusColor
  151. font: UM.Theme.getFont("large_bold")
  152. text: statusText
  153. }
  154. UM.Label
  155. {
  156. id: percentageLabel
  157. anchors.top: parent.top
  158. anchors.right: progressBar.right
  159. color: base.statusColor
  160. font: UM.Theme.getFont("large_bold")
  161. text: Math.round(progress) + "%"
  162. visible: showProgress
  163. }
  164. UM.ProgressBar
  165. {
  166. id: progressBar
  167. from: 0
  168. to: 100
  169. value: 0
  170. //Doing this in an explicit binding since the implicit binding breaks on occasion.
  171. Binding
  172. {
  173. target: progressBar
  174. property: "value"
  175. value: base.progress
  176. }
  177. visible: showProgress
  178. indeterminate:
  179. {
  180. if(!printerConnected)
  181. {
  182. return false;
  183. }
  184. if(base.activePrintJob == null)
  185. {
  186. return false
  187. }
  188. switch(base.activePrintJob.state)
  189. {
  190. case "pausing":
  191. case "resuming":
  192. return true;
  193. default:
  194. return false;
  195. }
  196. }
  197. property string backgroundColor: UM.Theme.getColor("progressbar_background")
  198. property string controlColor: base.statusColor
  199. width: parent.width - 2 * UM.Theme.getSize("thick_margin").width
  200. height: UM.Theme.getSize("progressbar").height
  201. anchors.top: statusLabel.bottom
  202. anchors.topMargin: Math.round(UM.Theme.getSize("thick_margin").height / 4)
  203. anchors.left: parent.left
  204. anchors.leftMargin: UM.Theme.getSize("thick_margin").width
  205. }
  206. Row
  207. {
  208. id: buttonsRow
  209. height: abortButton.height
  210. anchors.top: progressBar.bottom
  211. anchors.topMargin: UM.Theme.getSize("thick_margin").height
  212. anchors.right: parent.right
  213. anchors.rightMargin: UM.Theme.getSize("thick_margin").width
  214. spacing: UM.Theme.getSize("default_margin").width
  215. Row
  216. {
  217. id: additionalComponentsRow
  218. spacing: UM.Theme.getSize("default_margin").width
  219. }
  220. Component.onCompleted: {
  221. buttonsRow.updateAdditionalComponents("monitorButtons")
  222. }
  223. Connections
  224. {
  225. target: CuraApplication
  226. function onAdditionalComponentsChanged() { buttonsRow.updateAdditionalComponents("monitorButtons") }
  227. }
  228. function updateAdditionalComponents (areaId) {
  229. if(areaId == "monitorButtons") {
  230. for (var component in CuraApplication.additionalComponents["monitorButtons"]) {
  231. CuraApplication.additionalComponents["monitorButtons"][component].parent = additionalComponentsRow
  232. }
  233. }
  234. }
  235. Cura.SecondaryButton
  236. {
  237. id: pauseResumeButton
  238. height: UM.Theme.getSize("save_button_save_to_button").height
  239. property bool userClicked: false
  240. property string lastJobState: ""
  241. visible: printerConnected && activePrinter != null &&activePrinter.canPause
  242. enabled: (!userClicked) && printerConnected && printerAcceptsCommands && activePrintJob != null &&
  243. (["paused", "printing"].indexOf(activePrintJob.state) >= 0)
  244. text: {
  245. if (!printerConnected || activePrintJob == null)
  246. {
  247. return catalog.i18nc("@label", "Pause");
  248. }
  249. if (activePrintJob.state == "paused")
  250. {
  251. return catalog.i18nc("@label", "Resume");
  252. }
  253. else
  254. {
  255. return catalog.i18nc("@label", "Pause");
  256. }
  257. }
  258. onClicked:
  259. {
  260. if(activePrintJob == null)
  261. {
  262. return // Do nothing!
  263. }
  264. if(activePrintJob.state == "paused")
  265. {
  266. activePrintJob.setState("print");
  267. }
  268. else if(activePrintJob.state == "printing")
  269. {
  270. activePrintJob.setState("pause");
  271. }
  272. }
  273. }
  274. Cura.SecondaryButton
  275. {
  276. id: abortButton
  277. visible: printerConnected && activePrinter != null && activePrinter.canAbort
  278. enabled: printerConnected && printerAcceptsCommands && activePrintJob != null &&
  279. (["paused", "printing", "pre_print"].indexOf(activePrintJob.state) >= 0)
  280. height: UM.Theme.getSize("save_button_save_to_button").height
  281. text: catalog.i18nc("@label", "Abort Print")
  282. onClicked: confirmationDialog.open()
  283. }
  284. Cura.MessageDialog
  285. {
  286. id: confirmationDialog
  287. title: catalog.i18nc("@window:title", "Abort print")
  288. text: catalog.i18nc("@label", "Are you sure you want to abort the print?")
  289. standardButtons: Cura.MessageDialog.Yes | Cura.MessageDialog.No
  290. onAccepted: activePrintJob.setState("abort")
  291. }
  292. }
  293. }