PrepareMenu.qml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. }
  47. Cura.ConfigurationMenu
  48. {
  49. id: printerSetup
  50. Layout.fillHeight: true
  51. Layout.fillWidth: true
  52. Layout.preferredWidth: parent.machineSelectorWidth * 2
  53. }
  54. Item
  55. {
  56. id: printSetupSelectorItem
  57. // This is a work around to prevent the printSetupSelector from having to be re-loaded every time
  58. // a stage switch is done.
  59. children: [printSetupSelector]
  60. height: childrenRect.height
  61. width: childrenRect.width
  62. }
  63. }
  64. //Pop-up shown when there are multiple items to select from.
  65. Cura.ExpandablePopup
  66. {
  67. id: openFileMenu
  68. visible: prepareMenu.fileProviderModel.count > 1
  69. contentAlignment: Cura.ExpandablePopup.ContentAlignment.AlignLeft
  70. headerCornerSide: Cura.RoundedRectangle.Direction.All
  71. headerPadding: Math.round((parent.height - UM.Theme.getSize("button_icon").height) / 2)
  72. contentPadding: UM.Theme.getSize("default_lining").width
  73. enabled: visible
  74. height: parent.height
  75. width: visible ? (headerPadding * 3 + UM.Theme.getSize("button_icon").height + iconSize) : 0
  76. headerItem: UM.ColorImage
  77. {
  78. id: menuIcon
  79. source: UM.Theme.getIcon("Folder", "medium")
  80. color: UM.Theme.getColor("icon")
  81. }
  82. contentItem: Item
  83. {
  84. id: popup
  85. Column
  86. {
  87. id: openProviderColumn
  88. // Automatically set the width to fit the widest MenuItem
  89. // Based on https://martin.rpdev.net/2018/03/13/qt-quick-controls-2-automatically-set-the-width-of-menus.html
  90. function setWidth()
  91. {
  92. var result = 0;
  93. var padding = 0;
  94. for (var i = 0; i < fileProviderRepeater.count; ++i) {
  95. var item = fileProviderRepeater.itemAt(i);
  96. if (item.hasOwnProperty("implicitWidth"))
  97. {
  98. var itemWidth = item.implicitWidth;
  99. result = Math.max(itemWidth, result);
  100. padding = Math.max(item.padding, padding);
  101. }
  102. }
  103. return result + padding * 2;
  104. }
  105. width: setWidth()
  106. Repeater
  107. {
  108. id: fileProviderRepeater
  109. model: prepareMenu.fileProviderModel
  110. delegate: Button
  111. {
  112. leftPadding: UM.Theme.getSize("default_margin").width
  113. rightPadding: UM.Theme.getSize("default_margin").width
  114. width: openProviderColumn.width
  115. height: UM.Theme.getSize("action_button").height
  116. hoverEnabled: true
  117. contentItem: UM.Label
  118. {
  119. text: model.displayText
  120. font: UM.Theme.getFont("medium")
  121. width: contentWidth
  122. height: parent.height
  123. }
  124. onClicked:
  125. {
  126. if(model.index == 0) //The 0th element is the "From Disk" option, which should activate the open local file dialog.
  127. {
  128. Cura.Actions.open.trigger();
  129. }
  130. else
  131. {
  132. prepareMenu.fileProviderModel.trigger(model.name);
  133. }
  134. }
  135. background: Rectangle
  136. {
  137. color: parent.hovered ? UM.Theme.getColor("action_button_hovered") : "transparent"
  138. radius: UM.Theme.getSize("action_button_radius").width
  139. width: popup.width
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. //If there is just a single item, show a button instead that directly chooses the one option.
  147. Button
  148. {
  149. id: openFileButton
  150. visible: prepareMenu.fileProviderModel.count <= 1
  151. height: parent.height
  152. width: visible ? height : 0 //Square button (and don't take up space if invisible).
  153. onClicked: Cura.Actions.open.trigger()
  154. enabled: visible && prepareMenu.fileProviderModel.count > 0
  155. hoverEnabled: true
  156. contentItem: Item
  157. {
  158. UM.ColorImage
  159. {
  160. id: buttonIcon
  161. source: UM.Theme.getIcon("Folder", "medium")
  162. anchors.centerIn: parent
  163. width: UM.Theme.getSize("button_icon").width
  164. height: UM.Theme.getSize("button_icon").height
  165. color: UM.Theme.getColor("icon")
  166. }
  167. }
  168. background: Rectangle
  169. {
  170. id: background
  171. height: parent.height
  172. width: parent.width
  173. border.color: UM.Theme.getColor("lining")
  174. border.width: UM.Theme.getSize("default_lining").width
  175. radius: UM.Theme.getSize("default_radius").width
  176. color: openFileButton.hovered ? UM.Theme.getColor("action_button_hovered") : UM.Theme.getColor("action_button")
  177. }
  178. }
  179. }
  180. }