ActionButton.qml 4.3 KB

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