ActionButton.qml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2021 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.7
  4. import QtQuick.Controls 2.1
  5. import QtGraphicalEffects 1.0 // For the dropshadow
  6. import UM 1.5 as UM
  7. import Cura 1.0 as Cura
  8. Button
  9. {
  10. id: button
  11. property bool isIconOnRightSide: false
  12. property alias iconSource: buttonIconLeft.source
  13. property real iconSize: UM.Theme.getSize("action_button_icon").height
  14. property alias textFont: buttonText.font
  15. property alias cornerRadius: backgroundRect.radius
  16. property alias tooltip: tooltip.tooltipText
  17. property alias tooltipWidth: tooltip.width
  18. property color color: UM.Theme.getColor("primary")
  19. property color hoverColor: UM.Theme.getColor("primary_hover")
  20. property color disabledColor: color
  21. property color textColor: UM.Theme.getColor("button_text")
  22. property color textHoverColor: textColor
  23. property color textDisabledColor: disabledColor
  24. property color outlineColor: color
  25. property color outlineHoverColor: outlineColor
  26. property color outlineDisabledColor: disabledColor
  27. property alias busy: busyIndicator.visible
  28. property bool underlineTextOnHover: false
  29. property alias toolTipContentAlignment: tooltip.contentAlignment
  30. // This property is used to indicate whether the button has a fixed width or the width would depend on the contents
  31. // Be careful when using fixedWidthMode, the translated texts can be too long that they won't fit. In any case,
  32. // we elide the text to the right so the text will be cut off with the three dots at the end.
  33. property var fixedWidthMode: false
  34. // This property is used when the space for the button is limited. In case the button needs to grow with the text,
  35. // but it can exceed a maximum, then this value have to be set.
  36. property int maximumWidth: 0
  37. // These properties are deprecated.
  38. // To (maybe) prevent a major SDK upgrade, mark them as deprecated instead of just outright removing them.
  39. // Note, if you still want rounded corners, use (something based on) Cura.RoundedRectangle.
  40. property alias cornerSide: deprecatedProperties.cornerSide
  41. property alias shadowColor: deprecatedProperties.shadowColor
  42. property alias shadowEnabled: deprecatedProperties.shadowEnabled
  43. Item
  44. {
  45. id: deprecatedProperties
  46. visible: false
  47. enabled: false
  48. width: 0
  49. height: 0
  50. property var cornerSide: null
  51. property var shadowColor: null
  52. property var shadowEnabled: null
  53. onCornerSideChanged:
  54. {
  55. if (cornerSide != null)
  56. {
  57. CuraApplication.writeToLog("w", "'ActionButton.cornerSide' is deprecated since 4.11. Rounded corners can still be made with 'Cura.RoundedRectangle'.");
  58. }
  59. }
  60. onShadowColorChanged:
  61. {
  62. if (shadowColor != null)
  63. {
  64. CuraApplication.writeToLog("w", "'ActionButton.shadowColor' is deprecated since 4.11.")
  65. }
  66. }
  67. onShadowEnabledChanged:
  68. {
  69. if (shadowEnabled != null)
  70. {
  71. CuraApplication.writeToLog("w", "'ActionButton.shadowEnabled' is deprecated since 4.11.")
  72. }
  73. }
  74. }
  75. leftPadding: UM.Theme.getSize("default_margin").width
  76. rightPadding: UM.Theme.getSize("default_margin").width
  77. height: UM.Theme.getSize("action_button").height
  78. hoverEnabled: true
  79. onHoveredChanged:
  80. {
  81. if(underlineTextOnHover)
  82. {
  83. buttonText.font.underline = hovered
  84. }
  85. }
  86. contentItem: Row
  87. {
  88. spacing: UM.Theme.getSize("narrow_margin").width
  89. height: button.height
  90. //Left side icon. Only displayed if !isIconOnRightSide.
  91. UM.RecolorImage
  92. {
  93. id: buttonIconLeft
  94. source: ""
  95. height: visible ? button.iconSize : 0
  96. width: visible ? height : 0
  97. sourceSize.width: width
  98. sourceSize.height: height
  99. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor) : button.textDisabledColor
  100. visible: source != "" && !button.isIconOnRightSide
  101. anchors.verticalCenter: parent.verticalCenter
  102. }
  103. TextMetrics
  104. {
  105. id: buttonTextMetrics
  106. text: buttonText.text
  107. font: buttonText.font
  108. elide: buttonText.elide
  109. elideWidth: buttonText.width
  110. }
  111. UM.Label
  112. {
  113. id: buttonText
  114. text: button.text
  115. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor): button.textDisabledColor
  116. font: UM.Theme.getFont("medium")
  117. visible: text != ""
  118. height: parent.height
  119. anchors.verticalCenter: parent.verticalCenter
  120. horizontalAlignment: Text.AlignHCenter
  121. elide: Text.ElideRight
  122. Binding
  123. {
  124. // When setting width directly, an unjust binding loop warning would be triggered,
  125. // because button.width is part of this expression.
  126. // Using parent.width is fine in fixedWidthMode.
  127. target: buttonText
  128. property: "width"
  129. value: button.fixedWidthMode ? (button.width - button.leftPadding - button.rightPadding)
  130. : ((button.maximumWidth != 0 && button.implicitContentWidth > button.maximumWidth) ? (button.maximumWidth - (button.width - button.implicitContentWidth) * 2) : undefined)
  131. }
  132. }
  133. //Right side icon. Only displayed if isIconOnRightSide.
  134. UM.RecolorImage
  135. {
  136. id: buttonIconRight
  137. source: buttonIconLeft.source
  138. height: visible ? button.iconSize : 0
  139. width: visible ? height : 0
  140. sourceSize.width: width
  141. sourceSize.height: height
  142. color: buttonIconLeft.color
  143. visible: source != "" && button.isIconOnRightSide
  144. anchors.verticalCenter: buttonIconLeft.verticalCenter
  145. }
  146. }
  147. background: Cura.RoundedRectangle
  148. {
  149. id: backgroundRect
  150. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  151. border.width: UM.Theme.getSize("default_lining").width
  152. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  153. // Disable the rounded-ness of this rectangle. We can't use a normal Rectangle here yet, as the API/SDK has only just been deprecated.
  154. radius: 0
  155. cornerSide: Cura.RoundedRectangle.Direction.None
  156. }
  157. UM.ToolTip
  158. {
  159. id: tooltip
  160. visible:
  161. {
  162. if (!button.hovered)
  163. {
  164. return false;
  165. }
  166. if (tooltipText == button.text)
  167. {
  168. return buttonTextMetrics.elidedText != buttonText.text;
  169. }
  170. return true;
  171. }
  172. targetPoint: Qt.point(parent.x, Math.round(parent.y + parent.height / 2))
  173. }
  174. BusyIndicator
  175. {
  176. id: busyIndicator
  177. anchors.centerIn: parent
  178. width: height
  179. height: parent.height
  180. visible: false
  181. RotationAnimator
  182. {
  183. target: busyIndicator.contentItem
  184. running: busyIndicator.visible && busyIndicator.running
  185. from: 0
  186. to: 360
  187. loops: Animation.Infinite
  188. duration: 2500
  189. }
  190. }
  191. }