ActionButton.qml 5.2 KB

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