ActionButton.qml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.text
  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. // This property is used to indicate whether the button has a fixed width or the width would depend on the contents
  29. // Be careful when using fixedWidthMode, the translated texts can be too long that they won't fit. In any case,
  30. // we elide the text to the right so the text will be cut off with the three dots at the end.
  31. property var fixedWidthMode: false
  32. leftPadding: UM.Theme.getSize("default_margin").width
  33. rightPadding: UM.Theme.getSize("default_margin").width
  34. height: UM.Theme.getSize("action_button").height
  35. hoverEnabled: true
  36. contentItem: Row
  37. {
  38. //Left side icon. Only displayed if !isIconOnRightSide.
  39. UM.RecolorImage
  40. {
  41. id: buttonIconLeft
  42. source: ""
  43. height: buttonText.height
  44. width: visible ? height : 0
  45. sourceSize.width: width
  46. sourceSize.height: height
  47. color: button.hovered ? button.textHoverColor : button.textColor
  48. visible: source != "" && !button.isIconOnRightSide
  49. anchors.verticalCenter: parent.verticalCenter
  50. }
  51. Label
  52. {
  53. id: buttonText
  54. text: button.text
  55. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor): button.textDisabledColor
  56. font: UM.Theme.getFont("medium")
  57. visible: text != ""
  58. renderType: Text.NativeRendering
  59. anchors.verticalCenter: parent.verticalCenter
  60. width: fixedWidthMode ? button.width - button.leftPadding - button.rightPadding : undefined
  61. horizontalAlignment: Text.AlignHCenter
  62. elide: Text.ElideRight
  63. }
  64. //Right side icon. Only displayed if isIconOnRightSide.
  65. UM.RecolorImage
  66. {
  67. id: buttonIconRight
  68. source: buttonIconLeft.source
  69. height: buttonText.height
  70. width: visible ? height : 0
  71. sourceSize.width: width
  72. sourceSize.height: height
  73. color: buttonIconLeft.color
  74. visible: source != "" && button.isIconOnRightSide
  75. anchors.verticalCenter: buttonIconLeft.verticalCenter
  76. }
  77. }
  78. background: Cura.RoundedRectangle
  79. {
  80. id: backgroundRect
  81. cornerSide: Cura.RoundedRectangle.Direction.All
  82. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  83. radius: UM.Theme.getSize("action_button_radius").width
  84. border.width: UM.Theme.getSize("default_lining").width
  85. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  86. }
  87. DropShadow
  88. {
  89. id: shadow
  90. // Don't blur the shadow
  91. radius: 0
  92. anchors.fill: backgroundRect
  93. source: backgroundRect
  94. verticalOffset: 2
  95. visible: false
  96. // Should always be drawn behind the background.
  97. z: backgroundRect.z - 1
  98. }
  99. ToolTip
  100. {
  101. id: tooltip
  102. text: ""
  103. delay: 500
  104. visible: text != "" && button.hovered
  105. }
  106. }