ActionButton.qml 5.7 KB

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