MonitorButton.qml 10 KB

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