Toolbar.qml 8.6 KB

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