ActionButton.qml 5.8 KB

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