NumericNodePropertyDisplay.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <QGraphicsProxyWidget>
  9. #include <Components/NodePropertyDisplays/NumericNodePropertyDisplay.h>
  10. #include <GraphCanvas/Components/SceneBus.h>
  11. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  12. #include <GraphCanvas/Components/Slots/SlotBus.h>
  13. #include <GraphCanvas/Widgets/NodePropertyBus.h>
  14. #include <Widgets/GraphCanvasLabel.h>
  15. namespace GraphCanvas
  16. {
  17. ///////////////////////////////
  18. // NumericNodePropertyDisplay
  19. ///////////////////////////////
  20. NumericNodePropertyDisplay::NumericNodePropertyDisplay(NumericDataInterface* dataInterface)
  21. : NodePropertyDisplay(dataInterface)
  22. , m_dataInterface(dataInterface)
  23. , m_displayLabel(nullptr)
  24. , m_spinBox(nullptr)
  25. , m_proxyWidget(nullptr)
  26. {
  27. m_disabledLabel = aznew GraphCanvasLabel();
  28. m_displayLabel = aznew GraphCanvasLabel();
  29. }
  30. NumericNodePropertyDisplay::~NumericNodePropertyDisplay()
  31. {
  32. CleanupProxyWidget();
  33. delete m_dataInterface;
  34. delete m_displayLabel;
  35. delete m_disabledLabel;
  36. }
  37. void NumericNodePropertyDisplay::RefreshStyle()
  38. {
  39. m_disabledLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisabledLabelStyle("double").c_str());
  40. m_displayLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisplayLabelStyle("double").c_str());
  41. if (m_spinBox)
  42. {
  43. QSizeF minimumSize = m_displayLabel->minimumSize();
  44. QSizeF maximumSize = m_displayLabel->maximumSize();
  45. m_spinBox->setMinimumSize(aznumeric_cast<int>(minimumSize.width()), aznumeric_cast<int>(minimumSize.height()));
  46. m_spinBox->setMaximumSize(aznumeric_cast<int>(maximumSize.width()), aznumeric_cast<int>(maximumSize.height()));
  47. }
  48. }
  49. void NumericNodePropertyDisplay::UpdateDisplay()
  50. {
  51. double value = m_dataInterface->GetNumber();
  52. AZStd::string displayValue = AZStd::string::format("%.*g%s", m_dataInterface->GetDisplayDecimalPlaces(), value, m_dataInterface->GetSuffix());
  53. m_displayLabel->SetLabel(displayValue);
  54. if (m_spinBox)
  55. {
  56. QSignalBlocker signalBlocker(m_spinBox);
  57. m_spinBox->setValue(value);
  58. m_spinBox->deselectAll();
  59. }
  60. if (m_proxyWidget)
  61. {
  62. m_proxyWidget->update();
  63. }
  64. }
  65. QGraphicsLayoutItem* NumericNodePropertyDisplay::GetDisabledGraphicsLayoutItem()
  66. {
  67. CleanupProxyWidget();
  68. return m_disabledLabel;
  69. }
  70. QGraphicsLayoutItem* NumericNodePropertyDisplay::GetDisplayGraphicsLayoutItem()
  71. {
  72. CleanupProxyWidget();
  73. return m_displayLabel;
  74. }
  75. QGraphicsLayoutItem* NumericNodePropertyDisplay::GetEditableGraphicsLayoutItem()
  76. {
  77. SetupProxyWidget();
  78. return m_proxyWidget;
  79. }
  80. void NumericNodePropertyDisplay::OnDragDropStateStateChanged(const DragDropState& dragState)
  81. {
  82. Styling::StyleHelper& styleHelper = m_displayLabel->GetStyleHelper();
  83. UpdateStyleForDragDrop(dragState, styleHelper);
  84. m_displayLabel->update();
  85. }
  86. void NumericNodePropertyDisplay::EditStart()
  87. {
  88. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::LockEditState, this);
  89. TryAndSelectNode();
  90. }
  91. void NumericNodePropertyDisplay::SubmitValue()
  92. {
  93. if (m_spinBox)
  94. {
  95. m_dataInterface->SetNumber(m_spinBox->value());
  96. m_spinBox->selectAll();
  97. }
  98. else
  99. {
  100. AZ_Error("GraphCanvas", false, "spin box doesn't exist!");
  101. }
  102. UpdateDisplay();
  103. }
  104. void NumericNodePropertyDisplay::EditFinished()
  105. {
  106. SubmitValue();
  107. if (m_spinBox)
  108. {
  109. m_spinBox->deselectAll();
  110. }
  111. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::UnlockEditState, this);
  112. }
  113. void NumericNodePropertyDisplay::SetupProxyWidget()
  114. {
  115. if (!m_spinBox)
  116. {
  117. m_proxyWidget = new QGraphicsProxyWidget();
  118. m_proxyWidget->setFlag(QGraphicsItem::ItemIsFocusable, true);
  119. m_proxyWidget->setFocusPolicy(Qt::StrongFocus);
  120. m_spinBox = aznew Internal::FocusableDoubleSpinBox();
  121. m_spinBox->setProperty("HasNoWindowDecorations", true);
  122. QObject::connect(m_spinBox, &Internal::FocusableDoubleSpinBox::OnFocusIn, [this]() { EditStart(); });
  123. QObject::connect(m_spinBox, &Internal::FocusableDoubleSpinBox::OnFocusOut, [this]() { EditFinished(); });
  124. QObject::connect(m_spinBox, &QDoubleSpinBox::editingFinished, [this]() { SubmitValue(); });
  125. m_spinBox->setMinimum(m_dataInterface->GetMin());
  126. m_spinBox->setMaximum(m_dataInterface->GetMax());
  127. m_spinBox->setSuffix(QString(m_dataInterface->GetSuffix()));
  128. m_spinBox->setDecimals(m_dataInterface->GetDecimalPlaces());
  129. m_spinBox->SetDisplayDecimals(m_dataInterface->GetDisplayDecimalPlaces());
  130. m_proxyWidget->setWidget(m_spinBox);
  131. UpdateDisplay();
  132. RefreshStyle();
  133. RegisterShortcutDispatcher(m_spinBox);
  134. }
  135. }
  136. void NumericNodePropertyDisplay::CleanupProxyWidget()
  137. {
  138. if (m_spinBox)
  139. {
  140. UnregisterShortcutDispatcher(m_spinBox);
  141. delete m_spinBox; // NB: this implicitly deletes m_proxy widget
  142. m_spinBox = nullptr;
  143. m_proxyWidget = nullptr;
  144. }
  145. }
  146. #include <Source/Components/NodePropertyDisplays/moc_NumericNodePropertyDisplay.cpp>
  147. }