MonitorButton.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright (c) 2016 Ultimaker B.V.
  2. // Cura is released under the terms of the AGPLv3 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. Rectangle
  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 real progress: printerConnected ? Cura.MachineManager.printerOutputDevices[0].progress : 0
  17. property int backendState: UM.Backend.state
  18. property bool showProgress: {
  19. // determine if we need to show the progress bar + percentage
  20. if(!printerConnected || !printerAcceptsCommands) {
  21. return false;
  22. }
  23. switch(Cura.MachineManager.printerOutputDevices[0].jobState)
  24. {
  25. case "printing":
  26. case "paused":
  27. return true;
  28. case "pre_print": // heating, etc.
  29. case "wait_cleanup":
  30. case "offline":
  31. case "abort": // note sure if this jobState actually occurs in the wild
  32. case "error": // after clicking abort you apparently get "error"
  33. case "ready": // ready to print or getting ready
  34. case "": // ready to print or getting ready
  35. default:
  36. return false;
  37. }
  38. }
  39. property variant statusColor:
  40. {
  41. if(!printerConnected || !printerAcceptsCommands)
  42. return UM.Theme.getColor("text");
  43. switch(Cura.MachineManager.printerOutputDevices[0].jobState)
  44. {
  45. case "printing":
  46. case "pre_print":
  47. case "wait_cleanup":
  48. return UM.Theme.getColor("status_busy");
  49. case "ready":
  50. case "":
  51. return UM.Theme.getColor("status_ready");
  52. case "paused":
  53. return UM.Theme.getColor("status_paused");
  54. case "error":
  55. return UM.Theme.getColor("status_stopped");
  56. case "offline":
  57. return UM.Theme.getColor("status_offline");
  58. default:
  59. return UM.Theme.getColor("text");
  60. }
  61. }
  62. property bool activity: Printer.getPlatformActivity;
  63. property int totalHeight: childrenRect.height + UM.Theme.getSize("default_margin").height
  64. property string fileBaseName
  65. property string statusText:
  66. {
  67. if(!printerConnected)
  68. return catalog.i18nc("@label:MonitorStatus", "Not connected to a printer");
  69. if(!printerAcceptsCommands)
  70. return catalog.i18nc("@label:MonitorStatus", "Printer does not accept commands");
  71. var printerOutputDevice = Cura.MachineManager.printerOutputDevices[0]
  72. switch(printerOutputDevice.jobState)
  73. {
  74. case "offline":
  75. return catalog.i18nc("@label:MonitorStatus", "Lost connection with the printer");
  76. case "printing":
  77. return catalog.i18nc("@label:MonitorStatus", "Printing...");
  78. case "paused":
  79. return catalog.i18nc("@label:MonitorStatus", "Paused");
  80. case "pre_print":
  81. return catalog.i18nc("@label:MonitorStatus", "Preparing...");
  82. case "wait_cleanup":
  83. return catalog.i18nc("@label:MonitorStatus", "Please remove the print");
  84. case "error":
  85. return printerOutputDevice.errorText;
  86. default:
  87. return " ";
  88. }
  89. }
  90. Label
  91. {
  92. id: statusLabel
  93. width: parent.width - 2 * UM.Theme.getSize("default_margin").width
  94. anchors.top: parent.top
  95. anchors.left: parent.left
  96. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  97. color: base.statusColor
  98. font: UM.Theme.getFont("large")
  99. text: statusText
  100. }
  101. Label
  102. {
  103. id: percentageLabel
  104. anchors.top: parent.top
  105. anchors.right: progressBar.right
  106. color: base.statusColor
  107. font: UM.Theme.getFont("large")
  108. text: Math.round(progress) + "%"
  109. visible: showProgress
  110. }
  111. Rectangle
  112. {
  113. id: progressBar
  114. width: parent.width - 2 * UM.Theme.getSize("default_margin").width
  115. height: UM.Theme.getSize("progressbar").height
  116. anchors.top: statusLabel.bottom
  117. anchors.topMargin: UM.Theme.getSize("default_margin").height / 4
  118. anchors.left: parent.left
  119. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  120. radius: UM.Theme.getSize("progressbar_radius").width
  121. color: UM.Theme.getColor("progressbar_background")
  122. visible: showProgress
  123. Rectangle
  124. {
  125. width: Math.max(parent.width * base.progress / 100)
  126. height: parent.height
  127. color: base.statusColor
  128. radius: UM.Theme.getSize("progressbar_radius").width
  129. }
  130. }
  131. Button
  132. {
  133. id: abortButton
  134. visible: printerConnected
  135. enabled: printerConnected && Cura.MachineManager.printerOutputDevices[0].acceptsCommands &&
  136. (["paused", "printing", "pre_print"].indexOf(Cura.MachineManager.printerOutputDevices[0].jobState) >= 0)
  137. height: UM.Theme.getSize("save_button_save_to_button").height
  138. anchors.top: progressBar.bottom
  139. anchors.topMargin: UM.Theme.getSize("default_margin").height
  140. anchors.right: parent.right
  141. anchors.rightMargin: UM.Theme.getSize("default_margin").width
  142. text: catalog.i18nc("@label:", "Abort Print")
  143. onClicked: confirmationDialog.visible = true
  144. style: ButtonStyle
  145. {
  146. background: Rectangle
  147. {
  148. border.width: UM.Theme.getSize("default_lining").width
  149. border.color:
  150. {
  151. if(!control.enabled)
  152. return UM.Theme.getColor("action_button_disabled_border");
  153. else if(control.pressed)
  154. return UM.Theme.getColor("action_button_active_border");
  155. else if(control.hovered)
  156. return UM.Theme.getColor("action_button_hovered_border");
  157. else
  158. return UM.Theme.getColor("action_button_border");
  159. }
  160. color:
  161. {
  162. if(!control.enabled)
  163. return UM.Theme.getColor("action_button_disabled");
  164. else if(control.pressed)
  165. return UM.Theme.getColor("action_button_active");
  166. else if(control.hovered)
  167. return UM.Theme.getColor("action_button_hovered");
  168. else
  169. return UM.Theme.getColor("action_button");
  170. }
  171. Behavior on color { ColorAnimation { duration: 50; } }
  172. implicitWidth: actualLabel.contentWidth + (UM.Theme.getSize("default_margin").width * 2)
  173. Label
  174. {
  175. id: actualLabel
  176. anchors.centerIn: parent
  177. color:
  178. {
  179. if(!control.enabled)
  180. return UM.Theme.getColor("action_button_disabled_text");
  181. else if(control.pressed)
  182. return UM.Theme.getColor("action_button_active_text");
  183. else if(control.hovered)
  184. return UM.Theme.getColor("action_button_hovered_text");
  185. else
  186. return UM.Theme.getColor("action_button_text");
  187. }
  188. font: UM.Theme.getFont("action_button")
  189. text: control.text;
  190. }
  191. }
  192. label: Item { }
  193. }
  194. }
  195. MessageDialog
  196. {
  197. id: confirmationDialog
  198. title: catalog.i18nc("@window:title", "Abort print")
  199. icon: StandardIcon.Warning
  200. text: catalog.i18nc("@label", "Are you sure you want to abort the print?")
  201. standardButtons: StandardButton.Yes | StandardButton.No
  202. Component.onCompleted: visible = false
  203. onYes: Cura.MachineManager.printerOutputDevices[0].setJobState("abort")
  204. }
  205. Button
  206. {
  207. id: pauseResumeButton
  208. height: UM.Theme.getSize("save_button_save_to_button").height
  209. anchors.top: progressBar.bottom
  210. anchors.topMargin: UM.Theme.getSize("default_margin").height
  211. anchors.right: abortButton.left
  212. anchors.rightMargin: UM.Theme.getSize("default_margin").width
  213. property bool userClicked: false
  214. property string lastJobState: ""
  215. visible: printerConnected
  216. enabled: (!userClicked) && printerConnected && Cura.MachineManager.printerOutputDevices[0].acceptsCommands &&
  217. (["paused", "printing"].indexOf(Cura.MachineManager.printerOutputDevices[0].jobState) >= 0)
  218. text: {
  219. var result = "";
  220. if (!printerConnected) {
  221. return "";
  222. }
  223. var jobState = Cura.MachineManager.printerOutputDevices[0].jobState;
  224. if (lastJobState != jobState) {
  225. // the userClicked message must disappear when an "automated" jobState comes by
  226. userClicked = false;
  227. lastJobState = jobState;
  228. }
  229. if (jobState == "paused")
  230. {
  231. if (userClicked) {
  232. // User feedback for pretending we're already in "printing" mode.
  233. result = catalog.i18nc("@label:", "Pause");
  234. } else {
  235. result = catalog.i18nc("@label:", "Resume");
  236. }
  237. } else {
  238. if (userClicked) {
  239. // User feedback for pretending we're already in "pause" mode.
  240. result = catalog.i18nc("@label:", "Resume");
  241. } else {
  242. result = catalog.i18nc("@label:", "Pause");
  243. }
  244. }
  245. return result;
  246. }
  247. onClicked: {
  248. var newJobState = Cura.MachineManager.printerOutputDevices[0].jobState == "paused" ? "print" : "pause";
  249. Cura.MachineManager.printerOutputDevices[0].setJobState(newJobState);
  250. userClicked = true;
  251. }
  252. style: ButtonStyle
  253. {
  254. background: Rectangle
  255. {
  256. border.width: UM.Theme.getSize("default_lining").width
  257. border.color:
  258. {
  259. if(!control.enabled)
  260. return UM.Theme.getColor("action_button_disabled_border");
  261. else if(control.pressed)
  262. return UM.Theme.getColor("action_button_active_border");
  263. else if(control.hovered)
  264. return UM.Theme.getColor("action_button_hovered_border");
  265. else
  266. return UM.Theme.getColor("action_button_border");
  267. }
  268. color:
  269. {
  270. if(!control.enabled)
  271. return UM.Theme.getColor("action_button_disabled");
  272. else if(control.pressed)
  273. return UM.Theme.getColor("action_button_active");
  274. else if(control.hovered)
  275. return UM.Theme.getColor("action_button_hovered");
  276. else
  277. return UM.Theme.getColor("action_button");
  278. }
  279. Behavior on color { ColorAnimation { duration: 50; } }
  280. implicitWidth: actualLabel.contentWidth + (UM.Theme.getSize("default_margin").width * 2)
  281. Label
  282. {
  283. id: actualLabel
  284. anchors.centerIn: parent
  285. color:
  286. {
  287. if(!control.enabled)
  288. return UM.Theme.getColor("action_button_disabled_text");
  289. else if(control.pressed)
  290. return UM.Theme.getColor("action_button_active_text");
  291. else if(control.hovered)
  292. return UM.Theme.getColor("action_button_hovered_text");
  293. else
  294. return UM.Theme.getColor("action_button_text");
  295. }
  296. font: UM.Theme.getFont("action_button")
  297. text: control.text
  298. }
  299. }
  300. label: Item { }
  301. }
  302. }
  303. }