MonitorButton.qml 11 KB

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