ActionButton.qml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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. UM.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. height: parent.height
  118. anchors.verticalCenter: parent.verticalCenter
  119. horizontalAlignment: Text.AlignHCenter
  120. elide: Text.ElideRight
  121. Binding
  122. {
  123. // When setting width directly, an unjust binding loop warning would be triggered,
  124. // because button.width is part of this expression.
  125. // Using parent.width is fine in fixedWidthMode.
  126. target: buttonText
  127. property: "width"
  128. value: button.fixedWidthMode ? button.width - button.leftPadding - button.rightPadding
  129. : ((maximumWidth != 0 && button.contentWidth > maximumWidth) ? maximumWidth : undefined)
  130. }
  131. }
  132. //Right side icon. Only displayed if isIconOnRightSide.
  133. UM.RecolorImage
  134. {
  135. id: buttonIconRight
  136. source: buttonIconLeft.source
  137. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  138. width: visible ? height : 0
  139. sourceSize.width: width
  140. sourceSize.height: height
  141. color: buttonIconLeft.color
  142. visible: source != "" && button.isIconOnRightSide
  143. anchors.verticalCenter: buttonIconLeft.verticalCenter
  144. }
  145. }
  146. background: Cura.RoundedRectangle
  147. {
  148. id: backgroundRect
  149. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  150. border.width: UM.Theme.getSize("default_lining").width
  151. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  152. // 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.
  153. radius: 0
  154. cornerSide: Cura.RoundedRectangle.Direction.None
  155. }
  156. Cura.ToolTip
  157. {
  158. id: tooltip
  159. visible:
  160. {
  161. if (!button.hovered)
  162. {
  163. return false;
  164. }
  165. if (tooltipText == button.text)
  166. {
  167. return buttonTextMetrics.elidedText != buttonText.text;
  168. }
  169. return true;
  170. }
  171. targetPoint: Qt.point(parent.x, Math.round(parent.y + parent.height / 2))
  172. }
  173. BusyIndicator
  174. {
  175. id: busyIndicator
  176. anchors.centerIn: parent
  177. width: height
  178. height: parent.height
  179. visible: false
  180. RotationAnimator
  181. {
  182. target: busyIndicator.contentItem
  183. running: busyIndicator.visible && busyIndicator.running
  184. from: 0
  185. to: 360
  186. loops: Animation.Infinite
  187. duration: 2500
  188. }
  189. }
  190. }