AddLocalPrinterScrollView.qml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright (c) 2023 UltiMaker
  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. // Trigger update on base.currentSections
  62. base.currentSections = base.currentSections;
  63. }
  64. Row
  65. {
  66. id: localPrinterSelectionItem
  67. anchors.fill: parent
  68. //Selecting a local printer to add from this list.
  69. ListView
  70. {
  71. id: machineList
  72. width: Math.floor(parent.width * 0.48)
  73. height: parent.height
  74. clip: true
  75. ScrollBar.vertical: UM.ScrollBar {}
  76. model: UM.DefinitionContainersModel
  77. {
  78. id: machineDefinitionsModel
  79. filter: { "visible": true }
  80. sectionProperty: "manufacturer"
  81. preferredSections: preferredCategories
  82. }
  83. section.property: "section"
  84. section.delegate: Button
  85. {
  86. id: button
  87. width: machineList.width
  88. height: UM.Theme.getSize("action_button").height
  89. text: section
  90. property bool isActive: base.currentSections.has(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.ColorImage
  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. color: UM.Theme.getColor("text")
  107. source: isActive ? UM.Theme.getIcon("ChevronSingleDown") : UM.Theme.getIcon("ChevronSingleRight")
  108. }
  109. UM.Label
  110. {
  111. id: label
  112. anchors.left: arrow.right
  113. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  114. text: button.text
  115. font: UM.Theme.getFont("default_bold")
  116. }
  117. }
  118. onClicked:
  119. {
  120. if (base.currentSections.has(section))
  121. {
  122. base.currentSections.delete(section);
  123. }
  124. else
  125. {
  126. base.currentSections.add(section);
  127. base.updateCurrentItemUponSectionChange(section);
  128. }
  129. // Trigger update on base.currentSections
  130. base.currentSections = base.currentSections;
  131. }
  132. }
  133. delegate: Cura.RadioButton
  134. {
  135. id: radioButton
  136. anchors
  137. {
  138. left: parent !== null ? parent.left : undefined
  139. leftMargin: UM.Theme.getSize("standard_list_lineheight").width
  140. right: parent !== null ? parent.right : undefined
  141. rightMargin: UM.Theme.getSize("default_margin").width
  142. }
  143. height: visible ? UM.Theme.getSize("standard_list_lineheight").height : 0 //This causes the scrollbar to vary in length due to QTBUG-76830.
  144. checked: machineList.currentIndex == index
  145. text: name
  146. visible: base.currentSections.has(section)
  147. onClicked: machineList.currentIndex = index
  148. }
  149. }
  150. // Vertical line
  151. Rectangle
  152. {
  153. id: verticalLine
  154. anchors.top: parent.top
  155. height: parent.height - UM.Theme.getSize("default_lining").height
  156. width: UM.Theme.getSize("default_lining").height
  157. color: UM.Theme.getColor("lining")
  158. }
  159. // User-editable printer name row
  160. Column
  161. {
  162. width: Math.floor(parent.width * 0.52)
  163. spacing: UM.Theme.getSize("default_margin").width
  164. padding: UM.Theme.getSize("default_margin").width
  165. UM.Label
  166. {
  167. width: parent.width - (2 * UM.Theme.getSize("default_margin").width)
  168. text: base.getMachineName()
  169. color: UM.Theme.getColor("primary_button")
  170. font: UM.Theme.getFont("huge")
  171. elide: Text.ElideRight
  172. }
  173. Grid
  174. {
  175. width: parent.width
  176. columns: 2
  177. rowSpacing: UM.Theme.getSize("default_lining").height
  178. columnSpacing: UM.Theme.getSize("default_margin").width
  179. verticalItemAlignment: Grid.AlignVCenter
  180. UM.Label
  181. {
  182. id: manufacturerLabel
  183. text: catalog.i18nc("@label", "Manufacturer")
  184. }
  185. UM.Label
  186. {
  187. text: base.getMachineMetaDataEntry("manufacturer")
  188. width: parent.width - manufacturerLabel.width
  189. wrapMode: Text.WordWrap
  190. }
  191. UM.Label
  192. {
  193. id: profileAuthorLabel
  194. text: catalog.i18nc("@label", "Profile author")
  195. }
  196. UM.Label
  197. {
  198. text: base.getMachineMetaDataEntry("author")
  199. width: parent.width - profileAuthorLabel.width
  200. wrapMode: Text.WordWrap
  201. }
  202. UM.Label
  203. {
  204. id: printerNameLabel
  205. text: catalog.i18nc("@label", "Printer name")
  206. }
  207. Cura.TextField
  208. {
  209. id: printerNameTextField
  210. placeholderText: catalog.i18nc("@text", "Please name your printer")
  211. maximumLength: 40
  212. width: parent.width - (printerNameLabel.width + (3 * UM.Theme.getSize("default_margin").width))
  213. validator: RegularExpressionValidator
  214. {
  215. regularExpression: printerNameTextField.machineNameValidator.machineNameRegex
  216. }
  217. property var machineNameValidator: Cura.MachineNameValidator { }
  218. }
  219. }
  220. }
  221. }
  222. }