ActionButton.qml 7.5 KB

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