ToolTip.qml 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  50. contentItem: Label
  51. {
  52. id: label
  53. text: tooltip.text
  54. font: tooltip.font
  55. wrapMode: Text.Wrap
  56. textFormat: Text.RichText
  57. color: UM.Theme.getColor("tooltip_text")
  58. renderType: Text.NativeRendering
  59. }
  60. function show() {
  61. opacity = 1
  62. }
  63. function hide() {
  64. opacity = 0
  65. }
  66. }