GraphCanvasLabel.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Component/EntityId.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzCore/std/string/string.h>
  12. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option")
  13. #include <QGraphicsWidget>
  14. AZ_POP_DISABLE_WARNING
  15. #include <GraphCanvas/Styling/StyleHelper.h>
  16. namespace GraphCanvas
  17. {
  18. //! The GraphCanvasLabel gives a QGraphicsWidget that is able to display text, and be placed into a layout.
  19. class GraphCanvasLabel
  20. : public QGraphicsWidget
  21. {
  22. public:
  23. AZ_CLASS_ALLOCATOR(GraphCanvasLabel, AZ::SystemAllocator);
  24. enum class WrapMode
  25. {
  26. MaximumWidth,
  27. BoundingWidth,
  28. ResizeToContent
  29. };
  30. enum class RoundedCornersMode
  31. {
  32. AllCorners,
  33. LeftCorners,
  34. RightCorners
  35. };
  36. GraphCanvasLabel(QGraphicsItem* parent = nullptr);
  37. ~GraphCanvasLabel() = default;
  38. void SetFontColor(const QColor& color);
  39. void ClearFontColor();
  40. void SetBorderColorOverride(const QBrush& borderOverride);
  41. const QBrush& GetBorderColorOverride() const;
  42. void ClearBorderColorOverride();
  43. void SetLabel(const AZStd::string& value);
  44. AZStd::string GetLabel() const { return AZStd::string(m_labelText.toUtf8().data()); }
  45. void SetSceneStyle(const AZ::EntityId& sceneId, const char* style);
  46. void SetStyle(const AZ::EntityId& entityId, const char* styleElement);
  47. void RefreshDisplay();
  48. void SetWrapMode(WrapMode wrapMode);
  49. //! Sets which corners to apply the radius to
  50. void SetRoundedCornersMode(RoundedCornersMode roundedCornerMode);
  51. QRectF GetDisplayedSize() const;
  52. //! Sets whether the text should elide if it grows beyond max-width
  53. //! (Note: currently incompatible with word wrap)
  54. void SetElide(bool elide);
  55. //! Sets whether the next should wrap if it grows beyond max-width
  56. //! (Note: currently incompatible with text elide)
  57. void SetWrap(bool wrap);
  58. //! Sets whether or not the text label will allow newlines in the text
  59. void SetAllowNewlines(bool allow);
  60. void SetDefaultAlignment(Qt::Alignment defaultAlignment);
  61. Styling::StyleHelper& GetStyleHelper();
  62. const Styling::StyleHelper& GetStyleHelper() const;
  63. void UpdateDisplayText();
  64. protected:
  65. void UpdateDesiredBounds();
  66. // QGraphicsItem
  67. bool event(QEvent* qEvent) override;
  68. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
  69. ////
  70. // QGraphicsLayoutItem
  71. QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const override;
  72. ////
  73. private:
  74. Qt::Alignment m_defaultAlignment;
  75. bool m_elide;
  76. bool m_wrap;
  77. bool m_allowNewlines;
  78. QString m_labelText;
  79. QString m_displayText;
  80. QSizeF m_maximumSize;
  81. QSizeF m_minimumSize;
  82. //! So, this is going to return the actual value of boundingRect(as seen when we actually render).
  83. //! For whatever reason, preferredSize, boundingRect, size, rect, and even calling boundingRect internally and
  84. //! passing that value back does not give me the modified visual size, but instead the static preferred size.
  85. //! This means when we put this into something that scales it up(due to an expanding size policy)
  86. //! We get back improper values. So to combat this, we'll store the value when we render, and hope for the best.
  87. QRectF m_displayedSize;
  88. //! This is influenced by the reflected value m_title.
  89. //! That's why we must update this value when m_title changes.
  90. QRectF m_desiredBounds;
  91. WrapMode m_wrapMode;
  92. RoundedCornersMode m_roundedCornersMode;
  93. bool m_hasBorderOverride;
  94. QBrush m_borderColorOverride;
  95. Styling::StyleHelper m_styleHelper;
  96. };
  97. }