ActionButton.qml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) 2020 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 cornerSide: backgroundRect.cornerSide
  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: textColor
  23. property color outlineColor: color
  24. property color outlineHoverColor: hoverColor
  25. property color outlineDisabledColor: outlineColor
  26. property alias shadowColor: shadow.color
  27. property alias shadowEnabled: shadow.visible
  28. property alias busy: busyIndicator.visible
  29. property bool underlineTextOnHover: false
  30. property alias toolTipContentAlignment: tooltip.contentAlignment
  31. // This property is used to indicate whether the button has a fixed width or the width would depend on the contents
  32. // Be careful when using fixedWidthMode, the translated texts can be too long that they won't fit. In any case,
  33. // we elide the text to the right so the text will be cut off with the three dots at the end.
  34. property var fixedWidthMode: false
  35. // This property is used when the space for the button is limited. In case the button needs to grow with the text,
  36. // but it can exceed a maximum, then this value have to be set.
  37. property int maximumWidth: 0
  38. leftPadding: UM.Theme.getSize("default_margin").width
  39. rightPadding: UM.Theme.getSize("default_margin").width
  40. height: UM.Theme.getSize("action_button").height
  41. hoverEnabled: true
  42. onHoveredChanged:
  43. {
  44. if(underlineTextOnHover)
  45. {
  46. buttonText.font.underline = hovered
  47. }
  48. }
  49. contentItem: Row
  50. {
  51. spacing: UM.Theme.getSize("narrow_margin").width
  52. height: button.height
  53. //Left side icon. Only displayed if !isIconOnRightSide.
  54. UM.RecolorImage
  55. {
  56. id: buttonIconLeft
  57. source: ""
  58. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  59. width: visible ? height : 0
  60. sourceSize.width: width
  61. sourceSize.height: height
  62. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor) : button.textDisabledColor
  63. visible: source != "" && !button.isIconOnRightSide
  64. anchors.verticalCenter: parent.verticalCenter
  65. }
  66. TextMetrics
  67. {
  68. id: buttonTextMetrics
  69. text: buttonText.text
  70. font: buttonText.font
  71. elide: buttonText.elide
  72. elideWidth: buttonText.width
  73. }
  74. Label
  75. {
  76. id: buttonText
  77. text: button.text
  78. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor): button.textDisabledColor
  79. font: UM.Theme.getFont("medium")
  80. visible: text != ""
  81. renderType: Text.NativeRendering
  82. height: parent.height
  83. anchors.verticalCenter: parent.verticalCenter
  84. horizontalAlignment: Text.AlignHCenter
  85. verticalAlignment: Text.AlignVCenter
  86. elide: Text.ElideRight
  87. Binding
  88. {
  89. // When settting width directly, an unjust binding loop warning would be triggered,
  90. // because button.width is part of this expression.
  91. // Using parent.width is fine in fixedWidthMode.
  92. target: buttonText
  93. property: "width"
  94. value: button.fixedWidthMode ? button.width - button.leftPadding - button.rightPadding
  95. : ((maximumWidth != 0 && button.contentWidth > maximumWidth) ? maximumWidth : undefined)
  96. }
  97. }
  98. //Right side icon. Only displayed if isIconOnRightSide.
  99. UM.RecolorImage
  100. {
  101. id: buttonIconRight
  102. source: buttonIconLeft.source
  103. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  104. width: visible ? height : 0
  105. sourceSize.width: width
  106. sourceSize.height: height
  107. color: buttonIconLeft.color
  108. visible: source != "" && button.isIconOnRightSide
  109. anchors.verticalCenter: buttonIconLeft.verticalCenter
  110. }
  111. }
  112. background: Cura.RoundedRectangle
  113. {
  114. id: backgroundRect
  115. cornerSide: Cura.RoundedRectangle.Direction.All
  116. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  117. radius: UM.Theme.getSize("action_button_radius").width
  118. border.width: UM.Theme.getSize("default_lining").width
  119. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  120. }
  121. DropShadow
  122. {
  123. id: shadow
  124. // Don't blur the shadow
  125. radius: 0
  126. anchors.fill: backgroundRect
  127. source: backgroundRect
  128. verticalOffset: 2
  129. visible: false
  130. // Should always be drawn behind the background.
  131. z: backgroundRect.z - 1
  132. }
  133. Cura.ToolTip
  134. {
  135. id: tooltip
  136. visible:
  137. {
  138. if (!button.hovered)
  139. {
  140. return false;
  141. }
  142. if (tooltipText == button.text)
  143. {
  144. return buttonTextMetrics.elidedText != buttonText.text;
  145. }
  146. return true;
  147. }
  148. targetPoint: Qt.point(parent.x, Math.round(parent.y + parent.height / 2))
  149. }
  150. BusyIndicator
  151. {
  152. id: busyIndicator
  153. anchors.centerIn: parent
  154. width: height
  155. height: parent.height
  156. visible: false
  157. RotationAnimator
  158. {
  159. target: busyIndicator.contentItem
  160. running: busyIndicator.visible && busyIndicator.running
  161. from: 0
  162. to: 360
  163. loops: Animation.Infinite
  164. duration: 2500
  165. }
  166. }
  167. }