VariablePaletteTableView.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <qaction.h>
  9. #include <qevent.h>
  10. #include <qheaderview.h>
  11. #include <qitemselectionmodel.h>
  12. #include <qscrollbar.h>
  13. #include <AzCore/Component/ComponentApplicationBus.h>
  14. #include <AzCore/Serialization/Utils.h>
  15. #include <AzCore/UserSettings/UserSettings.h>
  16. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  17. #include <GraphCanvas/Components/SceneBus.h>
  18. #include <GraphCanvas/Components/StyleBus.h>
  19. #include <Editor/GraphCanvas/GraphCanvasEditorNotificationBusId.h>
  20. #include <Editor/View/Widgets/VariablePanel/VariablePaletteTableView.h>
  21. #include <Editor/View/Widgets/DataTypePalette/DataTypePaletteModel.h>
  22. #include <Editor/View/Dialogs/ContainerWizard/ContainerWizard.h>
  23. #include <Editor/Settings.h>
  24. #include <Editor/Translation/TranslationHelper.h>
  25. #include <Editor/QtMetaTypes.h>
  26. #include <ScriptCanvas/Data/DataRegistry.h>
  27. namespace ScriptCanvasEditor
  28. {
  29. /////////////////////////////
  30. // VariablePaletteTableView
  31. /////////////////////////////
  32. VariablePaletteTableView::VariablePaletteTableView(QWidget* parent)
  33. : QTableView(parent)
  34. {
  35. m_containerWizard = aznew ContainerWizard(this);
  36. m_model = aznew DataTypePaletteModel(this);
  37. m_proxyModel = aznew DataTypePaletteSortFilterProxyModel(this);
  38. m_proxyModel->setSourceModel(m_model);
  39. m_proxyModel->sort(DataTypePaletteModel::ColumnIndex::Type);
  40. setModel(m_proxyModel);
  41. setItemDelegateForColumn(DataTypePaletteModel::Type, aznew GraphCanvas::IconDecoratedNameDelegate(this));
  42. viewport()->installEventFilter(this);
  43. horizontalHeader()->setSectionResizeMode(DataTypePaletteModel::ColumnIndex::Pinned, QHeaderView::ResizeMode::ResizeToContents);
  44. horizontalHeader()->setSectionResizeMode(DataTypePaletteModel::ColumnIndex::Type, QHeaderView::ResizeMode::Stretch);
  45. QObject::connect(this, &QAbstractItemView::clicked, this, &VariablePaletteTableView::OnClicked);
  46. QObject::connect(m_containerWizard, &ContainerWizard::CreateContainerVariable, this, &VariablePaletteTableView::OnCreateContainerVariable);
  47. QObject::connect(m_containerWizard, &ContainerWizard::ContainerPinned, this, &VariablePaletteTableView::OnContainerPinned);
  48. m_completer = new QCompleter();
  49. m_completer->setModel(m_proxyModel);
  50. m_completer->setCompletionColumn(DataTypePaletteModel::ColumnIndex::Type);
  51. m_completer->setCompletionMode(QCompleter::CompletionMode::InlineCompletion);
  52. m_completer->setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
  53. setMinimumSize(0,0);
  54. }
  55. VariablePaletteTableView::~VariablePaletteTableView()
  56. {
  57. m_model->SubmitPendingPinChanges();
  58. }
  59. void VariablePaletteTableView::SetActiveScene(const ScriptCanvas::ScriptCanvasId& scriptCanvasId)
  60. {
  61. m_containerWizard->SetActiveScriptCanvasId(scriptCanvasId);
  62. }
  63. void VariablePaletteTableView::PopulateVariablePalette(const AZStd::unordered_set< AZ::Uuid >& objectTypes)
  64. {
  65. clearSelection();
  66. m_model->ClearTypes();
  67. AZStd::unordered_set<AZ::Uuid> variableTypes;
  68. auto dataRegistry = ScriptCanvas::GetDataRegistry();
  69. for (const auto& dataTraitsPair : dataRegistry->m_typeIdTraitMap)
  70. {
  71. // Object type isn't valid on it's own. Need to skip that in order. Passed in types will all be
  72. // processed as an object type.
  73. if (dataTraitsPair.first == ScriptCanvas::Data::eType::BehaviorContextObject)
  74. {
  75. continue;
  76. }
  77. AZ::Uuid typeId = dataTraitsPair.second.m_dataTraits.GetAZType();
  78. if (typeId.IsNull() || typeId == azrtti_typeid<void>())
  79. {
  80. continue;
  81. }
  82. m_containerWizard->RegisterType(typeId);
  83. variableTypes.insert(typeId);
  84. }
  85. AZStd::intrusive_ptr<EditorSettings::ScriptCanvasEditorSettings> settings = AZ::UserSettings::CreateFind<EditorSettings::ScriptCanvasEditorSettings>(AZ_CRC("ScriptCanvasPreviewSettings", 0x1c5a2965), AZ::UserSettings::CT_LOCAL);
  86. for (const AZ::Uuid& objectId : objectTypes)
  87. {
  88. ScriptCanvas::Data::Type type = dataRegistry->m_typeIdTraitMap[ScriptCanvas::Data::eType::BehaviorContextObject].m_dataTraits.GetSCType(objectId);
  89. if (!type.IsValid() || !dataRegistry->m_creatableTypes.contains(type))
  90. {
  91. continue;
  92. }
  93. // For now, we need to register all of the objectId's with the container wizard
  94. // in order to properly populate the list of valid container configurations.
  95. m_containerWizard->RegisterType(objectId);
  96. // Sanitize containers so we only put in information about the generic container type
  97. if (AZ::Utils::IsContainerType(objectId))
  98. {
  99. variableTypes.insert(AZ::Utils::GetGenericContainerType(objectId));
  100. }
  101. else
  102. {
  103. variableTypes.insert(objectId);
  104. }
  105. }
  106. // Since we gated containers to make them genrealized buckets, we now need to go through
  107. // and register in the custom defined types for each of the container types that we created.
  108. for (const AZ::Uuid& pinnedTypeId : settings->m_pinnedDataTypes)
  109. {
  110. if (variableTypes.find(pinnedTypeId) == variableTypes.end())
  111. {
  112. variableTypes.insert(pinnedTypeId);
  113. }
  114. }
  115. m_model->PopulateVariablePalette(variableTypes);
  116. }
  117. void VariablePaletteTableView::SetFilter(const QString& filter)
  118. {
  119. m_model->SubmitPendingPinChanges();
  120. clearSelection();
  121. m_proxyModel->SetFilter(filter);
  122. }
  123. QCompleter* VariablePaletteTableView::GetVariableCompleter()
  124. {
  125. return m_completer;
  126. }
  127. void VariablePaletteTableView::TryCreateVariableByTypeName(const AZStd::string& typeName)
  128. {
  129. AZ::TypeId typeId = m_model->FindTypeIdForTypeName(typeName);
  130. // Only want to go into the wizard, if we have a generic type.
  131. if (AZ::Utils::IsGenericContainerType(typeId))
  132. {
  133. m_containerWizard->ShowWizard(typeId);
  134. }
  135. else if (!typeId.IsNull() && typeId != azrtti_typeid<void>())
  136. {
  137. emit CreateVariable(ScriptCanvas::Data::FromAZType(typeId));
  138. }
  139. }
  140. void VariablePaletteTableView::hideEvent(QHideEvent* hideEvent)
  141. {
  142. m_model->SubmitPendingPinChanges();
  143. clearSelection();
  144. QTableView::hideEvent(hideEvent);
  145. }
  146. void VariablePaletteTableView::showEvent(QShowEvent* showEvent)
  147. {
  148. QTableView::showEvent(showEvent);
  149. clearSelection();
  150. scrollToTop();
  151. m_proxyModel->invalidate();
  152. }
  153. const DataTypePaletteModel* VariablePaletteTableView::GetVariableTypePaletteModel() const
  154. {
  155. return m_model;
  156. }
  157. AZStd::vector< AZ::TypeId > VariablePaletteTableView::GetArrayTypes() const
  158. {
  159. AZStd::vector< AZ::TypeId > arrayTypes;
  160. const auto& finalTypeMapping = m_containerWizard->GetFinalTypeMapping();
  161. for (auto typePair : finalTypeMapping)
  162. {
  163. if (ScriptCanvas::Data::IsVectorContainerType(ScriptCanvas::Data::FromAZType(typePair.second)))
  164. {
  165. arrayTypes.emplace_back(typePair.second);
  166. }
  167. }
  168. return arrayTypes;
  169. }
  170. AZStd::vector< AZ::TypeId > VariablePaletteTableView::GetMapTypes() const
  171. {
  172. AZStd::vector< AZ::TypeId > mapTypes;
  173. const auto& finalTypeMapping = m_containerWizard->GetFinalTypeMapping();
  174. for (auto typePair : finalTypeMapping)
  175. {
  176. if (ScriptCanvas::Data::IsMapContainerType(ScriptCanvas::Data::FromAZType(typePair.second)))
  177. {
  178. mapTypes.emplace_back(typePair.second);
  179. }
  180. }
  181. return mapTypes;
  182. }
  183. void VariablePaletteTableView::OnClicked(const QModelIndex& index)
  184. {
  185. AZ::TypeId typeId;
  186. QModelIndex sourceIndex = m_proxyModel->mapToSource(index);
  187. if (sourceIndex.isValid())
  188. {
  189. typeId = m_model->FindTypeIdForIndex(sourceIndex);
  190. }
  191. if (!typeId.IsNull() && typeId != azrtti_typeid<void>())
  192. {
  193. if (index.column() == DataTypePaletteModel::ColumnIndex::Pinned)
  194. {
  195. m_model->TogglePendingPinChange(typeId);
  196. m_model->dataChanged(sourceIndex, sourceIndex);
  197. }
  198. else if (AZ::Utils::IsGenericContainerType(typeId))
  199. {
  200. m_containerWizard->ShowWizard(typeId);
  201. }
  202. else
  203. {
  204. emit CreateVariable(ScriptCanvas::Data::FromAZType(typeId));
  205. }
  206. }
  207. }
  208. void VariablePaletteTableView::OnContainerPinned(const AZ::TypeId& typeId)
  209. {
  210. m_model->AddDataType(typeId);
  211. }
  212. void VariablePaletteTableView::OnCreateContainerVariable(const AZStd::string& variableName, const AZ::TypeId& typeId)
  213. {
  214. ScriptCanvas::Data::Type dataType = ScriptCanvas::Data::FromAZType(typeId);
  215. emit CreateNamedVariable(variableName, dataType);
  216. }
  217. #include <Editor/View/Widgets/VariablePanel/moc_VariablePaletteTableView.cpp>
  218. }