MonitorContextMenu.qml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.3
  4. import QtQuick.Controls 2.15
  5. import UM 1.5 as UM
  6. import Cura 1.6 as Cura
  7. /**
  8. * A MonitorInfoBlurb is an extension of the GenericPopUp used to show static information (vs. interactive context
  9. * menus). It accepts some text (text), an item to link to to (target), and a specification of which side of the target
  10. * to appear on (direction). It also sets the GenericPopUp's color to black, to differentiate itself from a menu.
  11. */
  12. Item
  13. {
  14. id: monitorContextMenu
  15. property alias target: popUp.target
  16. property var printJob: null
  17. //Everything in the pop-up only gets evaluated when showing the pop-up.
  18. //However we want to show the button for showing the pop-up only if there is anything visible inside it.
  19. //So compute here the visibility of the menu items, so that we can use it for the visibility of the button.
  20. property bool sendToTopVisible:
  21. {
  22. if (printJob && printJob.state in ("queued", "error") && !isAssigned(printJob)) {
  23. if (OutputDevice && OutputDevice.queuedPrintJobs[0] && OutputDevice.canWriteOthersPrintJobs) {
  24. return OutputDevice.queuedPrintJobs[0].key != printJob.key;
  25. }
  26. }
  27. return false;
  28. }
  29. property bool deleteVisible:
  30. {
  31. if(!printJob)
  32. {
  33. return false;
  34. }
  35. if(printJob.isMine)
  36. {
  37. if(!OutputDevice.canWriteOwnPrintJobs)
  38. {
  39. return false;
  40. }
  41. }
  42. else
  43. {
  44. if(!OutputDevice.canWriteOthersPrintJobs)
  45. {
  46. return false;
  47. }
  48. }
  49. var states = ["queued", "error", "sent_to_printer"];
  50. return states.indexOf(printJob.state) !== -1;
  51. }
  52. property bool pauseVisible:
  53. {
  54. if(!printJob)
  55. {
  56. return false;
  57. }
  58. if(printJob.isMine)
  59. {
  60. if(!OutputDevice.canWriteOwnPrintJobs)
  61. {
  62. return false;
  63. }
  64. }
  65. else
  66. {
  67. if(!OutputDevice.canWriteOthersPrintJobs)
  68. {
  69. return false;
  70. }
  71. }
  72. var states = ["printing", "pausing", "paused", "resuming"];
  73. return states.indexOf(printJob.state) !== -1;
  74. }
  75. property bool abortVisible:
  76. {
  77. if(!printJob)
  78. {
  79. return false;
  80. }
  81. if(printJob.isMine)
  82. {
  83. if(!OutputDevice.canWriteOwnPrintJobs)
  84. {
  85. return false;
  86. }
  87. }
  88. else
  89. {
  90. if(!OutputDevice.canWriteOthersPrintJobs)
  91. {
  92. return false;
  93. }
  94. }
  95. var states = ["pre_print", "printing", "pausing", "paused", "resuming"];
  96. return states.indexOf(printJob.state) !== -1;
  97. }
  98. property bool hasItems: sendToTopVisible || deleteVisible || pauseVisible || abortVisible
  99. GenericPopUp
  100. {
  101. id: popUp
  102. // Which way should the pop-up point? Default is up, but will flip when required
  103. direction: "up"
  104. // Use dark grey for info blurbs and white for context menus
  105. color: UM.Theme.getColor("monitor_context_menu")
  106. contentItem: Item
  107. {
  108. id: contentWrapper
  109. implicitWidth: childrenRect.width
  110. implicitHeight: menuItems.height + UM.Theme.getSize("default_margin").height
  111. Column
  112. {
  113. id: menuItems
  114. width: 144 * screenScaleFactor
  115. anchors
  116. {
  117. top: parent.top
  118. topMargin: Math.floor(UM.Theme.getSize("default_margin").height / 2)
  119. }
  120. spacing: Math.floor(UM.Theme.getSize("default_margin").height / 2)
  121. PrintJobContextMenuItem
  122. {
  123. onClicked:
  124. {
  125. sendToTopConfirmationDialog.visible = true;
  126. popUp.close();
  127. }
  128. text: catalog.i18nc("@label", "Move to top");
  129. visible: monitorContextMenu.sendToTopVisible
  130. }
  131. PrintJobContextMenuItem
  132. {
  133. onClicked:
  134. {
  135. deleteConfirmationDialog.visible = true;
  136. popUp.close();
  137. }
  138. text: catalog.i18nc("@label", "Delete");
  139. visible: monitorContextMenu.deleteVisible
  140. }
  141. PrintJobContextMenuItem
  142. {
  143. enabled: visible && !(printJob.state == "pausing" || printJob.state == "resuming");
  144. onClicked:
  145. {
  146. if (printJob.state == "paused")
  147. {
  148. printJob.setState("resume");
  149. popUp.close();
  150. return;
  151. }
  152. if (printJob.state == "printing")
  153. {
  154. printJob.setState("pause");
  155. popUp.close();
  156. return;
  157. }
  158. }
  159. text:
  160. {
  161. if(!printJob)
  162. {
  163. return "";
  164. }
  165. switch(printJob.state)
  166. {
  167. case "paused":
  168. return catalog.i18nc("@label", "Resume");
  169. case "pausing":
  170. return catalog.i18nc("@label", "Pausing...");
  171. case "resuming":
  172. return catalog.i18nc("@label", "Resuming...");
  173. default:
  174. catalog.i18nc("@label", "Pause");
  175. }
  176. }
  177. visible: monitorContextMenu.pauseVisible
  178. }
  179. PrintJobContextMenuItem
  180. {
  181. enabled: visible && printJob.state !== "aborting";
  182. onClicked:
  183. {
  184. abortConfirmationDialog.visible = true;
  185. popUp.close();
  186. }
  187. text: printJob && printJob.state == "aborting" ? catalog.i18nc("@label", "Aborting...") : catalog.i18nc("@label", "Abort");
  188. visible: monitorContextMenu.abortVisible
  189. }
  190. }
  191. }
  192. }
  193. Cura.MessageDialog
  194. {
  195. id: sendToTopConfirmationDialog
  196. onAccepted: OutputDevice.sendJobToTop(printJob.key)
  197. standardButtons: Dialog.Yes | Dialog.No
  198. text: printJob && printJob.name ? catalog.i18nc("@label %1 is the name of a print job.", "Are you sure you want to move %1 to the top of the queue?").arg(printJob.name) : ""
  199. title: catalog.i18nc("@window:title", "Move print job to top")
  200. }
  201. Cura.MessageDialog
  202. {
  203. id: deleteConfirmationDialog
  204. onAccepted: OutputDevice.deleteJobFromQueue(printJob.key)
  205. standardButtons: Dialog.Yes | Dialog.No
  206. text: printJob && printJob.name ? catalog.i18nc("@label %1 is the name of a print job.", "Are you sure you want to delete %1?").arg(printJob.name) : ""
  207. title: catalog.i18nc("@window:title", "Delete print job")
  208. }
  209. Cura.MessageDialog
  210. {
  211. id: abortConfirmationDialog
  212. onAccepted: printJob.setState("abort")
  213. standardButtons: Dialog.Yes | Dialog.No
  214. text: printJob && printJob.name ? catalog.i18nc("@label %1 is the name of a print job.", "Are you sure you want to abort %1?").arg(printJob.name) : ""
  215. title: catalog.i18nc("@window:title", "Abort print")
  216. }
  217. function switchPopupState() {
  218. popUp.visible ? popUp.close() : popUp.open()
  219. }
  220. function open() {
  221. popUp.open()
  222. }
  223. function close() {
  224. popUp.close()
  225. }
  226. function isAssigned(job) {
  227. if (!job) {
  228. return false;
  229. }
  230. return job.assignedPrinter ? true : false;
  231. }
  232. }