AddPrinterByIpContent.qml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright (c) 2019 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 QtQuick.Layouts 1.3
  6. import UM 1.3 as UM
  7. import Cura 1.5 as Cura
  8. //
  9. // This component contains the content for the 'by IP' page of the "Add New Printer" flow of the on-boarding process.
  10. //
  11. Item
  12. {
  13. UM.I18nCatalog { id: catalog; name: "cura" }
  14. id: addPrinterByIpScreen
  15. // If there's a manual address resolve request in progress.
  16. property bool hasRequestInProgress: CuraApplication.getDiscoveredPrintersModel().hasManualDeviceRequestInProgress
  17. // Indicates if a request has finished.
  18. property bool hasRequestFinished: false
  19. property var discoveredPrinter: null
  20. property bool isPrinterDiscovered: discoveredPrinter != null
  21. // A printer can only be added if it doesn't have an unknown type and it's the host of a group.
  22. property bool canAddPrinter: isPrinterDiscovered && !discoveredPrinter.isUnknownMachineType && discoveredPrinter.isHostOfGroup
  23. // For validating IP address
  24. property var networkingUtil: Cura.NetworkingUtil {}
  25. // Make sure to cancel the current request when this page closes.
  26. onVisibleChanged:
  27. {
  28. if (!visible)
  29. {
  30. CuraApplication.getDiscoveredPrintersModel().cancelCurrentManualDeviceRequest()
  31. }
  32. }
  33. Label
  34. {
  35. id: titleLabel
  36. anchors.top: parent.top
  37. anchors.horizontalCenter: parent.horizontalCenter
  38. horizontalAlignment: Text.AlignHCenter
  39. text: catalog.i18nc("@label", "Add printer by IP address")
  40. color: UM.Theme.getColor("primary_button")
  41. font: UM.Theme.getFont("huge")
  42. renderType: Text.NativeRendering
  43. }
  44. Item
  45. {
  46. anchors.top: titleLabel.bottom
  47. anchors.bottom: connectButton.top
  48. anchors.topMargin: UM.Theme.getSize("default_margin").height
  49. anchors.bottomMargin: UM.Theme.getSize("default_margin").height
  50. anchors.left: parent.left
  51. anchors.right: parent.right
  52. Item
  53. {
  54. anchors.left: parent.left
  55. anchors.right: parent.right
  56. anchors.margins: UM.Theme.getSize("default_margin").width
  57. Label
  58. {
  59. id: explainLabel
  60. height: contentHeight
  61. anchors.left: parent.left
  62. anchors.right: parent.right
  63. anchors.top: parent.top
  64. font: UM.Theme.getFont("default")
  65. color: UM.Theme.getColor("text")
  66. renderType: Text.NativeRendering
  67. text: catalog.i18nc("@label", "Enter the IP address or hostname of your printer on the network.")
  68. }
  69. Item
  70. {
  71. id: userInputFields
  72. height: childrenRect.height
  73. anchors.left: parent.left
  74. anchors.right: parent.right
  75. anchors.top: explainLabel.bottom
  76. anchors.topMargin: UM.Theme.getSize("default_margin").width
  77. Cura.TextField
  78. {
  79. id: hostnameField
  80. width: (parent.width / 2) | 0
  81. height: addPrinterButton.height
  82. anchors.verticalCenter: addPrinterButton.verticalCenter
  83. anchors.left: parent.left
  84. signal invalidInputDetected()
  85. onInvalidInputDetected: invalidInputLabel.visible = true
  86. validator: RegExpValidator
  87. {
  88. regExp: /([a-zA-Z0-9.:]+)?/
  89. }
  90. onTextEdited: invalidInputLabel.visible = false
  91. placeholderText: catalog.i18nc("@text", "Place enter your printer's IP address.")
  92. enabled: { ! (addPrinterByIpScreen.hasRequestInProgress || addPrinterByIpScreen.isPrinterDiscovered) }
  93. onAccepted: addPrinterButton.clicked()
  94. }
  95. Label
  96. {
  97. id: invalidInputLabel
  98. anchors.top: hostnameField.bottom
  99. anchors.topMargin: UM.Theme.getSize("default_margin").height
  100. anchors.left: parent.left
  101. visible: false
  102. text: catalog.i18nc("@text", "Please enter a valid IP address.")
  103. font: UM.Theme.getFont("default")
  104. color: UM.Theme.getColor("text")
  105. renderType: Text.NativeRendering
  106. }
  107. Cura.SecondaryButton
  108. {
  109. id: addPrinterButton
  110. anchors.top: parent.top
  111. anchors.left: hostnameField.right
  112. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  113. text: catalog.i18nc("@button", "Add")
  114. enabled: !addPrinterByIpScreen.hasRequestInProgress && !addPrinterByIpScreen.isPrinterDiscovered && (hostnameField.state != "invalid" && hostnameField.text != "")
  115. onClicked:
  116. {
  117. const address = hostnameField.text
  118. if (!networkingUtil.isValidIP(address))
  119. {
  120. hostnameField.invalidInputDetected()
  121. return
  122. }
  123. // This address is already in the discovered printer model, no need to add a manual discovery.
  124. if (CuraApplication.getDiscoveredPrintersModel().discoveredPrintersByAddress[address])
  125. {
  126. addPrinterByIpScreen.discoveredPrinter = CuraApplication.getDiscoveredPrintersModel().discoveredPrintersByAddress[address]
  127. return
  128. }
  129. CuraApplication.getDiscoveredPrintersModel().checkManualDevice(address)
  130. }
  131. busy: addPrinterByIpScreen.hasRequestInProgress
  132. }
  133. }
  134. Item
  135. {
  136. width: parent.width
  137. anchors.top: userInputFields.bottom
  138. anchors.margins: UM.Theme.getSize("default_margin").width
  139. Label
  140. {
  141. id: waitResponseLabel
  142. anchors.top: parent.top
  143. anchors.margins: UM.Theme.getSize("default_margin").width
  144. font: UM.Theme.getFont("default")
  145. color: UM.Theme.getColor("text")
  146. renderType: Text.NativeRendering
  147. visible: addPrinterByIpScreen.hasRequestInProgress || (addPrinterByIpScreen.hasRequestFinished && !addPrinterByIpScreen.isPrinterDiscovered)
  148. text:
  149. {
  150. if (addPrinterByIpScreen.hasRequestFinished)
  151. {
  152. catalog.i18nc("@label", "Could not connect to device.")
  153. }
  154. else
  155. {
  156. catalog.i18nc("@label", "The printer at this address has not responded yet.")
  157. }
  158. }
  159. }
  160. Item
  161. {
  162. id: printerInfoLabels
  163. anchors.left: parent.left
  164. anchors.right: parent.right
  165. anchors.top: parent.top
  166. anchors.margins: UM.Theme.getSize("default_margin").width
  167. visible: addPrinterByIpScreen.isPrinterDiscovered
  168. Label
  169. {
  170. id: printerNameLabel
  171. anchors.top: parent.top
  172. font: UM.Theme.getFont("large")
  173. color: UM.Theme.getColor("text")
  174. renderType: Text.NativeRendering
  175. text: !addPrinterByIpScreen.isPrinterDiscovered ? "???" : addPrinterByIpScreen.discoveredPrinter.name
  176. }
  177. Label
  178. {
  179. id: printerCannotBeAddedLabel
  180. width: parent.width
  181. anchors.top: printerNameLabel.bottom
  182. anchors.topMargin: UM.Theme.getSize("default_margin").height
  183. text: catalog.i18nc("@label", "This printer cannot be added because it's an unknown printer or it's not the host of a group.")
  184. visible: addPrinterByIpScreen.hasRequestFinished && !addPrinterByIpScreen.canAddPrinter
  185. font: UM.Theme.getFont("default_bold")
  186. color: UM.Theme.getColor("text")
  187. renderType: Text.NativeRendering
  188. wrapMode: Text.WordWrap
  189. }
  190. GridLayout
  191. {
  192. id: printerInfoGrid
  193. anchors.top: printerCannotBeAddedLabel ? printerCannotBeAddedLabel.bottom : printerNameLabel.bottom
  194. anchors.margins: UM.Theme.getSize("default_margin").width
  195. columns: 2
  196. columnSpacing: UM.Theme.getSize("default_margin").width
  197. Label
  198. {
  199. text: catalog.i18nc("@label", "Type")
  200. font: UM.Theme.getFont("default")
  201. color: UM.Theme.getColor("text")
  202. renderType: Text.NativeRendering
  203. }
  204. Label
  205. {
  206. id: typeText
  207. text: !addPrinterByIpScreen.isPrinterDiscovered ? "?" : addPrinterByIpScreen.discoveredPrinter.readableMachineType
  208. font: UM.Theme.getFont("default")
  209. color: UM.Theme.getColor("text")
  210. renderType: Text.NativeRendering
  211. }
  212. Label
  213. {
  214. text: catalog.i18nc("@label", "Firmware version")
  215. font: UM.Theme.getFont("default")
  216. color: UM.Theme.getColor("text")
  217. renderType: Text.NativeRendering
  218. }
  219. Label
  220. {
  221. id: firmwareText
  222. text: !addPrinterByIpScreen.isPrinterDiscovered ? "0.0.0.0" : addPrinterByIpScreen.discoveredPrinter.device.getProperty("firmware_version")
  223. font: UM.Theme.getFont("default")
  224. color: UM.Theme.getColor("text")
  225. renderType: Text.NativeRendering
  226. }
  227. Label
  228. {
  229. text: catalog.i18nc("@label", "Address")
  230. font: UM.Theme.getFont("default")
  231. color: UM.Theme.getColor("text")
  232. renderType: Text.NativeRendering
  233. }
  234. Label
  235. {
  236. id: addressText
  237. text: !addPrinterByIpScreen.isPrinterDiscovered ? "0.0.0.0" : addPrinterByIpScreen.discoveredPrinter.address
  238. font: UM.Theme.getFont("default")
  239. color: UM.Theme.getColor("text")
  240. renderType: Text.NativeRendering
  241. }
  242. }
  243. Connections
  244. {
  245. target: CuraApplication.getDiscoveredPrintersModel()
  246. onManualDeviceRequestFinished:
  247. {
  248. var discovered_printers_model = CuraApplication.getDiscoveredPrintersModel()
  249. var printer = discovered_printers_model.discoveredPrintersByAddress[hostnameField.text]
  250. if (printer)
  251. {
  252. addPrinterByIpScreen.discoveredPrinter = printer
  253. }
  254. addPrinterByIpScreen.hasRequestFinished = true
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. Cura.PrimaryButton
  262. {
  263. id: backButton
  264. anchors.left: parent.left
  265. anchors.bottom: parent.bottom
  266. text: catalog.i18nc("@button", "Back")
  267. onClicked:
  268. {
  269. CuraApplication.getDiscoveredPrintersModel().cancelCurrentManualDeviceRequest()
  270. base.showPreviousPage()
  271. }
  272. }
  273. Cura.PrimaryButton
  274. {
  275. id: connectButton
  276. anchors.right: parent.right
  277. anchors.bottom: parent.bottom
  278. text: catalog.i18nc("@button", "Connect")
  279. onClicked:
  280. {
  281. CuraApplication.getDiscoveredPrintersModel().createMachineFromDiscoveredPrinter(discoveredPrinter)
  282. base.showNextPage()
  283. }
  284. enabled: addPrinterByIpScreen.canAddPrinter
  285. }
  286. }