MachineSelector.qml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 2.3
  5. import UM 1.5 as UM
  6. import Cura 1.1 as Cura
  7. Cura.ExpandablePopup
  8. {
  9. id: machineSelector
  10. property Cura.MachineManager machineManager
  11. property bool isNetworkPrinter: machineManager.activeMachineHasNetworkConnection
  12. property bool isConnectedCloudPrinter: machineManager.activeMachineHasCloudConnection
  13. property bool isCloudRegistered: machineManager.activeMachineHasCloudRegistration
  14. property bool isGroup: machineManager.activeMachineIsGroup
  15. property string machineName: {
  16. if (isNetworkPrinter && machineManager.activeMachineNetworkGroupName != "")
  17. {
  18. return machineManager.activeMachineNetworkGroupName
  19. }
  20. if (machineManager.activeMachine != null)
  21. {
  22. return machineManager.activeMachine.name
  23. }
  24. return ""
  25. }
  26. property alias machineListModel: machineSelectorList.model
  27. property alias onSelectPrinter: machineSelectorList.onSelectPrinter
  28. property list<Item> buttons
  29. property string connectionStatus: {
  30. if (isNetworkPrinter)
  31. {
  32. return "printer_connected"
  33. }
  34. else if (isConnectedCloudPrinter && Cura.API.connectionStatus.isInternetReachable)
  35. {
  36. return "printer_cloud_connected"
  37. }
  38. else if (isCloudRegistered)
  39. {
  40. return "printer_cloud_not_available"
  41. }
  42. else
  43. {
  44. return ""
  45. }
  46. }
  47. function getConnectionStatusMessage() {
  48. if (connectionStatus == "printer_cloud_not_available")
  49. {
  50. if(Cura.API.connectionStatus.isInternetReachable)
  51. {
  52. if (Cura.API.account.isLoggedIn)
  53. {
  54. if (machineManager.activeMachineIsLinkedToCurrentAccount)
  55. {
  56. return catalog.i18nc("@status", "The cloud printer is offline. Please check if the printer is turned on and connected to the internet.")
  57. }
  58. else
  59. {
  60. return catalog.i18nc("@status", "This printer is not linked to your account. Please visit the Ultimaker Digital Factory to establish a connection.")
  61. }
  62. }
  63. else
  64. {
  65. return catalog.i18nc("@status", "The cloud connection is currently unavailable. Please sign in to connect to the cloud printer.")
  66. }
  67. }
  68. else
  69. {
  70. return catalog.i18nc("@status", "The cloud connection is currently unavailable. Please check your internet connection.")
  71. }
  72. }
  73. else
  74. {
  75. return ""
  76. }
  77. }
  78. contentPadding: UM.Theme.getSize("default_lining").width
  79. contentAlignment: Cura.ExpandablePopup.ContentAlignment.AlignLeft
  80. UM.I18nCatalog
  81. {
  82. id: catalog
  83. name: "cura"
  84. }
  85. headerItem: Cura.IconWithText
  86. {
  87. text: machineName
  88. source:
  89. {
  90. if (isGroup)
  91. {
  92. return UM.Theme.getIcon("PrinterTriple", "medium")
  93. }
  94. else if (isNetworkPrinter || isCloudRegistered)
  95. {
  96. return UM.Theme.getIcon("Printer", "medium")
  97. }
  98. else
  99. {
  100. return ""
  101. }
  102. }
  103. font: UM.Theme.getFont("medium")
  104. iconColor: UM.Theme.getColor("machine_selector_printer_icon")
  105. iconSize: source != "" ? UM.Theme.getSize("machine_selector_icon").width: 0
  106. UM.ColorImage
  107. {
  108. id: connectionStatusImage
  109. anchors
  110. {
  111. bottom: parent.bottom
  112. bottomMargin: - Math.round(height * 1 / 6)
  113. left: parent.left
  114. leftMargin: iconSize - Math.round(width * 5 / 6)
  115. }
  116. source:
  117. {
  118. if (connectionStatus == "printer_connected")
  119. {
  120. return UM.Theme.getIcon("CheckBlueBG", "low")
  121. }
  122. else if (connectionStatus == "printer_cloud_connected" || connectionStatus == "printer_cloud_not_available")
  123. {
  124. return UM.Theme.getIcon("CloudBadge", "low")
  125. }
  126. else
  127. {
  128. return ""
  129. }
  130. }
  131. width: UM.Theme.getSize("printer_status_icon").width
  132. height: UM.Theme.getSize("printer_status_icon").height
  133. color: connectionStatus == "printer_cloud_not_available" ? UM.Theme.getColor("cloud_unavailable") : UM.Theme.getColor("primary")
  134. visible: (isNetworkPrinter || isCloudRegistered) && source != ""
  135. // Make a themable circle in the background so we can change it in other themes
  136. Rectangle
  137. {
  138. id: iconBackground
  139. anchors.centerIn: parent
  140. width: parent.width - 1.5 //1.5 pixels smaller, (at least sqrt(2), regardless of screen pixel scale) so that the circle doesn't show up behind the icon due to anti-aliasing.
  141. height: parent.height - 1.5
  142. radius: width / 2
  143. color: UM.Theme.getColor("connection_badge_background")
  144. z: parent.z - 1
  145. }
  146. }
  147. // Connection status tooltip hover area
  148. MouseArea
  149. {
  150. id: connectionStatusTooltipHoverArea
  151. anchors.fill: parent
  152. hoverEnabled: getConnectionStatusMessage() !== ""
  153. acceptedButtons: Qt.NoButton // react to hover only, don't steal clicks
  154. onEntered:
  155. {
  156. machineSelector.mouseArea.entered() // we want both this and the outer area to be entered
  157. tooltip.tooltipText = getConnectionStatusMessage()
  158. tooltip.show()
  159. }
  160. onExited: { tooltip.hide() }
  161. }
  162. UM.ToolTip
  163. {
  164. id: tooltip
  165. width: 250 * screenScaleFactor
  166. tooltipText: getConnectionStatusMessage()
  167. arrowSize: UM.Theme.getSize("button_tooltip_arrow").width
  168. x: connectionStatusImage.x - UM.Theme.getSize("narrow_margin").width
  169. y: connectionStatusImage.y + connectionStatusImage.height + UM.Theme.getSize("narrow_margin").height
  170. z: popup.z + 1
  171. targetPoint: Qt.point(
  172. connectionStatusImage.x + Math.round(connectionStatusImage.width / 2),
  173. connectionStatusImage.y
  174. )
  175. }
  176. }
  177. property int minDropDownWidth: UM.Theme.getSize("machine_selector_widget_content").width
  178. property int maxDropDownHeight: UM.Theme.getSize("machine_selector_widget_content").height
  179. contentItem: Item
  180. {
  181. id: popup
  182. implicitWidth: Math.max(machineSelector.width, minDropDownWidth)
  183. implicitHeight: Math.min(machineSelectorList.contentHeight + separator.height + buttonRow.height, maxDropDownHeight) //Maximum height is the theme entry.
  184. MachineSelectorList
  185. {
  186. id: machineSelectorList
  187. anchors
  188. {
  189. left: parent.left
  190. leftMargin: UM.Theme.getSize("default_lining").width
  191. right: parent.right
  192. rightMargin: UM.Theme.getSize("default_lining").width
  193. top: parent.top
  194. bottom: separator.top
  195. }
  196. clip: true
  197. }
  198. Rectangle
  199. {
  200. id: separator
  201. anchors.bottom: buttonRow.top
  202. width: parent.width
  203. height: UM.Theme.getSize("default_lining").height
  204. color: UM.Theme.getColor("lining")
  205. }
  206. Row
  207. {
  208. id: buttonRow
  209. anchors.bottom: parent.bottom
  210. anchors.left: parent.left
  211. anchors.right: parent.right
  212. padding: UM.Theme.getSize("default_margin").width
  213. spacing: UM.Theme.getSize("default_margin").width
  214. children: buttons
  215. }
  216. states: [
  217. State {
  218. name: "noButtons"
  219. when: !buttons || buttons.length == 0
  220. PropertyChanges
  221. {
  222. target: buttonRow
  223. height: 0
  224. padding: 0
  225. }
  226. PropertyChanges
  227. {
  228. target: separator
  229. height: 0
  230. }
  231. }
  232. ]
  233. }
  234. }