PrepareMenu.qml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright (c) 2021 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.9
  4. import QtQuick.Layouts 1.1
  5. import QtQuick.Controls 2.3
  6. import UM 1.5 as UM
  7. import Cura 1.1 as Cura
  8. Item
  9. {
  10. id: prepareMenu
  11. property var fileProviderModel: CuraApplication.getFileProviderModel()
  12. UM.I18nCatalog
  13. {
  14. id: catalog
  15. name: "cura"
  16. }
  17. anchors
  18. {
  19. left: parent.left
  20. right: parent.right
  21. leftMargin: UM.Theme.getSize("wide_margin").width * 2
  22. rightMargin: UM.Theme.getSize("wide_margin").width * 2
  23. }
  24. // Item to ensure that all of the buttons are nicely centered.
  25. Item
  26. {
  27. anchors.fill: parent
  28. RowLayout
  29. {
  30. id: itemRow
  31. anchors.left: parent.left
  32. anchors.right: parent.right
  33. anchors.leftMargin: UM.Theme.getSize("default_margin").width + openFileButton.width + openFileMenu.width
  34. property int machineSelectorWidth: Math.round((width - printSetupSelectorItem.width) / 3)
  35. height: parent.height
  36. // This is a trick to make sure that the borders of the two adjacent buttons' borders overlap. Otherwise
  37. // there will be double border (one from each button)
  38. spacing: -UM.Theme.getSize("default_lining").width
  39. Cura.MachineSelector
  40. {
  41. id: machineSelection
  42. headerCornerSide: Cura.RoundedRectangle.Direction.Left
  43. Layout.preferredWidth: parent.machineSelectorWidth
  44. Layout.fillWidth: true
  45. Layout.fillHeight: true
  46. machineManager: Cura.MachineManager
  47. onSelectPrinter: function(machine)
  48. {
  49. toggleContent();
  50. Cura.MachineManager.setActiveMachine(machine.id);
  51. }
  52. machineListModel: Cura.MachineListModel {}
  53. buttons: [
  54. Cura.SecondaryButton
  55. {
  56. id: addPrinterButton
  57. leftPadding: UM.Theme.getSize("default_margin").width
  58. rightPadding: UM.Theme.getSize("default_margin").width
  59. text: catalog.i18nc("@button", "Add printer")
  60. // The maximum width of the button is half of the total space, minus the padding of the parent, the left
  61. // padding of the component and half the spacing because of the space between buttons.
  62. fixedWidthMode: true
  63. width: Math.round(parent.width / 2 - leftPadding * 1.5)
  64. onClicked:
  65. {
  66. machineSelection.toggleContent()
  67. Cura.Actions.addMachine.trigger()
  68. }
  69. },
  70. Cura.SecondaryButton
  71. {
  72. id: managePrinterButton
  73. leftPadding: UM.Theme.getSize("default_margin").width
  74. rightPadding: UM.Theme.getSize("default_margin").width
  75. text: catalog.i18nc("@button", "Manage printers")
  76. fixedWidthMode: true
  77. // The maximum width of the button is half of the total space, minus the padding of the parent, the right
  78. // padding of the component and half the spacing because of the space between buttons.
  79. width: Math.round(parent.width / 2 - rightPadding * 1.5)
  80. onClicked:
  81. {
  82. machineSelection.toggleContent()
  83. Cura.Actions.configureMachines.trigger()
  84. }
  85. }
  86. ]
  87. }
  88. Cura.ConfigurationMenu
  89. {
  90. id: printerSetup
  91. Layout.fillHeight: true
  92. Layout.fillWidth: true
  93. Layout.preferredWidth: parent.machineSelectorWidth * 2
  94. }
  95. Item
  96. {
  97. id: printSetupSelectorItem
  98. // This is a work around to prevent the printSetupSelector from having to be re-loaded every time
  99. // a stage switch is done.
  100. children: [printSetupSelector]
  101. height: childrenRect.height
  102. width: childrenRect.width
  103. }
  104. }
  105. //Pop-up shown when there are multiple items to select from.
  106. Cura.ExpandablePopup
  107. {
  108. id: openFileMenu
  109. visible: prepareMenu.fileProviderModel.count > 1
  110. contentAlignment: Cura.ExpandablePopup.ContentAlignment.AlignLeft
  111. headerCornerSide: Cura.RoundedRectangle.Direction.All
  112. headerPadding: Math.round((parent.height - UM.Theme.getSize("button_icon").height) / 2)
  113. contentPadding: UM.Theme.getSize("default_lining").width
  114. enabled: visible
  115. height: parent.height
  116. width: visible ? (headerPadding * 3 + UM.Theme.getSize("button_icon").height + iconSize) : 0
  117. headerItem: UM.ColorImage
  118. {
  119. id: menuIcon
  120. source: UM.Theme.getIcon("Folder", "medium")
  121. color: UM.Theme.getColor("icon")
  122. }
  123. contentItem: Item
  124. {
  125. id: popup
  126. Column
  127. {
  128. id: openProviderColumn
  129. // Automatically set the width to fit the widest MenuItem
  130. // Based on https://martin.rpdev.net/2018/03/13/qt-quick-controls-2-automatically-set-the-width-of-menus.html
  131. function setWidth()
  132. {
  133. var result = 0;
  134. var padding = 0;
  135. for (var i = 0; i < fileProviderRepeater.count; ++i) {
  136. var item = fileProviderRepeater.itemAt(i);
  137. if (item.hasOwnProperty("implicitWidth"))
  138. {
  139. var itemWidth = item.implicitWidth;
  140. result = Math.max(itemWidth, result);
  141. padding = Math.max(item.padding, padding);
  142. }
  143. }
  144. return result + padding * 2;
  145. }
  146. width: setWidth()
  147. Repeater
  148. {
  149. id: fileProviderRepeater
  150. model: prepareMenu.fileProviderModel
  151. delegate: Button
  152. {
  153. leftPadding: UM.Theme.getSize("default_margin").width
  154. rightPadding: UM.Theme.getSize("default_margin").width
  155. width: openProviderColumn.width
  156. height: UM.Theme.getSize("action_button").height
  157. hoverEnabled: true
  158. contentItem: UM.Label
  159. {
  160. text: model.displayText
  161. font: UM.Theme.getFont("medium")
  162. width: contentWidth
  163. height: parent.height
  164. }
  165. onClicked:
  166. {
  167. if(model.index == 0) //The 0th element is the "From Disk" option, which should activate the open local file dialog.
  168. {
  169. Cura.Actions.open.trigger();
  170. }
  171. else
  172. {
  173. prepareMenu.fileProviderModel.trigger(model.name);
  174. }
  175. }
  176. background: Rectangle
  177. {
  178. color: parent.hovered ? UM.Theme.getColor("action_button_hovered") : "transparent"
  179. radius: UM.Theme.getSize("action_button_radius").width
  180. width: popup.width
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. //If there is just a single item, show a button instead that directly chooses the one option.
  188. Button
  189. {
  190. id: openFileButton
  191. visible: prepareMenu.fileProviderModel.count <= 1
  192. height: parent.height
  193. width: visible ? height : 0 //Square button (and don't take up space if invisible).
  194. onClicked: Cura.Actions.open.trigger()
  195. enabled: visible && prepareMenu.fileProviderModel.count > 0
  196. hoverEnabled: true
  197. contentItem: Item
  198. {
  199. UM.ColorImage
  200. {
  201. id: buttonIcon
  202. source: UM.Theme.getIcon("Folder", "medium")
  203. anchors.centerIn: parent
  204. width: UM.Theme.getSize("button_icon").width
  205. height: UM.Theme.getSize("button_icon").height
  206. color: UM.Theme.getColor("icon")
  207. }
  208. }
  209. background: Rectangle
  210. {
  211. id: background
  212. height: parent.height
  213. width: parent.width
  214. border.color: UM.Theme.getColor("lining")
  215. border.width: UM.Theme.getSize("default_lining").width
  216. radius: UM.Theme.getSize("default_radius").width
  217. color: openFileButton.hovered ? UM.Theme.getColor("action_button_hovered") : UM.Theme.getColor("action_button")
  218. }
  219. }
  220. }
  221. }