ToolTip.qml 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.3
  5. import UM 1.0 as UM
  6. import Cura 1.0 as Cura
  7. ToolTip
  8. {
  9. enum ContentAlignment
  10. {
  11. AlignLeft,
  12. AlignRight
  13. }
  14. // Defines the alignment of the content, by default to the left
  15. property int contentAlignment: Cura.ToolTip.ContentAlignment.AlignRight
  16. property alias tooltipText: tooltip.text
  17. property alias arrowSize: backgroundRect.arrowSize
  18. property var targetPoint: Qt.point(parent.x, y + Math.round(height/2))
  19. id: tooltip
  20. text: ""
  21. delay: 500
  22. font: UM.Theme.getFont("default")
  23. visible: opacity != 0.0
  24. opacity: 0.0 // initially hidden
  25. Behavior on opacity
  26. {
  27. NumberAnimation { duration: 100; }
  28. }
  29. onAboutToShow: show()
  30. onAboutToHide: hide()
  31. // If the text is not set, just set the height to 0 to prevent it from showing
  32. height: text != "" ? label.contentHeight + 2 * UM.Theme.getSize("thin_margin").width: 0
  33. x:
  34. {
  35. if (contentAlignment == Cura.ToolTip.ContentAlignment.AlignLeft)
  36. {
  37. return (label.width + Math.round(UM.Theme.getSize("default_arrow").width * 1.2) + padding * 2) * -1
  38. }
  39. return parent.width + Math.round(UM.Theme.getSize("default_arrow").width * 1.2 + padding)
  40. }
  41. y: Math.round(parent.height / 2 - label.height / 2 ) - padding
  42. padding: UM.Theme.getSize("thin_margin").width
  43. background: UM.PointingRectangle
  44. {
  45. id: backgroundRect
  46. color: UM.Theme.getColor("tooltip")
  47. target: Qt.point(targetPoint.x - tooltip.x, targetPoint.y - tooltip.y)
  48. arrowSize: UM.Theme.getSize("default_arrow").width
  49. visible: tooltip.height != 0
  50. }
  51. contentItem: Label
  52. {
  53. id: label
  54. text: tooltip.text
  55. font: tooltip.font
  56. wrapMode: Text.Wrap
  57. textFormat: Text.RichText
  58. color: UM.Theme.getColor("tooltip_text")
  59. renderType: Text.NativeRendering
  60. }
  61. function show() {
  62. opacity = 1
  63. }
  64. function hide() {
  65. opacity = 0
  66. }
  67. }