AddLocalPrinterScrollView.qml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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: preferredCategory
  21. // By default (when this list shows up) we always expand the "Ultimaker" section.
  22. property string preferredCategory: "Ultimaker"
  23. property int maxItemCountAtOnce: 10 // show at max 10 items at once, otherwise you need to scroll.
  24. // User-editable printer name
  25. property alias printerName: printerNameTextField.text
  26. property alias isPrinterNameValid: printerNameTextField.acceptableInput
  27. onCurrentItemChanged:
  28. {
  29. printerName = currentItem == null ? "" : currentItem.name
  30. }
  31. function updateCurrentItemUponSectionChange()
  32. {
  33. // Find the first machine from this section
  34. for (var i = 0; i < machineList.count; i++)
  35. {
  36. var item = machineList.model.getItem(i)
  37. if (item.section == base.currentSection)
  38. {
  39. machineList.currentIndex = i
  40. break
  41. }
  42. }
  43. }
  44. Component.onCompleted:
  45. {
  46. updateCurrentItemUponSectionChange()
  47. }
  48. Item
  49. {
  50. id: localPrinterSelectionItem
  51. anchors.left: parent.left
  52. anchors.right: parent.right
  53. anchors.top: parent.top
  54. height: childrenRect.height
  55. // ScrollView + ListView for selecting a local printer to add
  56. ScrollView
  57. {
  58. id: scrollView
  59. anchors.left: parent.left
  60. anchors.right: parent.right
  61. anchors.top: parent.top
  62. height: maxItemCountAtOnce * UM.Theme.getSize("action_button").height
  63. ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
  64. ScrollBar.vertical.policy: ScrollBar.AsNeeded
  65. clip: true
  66. ListView
  67. {
  68. id: machineList
  69. cacheBuffer: 1000000 // Set a large cache to effectively just cache every list item.
  70. model: UM.DefinitionContainersModel
  71. {
  72. id: machineDefinitionsModel
  73. filter: { "visible": true }
  74. sectionProperty: "category"
  75. preferredSectionValue: preferredCategory
  76. }
  77. section.property: "section"
  78. section.delegate: sectionHeader
  79. delegate: machineButton
  80. }
  81. Component
  82. {
  83. id: sectionHeader
  84. Button
  85. {
  86. id: button
  87. width: ListView.view.width
  88. height: UM.Theme.getSize("action_button").height
  89. text: section
  90. property bool isActive: base.currentSection == section
  91. background: Rectangle
  92. {
  93. anchors.fill: parent
  94. color: isActive ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  95. }
  96. contentItem: Item
  97. {
  98. width: childrenRect.width
  99. height: UM.Theme.getSize("action_button").height
  100. UM.RecolorImage
  101. {
  102. id: arrow
  103. anchors.left: parent.left
  104. width: UM.Theme.getSize("standard_arrow").width
  105. height: UM.Theme.getSize("standard_arrow").height
  106. sourceSize.width: width
  107. sourceSize.height: height
  108. color: UM.Theme.getColor("text")
  109. source: base.currentSection == section ? UM.Theme.getIcon("arrow_bottom") : UM.Theme.getIcon("arrow_right")
  110. }
  111. Label
  112. {
  113. id: label
  114. anchors.left: arrow.right
  115. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  116. verticalAlignment: Text.AlignVCenter
  117. text: button.text
  118. font: UM.Theme.getFont("default_bold")
  119. color: UM.Theme.getColor("text")
  120. renderType: Text.NativeRendering
  121. }
  122. }
  123. onClicked:
  124. {
  125. base.currentSection = section
  126. base.updateCurrentItemUponSectionChange()
  127. }
  128. }
  129. }
  130. Component
  131. {
  132. id: machineButton
  133. Cura.RadioButton
  134. {
  135. id: radioButton
  136. anchors.left: parent.left
  137. anchors.leftMargin: UM.Theme.getSize("standard_list_lineheight").width
  138. anchors.right: parent.right
  139. anchors.rightMargin: UM.Theme.getSize("default_margin").width
  140. height: visible ? UM.Theme.getSize("standard_list_lineheight").height : 0
  141. checked: ListView.view.currentIndex == index
  142. text: name
  143. visible: base.currentSection == section
  144. onClicked: ListView.view.currentIndex = index
  145. }
  146. }
  147. }
  148. }
  149. // Horizontal line
  150. Rectangle
  151. {
  152. id: horizontalLine
  153. anchors.top: localPrinterSelectionItem.bottom
  154. anchors.left: parent.left
  155. anchors.right: parent.right
  156. height: UM.Theme.getSize("default_lining").height
  157. color: UM.Theme.getColor("lining")
  158. }
  159. // User-editable printer name row
  160. Row
  161. {
  162. anchors.top: horizontalLine.bottom
  163. anchors.left: parent.left
  164. anchors.right: parent.right
  165. anchors.topMargin: UM.Theme.getSize("default_lining").height
  166. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  167. spacing: UM.Theme.getSize("default_margin").width
  168. Label
  169. {
  170. text: catalog.i18nc("@label", "Printer name")
  171. anchors.verticalCenter: parent.verticalCenter
  172. font: UM.Theme.getFont("medium")
  173. color: UM.Theme.getColor("text")
  174. verticalAlignment: Text.AlignVCenter
  175. renderType: Text.NativeRendering
  176. }
  177. Cura.TextField
  178. {
  179. id: printerNameTextField
  180. anchors.verticalCenter: parent.verticalCenter
  181. width: (parent.width / 2) | 0
  182. placeholderText: catalog.i18nc("@text", "Please give your printer a name")
  183. maximumLength: 40
  184. validator: RegExpValidator
  185. {
  186. regExp: printerNameTextField.machineNameValidator.machineNameRegex
  187. }
  188. property var machineNameValidator: Cura.MachineNameValidator { }
  189. }
  190. }
  191. }