AddNetworkPrinterScrollView.qml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (c) 2022 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.5 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: networkPrinterListView.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: networkPrinterListView.visible ? networkPrinterListView.height : noPrinterLabel.height
  27. anchors.left: parent.left
  28. anchors.right: parent.right
  29. anchors.top: parent.top
  30. UM.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. visible: networkPrinterListView.count == 0 // Do not show if there are discovered devices.
  38. }
  39. ListView
  40. {
  41. id: networkPrinterListView
  42. anchors.top: parent.top
  43. anchors.left: parent.left
  44. anchors.right: parent.right
  45. height: Math.min(contentHeight, (maxItemCountAtOnce * UM.Theme.getSize("action_button").height) - UM.Theme.getSize("default_margin").height)
  46. ScrollBar.vertical: UM.ScrollBar
  47. {
  48. id: networkPrinterScrollBar
  49. }
  50. clip: true
  51. property int maxItemCountAtOnce: 8 // show at max 8 items at once, otherwise you need to scroll.
  52. visible: networkPrinterListView.count > 0
  53. model: contentLoader.enabled ? CuraApplication.getDiscoveredPrintersModel().discoveredPrinters: undefined
  54. cacheBuffer: 1000000 // Set a large cache to effectively just cache every list item.
  55. section.property: "modelData.sectionName"
  56. section.criteria: ViewSection.FullString
  57. section.delegate: UM.Label
  58. {
  59. anchors.left: parent.left
  60. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  61. width: parent.width - networkPrinterScrollBar.width - UM.Theme.getSize("default_margin").width
  62. height: UM.Theme.getSize("setting_control").height
  63. text: section
  64. color: UM.Theme.getColor("small_button_text")
  65. }
  66. Component.onCompleted:
  67. {
  68. var toSelectIndex = -1
  69. // Select the first one that's not "unknown" and is the host a group by default.
  70. for (var i = 0; i < count; i++)
  71. {
  72. if (!model[i].isUnknownMachineType && model[i].isHostOfGroup)
  73. {
  74. toSelectIndex = i
  75. break
  76. }
  77. }
  78. currentIndex = toSelectIndex
  79. }
  80. // CURA-6483 For some reason currentIndex can be reset to 0. This check is here to prevent automatically
  81. // selecting an unknown or non-host printer.
  82. onCurrentIndexChanged:
  83. {
  84. var item = model[currentIndex]
  85. if (!item || item.isUnknownMachineType || !item.isHostOfGroup)
  86. {
  87. currentIndex = -1
  88. }
  89. }
  90. delegate: Cura.MachineSelectorButton
  91. {
  92. text: modelData.device.name
  93. width: networkPrinterListView.width - networkPrinterScrollBar.width
  94. outputDevice: modelData.device
  95. enabled: !modelData.isUnknownMachineType && modelData.isHostOfGroup
  96. printerTypeLabelAutoFit: true
  97. // update printer types for all items in the list
  98. updatePrinterTypesOnlyWhenChecked: false
  99. updatePrinterTypesFunction: updateMachineTypes
  100. // show printer type as it is
  101. printerTypeLabelConversionFunction: function(value) { return value }
  102. function updateMachineTypes()
  103. {
  104. printerTypesList = [ modelData.readableMachineType ]
  105. }
  106. checkable: false
  107. selected: networkPrinterListView.currentIndex == model.index
  108. onClicked:
  109. {
  110. networkPrinterListView.currentIndex = index
  111. }
  112. }
  113. }
  114. }
  115. // Horizontal line separating the buttons (below) and the discovered network printers (above)
  116. Rectangle
  117. {
  118. id: separator
  119. anchors.left: parent.left
  120. anchors.top: networkPrinterInfo.bottom
  121. anchors.right: parent.right
  122. height: UM.Theme.getSize("default_lining").height
  123. color: UM.Theme.getColor("lining")
  124. }
  125. Item
  126. {
  127. id: controlsRectangle
  128. anchors.left: parent.left
  129. anchors.right: parent.right
  130. anchors.top: separator.bottom
  131. height: UM.Theme.getSize("message_action_button").height + UM.Theme.getSize("default_margin").height
  132. Cura.SecondaryButton
  133. {
  134. id: refreshButton
  135. anchors.left: parent.left
  136. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  137. anchors.verticalCenter: parent.verticalCenter
  138. text: catalog.i18nc("@label", "Refresh")
  139. height: UM.Theme.getSize("message_action_button").height
  140. onClicked: base.refreshButtonClicked()
  141. }
  142. Cura.SecondaryButton
  143. {
  144. id: addPrinterByIpButton
  145. anchors.left: refreshButton.right
  146. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  147. anchors.verticalCenter: parent.verticalCenter
  148. text: catalog.i18nc("@label", "Add printer by IP")
  149. height: UM.Theme.getSize("message_action_button").height
  150. onClicked: base.addByIpButtonClicked()
  151. }
  152. Item
  153. {
  154. id: troubleshootingButton
  155. anchors.right: parent.right
  156. anchors.rightMargin: UM.Theme.getSize("thin_margin").width
  157. anchors.verticalCenter: parent.verticalCenter
  158. height: troubleshootingLinkIcon.height
  159. width: troubleshootingLinkIcon.width + troubleshootingLabel.width + UM.Theme.getSize("thin_margin").width
  160. UM.ColorImage
  161. {
  162. id: troubleshootingLinkIcon
  163. anchors.right: troubleshootingLabel.left
  164. anchors.rightMargin: UM.Theme.getSize("thin_margin").width
  165. anchors.verticalCenter: parent.verticalCenter
  166. height: troubleshootingLabel.height
  167. width: height
  168. color: UM.Theme.getColor("text_link")
  169. source: UM.Theme.getIcon("LinkExternal")
  170. }
  171. UM.Label
  172. {
  173. id: troubleshootingLabel
  174. anchors.right: parent.right
  175. anchors.verticalCenter: parent.verticalCenter
  176. text: catalog.i18nc("@label", "Troubleshooting")
  177. color: UM.Theme.getColor("text_link")
  178. }
  179. MouseArea
  180. {
  181. anchors.fill: parent
  182. hoverEnabled: true
  183. onClicked:
  184. {
  185. // open the troubleshooting URL with web browser
  186. const url = "https://ultimaker.com/in/cura/troubleshooting/network?utm_source=cura&utm_medium=software&utm_campaign=add-network-printer"
  187. Qt.openUrlExternally(url)
  188. }
  189. onEntered:
  190. {
  191. troubleshootingLabel.font.underline = true
  192. }
  193. onExited:
  194. {
  195. troubleshootingLabel.font.underline = false
  196. }
  197. }
  198. }
  199. }
  200. }