AddLocalPrinterScrollView.qml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (c) 2022 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.15
  4. import QtQuick.Controls 2.14
  5. import UM 1.5 as UM
  6. import Cura 1.1 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. // The currently selected machine item in the local machine list.
  15. property var currentItem: machineList.currentIndex >= 0 ? machineList.model.getItem(machineList.currentIndex) : null
  16. // The currently active (expanded) section/category, where section/category is the grouping of local machine items.
  17. property var currentSections: new Set()
  18. // By default (when this list shows up) we always expand the "Ultimaker" section.
  19. property var preferredCategories: {
  20. "Ultimaker B.V.": -2,
  21. "Custom": -1
  22. }
  23. // User-editable printer name
  24. property alias printerName: printerNameTextField.text
  25. property alias isPrinterNameValid: printerNameTextField.acceptableInput
  26. onCurrentItemChanged:
  27. {
  28. printerName = currentItem == null ? "" : currentItem.name
  29. }
  30. function updateCurrentItemUponSectionChange(section)
  31. {
  32. // Find the first machine from this section
  33. for (var i = 0; i < machineList.count; i ++)
  34. {
  35. const item = machineList.model.getItem(i);
  36. if (item.section == section)
  37. {
  38. machineList.currentIndex = i;
  39. break;
  40. }
  41. }
  42. }
  43. function getMachineName()
  44. {
  45. return machineList.model.getItem(machineList.currentIndex) != undefined ? machineList.model.getItem(machineList.currentIndex).name : "";
  46. }
  47. function getMachineMetaDataEntry(key)
  48. {
  49. var metadata = machineList.model.getItem(machineList.currentIndex) != undefined ? machineList.model.getItem(machineList.currentIndex).metadata : undefined;
  50. if (metadata)
  51. {
  52. return metadata[key];
  53. }
  54. return undefined;
  55. }
  56. Component.onCompleted:
  57. {
  58. const initialSection = "Ultimaker B.V.";
  59. base.currentSections.add(initialSection);
  60. updateCurrentItemUponSectionChange(initialSection);
  61. }
  62. Row
  63. {
  64. id: localPrinterSelectionItem
  65. anchors.fill: parent
  66. //Selecting a local printer to add from this list.
  67. ListView
  68. {
  69. id: machineList
  70. width: Math.floor(parent.width * 0.48)
  71. height: parent.height
  72. clip: true
  73. ScrollBar.vertical: UM.ScrollBar {}
  74. model: UM.DefinitionContainersModel
  75. {
  76. id: machineDefinitionsModel
  77. filter: { "visible": true }
  78. sectionProperty: "manufacturer"
  79. preferredSections: preferredCategories
  80. }
  81. section.property: "section"
  82. section.delegate: Button
  83. {
  84. id: button
  85. width: machineList.width
  86. height: UM.Theme.getSize("action_button").height
  87. text: section
  88. property bool isActive: base.currentSections.has(section)
  89. background: Rectangle
  90. {
  91. anchors.fill: parent
  92. color: isActive ? UM.Theme.getColor("setting_control_highlight") : "transparent"
  93. }
  94. contentItem: Item
  95. {
  96. width: childrenRect.width
  97. height: UM.Theme.getSize("action_button").height
  98. UM.ColorImage
  99. {
  100. id: arrow
  101. anchors.left: parent.left
  102. width: UM.Theme.getSize("standard_arrow").width
  103. height: UM.Theme.getSize("standard_arrow").height
  104. color: UM.Theme.getColor("text")
  105. source: isActive ? UM.Theme.getIcon("ChevronSingleDown") : UM.Theme.getIcon("ChevronSingleRight")
  106. }
  107. UM.Label
  108. {
  109. id: label
  110. anchors.left: arrow.right
  111. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  112. text: button.text
  113. font: UM.Theme.getFont("default_bold")
  114. }
  115. }
  116. onClicked:
  117. {
  118. if (base.currentSections.has(section))
  119. {
  120. base.currentSections.delete(section);
  121. }
  122. else
  123. {
  124. base.currentSections.add(section);
  125. base.updateCurrentItemUponSectionChange(section);
  126. }
  127. // Trigger update on base.currentSections
  128. base.currentSections = base.currentSections;
  129. }
  130. }
  131. delegate: Cura.RadioButton
  132. {
  133. id: radioButton
  134. anchors
  135. {
  136. left: parent !== null ? parent.left : undefined
  137. leftMargin: UM.Theme.getSize("standard_list_lineheight").width
  138. right: parent !== null ? parent.right : undefined
  139. rightMargin: UM.Theme.getSize("default_margin").width
  140. }
  141. height: visible ? UM.Theme.getSize("standard_list_lineheight").height : 0 //This causes the scrollbar to vary in length due to QTBUG-76830.
  142. checked: machineList.currentIndex == index
  143. text: name
  144. visible: base.currentSections.has(section)
  145. onClicked: machineList.currentIndex = index
  146. }
  147. }
  148. // Vertical line
  149. Rectangle
  150. {
  151. id: verticalLine
  152. anchors.top: parent.top
  153. height: parent.height - UM.Theme.getSize("default_lining").height
  154. width: UM.Theme.getSize("default_lining").height
  155. color: UM.Theme.getColor("lining")
  156. }
  157. // User-editable printer name row
  158. Column
  159. {
  160. width: Math.floor(parent.width * 0.52)
  161. spacing: UM.Theme.getSize("default_margin").width
  162. padding: UM.Theme.getSize("default_margin").width
  163. UM.Label
  164. {
  165. width: parent.width - (2 * UM.Theme.getSize("default_margin").width)
  166. text: base.getMachineName()
  167. color: UM.Theme.getColor("primary_button")
  168. font: UM.Theme.getFont("huge")
  169. elide: Text.ElideRight
  170. }
  171. Grid
  172. {
  173. width: parent.width
  174. columns: 2
  175. rowSpacing: UM.Theme.getSize("default_lining").height
  176. columnSpacing: UM.Theme.getSize("default_margin").width
  177. verticalItemAlignment: Grid.AlignVCenter
  178. UM.Label
  179. {
  180. id: manufacturerLabel
  181. text: catalog.i18nc("@label", "Manufacturer")
  182. }
  183. UM.Label
  184. {
  185. text: base.getMachineMetaDataEntry("manufacturer")
  186. width: parent.width - manufacturerLabel.width
  187. wrapMode: Text.WordWrap
  188. }
  189. UM.Label
  190. {
  191. id: profileAuthorLabel
  192. text: catalog.i18nc("@label", "Profile author")
  193. }
  194. UM.Label
  195. {
  196. text: base.getMachineMetaDataEntry("author")
  197. width: parent.width - profileAuthorLabel.width
  198. wrapMode: Text.WordWrap
  199. }
  200. UM.Label
  201. {
  202. id: printerNameLabel
  203. text: catalog.i18nc("@label", "Printer name")
  204. }
  205. Cura.TextField
  206. {
  207. id: printerNameTextField
  208. placeholderText: catalog.i18nc("@text", "Please name your printer")
  209. maximumLength: 40
  210. width: parent.width - (printerNameLabel.width + (3 * UM.Theme.getSize("default_margin").width))
  211. validator: RegularExpressionValidator
  212. {
  213. regularExpression: printerNameTextField.machineNameValidator.machineNameRegex
  214. }
  215. property var machineNameValidator: Cura.MachineNameValidator { }
  216. }
  217. }
  218. }
  219. }
  220. }