ActionButton.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. leftPadding: UM.Theme.getSize("default_margin").width
  35. rightPadding: UM.Theme.getSize("default_margin").width
  36. height: UM.Theme.getSize("action_button").height
  37. hoverEnabled: true
  38. contentItem: Row
  39. {
  40. spacing: UM.Theme.getSize("narrow_margin").width
  41. height: button.height
  42. //Left side icon. Only displayed if !isIconOnRightSide.
  43. UM.RecolorImage
  44. {
  45. id: buttonIconLeft
  46. source: ""
  47. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  48. width: visible ? height : 0
  49. sourceSize.width: width
  50. sourceSize.height: height
  51. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor) : button.textDisabledColor
  52. visible: source != "" && !button.isIconOnRightSide
  53. anchors.verticalCenter: parent.verticalCenter
  54. }
  55. Label
  56. {
  57. id: buttonText
  58. text: button.text
  59. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor): button.textDisabledColor
  60. font: UM.Theme.getFont("medium")
  61. visible: text != ""
  62. renderType: Text.NativeRendering
  63. height: parent.height
  64. anchors.verticalCenter: parent.verticalCenter
  65. width: fixedWidthMode ? button.width - button.leftPadding - button.rightPadding : undefined
  66. horizontalAlignment: Text.AlignHCenter
  67. verticalAlignment: Text.AlignVCenter
  68. elide: Text.ElideRight
  69. }
  70. //Right side icon. Only displayed if isIconOnRightSide.
  71. UM.RecolorImage
  72. {
  73. id: buttonIconRight
  74. source: buttonIconLeft.source
  75. height: visible ? UM.Theme.getSize("action_button_icon").height : 0
  76. width: visible ? height : 0
  77. sourceSize.width: width
  78. sourceSize.height: height
  79. color: buttonIconLeft.color
  80. visible: source != "" && button.isIconOnRightSide
  81. anchors.verticalCenter: buttonIconLeft.verticalCenter
  82. }
  83. }
  84. background: Cura.RoundedRectangle
  85. {
  86. id: backgroundRect
  87. cornerSide: Cura.RoundedRectangle.Direction.All
  88. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  89. radius: UM.Theme.getSize("action_button_radius").width
  90. border.width: UM.Theme.getSize("default_lining").width
  91. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  92. }
  93. DropShadow
  94. {
  95. id: shadow
  96. // Don't blur the shadow
  97. radius: 0
  98. anchors.fill: backgroundRect
  99. source: backgroundRect
  100. verticalOffset: 2
  101. visible: false
  102. // Should always be drawn behind the background.
  103. z: backgroundRect.z - 1
  104. }
  105. Cura.ToolTip
  106. {
  107. id: tooltip
  108. visible: button.hovered
  109. }
  110. BusyIndicator
  111. {
  112. id: busyIndicator
  113. anchors.centerIn: parent
  114. width: height
  115. height: parent.height
  116. visible: false
  117. RotationAnimator
  118. {
  119. target: busyIndicator.contentItem
  120. running: busyIndicator.visible && busyIndicator.running
  121. from: 0
  122. to: 360
  123. loops: Animation.Infinite
  124. duration: 2500
  125. }
  126. }
  127. }