ActionButton.qml 4.8 KB

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