ManifestWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <QLabel>
  9. #include <QPushButton>
  10. #include <SceneWidgets/ui_ManifestWidget.h>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  13. #include <SceneAPI/SceneCore/Events/ManifestMetaInfoBus.h>
  14. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidget.h>
  15. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidgetPage.h>
  16. #include <SceneAPI/SceneUI/CommonWidgets/OverlayWidget.h>
  17. #include <SceneAPI/SceneUI/SceneWidgets/SceneGraphWidget.h>
  18. #include <AzCore/std/sort.h>
  19. namespace AZ
  20. {
  21. namespace SceneAPI
  22. {
  23. namespace UI
  24. {
  25. ManifestWidget::ManifestWidget(SerializeContext* serializeContext, QWidget* parent)
  26. : QWidget(parent)
  27. , ui(new Ui::ManifestWidget())
  28. , m_serializeContext(serializeContext)
  29. {
  30. ui->setupUi(this);
  31. ui->m_tabs->setOverflowButtonSpacing(true);
  32. }
  33. ManifestWidget::~ManifestWidget()
  34. {
  35. }
  36. void ManifestWidget::ResetScene()
  37. {
  38. ui->m_tabs->clear();
  39. m_pages.clear();
  40. m_scene.reset();
  41. }
  42. void ManifestWidget::BuildFromScene(const AZStd::shared_ptr<Containers::Scene>& scene)
  43. {
  44. AZ_PROFILE_FUNCTION(Editor);
  45. ui->m_tabs->clear();
  46. m_pages.clear();
  47. m_scene = scene;
  48. if (!scene)
  49. {
  50. return;
  51. }
  52. BuildPages();
  53. Containers::SceneManifest& manifest = scene->GetManifest();
  54. for (auto& value : manifest.GetValueStorage())
  55. {
  56. AddObject(value);
  57. }
  58. for (ManifestWidgetPage* page : m_pages)
  59. {
  60. page->RefreshPage();
  61. }
  62. // Make sure to reset the active tab if the active tab is now empty
  63. ManifestWidgetPage* currentPage = qobject_cast<ManifestWidgetPage*>(ui->m_tabs->currentWidget());
  64. if (currentPage == nullptr || currentPage->ObjectCount() == 0)
  65. {
  66. for (ManifestWidgetPage* page : m_pages)
  67. {
  68. if (page->ObjectCount() > 0)
  69. {
  70. ui->m_tabs->setCurrentWidget(page);
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. bool ManifestWidget::AddObject(const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  77. {
  78. AZ_PROFILE_FUNCTION(Editor);
  79. for (ManifestWidgetPage* page : m_pages)
  80. {
  81. if (page->SupportsType(object))
  82. {
  83. return page->AddObject(object);
  84. }
  85. }
  86. return false;
  87. }
  88. bool ManifestWidget::RemoveObject(const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  89. {
  90. for (ManifestWidgetPage* page : m_pages)
  91. {
  92. if (page->SupportsType(object))
  93. {
  94. return page->RemoveObject(object);
  95. }
  96. }
  97. return false;
  98. }
  99. AZStd::shared_ptr<Containers::Scene> ManifestWidget::GetScene()
  100. {
  101. return m_scene;
  102. }
  103. AZStd::shared_ptr<const Containers::Scene> ManifestWidget::GetScene() const
  104. {
  105. return m_scene;
  106. }
  107. ManifestWidget* ManifestWidget::FindRoot(QWidget* child)
  108. {
  109. while (child != nullptr)
  110. {
  111. ManifestWidget* manifestWidget = qobject_cast<ManifestWidget*>(child);
  112. if (manifestWidget)
  113. {
  114. return manifestWidget;
  115. }
  116. else
  117. {
  118. child = child->parentWidget();
  119. }
  120. }
  121. return nullptr;
  122. }
  123. const ManifestWidget* ManifestWidget::FindRoot(const QWidget* child)
  124. {
  125. while (child != nullptr)
  126. {
  127. const ManifestWidget* manifestWidget = qobject_cast<const ManifestWidget*>(child);
  128. if (manifestWidget)
  129. {
  130. return manifestWidget;
  131. }
  132. else
  133. {
  134. child = child->parentWidget();
  135. }
  136. }
  137. return nullptr;
  138. }
  139. void ManifestWidget::BuildPages()
  140. {
  141. if (!m_scene)
  142. {
  143. return;
  144. }
  145. Events::ManifestMetaInfo::CategoryRegistrationList categories;
  146. EBUS_EVENT(Events::ManifestMetaInfoBus, GetCategoryAssignments, categories, *m_scene);
  147. AZStd::sort(categories.begin(), categories.end(),
  148. [](const Events::ManifestMetaInfo::CategoryRegistration& lhs, const Events::ManifestMetaInfo::CategoryRegistration& rhs)
  149. {
  150. return rhs.m_preferredOrder > lhs.m_preferredOrder;
  151. }
  152. );
  153. AZStd::string currentCategory;
  154. AZStd::vector<AZ::Uuid> types;
  155. for (auto& category : categories)
  156. {
  157. if (category.m_categoryName != currentCategory)
  158. {
  159. // Skip first occurrence.
  160. if (!currentCategory.empty())
  161. {
  162. ManifestWidgetPage* page = new ManifestWidgetPage(m_serializeContext, AZStd::move(types));
  163. AddPage(currentCategory.c_str(), page);
  164. }
  165. currentCategory = category.m_categoryName;
  166. AZ_Assert(types.empty(), "Expecting vectors to be empty after being moved.");
  167. }
  168. types.push_back(category.m_categoryTargetGroupId);
  169. }
  170. // Add final page
  171. if (!currentCategory.empty())
  172. {
  173. ManifestWidgetPage* page = new ManifestWidgetPage(m_serializeContext, AZStd::move(types));
  174. AddPage(currentCategory.c_str(), page);
  175. }
  176. }
  177. void ManifestWidget::AddPage(const QString& category, ManifestWidgetPage* page)
  178. {
  179. m_pages.push_back(page);
  180. ui->m_tabs->addTab(page, category);
  181. connect(page, &ManifestWidgetPage::SaveClicked, this, &ManifestWidget::SaveClicked);
  182. connect(page, &ManifestWidgetPage::InspectClicked, this, &ManifestWidget::OnInspect);
  183. connect(page, &ManifestWidgetPage::ResetSettings, this, &ManifestWidget::OnSceneResetRequested);
  184. connect(page, &ManifestWidgetPage::ClearChanges, this, &ManifestWidget::OnClearUnsavedChangesRequested);
  185. connect(page, &ManifestWidgetPage::AssignScript, this, &ManifestWidget::OnAssignScript);
  186. connect(this, &ManifestWidget::AppendUnsavedChangesToTitle, page, &ManifestWidgetPage::AppendUnsavedChangesToTitle);
  187. connect(this, &ManifestWidget::EnableInspector, page, &ManifestWidgetPage::EnableInspector);
  188. }
  189. void ManifestWidget::SetInspectButtonVisibility(bool enableInspector)
  190. {
  191. emit EnableInspector(enableInspector);
  192. }
  193. } // namespace UI
  194. } // namespace SceneAPI
  195. } // namespace AZ
  196. #include <SceneWidgets/moc_ManifestWidget.cpp>