FindEntityWidget.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "FindEntityWidget.h"
  9. #include "FindEntityItemModel.h"
  10. #include "FindEntitySortFilterProxyModel.h"
  11. #include <AzCore/Component/ComponentApplication.h>
  12. #include <AzCore/Component/Entity.h>
  13. #include <AzCore/Component/EntityUtils.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Serialization/Utils.h>
  16. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  17. #include <AzToolsFramework/UI/ComponentPalette/ComponentPaletteUtil.hxx>
  18. #include <LyShine/UiBase.h>
  19. #include <LyShine/Bus/UiCanvasBus.h>
  20. #include <QPushButton>
  21. #include <QDialogButtonBox>
  22. #include <QTreeView>
  23. #include <QBoxLayout>
  24. #include <QScrollArea>
  25. Q_DECLARE_METATYPE(AZ::Uuid);
  26. namespace
  27. {
  28. bool AppearsInUiComponentMenu(const AZ::SerializeContext::ClassData& classData)
  29. {
  30. return AzToolsFramework::AppearsInAddComponentMenu(classData, AZ_CRC_CE("UI"));
  31. }
  32. }
  33. FindEntityWidget::FindEntityWidget(AZ::EntityId canvasEntityId, QWidget* pParent, Qt::WindowFlags flags)
  34. : QWidget(pParent, flags)
  35. {
  36. SetupUI();
  37. m_objectTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
  38. m_objectTree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  39. m_objectTree->setAutoScrollMargin(20);
  40. m_objectTree->setUniformRowHeights(true);
  41. m_objectTree->setHeaderHidden(true);
  42. m_listModel = aznew FindEntityItemModel(this);
  43. m_proxyModel = aznew FindEntitySortFilterProxyModel(this);
  44. m_proxyModel->setSourceModel(m_listModel);
  45. m_objectTree->setModel(m_proxyModel);
  46. AZ::SerializeContext* serializeContext = nullptr;
  47. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  48. if (serializeContext)
  49. {
  50. // Make a list of all components that are currently used in the canvas
  51. AZStd::unordered_set<AZ::Uuid> usedComponents;
  52. GetUsedComponents(canvasEntityId, usedComponents);
  53. AzToolsFramework::ComponentPaletteUtil::ComponentDataTable componentDataTable;
  54. AzToolsFramework::ComponentPaletteUtil::ComponentIconTable componentIconTable;
  55. AZ::ComponentDescriptor::DependencyArrayType serviceFilter;
  56. AzToolsFramework::ComponentPaletteUtil::BuildComponentTables(serializeContext, AppearsInUiComponentMenu, serviceFilter, componentDataTable, componentIconTable);
  57. for (const auto& categoryPair : componentDataTable)
  58. {
  59. const auto& componentMap = categoryPair.second;
  60. for (const auto& componentPair : componentMap)
  61. {
  62. auto iter = usedComponents.find(componentPair.second->m_typeId);
  63. if (iter != usedComponents.end())
  64. {
  65. usedComponents.erase(iter);
  66. m_searchWidget->AddTypeFilter(categoryPair.first, componentPair.first, QVariant::fromValue<AZ::Uuid>(componentPair.second->m_typeId));
  67. }
  68. }
  69. }
  70. }
  71. connect(m_objectTree->selectionModel(), &QItemSelectionModel::selectionChanged, this, &FindEntityWidget::OnSelectionChanged);
  72. connect(m_objectTree, &QTreeView::doubleClicked, this, &FindEntityWidget::OnItemDblClick);
  73. connect(m_searchWidget, &AzQtComponents::FilteredSearchWidget::TextFilterChanged, this, &FindEntityWidget::OnSearchTextChanged);
  74. connect(m_searchWidget, &AzQtComponents::FilteredSearchWidget::TypeFilterChanged, this, &FindEntityWidget::OnFilterChanged);
  75. connect(m_selectButton, &QPushButton::clicked, this, &FindEntityWidget::OnSelectClicked);
  76. connect(m_cancelButton, &QPushButton::clicked, this, &FindEntityWidget::OnCancelClicked);
  77. m_listModel->Initialize(canvasEntityId);
  78. m_objectTree->expandAll();
  79. // Select button starts off disabled and becomes enabled when there is a selection
  80. m_selectButton->setEnabled(false);
  81. }
  82. FindEntityWidget::~FindEntityWidget()
  83. {
  84. delete m_listModel;
  85. delete m_proxyModel;
  86. }
  87. AZ::EntityId FindEntityWidget::GetEntityIdFromIndex(const QModelIndex& index) const
  88. {
  89. if (index.isValid())
  90. {
  91. const QModelIndex modelIndex = m_proxyModel->mapToSource(index);
  92. if (modelIndex.isValid())
  93. {
  94. return m_listModel->GetEntityFromIndex(modelIndex);
  95. }
  96. }
  97. return AZ::EntityId();
  98. }
  99. QModelIndex FindEntityWidget::GetIndexFromEntityId(const AZ::EntityId& entityId) const
  100. {
  101. if (entityId.IsValid())
  102. {
  103. QModelIndex modelIndex = m_listModel->GetIndexFromEntity(entityId);
  104. if (modelIndex.isValid())
  105. {
  106. return m_proxyModel->mapFromSource(modelIndex);
  107. }
  108. }
  109. return QModelIndex();
  110. }
  111. void FindEntityWidget::SetupUI()
  112. {
  113. QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
  114. sizePolicy.setHorizontalStretch(0);
  115. sizePolicy.setVerticalStretch(0);
  116. setSizePolicy(sizePolicy);
  117. QVBoxLayout* verticalLayout = new QVBoxLayout(this);
  118. verticalLayout->setSizeConstraint(QLayout::SetMinimumSize);
  119. QHBoxLayout* horizontalLayoutSearch = new QHBoxLayout();
  120. horizontalLayoutSearch->setSpacing(0);
  121. m_searchWidget = new AzQtComponents::FilteredSearchWidget(this);
  122. horizontalLayoutSearch->addWidget(m_searchWidget);
  123. verticalLayout->addLayout(horizontalLayoutSearch);
  124. QScrollArea* objectList = new QScrollArea(this);
  125. objectList->setFocusPolicy(Qt::ClickFocus);
  126. objectList->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
  127. objectList->setWidgetResizable(true);
  128. QWidget* objectListContents = new QWidget();
  129. QVBoxLayout* verticalLayoutObjectListContents = new QVBoxLayout(objectListContents);
  130. verticalLayoutObjectListContents->setSpacing(0);
  131. verticalLayoutObjectListContents->setContentsMargins(0, 0, 0, 0);
  132. m_objectTree = new QTreeView(objectListContents);
  133. verticalLayoutObjectListContents->addWidget(m_objectTree);
  134. objectList->setWidget(objectListContents);
  135. verticalLayout->addWidget(objectList);
  136. QDialogButtonBox* buttonBox = new QDialogButtonBox(this);
  137. m_selectButton = new QPushButton(tr("Select in Hierarchy"));
  138. m_selectButton->setToolTip(tr("Select the selected elements in the Hierarchy."));
  139. m_selectButton->setDefault(true);
  140. m_selectButton->setAutoDefault(true);
  141. m_selectButton->setProperty("class", "Primary");
  142. m_cancelButton = new QPushButton(tr("Cancel"));
  143. m_cancelButton->setDefault(false);
  144. m_cancelButton->setAutoDefault(false);
  145. buttonBox->addButton(m_selectButton, QDialogButtonBox::ApplyRole);
  146. buttonBox->addButton(m_cancelButton, QDialogButtonBox::RejectRole);
  147. verticalLayout->addWidget(buttonBox, 1);
  148. }
  149. void FindEntityWidget::GetUsedComponents(AZ::EntityId canvasEntityId, AZStd::unordered_set<AZ::Uuid>& usedComponents)
  150. {
  151. LyShine::EntityArray entities;
  152. UiCanvasBus::Event(
  153. canvasEntityId,
  154. &UiCanvasBus::Events::FindElements,
  155. []([[maybe_unused]] const AZ::Entity* entity)
  156. {
  157. return true;
  158. },
  159. entities);
  160. for (AZ::Entity* entity : entities)
  161. {
  162. for (AZ::Component* component : entity->GetComponents())
  163. {
  164. const AZ::Uuid& componentType = azrtti_typeid(component);
  165. usedComponents.insert(componentType);
  166. }
  167. }
  168. }
  169. void FindEntityWidget::OnSelectionChanged(const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected)
  170. {
  171. // Update select button state
  172. m_selectButton->setEnabled(!selected.isEmpty());
  173. }
  174. void FindEntityWidget::OnItemDblClick([[maybe_unused]] const QModelIndex& index)
  175. {
  176. OnSelectClicked();
  177. }
  178. void FindEntityWidget::OnSearchTextChanged(const QString& activeTextFilter)
  179. {
  180. AZStd::string filterString = activeTextFilter.toUtf8().data();
  181. m_listModel->SearchStringChanged(filterString);
  182. m_proxyModel->UpdateFilter();
  183. m_objectTree->expandAll();
  184. }
  185. void FindEntityWidget::OnFilterChanged(const AzQtComponents::SearchTypeFilterList& activeTypeFilters)
  186. {
  187. AZStd::vector<AZ::Uuid> componentFilters;
  188. componentFilters.reserve(activeTypeFilters.count());
  189. for (auto filter : activeTypeFilters)
  190. {
  191. AZ::Uuid typeId = qvariant_cast<AZ::Uuid>(filter.metadata);
  192. componentFilters.push_back(typeId);
  193. }
  194. m_listModel->SearchFilterChanged(componentFilters);
  195. m_proxyModel->UpdateFilter();
  196. m_objectTree->expandAll();
  197. }
  198. void FindEntityWidget::OnSelectClicked()
  199. {
  200. AZStd::vector<AZ::EntityId> selectedEntities;
  201. QModelIndexList selectedIndexes = m_objectTree->selectionModel()->selectedIndexes();
  202. for (const QModelIndex& index : selectedIndexes)
  203. {
  204. const AZ::EntityId entityId = GetEntityIdFromIndex(index);
  205. if (entityId.IsValid())
  206. {
  207. selectedEntities.push_back(entityId);
  208. }
  209. }
  210. emit OnFinished(selectedEntities);
  211. }
  212. void FindEntityWidget::OnCancelClicked()
  213. {
  214. emit OnCanceled();
  215. }
  216. #include <moc_FindEntityWidget.cpp>