StringNodePropertyDisplay.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <QLineEdit>
  9. #include <QGraphicsProxyWidget>
  10. #include <Components/NodePropertyDisplays/StringNodePropertyDisplay.h>
  11. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  12. #include <GraphCanvas/Widgets/NodePropertyBus.h>
  13. #include <Widgets/GraphCanvasLabel.h>
  14. namespace GraphCanvas
  15. {
  16. //////////////////////////////
  17. // StringNodePropertyDisplay
  18. //////////////////////////////
  19. StringNodePropertyDisplay::StringNodePropertyDisplay(StringDataInterface* dataInterface)
  20. : NodePropertyDisplay(dataInterface)
  21. , m_dataInterface(dataInterface)
  22. , m_displayLabel(nullptr)
  23. , m_lineEdit(nullptr)
  24. , m_proxyWidget(nullptr)
  25. , m_isNudging(false)
  26. {
  27. m_dataInterface->RegisterDisplay(this);
  28. m_disabledLabel = aznew GraphCanvasLabel();
  29. m_displayLabel = aznew GraphCanvasLabel();
  30. if (m_dataInterface->ResizeToContents())
  31. {
  32. m_displayLabel->SetWrapMode(GraphCanvasLabel::WrapMode::ResizeToContent);
  33. m_displayLabel->SetElide(false);
  34. }
  35. }
  36. StringNodePropertyDisplay::~StringNodePropertyDisplay()
  37. {
  38. CleanupProxyWidget();
  39. delete m_dataInterface;
  40. delete m_displayLabel;
  41. delete m_disabledLabel;
  42. }
  43. void StringNodePropertyDisplay::RefreshStyle()
  44. {
  45. m_disabledLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisabledLabelStyle("string").c_str());
  46. m_displayLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisplayLabelStyle("string").c_str());
  47. QSizeF minimumSize = m_displayLabel->minimumSize();
  48. if (m_lineEdit)
  49. {
  50. m_lineEdit->setMinimumSize(aznumeric_cast<int>(minimumSize.width()), aznumeric_cast<int>(minimumSize.height()));
  51. if (m_dataInterface->ResizeToContents())
  52. {
  53. ResizeToContents();
  54. }
  55. else
  56. {
  57. int minWidth = aznumeric_cast<int>(m_displayLabel->size().width());
  58. m_lineEdit->setFixedWidth(AZStd::max(minWidth, 10));
  59. }
  60. }
  61. }
  62. void StringNodePropertyDisplay::UpdateDisplay()
  63. {
  64. AZStd::string value = m_dataInterface->GetString();
  65. m_displayLabel->SetLabel(value);
  66. if (m_lineEdit)
  67. {
  68. QSignalBlocker signalBlocker(m_lineEdit);
  69. m_lineEdit->setText(value.c_str());
  70. m_lineEdit->setCursorPosition(0);
  71. }
  72. ResizeToContents();
  73. }
  74. QGraphicsLayoutItem* StringNodePropertyDisplay::GetDisabledGraphicsLayoutItem()
  75. {
  76. CleanupProxyWidget();
  77. return m_disabledLabel;
  78. }
  79. QGraphicsLayoutItem* StringNodePropertyDisplay::GetDisplayGraphicsLayoutItem()
  80. {
  81. CleanupProxyWidget();
  82. return m_displayLabel;
  83. }
  84. QGraphicsLayoutItem* StringNodePropertyDisplay::GetEditableGraphicsLayoutItem()
  85. {
  86. SetupProxyWidget();
  87. return m_proxyWidget;
  88. }
  89. void StringNodePropertyDisplay::OnDragDropStateStateChanged(const DragDropState& dragState)
  90. {
  91. Styling::StyleHelper& styleHelper = m_displayLabel->GetStyleHelper();
  92. UpdateStyleForDragDrop(dragState, styleHelper);
  93. m_displayLabel->update();
  94. }
  95. void StringNodePropertyDisplay::OnSystemTick()
  96. {
  97. EditFinished();
  98. AZ::SystemTickBus::Handler::BusDisconnect();
  99. }
  100. void StringNodePropertyDisplay::EditStart()
  101. {
  102. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::LockEditState, this);
  103. SceneRequestBus::Event(GetSceneId(), &SceneRequests::CancelNudging);
  104. TryAndSelectNode();
  105. }
  106. void StringNodePropertyDisplay::SubmitValue()
  107. {
  108. if (m_lineEdit)
  109. {
  110. AZStd::string value = m_lineEdit->text().toUtf8().data();
  111. m_dataInterface->SetString(value);
  112. m_lineEdit->setCursorPosition(m_lineEdit->text().size());
  113. m_lineEdit->selectAll();
  114. }
  115. else
  116. {
  117. AZ_Error("GraphCanvas", false, "line edit doesn't exist!");
  118. }
  119. }
  120. void StringNodePropertyDisplay::EditFinished()
  121. {
  122. SubmitValue();
  123. UpdateDisplay();
  124. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::UnlockEditState, this);
  125. if (m_isNudging)
  126. {
  127. m_isNudging = false;
  128. SceneRequestBus::Event(GetSceneId(), &SceneRequests::FinalizeNudging);
  129. }
  130. }
  131. void StringNodePropertyDisplay::OnFocusOut()
  132. {
  133. // String property changes can sometimes change the visual layouts of nodes.
  134. // Need to delay this to the start of the next tick to avoid running into
  135. // issues with Qt processing.
  136. AZ::SystemTickBus::Handler::BusConnect();
  137. }
  138. void StringNodePropertyDisplay::SetupProxyWidget()
  139. {
  140. if (!m_lineEdit)
  141. {
  142. m_proxyWidget = new QGraphicsProxyWidget();
  143. m_lineEdit = aznew Internal::FocusableLineEdit();
  144. m_lineEdit->setProperty("HasNoWindowDecorations", true);
  145. m_lineEdit->setEnabled(true);
  146. QObject::connect(m_lineEdit, &QLineEdit::textChanged, [this]() { ResizeToContents(); });
  147. QObject::connect(m_lineEdit, &Internal::FocusableLineEdit::OnFocusIn, [this]() { EditStart(); });
  148. QObject::connect(m_lineEdit, &Internal::FocusableLineEdit::OnFocusOut, [this]() { EditFinished(); });
  149. QObject::connect(m_lineEdit, &QLineEdit::editingFinished, [this]() {
  150. SubmitValue();
  151. UpdateDisplay();
  152. });
  153. m_proxyWidget->setWidget(m_lineEdit);
  154. UpdateDisplay();
  155. RefreshStyle();
  156. RegisterShortcutDispatcher(m_lineEdit);
  157. }
  158. }
  159. void StringNodePropertyDisplay::CleanupProxyWidget()
  160. {
  161. if (m_lineEdit)
  162. {
  163. UnregisterShortcutDispatcher(m_lineEdit);
  164. delete m_lineEdit; // NB: this implicitly deletes m_proxy widget
  165. m_lineEdit = nullptr;
  166. m_proxyWidget = nullptr;
  167. }
  168. }
  169. void StringNodePropertyDisplay::ResizeToContents()
  170. {
  171. if (m_lineEdit && m_dataInterface->ResizeToContents())
  172. {
  173. int originalWidth = m_lineEdit->width();
  174. m_displayLabel->SetLabel(m_lineEdit->text().toUtf8().data());
  175. QFontMetrics fm = m_lineEdit->fontMetrics();
  176. int width = aznumeric_cast<int>(fm.boundingRect(m_lineEdit->text()).width());
  177. if (width < m_displayLabel->minimumSize().width())
  178. {
  179. width = aznumeric_cast<int>(m_displayLabel->minimumSize().width()) + 2;
  180. }
  181. m_lineEdit->setFixedWidth(width);
  182. // Don't want to start nudging unless we actually have the focus
  183. if (width != originalWidth && m_lineEdit->hasFocus())
  184. {
  185. NodeUIRequestBus::Event(GetNodeId(), &NodeUIRequests::AdjustSize);
  186. if (!m_isNudging)
  187. {
  188. m_isNudging = true;
  189. AZStd::unordered_set< NodeId > fixedNodes = { GetNodeId() };
  190. SceneRequestBus::Event(GetSceneId(), &SceneRequests::StartNudging, fixedNodes);
  191. }
  192. }
  193. }
  194. if (m_proxyWidget)
  195. {
  196. m_proxyWidget->update();
  197. }
  198. }
  199. #include <Source/Components/NodePropertyDisplays/moc_StringNodePropertyDisplay.cpp>
  200. }