AddMachineDialog.qml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (c) 2016 Ultimaker B.V.
  2. // Cura is released under the terms of the AGPLv3 or higher.
  3. import QtQuick 2.2
  4. import QtQuick.Controls 1.1
  5. import QtQuick.Layouts 1.1
  6. import QtQuick.Window 2.1
  7. import QtQuick.Controls.Styles 1.1
  8. import UM 1.2 as UM
  9. import Cura 1.0 as Cura
  10. UM.Dialog
  11. {
  12. id: base
  13. title: catalog.i18nc("@title:window", "Add Printer")
  14. property bool firstRun: false
  15. property string preferredCategory: "Ultimaker"
  16. property string activeCategory: preferredCategory
  17. onVisibilityChanged:
  18. {
  19. // Reset selection and machine name
  20. if (visible) {
  21. activeCategory = preferredCategory;
  22. machineList.currentIndex = 0;
  23. machineName.text = getMachineName();
  24. }
  25. }
  26. signal machineAdded(string id)
  27. function getMachineName()
  28. {
  29. var name = machineList.model.getItem(machineList.currentIndex) != undefined ? machineList.model.getItem(machineList.currentIndex).name : ""
  30. return name
  31. }
  32. ScrollView
  33. {
  34. id: machinesHolder
  35. anchors
  36. {
  37. left: parent.left;
  38. top: parent.top;
  39. right: parent.right;
  40. bottom: parent.bottom;
  41. }
  42. ListView
  43. {
  44. id: machineList
  45. model: UM.DefinitionContainersModel
  46. {
  47. id: machineDefinitionsModel
  48. filter: { "visible": true }
  49. sectionProperty: "category"
  50. preferredSectionValue: preferredCategory
  51. }
  52. section.property: "section"
  53. section.delegate: Button
  54. {
  55. text: section
  56. style: ButtonStyle
  57. {
  58. background: Rectangle
  59. {
  60. border.width: 0
  61. color: "transparent";
  62. height: UM.Theme.getSize("standard_list_lineheight").height
  63. width: machineList.width
  64. }
  65. label: Label
  66. {
  67. anchors.left: parent.left
  68. anchors.leftMargin: UM.Theme.getSize("standard_arrow").width + UM.Theme.getSize("default_margin").width
  69. text: control.text
  70. color: palette.windowText
  71. font.bold: true
  72. UM.RecolorImage
  73. {
  74. id: downArrow
  75. anchors.verticalCenter: parent.verticalCenter
  76. anchors.right: parent.left
  77. anchors.rightMargin: UM.Theme.getSize("default_margin").width
  78. width: UM.Theme.getSize("standard_arrow").width
  79. height: UM.Theme.getSize("standard_arrow").height
  80. sourceSize.width: width
  81. sourceSize.height: width
  82. color: palette.windowText
  83. source: base.activeCategory == section ? UM.Theme.getIcon("arrow_bottom") : UM.Theme.getIcon("arrow_right")
  84. }
  85. }
  86. }
  87. onClicked:
  88. {
  89. base.activeCategory = section;
  90. if (machineList.model.getItem(machineList.currentIndex).section != section) {
  91. // Find the first machine from this section
  92. for(var i = 0; i < sortedMachineDefinitionsModel.count; i++) {
  93. var item = sortedMachineDefinitionsModel.getItem(i);
  94. if (item.section == section) {
  95. machineList.currentIndex = i;
  96. break;
  97. }
  98. }
  99. }
  100. machineName.text = getMachineName();
  101. }
  102. }
  103. delegate: RadioButton
  104. {
  105. id: machineButton
  106. anchors.left: parent.left
  107. anchors.leftMargin: UM.Theme.getSize("standard_list_lineheight").width
  108. opacity: 1;
  109. height: UM.Theme.getSize("standard_list_lineheight").height;
  110. checked: ListView.isCurrentItem;
  111. exclusiveGroup: printerGroup;
  112. text: model.name
  113. onClicked:
  114. {
  115. ListView.view.currentIndex = index;
  116. machineName.text = getMachineName()
  117. }
  118. states: State
  119. {
  120. name: "collapsed";
  121. when: base.activeCategory != model.section;
  122. PropertyChanges { target: machineButton; opacity: 0; height: 0; }
  123. }
  124. transitions:
  125. [
  126. Transition
  127. {
  128. to: "collapsed";
  129. SequentialAnimation
  130. {
  131. NumberAnimation { property: "opacity"; duration: 75; }
  132. NumberAnimation { property: "height"; duration: 75; }
  133. }
  134. },
  135. Transition
  136. {
  137. from: "collapsed";
  138. SequentialAnimation
  139. {
  140. NumberAnimation { property: "height"; duration: 75; }
  141. NumberAnimation { property: "opacity"; duration: 75; }
  142. }
  143. }
  144. ]
  145. }
  146. }
  147. }
  148. TextField
  149. {
  150. id: machineName;
  151. text: getMachineName()
  152. implicitWidth: UM.Theme.getSize("standard_list_input").width
  153. maximumLength: 40
  154. //validator: Cura.MachineNameValidator { } //TODO: Gives a segfault in PyQt5.6. For now, we must use a signal on text changed.
  155. validator: RegExpValidator
  156. {
  157. regExp: {
  158. machineName.machine_name_validator.machineNameRegex
  159. }
  160. }
  161. property var machine_name_validator: Cura.MachineNameValidator { }
  162. anchors.bottom:parent.bottom
  163. }
  164. Button
  165. {
  166. text: catalog.i18nc("@action:button", "Add Printer")
  167. anchors.bottom: parent.bottom
  168. anchors.right: parent.right
  169. onClicked: addMachine()
  170. }
  171. onAccepted: addMachine()
  172. function addMachine()
  173. {
  174. base.visible = false
  175. var item = machineList.model.getItem(machineList.currentIndex);
  176. Cura.MachineManager.addMachine(machineName.text, item.id)
  177. base.machineAdded(item.id) // Emit signal that the user added a machine.
  178. }
  179. Item
  180. {
  181. UM.I18nCatalog
  182. {
  183. id: catalog;
  184. name: "cura";
  185. }
  186. SystemPalette { id: palette }
  187. ExclusiveGroup { id: printerGroup; }
  188. }
  189. }