ActionButton.qml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2018 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. width: fixedWidthMode ? button.width - button.leftPadding - button.rightPadding : ((maximumWidth != 0 && contentWidth > maximumWidth) ? maximumWidth : undefined)
  77. horizontalAlignment: Text.AlignHCenter
  78. verticalAlignment: Text.AlignVCenter
  79. elide: Text.ElideRight
  80. }
  81. //Right side icon. Only displayed if isIconOnRightSide.
  82. UM.RecolorImage
  83. {
  84. id: buttonIconRight
  85. source: buttonIconLeft.source
  86. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  87. width: visible ? height : 0
  88. sourceSize.width: width
  89. sourceSize.height: height
  90. color: buttonIconLeft.color
  91. visible: source != "" && button.isIconOnRightSide
  92. anchors.verticalCenter: buttonIconLeft.verticalCenter
  93. }
  94. }
  95. background: Cura.RoundedRectangle
  96. {
  97. id: backgroundRect
  98. cornerSide: Cura.RoundedRectangle.Direction.All
  99. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  100. radius: UM.Theme.getSize("action_button_radius").width
  101. border.width: UM.Theme.getSize("default_lining").width
  102. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  103. }
  104. DropShadow
  105. {
  106. id: shadow
  107. // Don't blur the shadow
  108. radius: 0
  109. anchors.fill: backgroundRect
  110. source: backgroundRect
  111. verticalOffset: 2
  112. visible: false
  113. // Should always be drawn behind the background.
  114. z: backgroundRect.z - 1
  115. }
  116. Cura.ToolTip
  117. {
  118. id: tooltip
  119. visible: button.hovered && buttonTextMetrics.elidedText != buttonText.text
  120. }
  121. BusyIndicator
  122. {
  123. id: busyIndicator
  124. anchors.centerIn: parent
  125. width: height
  126. height: parent.height
  127. visible: false
  128. RotationAnimator
  129. {
  130. target: busyIndicator.contentItem
  131. running: busyIndicator.visible && busyIndicator.running
  132. from: 0
  133. to: 360
  134. loops: Animation.Infinite
  135. duration: 2500
  136. }
  137. }
  138. }