ActionButton.qml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 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. leftPadding: UM.Theme.getSize("default_margin").width
  38. rightPadding: UM.Theme.getSize("default_margin").width
  39. height: UM.Theme.getSize("action_button").height
  40. hoverEnabled: true
  41. contentItem: Row
  42. {
  43. spacing: UM.Theme.getSize("narrow_margin").width
  44. height: button.height
  45. //Left side icon. Only displayed if !isIconOnRightSide.
  46. UM.RecolorImage
  47. {
  48. id: buttonIconLeft
  49. source: ""
  50. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  51. width: visible ? height : 0
  52. sourceSize.width: width
  53. sourceSize.height: height
  54. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor) : button.textDisabledColor
  55. visible: source != "" && !button.isIconOnRightSide
  56. anchors.verticalCenter: parent.verticalCenter
  57. }
  58. TextMetrics
  59. {
  60. id: buttonTextMetrics
  61. text: buttonText.text
  62. font: buttonText.font
  63. elide: buttonText.elide
  64. elideWidth: buttonText.width
  65. }
  66. Label
  67. {
  68. id: buttonText
  69. text: button.text
  70. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor): button.textDisabledColor
  71. font: UM.Theme.getFont("medium")
  72. visible: text != ""
  73. renderType: Text.NativeRendering
  74. height: parent.height
  75. anchors.verticalCenter: parent.verticalCenter
  76. horizontalAlignment: Text.AlignHCenter
  77. verticalAlignment: Text.AlignVCenter
  78. elide: Text.ElideRight
  79. Binding
  80. {
  81. // When settting width directly, an unjust binding loop warning would be triggered,
  82. // because button.width is part of this expression.
  83. // Using parent.width is fine in fixedWidthMode.
  84. target: buttonText
  85. property: "width"
  86. value: button.fixedWidthMode ? button.width - button.leftPadding - button.rightPadding
  87. : ((maximumWidth != 0 && button.contentWidth > maximumWidth) ? maximumWidth : undefined)
  88. }
  89. }
  90. //Right side icon. Only displayed if isIconOnRightSide.
  91. UM.RecolorImage
  92. {
  93. id: buttonIconRight
  94. source: buttonIconLeft.source
  95. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  96. width: visible ? height : 0
  97. sourceSize.width: width
  98. sourceSize.height: height
  99. color: buttonIconLeft.color
  100. visible: source != "" && button.isIconOnRightSide
  101. anchors.verticalCenter: buttonIconLeft.verticalCenter
  102. }
  103. }
  104. background: Cura.RoundedRectangle
  105. {
  106. id: backgroundRect
  107. cornerSide: Cura.RoundedRectangle.Direction.All
  108. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  109. radius: UM.Theme.getSize("action_button_radius").width
  110. border.width: UM.Theme.getSize("default_lining").width
  111. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  112. }
  113. DropShadow
  114. {
  115. id: shadow
  116. // Don't blur the shadow
  117. radius: 0
  118. anchors.fill: backgroundRect
  119. source: backgroundRect
  120. verticalOffset: 2
  121. visible: false
  122. // Should always be drawn behind the background.
  123. z: backgroundRect.z - 1
  124. }
  125. Cura.ToolTip
  126. {
  127. id: tooltip
  128. visible:
  129. {
  130. if (!button.hovered)
  131. {
  132. return false;
  133. }
  134. if (tooltipText == button.text)
  135. {
  136. return buttonTextMetrics.elidedText != buttonText.text;
  137. }
  138. return true;
  139. }
  140. targetPoint: Qt.point(parent.x, Math.round(parent.y + parent.height / 2))
  141. }
  142. BusyIndicator
  143. {
  144. id: busyIndicator
  145. anchors.centerIn: parent
  146. width: height
  147. height: parent.height
  148. visible: false
  149. RotationAnimator
  150. {
  151. target: busyIndicator.contentItem
  152. running: busyIndicator.visible && busyIndicator.running
  153. from: 0
  154. to: 360
  155. loops: Animation.Infinite
  156. duration: 2500
  157. }
  158. }
  159. }