Toolbar.qml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (c) 2018 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.2
  4. import QtQuick.Controls 2.3
  5. import UM 1.5 as UM
  6. import Cura 1.0 as Cura
  7. Item
  8. {
  9. id: base
  10. width: buttons.width
  11. height: buttons.height
  12. property int activeY
  13. Item
  14. {
  15. id: buttons
  16. width: parent.visible ? toolButtons.width : 0
  17. height: childrenRect.height
  18. Behavior on width { NumberAnimation { duration: 100 } }
  19. // Used to create a rounded rectangle behind the toolButtons
  20. Rectangle
  21. {
  22. anchors
  23. {
  24. fill: toolButtons
  25. leftMargin: -radius - border.width
  26. rightMargin: -border.width
  27. topMargin: -border.width
  28. bottomMargin: -border.width
  29. }
  30. radius: UM.Theme.getSize("default_radius").width
  31. color: UM.Theme.getColor("lining")
  32. }
  33. Column
  34. {
  35. id: toolButtons
  36. anchors.top: parent.top
  37. anchors.right: parent.right
  38. Repeater
  39. {
  40. id: repeat
  41. model: UM.ToolModel { id: toolsModel }
  42. width: childrenRect.width
  43. height: childrenRect.height
  44. delegate: UM.ToolbarButton
  45. {
  46. text: model.name + (model.shortcut ? ` ("${model.shortcut}")` : "")
  47. checkable: true
  48. checked: model.active
  49. enabled: model.enabled && UM.Selection.hasSelection && UM.Controller.toolsEnabled
  50. isTopElement: toolsModel.getItem(0).id == model.id
  51. isBottomElement: toolsModel.getItem(toolsModel.count - 1).id == model.id
  52. toolItem: UM.ColorImage
  53. {
  54. source: UM.Theme.getIcon(model.icon) != "" ? UM.Theme.getIcon(model.icon) : "file:///" + model.location + "/" + model.icon
  55. color: UM.Theme.getColor("icon")
  56. }
  57. onCheckedChanged:
  58. {
  59. if (checked)
  60. {
  61. base.activeY = y;
  62. }
  63. //Clear focus when tools change. This prevents the tool grabbing focus when activated.
  64. //Grabbing focus prevents items from being deleted.
  65. //Apparently this was only a problem on MacOS.
  66. forceActiveFocus();
  67. }
  68. //Workaround since using ToolButton's onClicked would break the binding of the checked property, instead
  69. //just catch the click so we do not trigger that behaviour.
  70. MouseArea
  71. {
  72. anchors.fill: parent;
  73. onClicked:
  74. {
  75. forceActiveFocus() //First grab focus, so all the text fields are updated
  76. if(parent.checked)
  77. {
  78. UM.Controller.setActiveTool(null);
  79. }
  80. else
  81. {
  82. UM.Controller.setActiveTool(model.id);
  83. }
  84. base.state = (index < toolsModel.count/2) ? "anchorAtTop" : "anchorAtBottom";
  85. }
  86. }
  87. }
  88. }
  89. }
  90. // Used to create a rounded rectangle behind the extruderButtons
  91. Rectangle
  92. {
  93. anchors
  94. {
  95. fill: extruderButtons
  96. leftMargin: -radius - border.width
  97. rightMargin: -border.width
  98. topMargin: -border.width
  99. bottomMargin: -border.width
  100. }
  101. radius: UM.Theme.getSize("default_radius").width
  102. color: UM.Theme.getColor("lining")
  103. visible: extrudersModel.items.length > 1
  104. }
  105. Column
  106. {
  107. id: extruderButtons
  108. anchors.topMargin: UM.Theme.getSize("default_margin").height
  109. anchors.top: toolButtons.bottom
  110. anchors.right: parent.right
  111. Repeater
  112. {
  113. width: childrenRect.width
  114. height: childrenRect.height
  115. model: extrudersModel.items.length > 1 ? extrudersModel : 0
  116. delegate: ExtruderButton
  117. {
  118. extruder: model
  119. isTopElement: extrudersModel.getItem(0).id == model.id
  120. isBottomElement: extrudersModel.getItem(extrudersModel.rowCount() - 1).id == model.id
  121. }
  122. }
  123. }
  124. }
  125. property var extrudersModel: CuraApplication.getExtrudersModel()
  126. UM.PointingRectangle
  127. {
  128. id: panelBorder
  129. anchors.left: parent.right
  130. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  131. anchors.top: base.top
  132. anchors.topMargin: base.activeY
  133. z: buttons.z - 1
  134. target: Qt.point(-1, base.activeY + Math.round(UM.Theme.getSize("button").height / 2))
  135. arrowSize: UM.Theme.getSize("default_arrow").width
  136. width:
  137. {
  138. if (panel.item && panel.width > 0)
  139. {
  140. return Math.max(panel.width + 2 * UM.Theme.getSize("default_margin").width)
  141. }
  142. else
  143. {
  144. return 0;
  145. }
  146. }
  147. height: panel.item ? panel.height + 2 * UM.Theme.getSize("default_margin").height : 0
  148. opacity: panel.item && panel.width > 0 ? 1 : 0
  149. Behavior on opacity { NumberAnimation { duration: 100 } }
  150. color: UM.Theme.getColor("tool_panel_background")
  151. borderColor: UM.Theme.getColor("lining")
  152. borderWidth: UM.Theme.getSize("default_lining").width
  153. MouseArea //Catch all mouse events (so scene doesn't handle them)
  154. {
  155. anchors.fill: parent
  156. acceptedButtons: Qt.AllButtons
  157. onWheel: wheel.accepted = true
  158. }
  159. Loader
  160. {
  161. id: panel
  162. x: UM.Theme.getSize("default_margin").width
  163. y: UM.Theme.getSize("default_margin").height
  164. source: UM.ActiveTool.valid ? UM.ActiveTool.activeToolPanel : ""
  165. enabled: UM.Controller.toolsEnabled
  166. }
  167. }
  168. // This rectangle displays the information about the current angle etc. when
  169. // dragging a tool handle.
  170. Rectangle
  171. {
  172. id: toolInfo
  173. x: visible ? -base.x + base.mouseX + UM.Theme.getSize("default_margin").width: 0
  174. y: visible ? -base.y + base.mouseY + UM.Theme.getSize("default_margin").height: 0
  175. width: toolHint.width + UM.Theme.getSize("default_margin").width
  176. height: toolHint.height;
  177. color: UM.Theme.getColor("tooltip")
  178. Label
  179. {
  180. id: toolHint
  181. text: UM.ActiveTool.properties.getValue("ToolHint") != undefined ? UM.ActiveTool.properties.getValue("ToolHint") : ""
  182. color: UM.Theme.getColor("tooltip_text")
  183. font: UM.Theme.getFont("default")
  184. anchors.horizontalCenter: parent.horizontalCenter
  185. }
  186. visible: toolHint.text != ""
  187. }
  188. states: [
  189. State {
  190. name: "anchorAtTop"
  191. AnchorChanges {
  192. target: panelBorder
  193. anchors.top: base.top
  194. anchors.bottom: undefined
  195. }
  196. PropertyChanges {
  197. target: panelBorder
  198. anchors.topMargin: base.activeY
  199. }
  200. },
  201. State {
  202. name: "anchorAtBottom"
  203. AnchorChanges {
  204. target: panelBorder
  205. anchors.top: undefined
  206. anchors.bottom: base.top
  207. }
  208. PropertyChanges {
  209. target: panelBorder
  210. anchors.bottomMargin: {
  211. if (panelBorder.height > (base.activeY + UM.Theme.getSize("button").height)) {
  212. // panel is tall, align the top of the panel with the top of the first tool button
  213. return -panelBorder.height
  214. }
  215. // align the bottom of the panel with the bottom of the selected tool button
  216. return -(base.activeY + UM.Theme.getSize("button").height)
  217. }
  218. }
  219. }
  220. ]
  221. }