AddLocalPrinterScrollView.qml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 UM 1.3 as UM
  6. import Cura 1.0 as Cura
  7. //
  8. // This is the scroll view widget for adding a (local) printer. This scroll view shows a list view with printers
  9. // categorized into 3 categories: "Ultimaker", "Custom", and "Other".
  10. //
  11. Item
  12. {
  13. id: base
  14. height: childrenRect.height
  15. // The currently selected machine item in the local machine list.
  16. property var currentItem: (machineList.currentIndex >= 0)
  17. ? machineList.model.getItem(machineList.currentIndex)
  18. : null
  19. // The currently active (expanded) section/category, where section/category is the grouping of local machine items.
  20. property string currentSection: "Ultimaker B.V."
  21. // By default (when this list shows up) we always expand the "Ultimaker" section.
  22. property var preferredCategories: {
  23. "Ultimaker B.V.": -2,
  24. "Custom": -1
  25. }
  26. property int maxItemCountAtOnce: 10 // show at max 10 items at once, otherwise you need to scroll.
  27. // User-editable printer name
  28. property alias printerName: printerNameTextField.text
  29. property alias isPrinterNameValid: printerNameTextField.acceptableInput
  30. onCurrentItemChanged:
  31. {
  32. printerName = currentItem == null ? "" : currentItem.name
  33. }
  34. function updateCurrentItemUponSectionChange()
  35. {
  36. // Find the first machine from this section
  37. for (var i = 0; i < machineList.count; i++)
  38. {
  39. var item = machineList.model.getItem(i)
  40. if (item.section == base.currentSection)
  41. {
  42. machineList.currentIndex = i
  43. break
  44. }
  45. }
  46. }
  47. Component.onCompleted:
  48. {
  49. updateCurrentItemUponSectionChange()
  50. }
  51. Item
  52. {
  53. id: localPrinterSelectionItem
  54. anchors.left: parent.left
  55. anchors.right: parent.right
  56. anchors.top: parent.top
  57. height: childrenRect.height
  58. // ScrollView + ListView for selecting a local printer to add
  59. ScrollView
  60. {
  61. id: scrollView
  62. anchors.left: parent.left
  63. anchors.right: parent.right
  64. anchors.top: parent.top
  65. height: (maxItemCountAtOnce * UM.Theme.getSize("action_button").height) - UM.Theme.getSize("default_margin").height
  66. ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
  67. ScrollBar.vertical.policy: ScrollBar.AsNeeded
  68. clip: true
  69. ListView
  70. {
  71. id: machineList
  72. // CURA-6793
  73. // Enabling the buffer seems to cause the blank items issue. When buffer is enabled, if the ListView's
  74. // individual item has a dynamic change on its visibility, the ListView doesn't redraw itself.
  75. // The default value of cacheBuffer is platform-dependent, so we explicitly disable it here.
  76. cacheBuffer: 0
  77. boundsBehavior: Flickable.StopAtBounds
  78. flickDeceleration: 20000 // To prevent the flicking behavior.
  79. model: UM.DefinitionContainersModel
  80. {
  81. id: machineDefinitionsModel
  82. filter: { "visible": true }
  83. sectionProperty: "manufacturer"
  84. preferredSections: preferredCategories
  85. }
  86. section.property: "section"
  87. section.delegate: sectionHeader
  88. delegate: machineButton
  89. }
  90. Component
  91. {
  92. id: sectionHeader
  93. Button
  94. {
  95. id: button
  96. width: ListView.view.width
  97. height: UM.Theme.getSize("action_button").height
  98. text: section
  99. property bool isActive: base.currentSection == section
  100. background: Rectangle
  101. {
  102. anchors.fill: parent
  103. color: isActive ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  104. }
  105. contentItem: Item
  106. {
  107. width: childrenRect.width
  108. height: UM.Theme.getSize("action_button").height
  109. UM.RecolorImage
  110. {
  111. id: arrow
  112. anchors.left: parent.left
  113. width: UM.Theme.getSize("standard_arrow").width
  114. height: UM.Theme.getSize("standard_arrow").height
  115. sourceSize.width: width
  116. sourceSize.height: height
  117. color: UM.Theme.getColor("text")
  118. source: base.currentSection == section ? UM.Theme.getIcon("arrow_bottom") : UM.Theme.getIcon("arrow_right")
  119. }
  120. Label
  121. {
  122. id: label
  123. anchors.left: arrow.right
  124. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  125. verticalAlignment: Text.AlignVCenter
  126. text: button.text
  127. font: UM.Theme.getFont("default_bold")
  128. color: UM.Theme.getColor("text")
  129. renderType: Text.NativeRendering
  130. }
  131. }
  132. onClicked:
  133. {
  134. base.currentSection = section
  135. base.updateCurrentItemUponSectionChange()
  136. }
  137. }
  138. }
  139. Component
  140. {
  141. id: machineButton
  142. Cura.RadioButton
  143. {
  144. id: radioButton
  145. anchors.left: parent.left
  146. anchors.leftMargin: UM.Theme.getSize("standard_list_lineheight").width
  147. anchors.right: parent.right
  148. anchors.rightMargin: UM.Theme.getSize("default_margin").width
  149. height: visible ? UM.Theme.getSize("standard_list_lineheight").height : 0
  150. checked: ListView.view.currentIndex == index
  151. text: name
  152. visible: base.currentSection == section
  153. onClicked: ListView.view.currentIndex = index
  154. }
  155. }
  156. }
  157. }
  158. // Horizontal line
  159. Rectangle
  160. {
  161. id: horizontalLine
  162. anchors.top: localPrinterSelectionItem.bottom
  163. anchors.left: parent.left
  164. anchors.right: parent.right
  165. height: UM.Theme.getSize("default_lining").height
  166. color: UM.Theme.getColor("lining")
  167. }
  168. // User-editable printer name row
  169. Row
  170. {
  171. anchors.top: horizontalLine.bottom
  172. anchors.left: parent.left
  173. anchors.right: parent.right
  174. anchors.topMargin: UM.Theme.getSize("default_lining").height
  175. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  176. spacing: UM.Theme.getSize("default_margin").width
  177. Label
  178. {
  179. text: catalog.i18nc("@label", "Printer name")
  180. anchors.verticalCenter: parent.verticalCenter
  181. font: UM.Theme.getFont("medium")
  182. color: UM.Theme.getColor("text")
  183. verticalAlignment: Text.AlignVCenter
  184. renderType: Text.NativeRendering
  185. }
  186. Cura.TextField
  187. {
  188. id: printerNameTextField
  189. anchors.verticalCenter: parent.verticalCenter
  190. width: (parent.width / 2) | 0
  191. placeholderText: catalog.i18nc("@text", "Please give your printer a name")
  192. maximumLength: 40
  193. validator: RegExpValidator
  194. {
  195. regExp: printerNameTextField.machineNameValidator.machineNameRegex
  196. }
  197. property var machineNameValidator: Cura.MachineNameValidator { }
  198. }
  199. }
  200. }