MenuButton.qml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2019 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.10
  4. import QtQuick.Controls 2.3
  5. import UM 1.5 as UM
  6. import Cura 1.6 as Cura
  7. Button
  8. {
  9. // This is a work around for a qml issue. Since the default button uses a private implementation for contentItem
  10. // (the so called IconText), which handles the mnemonic conversion (aka; ensuring that &Button) text property
  11. // is rendered with the B underlined. Since we're also forced to mix controls 1.0 and 2.0 actions together,
  12. // we need a special property for the text of the label if we do want it to be rendered correctly, but don't want
  13. // another shortcut to be added (which will cause for "QQuickAction::event: Ambiguous shortcut overload: " to
  14. // happen.
  15. property string labelText: ""
  16. id: button
  17. hoverEnabled: true
  18. leftPadding: UM.Theme.getSize("default_margin").width
  19. implicitWidth: UM.Theme.getSize("menu").width
  20. implicitHeight: UM.Theme.getSize("menu").height + UM.Theme.getSize("narrow_margin").height
  21. background: Rectangle
  22. {
  23. height: button.height
  24. width: button.width
  25. color: button.hovered ? UM.Theme.getColor("background_2") : UM.Theme.getColor("background_1")
  26. }
  27. // Workaround to ensure that the mnemonic highlighting happens correctly
  28. function replaceText(txt)
  29. {
  30. var index = txt.indexOf("&")
  31. if(index >= 0)
  32. {
  33. txt = txt.replace(txt.substr(index, 2), ("<u>" + txt.substr(index + 1, 1) + "</u>"))
  34. }
  35. return txt
  36. }
  37. contentItem: Item
  38. {
  39. height: button.height
  40. width: button.width
  41. UM.RecolorImage
  42. {
  43. id: check
  44. height: UM.Theme.getSize("default_arrow").height
  45. width: height
  46. source: UM.Theme.getIcon("Check", "low")
  47. color: UM.Theme.getColor("setting_control_text")
  48. anchors.verticalCenter: parent.verticalCenter
  49. visible: button.checked
  50. }
  51. UM.Label
  52. {
  53. id: textLabel
  54. text: button.text != "" ? replaceText(button.text) : replaceText(button.labelText)
  55. height: contentHeight
  56. color: button.enabled ? UM.Theme.getColor("text") :UM.Theme.getColor("text_inactive")
  57. anchors.left: check.right
  58. anchors.leftMargin: UM.Theme.getSize("narrow_margin").width
  59. anchors.verticalCenter: parent.verticalCenter
  60. }
  61. }
  62. }