ActionButton.qml 5.9 KB

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