ActionButton.qml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 QtQuick.Layouts 1.3
  6. import QtGraphicalEffects 1.0 // For the dropshadow
  7. import UM 1.1 as UM
  8. Button
  9. {
  10. id: button
  11. property alias cursorShape: mouseArea.cursorShape
  12. property alias iconSource: buttonIcon.source
  13. property alias textFont: buttonText.font
  14. property alias cornerRadius: backgroundRect.radius
  15. property alias tooltip: tooltip.text
  16. property color color: UM.Theme.getColor("primary")
  17. property color hoverColor: UM.Theme.getColor("primary_hover")
  18. property color disabledColor: color
  19. property color textColor: UM.Theme.getColor("button_text")
  20. property color textHoverColor: UM.Theme.getColor("button_text_hover")
  21. property color textDisabledColor: textColor
  22. property color outlineColor: color
  23. property color outlineHoverColor: hoverColor
  24. property color outlineDisabledColor: outlineColor
  25. property alias shadowColor: shadow.color
  26. property alias shadowEnabled: shadow.visible
  27. // This property is used to indicate whether the button has a fixed width or the width would depend on the contents
  28. // Be careful when using fixedWidthMode, the translated texts can be too long that they won't fit. In any case,
  29. // we elide the text to the right so the text will be cut off with the three dots at the end.
  30. property var fixedWidthMode: false
  31. contentItem: Row
  32. {
  33. UM.RecolorImage
  34. {
  35. id: buttonIcon
  36. source: ""
  37. height: Math.round(0.6 * parent.height)
  38. width: height
  39. sourceSize.width: width
  40. sourceSize.height: height
  41. color: button.hovered ? button.textHoverColor : button.textColor
  42. visible: source != ""
  43. anchors.verticalCenter: parent.verticalCenter
  44. }
  45. Label
  46. {
  47. id: buttonText
  48. text: button.text
  49. color: button.enabled ? (button.hovered ? button.textHoverColor : button.textColor): button.textDisabledColor
  50. font: UM.Theme.getFont("action_button")
  51. visible: text != ""
  52. renderType: Text.NativeRendering
  53. anchors.verticalCenter: parent.verticalCenter
  54. width: fixedWidthMode ? button.width - button.leftPadding - button.rightPadding : undefined
  55. horizontalAlignment: Text.AlignHCenter
  56. elide: Text.ElideRight
  57. }
  58. }
  59. background: Rectangle
  60. {
  61. id: backgroundRect
  62. color: button.enabled ? (button.hovered ? button.hoverColor : button.color) : button.disabledColor
  63. radius: UM.Theme.getSize("action_button_radius").width
  64. border.width: UM.Theme.getSize("default_lining").width
  65. border.color: button.enabled ? (button.hovered ? button.outlineHoverColor : button.outlineColor) : button.outlineDisabledColor
  66. }
  67. DropShadow
  68. {
  69. id: shadow
  70. // Don't blur the shadow
  71. radius: 0
  72. anchors.fill: backgroundRect
  73. source: backgroundRect
  74. verticalOffset: 2
  75. visible: false
  76. // Should always be drawn behind the background.
  77. z: backgroundRect.z - 1
  78. }
  79. ToolTip
  80. {
  81. id: tooltip
  82. text: ""
  83. delay: 500
  84. visible: text != "" && button.hovered
  85. }
  86. MouseArea
  87. {
  88. id: mouseArea
  89. anchors.fill: parent
  90. // Ensure that the button will still accept the clicks on it's own.
  91. onPressed: mouse.accepted = false
  92. hoverEnabled: true
  93. }
  94. }