AddNetworkPrinterScrollView.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright (c) 2021 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.10
  4. import QtQuick.Controls 2.3
  5. import UM 1.3 as UM
  6. import Cura 1.1 as Cura
  7. //
  8. // This is the widget for adding a network printer. There are 2 parts in this widget. One is a scroll view of a list
  9. // of discovered network printers. Beneath the scroll view is a container with 3 buttons: "Refresh", "Add by IP", and
  10. // "Troubleshooting".
  11. //
  12. Item
  13. {
  14. id: base
  15. height: networkPrinterInfo.height + controlsRectangle.height
  16. property alias maxItemCountAtOnce: networkPrinterScrollView.maxItemCountAtOnce
  17. property var currentItem: (networkPrinterListView.currentIndex >= 0)
  18. ? networkPrinterListView.model[networkPrinterListView.currentIndex]
  19. : null
  20. signal refreshButtonClicked()
  21. signal addByIpButtonClicked()
  22. signal addCloudPrinterButtonClicked()
  23. Item
  24. {
  25. id: networkPrinterInfo
  26. height: networkPrinterScrollView.visible ? networkPrinterScrollView.height : noPrinterLabel.height
  27. anchors.left: parent.left
  28. anchors.right: parent.right
  29. anchors.top: parent.top
  30. Label
  31. {
  32. id: noPrinterLabel
  33. height: UM.Theme.getSize("setting_control").height + UM.Theme.getSize("default_margin").height
  34. anchors.left: parent.left
  35. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  36. text: catalog.i18nc("@label", "There is no printer found over your network.")
  37. color: UM.Theme.getColor("text")
  38. renderType: Text.NativeRendering
  39. verticalAlignment: Text.AlignVCenter
  40. visible: networkPrinterListView.count == 0 // Do not show if there are discovered devices.
  41. }
  42. ScrollView
  43. {
  44. id: networkPrinterScrollView
  45. anchors.top: parent.top
  46. anchors.left: parent.left
  47. anchors.right: parent.right
  48. ScrollBar.horizontal.policy: ScrollBar.AsNeeded
  49. ScrollBar.vertical.policy: ScrollBar.AsNeeded
  50. property int maxItemCountAtOnce: 8 // show at max 8 items at once, otherwise you need to scroll.
  51. height: Math.min(contentHeight, (maxItemCountAtOnce * UM.Theme.getSize("action_button").height) - UM.Theme.getSize("default_margin").height)
  52. visible: networkPrinterListView.count > 0
  53. clip: true
  54. ListView
  55. {
  56. id: networkPrinterListView
  57. anchors.fill: parent
  58. model: contentLoader.enabled ? CuraApplication.getDiscoveredPrintersModel().discoveredPrinters: undefined
  59. section.property: "modelData.sectionName"
  60. section.criteria: ViewSection.FullString
  61. section.delegate: sectionHeading
  62. boundsBehavior: Flickable.StopAtBounds
  63. flickDeceleration: 20000 // To prevent the flicking behavior.
  64. cacheBuffer: 1000000 // Set a large cache to effectively just cache every list item.
  65. Component.onCompleted:
  66. {
  67. var toSelectIndex = -1
  68. // Select the first one that's not "unknown" and is the host a group by default.
  69. for (var i = 0; i < count; i++)
  70. {
  71. if (!model[i].isUnknownMachineType && model[i].isHostOfGroup)
  72. {
  73. toSelectIndex = i
  74. break
  75. }
  76. }
  77. currentIndex = toSelectIndex
  78. }
  79. // CURA-6483 For some reason currentIndex can be reset to 0. This check is here to prevent automatically
  80. // selecting an unknown or non-host printer.
  81. onCurrentIndexChanged:
  82. {
  83. var item = model[currentIndex]
  84. if (!item || item.isUnknownMachineType || !item.isHostOfGroup)
  85. {
  86. currentIndex = -1
  87. }
  88. }
  89. Component
  90. {
  91. id: sectionHeading
  92. Label
  93. {
  94. anchors.left: parent.left
  95. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  96. height: UM.Theme.getSize("setting_control").height
  97. text: section
  98. font: UM.Theme.getFont("default")
  99. color: UM.Theme.getColor("small_button_text")
  100. verticalAlignment: Text.AlignVCenter
  101. renderType: Text.NativeRendering
  102. }
  103. }
  104. delegate: Cura.MachineSelectorButton
  105. {
  106. text: modelData.device.name
  107. width: networkPrinterListView.width
  108. outputDevice: modelData.device
  109. enabled: !modelData.isUnknownMachineType && modelData.isHostOfGroup
  110. printerTypeLabelAutoFit: true
  111. // update printer types for all items in the list
  112. updatePrinterTypesOnlyWhenChecked: false
  113. updatePrinterTypesFunction: updateMachineTypes
  114. // show printer type as it is
  115. printerTypeLabelConversionFunction: function(value) { return value }
  116. function updateMachineTypes()
  117. {
  118. printerTypesList = [ modelData.readableMachineType ]
  119. }
  120. checkable: false
  121. selected: ListView.view.currentIndex == model.index
  122. onClicked:
  123. {
  124. ListView.view.currentIndex = index
  125. }
  126. }
  127. }
  128. }
  129. }
  130. // Horizontal line separating the buttons (below) and the discovered network printers (above)
  131. Rectangle
  132. {
  133. id: separator
  134. anchors.left: parent.left
  135. anchors.top: networkPrinterInfo.bottom
  136. anchors.right: parent.right
  137. height: UM.Theme.getSize("default_lining").height
  138. color: UM.Theme.getColor("lining")
  139. }
  140. Item
  141. {
  142. id: controlsRectangle
  143. anchors.left: parent.left
  144. anchors.right: parent.right
  145. anchors.top: separator.bottom
  146. height: UM.Theme.getSize("message_action_button").height + UM.Theme.getSize("default_margin").height
  147. Cura.SecondaryButton
  148. {
  149. id: refreshButton
  150. anchors.left: parent.left
  151. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  152. anchors.verticalCenter: parent.verticalCenter
  153. text: catalog.i18nc("@label", "Refresh")
  154. height: UM.Theme.getSize("message_action_button").height
  155. onClicked: base.refreshButtonClicked()
  156. }
  157. Cura.SecondaryButton
  158. {
  159. id: addPrinterByIpButton
  160. anchors.left: refreshButton.right
  161. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  162. anchors.verticalCenter: parent.verticalCenter
  163. text: catalog.i18nc("@label", "Add printer by IP")
  164. height: UM.Theme.getSize("message_action_button").height
  165. onClicked: base.addByIpButtonClicked()
  166. }
  167. Cura.SecondaryButton
  168. {
  169. id: addCloudPrinterButton
  170. anchors.left: addPrinterByIpButton.right
  171. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  172. anchors.verticalCenter: parent.verticalCenter
  173. text: catalog.i18nc("@label", "Add cloud printer")
  174. height: UM.Theme.getSize("message_action_button").height
  175. onClicked: {
  176. CuraApplication.getDiscoveredCloudPrintersModel().clear()
  177. Cura.API.account.sync(true)
  178. base.addCloudPrinterButtonClicked()
  179. }
  180. }
  181. Item
  182. {
  183. id: troubleshootingButton
  184. anchors.right: parent.right
  185. anchors.rightMargin: UM.Theme.getSize("thin_margin").width
  186. anchors.verticalCenter: parent.verticalCenter
  187. height: troubleshootingLinkIcon.height
  188. width: troubleshootingLinkIcon.width + troubleshootingLabel.width + UM.Theme.getSize("thin_margin").width
  189. UM.RecolorImage
  190. {
  191. id: troubleshootingLinkIcon
  192. anchors.right: troubleshootingLabel.left
  193. anchors.rightMargin: UM.Theme.getSize("thin_margin").width
  194. anchors.verticalCenter: parent.verticalCenter
  195. height: troubleshootingLabel.height
  196. width: height
  197. sourceSize.height: width
  198. color: UM.Theme.getColor("text_link")
  199. source: UM.Theme.getIcon("LinkExternal")
  200. }
  201. Label
  202. {
  203. id: troubleshootingLabel
  204. anchors.right: parent.right
  205. anchors.verticalCenter: parent.verticalCenter
  206. text: catalog.i18nc("@label", "Troubleshooting")
  207. font: UM.Theme.getFont("default")
  208. color: UM.Theme.getColor("text_link")
  209. linkColor: UM.Theme.getColor("text_link")
  210. renderType: Text.NativeRendering
  211. }
  212. MouseArea
  213. {
  214. anchors.fill: parent
  215. hoverEnabled: true
  216. onClicked:
  217. {
  218. // open the troubleshooting URL with web browser
  219. const url = "https://ultimaker.com/in/cura/troubleshooting/network?utm_source=cura&utm_medium=software&utm_campaign=add-network-printer"
  220. Qt.openUrlExternally(url)
  221. }
  222. onEntered:
  223. {
  224. troubleshootingLabel.font.underline = true
  225. }
  226. onExited:
  227. {
  228. troubleshootingLabel.font.underline = false
  229. }
  230. }
  231. }
  232. }
  233. }