HeaderWidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <QEvent>
  9. #include <RowWidgets/ui_HeaderWidget.h>
  10. #include <AzCore/EBus/EBus.h>
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  14. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  15. #include <AzToolsFramework/Debug/TraceContext.h>
  16. #include <SceneAPI/SceneCore/Containers/Scene.h>
  17. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  18. #include <SceneAPI/SceneCore/DataTypes/Groups/ISceneNodeGroup.h>
  19. #include <SceneAPI/SceneCore/DataTypes/Rules/IUnmodifiableRule.h>
  20. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  21. #include <SceneAPI/SceneCore/Events/ManifestMetaInfoBus.h>
  22. #include <SceneAPI/SceneUI/RowWidgets/HeaderWidget.h>
  23. #include <SceneAPI/SceneUI/RowWidgets/ManifestVectorWidget.h>
  24. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidget.h>
  25. #include <QFile>
  26. static void InitSceneUIHeaderWidgetResources()
  27. {
  28. Q_INIT_RESOURCE(Icons);
  29. }
  30. namespace AZ
  31. {
  32. namespace SceneAPI
  33. {
  34. namespace UI
  35. {
  36. AZ_CLASS_ALLOCATOR_IMPL(HeaderWidget, SystemAllocator);
  37. HeaderWidget::HeaderWidget(QWidget* parent)
  38. : QWidget(parent)
  39. , ui(new Ui::HeaderWidget())
  40. , m_target(nullptr)
  41. , m_sceneManifest(nullptr)
  42. {
  43. InitSceneUIHeaderWidgetResources();
  44. ui->setupUi(this);
  45. ui->m_icon->hide();
  46. ui->m_deleteButton->setIcon(QIcon(":/stylesheet/img/close_small.svg"));
  47. connect(ui->m_deleteButton, &QToolButton::clicked, this, &HeaderWidget::DeleteObject);
  48. ui->m_deleteButton->hide();
  49. ManifestWidget* root = ManifestWidget::FindRoot(this);
  50. AZ_Assert(root, "HeaderWidget is not a child of the ManifestWidget");
  51. if (root)
  52. {
  53. m_sceneManifest = &root->GetScene()->GetManifest();
  54. }
  55. }
  56. void HeaderWidget::SetManifestObject(const DataTypes::IManifestObject* target)
  57. {
  58. AZ_TraceContext("New target", GetSerializedName(target));
  59. m_target = target;
  60. ui->m_nameLabel->setText(GetSerializedName(target));
  61. UpdateDeletable();
  62. UpdateUIForManifestObject(target);
  63. }
  64. const DataTypes::IManifestObject* HeaderWidget::GetManifestObject() const
  65. {
  66. return m_target;
  67. }
  68. bool HeaderWidget::ModifyTooltip(QString& toolTipString)
  69. {
  70. if (!m_target)
  71. {
  72. return false;
  73. }
  74. if (m_target->RTTI_IsTypeOf(DataTypes::IGroup::TYPEINFO_Uuid()))
  75. {
  76. const DataTypes::IGroup* group = azrtti_cast<const DataTypes::IGroup*>(m_target);
  77. const Containers::RuleContainer& rules = group->GetRuleContainerConst();
  78. // Multiple rules might change the tooltip, so loop through all rules.
  79. bool ruleChangedTooltip = false;
  80. // Rules don't all have access to Qt
  81. AZStd::string ruleTooltip;
  82. for (size_t ruleIndex = 0; ruleIndex < rules.GetRuleCount(); ++ruleIndex)
  83. {
  84. if (rules.GetRule(ruleIndex)->ModifyTooltip(ruleTooltip))
  85. {
  86. ruleChangedTooltip = true;
  87. }
  88. }
  89. if (ruleChangedTooltip)
  90. {
  91. toolTipString = QString("%1%2").arg(ruleTooltip.c_str()).arg(toolTipString);
  92. }
  93. return ruleChangedTooltip;
  94. }
  95. return false;
  96. }
  97. void HeaderWidget::DeleteObject()
  98. {
  99. AZ_TraceContext("Delete target", GetSerializedName(m_target));
  100. if (m_sceneManifest)
  101. {
  102. Containers::SceneManifest::Index index = m_sceneManifest->FindIndex(m_target);
  103. if (index != Containers::SceneManifest::s_invalidIndex)
  104. {
  105. ManifestWidget* root = ManifestWidget::FindRoot(this);
  106. AZStd::vector<const AZ::SceneAPI::DataTypes::IManifestObject*> otherObjectsToRemove;
  107. m_target->GetManifestObjectsToRemoveOnRemoved(otherObjectsToRemove, *m_sceneManifest);
  108. // The manifest object could be a root element at the manifest page level so it needs to be
  109. // removed from there as well in that case.
  110. if (root->RemoveObject(m_sceneManifest->GetValue(index)) && m_sceneManifest->RemoveEntry(m_target))
  111. {
  112. m_target = nullptr;
  113. // Hide and disable the button so when users spam the delete button only a single click is recorded.
  114. ui->m_deleteButton->hide();
  115. ui->m_deleteButton->setEnabled(false);
  116. for (auto* toRemove : otherObjectsToRemove)
  117. {
  118. index = m_sceneManifest->FindIndex(toRemove);
  119. root->RemoveObject(m_sceneManifest->GetValue(index));
  120. m_sceneManifest->RemoveEntry(toRemove);
  121. }
  122. return;
  123. }
  124. else
  125. {
  126. AZ_TracePrintf(Utilities::LogWindow, "Unable to delete manifest object from manifest.");
  127. }
  128. }
  129. }
  130. QObject* widget = this->parent();
  131. while (widget != nullptr)
  132. {
  133. ManifestVectorWidget* manifestVectorWidget = qobject_cast<ManifestVectorWidget*>(widget);
  134. if (manifestVectorWidget)
  135. {
  136. if (manifestVectorWidget->RemoveManifestObject(m_target))
  137. {
  138. m_target = nullptr;
  139. // Hide and disable the button so when users spam the delete button only a single click is recorded.
  140. ui->m_deleteButton->hide();
  141. ui->m_deleteButton->setEnabled(false);
  142. }
  143. else
  144. {
  145. AZ_TracePrintf(Utilities::WarningWindow, "Parent collection did not contain this ManifestObject");
  146. }
  147. return;
  148. }
  149. widget = widget->parent();
  150. }
  151. AZ_TracePrintf(Utilities::ErrorWindow, "No parent valid parent collection found.");
  152. }
  153. void HeaderWidget::UpdateDeletable()
  154. {
  155. ui->m_deleteButton->hide();
  156. // If this widget has the unmodifiable rule, then this can't be deleted.
  157. // Even though the delete button would be disabled, it's even more clear it can't be deleted if it's not visible.
  158. if (m_target->RTTI_IsTypeOf(DataTypes::IGroup::TYPEINFO_Uuid()))
  159. {
  160. const DataTypes::IGroup* sceneNodeGroup = azrtti_cast<const DataTypes::IGroup*>(m_target);
  161. const Containers::RuleContainer& rules = sceneNodeGroup->GetRuleContainerConst();
  162. if (rules.FindFirstByType<AZ::SceneAPI::DataTypes::IUnmodifiableRule>())
  163. {
  164. // This header is unmodifiable, so leave the delete button hidden.
  165. return;
  166. }
  167. }
  168. if (m_sceneManifest)
  169. {
  170. Containers::SceneManifest::Index index = m_sceneManifest->FindIndex(m_target);
  171. if (index != Containers::SceneManifest::s_invalidIndex)
  172. {
  173. ui->m_deleteButton->show();
  174. return;
  175. }
  176. }
  177. QObject* widget = this->parent();
  178. while(widget != nullptr)
  179. {
  180. ManifestVectorWidget* manifestVectorWidget = qobject_cast<ManifestVectorWidget*>(widget);
  181. if (manifestVectorWidget && manifestVectorWidget->ContainsManifestObject(m_target))
  182. {
  183. ui->m_deleteButton->show();
  184. break;
  185. }
  186. widget = widget->parent();
  187. }
  188. }
  189. const char* HeaderWidget::GetSerializedName(const DataTypes::IManifestObject* target) const
  190. {
  191. SerializeContext* context = nullptr;
  192. EBUS_EVENT_RESULT(context, ComponentApplicationBus, GetSerializeContext);
  193. if (context)
  194. {
  195. const SerializeContext::ClassData* classData = context->FindClassData(target->RTTI_GetType());
  196. if (classData)
  197. {
  198. if (classData->m_editData)
  199. {
  200. return classData->m_editData->m_name;
  201. }
  202. return classData->m_name;
  203. }
  204. }
  205. return "<type not registered>";
  206. }
  207. void HeaderWidget::UpdateUIForManifestObject(const DataTypes::IManifestObject* target)
  208. {
  209. if (!target)
  210. {
  211. return;
  212. }
  213. const AZ::Edit::ElementData* editorElementData = nullptr;
  214. const DataTypes::IGroup* sceneNodeGroup = nullptr;
  215. // Retrieve information from the edit context to figure out if this HeaderWidget
  216. // show have a visual divider, and potentially the icon.
  217. if (target->RTTI_IsTypeOf(DataTypes::IGroup::TYPEINFO_Uuid()))
  218. {
  219. sceneNodeGroup = azrtti_cast<const DataTypes::IGroup*>(target);
  220. AZ::SerializeContext* serializeContext = nullptr;
  221. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  222. AZ_Assert(serializeContext, "No serialize context");
  223. auto classData = serializeContext->FindClassData(target->RTTI_GetType());
  224. if (classData && classData->m_editData)
  225. {
  226. editorElementData = classData->m_editData->FindElementData(AZ::Edit::ClassElements::EditorData);
  227. if (auto categoryAttribute = editorElementData->FindAttribute(AZ::Edit::Attributes::CategoryStyle))
  228. {
  229. if (auto categoryAttributeData = azdynamic_cast<const AZ::Edit::AttributeData<const char*>*>(categoryAttribute))
  230. {
  231. AZStd::string categoryAttributeValue = categoryAttributeData->Get(&sceneNodeGroup);
  232. if (categoryAttributeValue.compare("display divider") == 0)
  233. {
  234. setStyleSheet("QFrame, QLabel {margin-top: 0px; font: bold;}");
  235. }
  236. }
  237. }
  238. }
  239. }
  240. // First, see if there's an icon registered on the ManifestMetaInfoBus.
  241. AZStd::string iconPath;
  242. EBUS_EVENT(Events::ManifestMetaInfoBus, GetIconPath, iconPath, *target);
  243. // If there isn't, then attempt to retrieve it from the edit context.
  244. if (iconPath.empty() && editorElementData)
  245. {
  246. // Search the edit context for an icon.
  247. // It will have been reflected like:
  248. // ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/MeshCollider.svg")
  249. if (auto iconAttribute = editorElementData->FindAttribute(AZ::Edit::Attributes::Icon))
  250. {
  251. if (auto iconAttributeData = azdynamic_cast<const AZ::Edit::AttributeData<const char*>*>(iconAttribute))
  252. {
  253. AZStd::string iconAttributeValue = iconAttributeData->Get(&sceneNodeGroup);
  254. if (!iconAttributeValue.empty())
  255. {
  256. iconPath = AZStd::move(iconAttributeValue);
  257. // The path is probably going to be relative to a scan directory,
  258. // especially if this node was defined in a Gem.
  259. // If a first check doesn't find the file, then see if the path can
  260. // be resolved via the asset system, and pull an absolute path from there.
  261. if (!QFile::exists(QString(iconPath.c_str())))
  262. {
  263. AZStd::string iconFullPath;
  264. bool pathFound = false;
  265. using AssetSysReqBus = AzToolsFramework::AssetSystemRequestBus;
  266. AssetSysReqBus::BroadcastResult(
  267. pathFound, &AssetSysReqBus::Events::GetFullSourcePathFromRelativeProductPath,
  268. iconPath, iconFullPath);
  269. if (pathFound)
  270. {
  271. iconPath = AZStd::move(iconFullPath);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. if (iconPath.empty())
  279. {
  280. ui->m_icon->hide();
  281. }
  282. else
  283. {
  284. ui->m_icon->setPixmap(QIcon(iconPath.c_str()).pixmap(QSize(ui->m_icon->size())));
  285. ui->m_icon->show();
  286. }
  287. }
  288. } // UI
  289. } // SceneAPI
  290. } // AZ
  291. #include <RowWidgets/moc_HeaderWidget.cpp>