CommentTextGraphicsWidget.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/PlatformDef.h>
  10. AZ_PUSH_DISABLE_WARNING(4251 4800 4244, "-Wunknown-warning-option")
  11. #if !defined(Q_MOC_RUN)
  12. #include <QGraphicsGridLayout>
  13. #include <QGraphicsLinearLayout>
  14. #include <QGraphicsProxyWidget>
  15. #include <QGraphicsWidget>
  16. #include <QPlainTextEdit>
  17. #include <QTimer>
  18. AZ_POP_DISABLE_WARNING
  19. #include <AzCore/Component/EntityId.h>
  20. #include <GraphCanvas/Components/Nodes/Comment/CommentBus.h>
  21. #include <GraphCanvas/Components/SceneBus.h>
  22. #include <GraphCanvas/Components/StyleBus.h>
  23. #include <GraphCanvas/Styling/StyleHelper.h>
  24. #include <Widgets/GraphCanvasLabel.h>
  25. #endif
  26. namespace GraphCanvas
  27. {
  28. namespace Internal
  29. {
  30. // Need to know when the text edit gets focus in order to
  31. // manage the layout display when the mouse hovers off, but the
  32. // widget still has focus. Qt does not expose focus events in any
  33. // signal way, so this exposes that functionality for me.
  34. class FocusableTextEdit
  35. : public QTextEdit
  36. {
  37. Q_OBJECT
  38. public:
  39. AZ_CLASS_ALLOCATOR(FocusableTextEdit, AZ::SystemAllocator, 0);
  40. FocusableTextEdit()
  41. : m_eatEnterKey(false)
  42. {
  43. setContextMenuPolicy(Qt::ContextMenuPolicy::PreventContextMenu);
  44. }
  45. ~FocusableTextEdit() = default;
  46. signals:
  47. void OnFocusIn();
  48. void OnFocusOut();
  49. void EnterPressed();
  50. private:
  51. void focusInEvent(QFocusEvent* focusEvent) override
  52. {
  53. QTextEdit::focusInEvent(focusEvent);
  54. emit OnFocusIn();
  55. }
  56. void focusOutEvent(QFocusEvent* focusEvent) override
  57. {
  58. QTextEdit::focusOutEvent(focusEvent);
  59. emit OnFocusOut();
  60. }
  61. void keyPressEvent(QKeyEvent* keyEvent) override
  62. {
  63. if (keyEvent->key() == Qt::Key_Enter
  64. || keyEvent->key() == Qt::Key_Return)
  65. {
  66. if (keyEvent->modifiers() == Qt::KeyboardModifier::NoModifier)
  67. {
  68. m_eatEnterKey = true;
  69. return;
  70. }
  71. }
  72. QTextEdit::keyPressEvent(keyEvent);
  73. }
  74. void keyReleaseEvent(QKeyEvent* keyEvent) override
  75. {
  76. if (keyEvent->key() == Qt::Key_Enter
  77. || keyEvent->key() == Qt::Key_Return)
  78. {
  79. if (m_eatEnterKey)
  80. {
  81. emit EnterPressed();
  82. m_eatEnterKey = false;
  83. }
  84. }
  85. QTextEdit::keyReleaseEvent(keyEvent);
  86. }
  87. bool m_eatEnterKey;
  88. };
  89. }
  90. //! The QGraphicsWidget for displaying the comment text
  91. // This class is not meant to be serializable
  92. class CommentTextGraphicsWidget
  93. : public QGraphicsWidget
  94. , public CommentLayoutRequestBus::Handler
  95. , public CommentUIRequestBus::Handler
  96. , public StyleNotificationBus::Handler
  97. {
  98. public:
  99. AZ_TYPE_INFO(CommentTextGraphicsWidget, "{1779F401-6A9F-42A8-B4B7-F7732DBEC462}");
  100. AZ_CLASS_ALLOCATOR(CommentTextGraphicsWidget, AZ::SystemAllocator, 0);
  101. static void Reflect(AZ::ReflectContext* context) = delete;
  102. CommentTextGraphicsWidget(const AZ::EntityId& targetId);
  103. ~CommentTextGraphicsWidget() override = default;
  104. void Activate();
  105. void Deactivate();
  106. void OnAddedToScene();
  107. void SetStyle(const AZStd::string& style);
  108. void UpdateLayout();
  109. void UpdateStyles();
  110. void RefreshDisplay();
  111. void SetComment(const AZStd::string& comment);
  112. AZStd::string GetComment() const;
  113. // NOTE: Currently the style helper does not signal out when it's value has changed.
  114. // As such, any modifications to the style helper will need to call OnStyleChanged in order
  115. // to propogate those changes.
  116. Styling::StyleHelper& GetStyleHelper();
  117. const Styling::StyleHelper& GetStyleHelper() const;
  118. void SetCommentMode(CommentMode commentMode);
  119. CommentMode GetCommentMode() const;
  120. // CommentUIRequestBus
  121. void SetEditable(bool editable) override;
  122. ////
  123. // CommentLayoutRequestBus
  124. QGraphicsLayoutItem* GetGraphicsLayoutItem() override;
  125. ////
  126. // StyleNotificationBus
  127. void OnStyleChanged() override;
  128. ////
  129. protected:
  130. void UpdateSizing();
  131. void SubmitValue();
  132. void UpdateSizePolicies();
  133. bool sceneEventFilter(QGraphicsItem*, QEvent* event) override;
  134. const AZ::EntityId& GetEntityId() const { return m_entityId; }
  135. void SetupProxyWidget();
  136. void CleanupProxyWidget();
  137. private:
  138. CommentTextGraphicsWidget(const CommentTextGraphicsWidget&) = delete;
  139. CommentMode m_commentMode;
  140. AZStd::string m_commentText;
  141. bool m_editable;
  142. bool m_layoutLock;
  143. QGraphicsLinearLayout* m_layout;
  144. GraphCanvasLabel* m_displayLabel;
  145. Internal::FocusableTextEdit* m_textEdit;
  146. QGraphicsProxyWidget* m_proxyWidget;
  147. Styling::StyleHelper m_styleHelper;
  148. AZStd::string m_style;
  149. QPointF m_initialClick;
  150. bool m_pressed;
  151. AZ::EntityId m_entityId;
  152. };
  153. }