Toolbar.qml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.RecolorImage
  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. sourceSize.height: Math.round(UM.Theme.getSize("button").height / 2)
  57. sourceSize.width: Math.round(UM.Theme.getSize("button").width / 2)
  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. Repeater
  114. {
  115. width: childrenRect.width
  116. height: childrenRect.height
  117. model: extrudersModel.items.length > 1 ? extrudersModel : 0
  118. delegate: ExtruderButton
  119. {
  120. extruder: model
  121. isTopElement: extrudersModel.getItem(0).id == model.id
  122. isBottomElement: extrudersModel.getItem(extrudersModel.rowCount() - 1).id == model.id
  123. }
  124. }
  125. }
  126. }
  127. property var extrudersModel: CuraApplication.getExtrudersModel()
  128. UM.PointingRectangle
  129. {
  130. id: panelBorder
  131. anchors.left: parent.right
  132. anchors.leftMargin: UM.Theme.getSize("default_margin").width
  133. anchors.top: base.top
  134. anchors.topMargin: base.activeY
  135. z: buttons.z - 1
  136. target: Qt.point(parent.right, base.activeY + Math.round(UM.Theme.getSize("button").height/2))
  137. arrowSize: UM.Theme.getSize("default_arrow").width
  138. width:
  139. {
  140. if (panel.item && panel.width > 0)
  141. {
  142. return Math.max(panel.width + 2 * UM.Theme.getSize("default_margin").width)
  143. }
  144. else
  145. {
  146. return 0;
  147. }
  148. }
  149. height: panel.item ? panel.height + 2 * UM.Theme.getSize("default_margin").height : 0
  150. opacity: panel.item && panel.width > 0 ? 1 : 0
  151. Behavior on opacity { NumberAnimation { duration: 100 } }
  152. color: UM.Theme.getColor("tool_panel_background")
  153. borderColor: UM.Theme.getColor("lining")
  154. borderWidth: UM.Theme.getSize("default_lining").width
  155. MouseArea //Catch all mouse events (so scene doesn't handle them)
  156. {
  157. anchors.fill: parent
  158. acceptedButtons: Qt.AllButtons
  159. onWheel: wheel.accepted = true
  160. }
  161. Loader
  162. {
  163. id: panel
  164. x: UM.Theme.getSize("default_margin").width
  165. y: UM.Theme.getSize("default_margin").height
  166. source: UM.ActiveTool.valid ? UM.ActiveTool.activeToolPanel : ""
  167. enabled: UM.Controller.toolsEnabled
  168. }
  169. }
  170. // This rectangle displays the information about the current angle etc. when
  171. // dragging a tool handle.
  172. Rectangle
  173. {
  174. id: toolInfo
  175. x: visible ? -base.x + base.mouseX + UM.Theme.getSize("default_margin").width: 0
  176. y: visible ? -base.y + base.mouseY + UM.Theme.getSize("default_margin").height: 0
  177. width: toolHint.width + UM.Theme.getSize("default_margin").width
  178. height: toolHint.height;
  179. color: UM.Theme.getColor("tooltip")
  180. Label
  181. {
  182. id: toolHint
  183. text: UM.ActiveTool.properties.getValue("ToolHint") != undefined ? UM.ActiveTool.properties.getValue("ToolHint") : ""
  184. color: UM.Theme.getColor("tooltip_text")
  185. font: UM.Theme.getFont("default")
  186. anchors.horizontalCenter: parent.horizontalCenter
  187. }
  188. visible: toolHint.text != ""
  189. }
  190. states: [
  191. State {
  192. name: "anchorAtTop"
  193. AnchorChanges {
  194. target: panelBorder
  195. anchors.top: base.top
  196. anchors.bottom: undefined
  197. }
  198. PropertyChanges {
  199. target: panelBorder
  200. anchors.topMargin: base.activeY
  201. }
  202. },
  203. State {
  204. name: "anchorAtBottom"
  205. AnchorChanges {
  206. target: panelBorder
  207. anchors.top: undefined
  208. anchors.bottom: base.top
  209. }
  210. PropertyChanges {
  211. target: panelBorder
  212. anchors.bottomMargin: {
  213. if (panelBorder.height > (base.activeY + UM.Theme.getSize("button").height)) {
  214. // panel is tall, align the top of the panel with the top of the first tool button
  215. return -panelBorder.height
  216. }
  217. // align the bottom of the panel with the bottom of the selected tool button
  218. return -(base.activeY + UM.Theme.getSize("button").height)
  219. }
  220. }
  221. }
  222. ]
  223. }