PropertyMiscCtrl.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "EditorDefs.h"
  9. #include "PropertyMiscCtrl.h"
  10. // Qt
  11. #include <QHBoxLayout>
  12. #include <QtWidgets/QLabel>
  13. #include <QLineEdit>
  14. #include <QtWidgets/QToolButton>
  15. #include <QtCore/QTimer>
  16. #include <QtUtilWin.h>
  17. // Editor
  18. #include "GenericSelectItemDialog.h"
  19. #include "QtViewPaneManager.h"
  20. UserPropertyEditor::UserPropertyEditor(QWidget *pParent /*= nullptr*/)
  21. : QWidget(pParent)
  22. , m_canEdit(false)
  23. , m_useTree(false)
  24. {
  25. m_valueLabel = new QLabel;
  26. QToolButton *mainButton = new QToolButton;
  27. mainButton->setText("..");
  28. connect(mainButton, &QToolButton::clicked, this, &UserPropertyEditor::onEditClicked);
  29. QHBoxLayout *mainLayout = new QHBoxLayout(this);
  30. mainLayout->addWidget(m_valueLabel, 1);
  31. mainLayout->addWidget(mainButton);
  32. mainLayout->setContentsMargins(1, 1, 1, 1);
  33. }
  34. void UserPropertyEditor::SetValue(const QString &value, bool notify /*= true*/)
  35. {
  36. if (m_value != value)
  37. {
  38. m_value = value;
  39. m_valueLabel->setText(m_value);
  40. if (notify)
  41. {
  42. emit ValueChanged(m_value);
  43. }
  44. }
  45. }
  46. void UserPropertyEditor::SetData(bool canEdit, bool useTree, const QString &treeSeparator, const QString &dialogTitle, const std::vector<IVariable::IGetCustomItems::SItem>& items)
  47. {
  48. m_canEdit = canEdit;
  49. m_useTree = useTree;
  50. m_treeSeparator = treeSeparator;
  51. m_dialogTitle = dialogTitle;
  52. m_items = items;
  53. }
  54. void UserPropertyEditor::onEditClicked()
  55. {
  56. // call the user supplied callback to fill-in items and get dialog title
  57. emit RefreshItems();
  58. if (m_canEdit) // if func didn't veto, show the dialog
  59. {
  60. CGenericSelectItemDialog gtDlg;
  61. if (m_useTree)
  62. {
  63. gtDlg.SetMode(CGenericSelectItemDialog::eMODE_TREE);
  64. if (!m_treeSeparator.isEmpty())
  65. {
  66. gtDlg.SetTreeSeparator(m_treeSeparator);
  67. }
  68. }
  69. gtDlg.SetItems(m_items);
  70. if (m_dialogTitle.isEmpty() == false)
  71. gtDlg.setWindowTitle(m_dialogTitle);
  72. gtDlg.PreSelectItem(GetValue());
  73. if (gtDlg.exec() == QDialog::Accepted)
  74. {
  75. QString selectedItemStr = gtDlg.GetSelectedItem();
  76. if (selectedItemStr.isEmpty() == false)
  77. {
  78. SetValue(selectedItemStr);
  79. }
  80. }
  81. }
  82. }
  83. QWidget* UserPopupWidgetHandler::CreateGUI(QWidget *pParent)
  84. {
  85. UserPropertyEditor* newCtrl = aznew UserPropertyEditor(pParent);
  86. connect(newCtrl, &UserPropertyEditor::ValueChanged, newCtrl, [newCtrl]()
  87. {
  88. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(
  89. &AzToolsFramework::PropertyEditorGUIMessages::Bus::Events::RequestWrite, newCtrl);
  90. });
  91. return newCtrl;
  92. }
  93. void UserPopupWidgetHandler::ConsumeAttribute(UserPropertyEditor* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  94. {
  95. Q_UNUSED(GUI);
  96. Q_UNUSED(attrib);
  97. Q_UNUSED(attrValue);
  98. Q_UNUSED(debugName);
  99. }
  100. void UserPopupWidgetHandler::WriteGUIValuesIntoProperty(size_t index, UserPropertyEditor* GUI, property_t& instance, AzToolsFramework::InstanceDataNode* node)
  101. {
  102. Q_UNUSED(index);
  103. Q_UNUSED(node);
  104. CReflectedVarUser val = instance;
  105. val.m_value = GUI->GetValue().toUtf8().data();
  106. instance = static_cast<property_t>(val);
  107. }
  108. bool UserPopupWidgetHandler::ReadValuesIntoGUI(size_t index, UserPropertyEditor* GUI, const property_t& instance, AzToolsFramework::InstanceDataNode* node)
  109. {
  110. Q_UNUSED(index);
  111. Q_UNUSED(node);
  112. CReflectedVarUser val = instance;
  113. assert(val.m_itemNames.size() == val.m_itemDescriptions.size());
  114. std::vector<IVariable::IGetCustomItems::SItem> items(val.m_itemNames.size());
  115. int i = -1;
  116. std::generate(items.begin(), items.end(), [&val, &i]() { ++i; return IVariable::IGetCustomItems::SItem(val.m_itemNames[i].c_str(), val.m_itemDescriptions[i].c_str());});
  117. GUI->SetData(val.m_enableEdit, val.m_useTree, val.m_treeSeparator.c_str(), val.m_dialogTitle.c_str(), items);
  118. GUI->SetValue(val.m_value.c_str(), false);
  119. return false;
  120. }
  121. #include <Controls/ReflectedPropertyControl/moc_PropertyMiscCtrl.cpp>
  122. QWidget* FloatCurveHandler::CreateGUI(QWidget *pParent)
  123. {
  124. CSplineCtrl *cSpline = new CSplineCtrl(pParent);
  125. cSpline->SetUpdateCallback([this](CSplineCtrl* spl) { OnSplineChange(spl); });
  126. cSpline->SetTimeRange(0, 1);
  127. cSpline->SetValueRange(0, 1);
  128. cSpline->SetGrid(12, 12);
  129. cSpline->setFixedHeight(52);
  130. return cSpline;
  131. }
  132. void FloatCurveHandler::OnSplineChange(CSplineCtrl*)
  133. {
  134. //AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(
  135. // &AzToolsFramework::PropertyEditorGUIMessages::Bus::Events::RequestWrite, splineWidget);
  136. }
  137. void FloatCurveHandler::ConsumeAttribute(CSplineCtrl *, AZ::u32, AzToolsFramework::PropertyAttributeReader*, const char*)
  138. {}
  139. void FloatCurveHandler::WriteGUIValuesIntoProperty([[maybe_unused]] size_t index, [[maybe_unused]] CSplineCtrl* GUI, [[maybe_unused]] property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  140. {
  141. //nothing to do here. the spline itself will have it's new values.
  142. }
  143. bool FloatCurveHandler::ReadValuesIntoGUI([[maybe_unused]] size_t index, CSplineCtrl* GUI, const property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  144. {
  145. GUI->SetSpline(reinterpret_cast<ISplineInterpolator*>(instance.m_spline));
  146. return false;
  147. }