MonitorButton.qml 10 KB

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