From 1c3a61983acf001cf0380c8dcd92eb5244475d17 Mon Sep 17 00:00:00 2001 From: Danilo Aimini <82231674+AMZN-daimini@users.noreply.github.com> Date: Wed, 9 Feb 2022 20:50:32 -0800 Subject: [PATCH] Refactor EditorEntityUiHandlerBaseto be explicitly Outliner-focused. This lays the groundwork for multiple widget-based handlers in the future. (#7443) Signed-off-by: Danilo Aimini <82231674+AMZN-daimini@users.noreply.github.com> --- .../EditorEntityUiHandlerBase.cpp | 10 +++++++++- .../EditorEntityUiHandlerBase.h | 20 +++++++++++-------- .../UI/Outliner/EntityOutlinerWidget.cpp | 2 +- .../UI/Prefab/LevelRootUiHandler.cpp | 13 +++++++----- .../UI/Prefab/LevelRootUiHandler.h | 2 +- .../UI/Prefab/PrefabUiHandler.cpp | 14 +++++++------ .../UI/Prefab/PrefabUiHandler.h | 2 +- 7 files changed, 40 insertions(+), 23 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.cpp index 866080a83f..4896a5af49 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.cpp @@ -7,6 +7,7 @@ */ #include +#include #include @@ -117,9 +118,16 @@ namespace AzToolsFramework { } - bool EditorEntityUiHandlerBase::OnEntityDoubleClick([[maybe_unused]] AZ::EntityId entityId) const + bool EditorEntityUiHandlerBase::OnOutlinerItemDoubleClick([[maybe_unused]] const QModelIndex& index) const { return false; } + AZ::EntityId EditorEntityUiHandlerBase::GetEntityIdFromIndex(const QModelIndex& index) + { + QModelIndex firstColumnIndex = index.siblingAtColumn(EntityOutlinerListModel::ColumnName); + + return AZ::EntityId(firstColumnIndex.data(EntityOutlinerListModel::EntityIdRole).value()); + } + } // namespace AzToolsFramework diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.h b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.h index 96d393efa1..948c26d710 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/EditorEntityUi/EditorEntityUiHandlerBase.h @@ -21,7 +21,6 @@ class QTreeView; namespace AzToolsFramework { //! Defines a handler that can customize entity UI appearance and behavior in the Entity Outliner. - //! This class is meant to be abstract, entities do not have a handler by default. class EditorEntityUiHandlerBase { protected: @@ -33,7 +32,7 @@ namespace AzToolsFramework public: EditorEntityUiHandlerId GetHandlerId(); - // # Entity Outliner + // # Entity Outliner Item //! Returns the item info string that is appended to the item name in the Outliner. virtual QString GenerateItemInfoString(AZ::EntityId entityId) const; @@ -41,10 +40,12 @@ namespace AzToolsFramework virtual QString GenerateItemTooltip(AZ::EntityId entityId) const; //! Returns the item icon pixmap to display in the Outliner. virtual QIcon GenerateItemIcon(AZ::EntityId entityId) const; - //! Returns whether the element's lock and visibility state should be accessible in the Outliner - virtual bool CanToggleLockVisibility(AZ::EntityId entityId) const; //! Returns whether the element's name should be editable virtual bool CanRename(AZ::EntityId entityId) const; + //! Returns whether the element's lock and visibility state should be accessible in the Outliner + virtual bool CanToggleLockVisibility(AZ::EntityId entityId) const; + + // Qt-specific painting functions //! Paints the background of the item in the Outliner. virtual void PaintItemBackground(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; @@ -54,24 +55,27 @@ namespace AzToolsFramework //! Paints the background of the descendant branches of the item in the Outliner. virtual void PaintDescendantBranchBackground(QPainter* painter, const QTreeView* view, const QRect& rect, const QModelIndex& index, const QModelIndex& descendantIndex) const; - //! Paints visual elements on the foreground of the item in the Outliner. virtual void PaintItemForeground(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; //! Paints visual elements on the foreground of the descendants of the item in the Outliner. virtual void PaintDescendantForeground(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, const QModelIndex& descendantIndex) const; + // Outliner-specific interactions + //! Triggered when the entity is clicked in the Outliner. //! @return True if the click has been handled and should not be propagated, false otherwise. virtual bool OnOutlinerItemClick(const QPoint& position, const QStyleOptionViewItem& option, const QModelIndex& index) const; + //! Triggered when the entity is double-clicked in the Outliner. + //! @return True if the double-click has been handled and should not be propagated, false otherwise. + virtual bool OnOutlinerItemDoubleClick(const QModelIndex& index) const; //! Triggered when an entity's children are expanded in the Outliner. virtual void OnOutlinerItemExpand(const QModelIndex& index) const; //! Triggered when an entity's children are collapsed in the Outliner. virtual void OnOutlinerItemCollapse(const QModelIndex& index) const; - //! Triggered when the entity is double clicked in the Outliner or in the Viewport. - //! @return True if the double click has been handled and should not be propagated, false otherwise. - virtual bool OnEntityDoubleClick(AZ::EntityId entityId) const; + protected: + static AZ::EntityId GetEntityIdFromIndex(const QModelIndex& index); private: EditorEntityUiHandlerId m_handlerId = 0; diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerWidget.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerWidget.cpp index 52b688543a..f4855cb715 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerWidget.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerWidget.cpp @@ -945,7 +945,7 @@ namespace AzToolsFramework { if (AZ::EntityId entityId = GetEntityIdFromIndex(index); auto entityUiHandler = m_editorEntityUiInterface->GetHandler(entityId)) { - entityUiHandler->OnEntityDoubleClick(entityId); + entityUiHandler->OnOutlinerItemDoubleClick(index); } } diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.cpp index b34bbff298..9c5c478a2f 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.cpp @@ -33,7 +33,7 @@ namespace AzToolsFramework } } - QIcon LevelRootUiHandler::GenerateItemIcon(AZ::EntityId /*entityId*/) const + QIcon LevelRootUiHandler::GenerateItemIcon([[maybe_unused]] AZ::EntityId entityId) const { return QIcon(m_levelRootIconPath); } @@ -62,17 +62,18 @@ namespace AzToolsFramework return infoString; } - bool LevelRootUiHandler::CanToggleLockVisibility(AZ::EntityId /*entityId*/) const + bool LevelRootUiHandler::CanToggleLockVisibility([[maybe_unused]] AZ::EntityId entityId) const { return false; } - bool LevelRootUiHandler::CanRename(AZ::EntityId /*entityId*/) const + bool LevelRootUiHandler::CanRename([[maybe_unused]] AZ::EntityId entityId) const { return false; } - void LevelRootUiHandler::PaintItemBackground(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const + void LevelRootUiHandler::PaintItemBackground( + QPainter* painter, const QStyleOptionViewItem& option, [[maybe_unused]] const QModelIndex& index) const { if (!painter) { @@ -94,8 +95,10 @@ namespace AzToolsFramework painter->restore(); } - bool LevelRootUiHandler::OnEntityDoubleClick(AZ::EntityId entityId) const + bool LevelRootUiHandler::OnOutlinerItemDoubleClick(const QModelIndex& index) const { + AZ::EntityId entityId = GetEntityIdFromIndex(index); + if (auto prefabFocusPublicInterface = AZ::Interface::Get(); !prefabFocusPublicInterface->IsOwningPrefabBeingFocused(entityId)) { diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.h b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.h index 3f7f56670e..a43aa0606a 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/LevelRootUiHandler.h @@ -33,7 +33,7 @@ namespace AzToolsFramework bool CanToggleLockVisibility(AZ::EntityId entityId) const override; bool CanRename(AZ::EntityId entityId) const override; void PaintItemBackground(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; - bool OnEntityDoubleClick(AZ::EntityId entityId) const override; + bool OnOutlinerItemDoubleClick(const QModelIndex& index) const override; private: Prefab::PrefabPublicInterface* m_prefabPublicInterface = nullptr; diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.cpp index 69d6ae82ca..f3579c223c 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.cpp @@ -99,7 +99,7 @@ namespace AzToolsFramework return; } - AZ::EntityId entityId(index.data(EntityOutlinerListModel::EntityIdRole).value()); + AZ::EntityId entityId = GetEntityIdFromIndex(index); const bool isFirstColumn = index.column() == EntityOutlinerListModel::ColumnName; const bool isLastColumn = index.column() == EntityOutlinerListModel::ColumnLockToggle; QModelIndex firstColumnIndex = index.siblingAtColumn(EntityOutlinerListModel::ColumnName); @@ -183,7 +183,7 @@ namespace AzToolsFramework return; } - AZ::EntityId entityId(index.data(EntityOutlinerListModel::EntityIdRole).value()); + AZ::EntityId entityId = GetEntityIdFromIndex(index); const QTreeView* outlinerTreeView(qobject_cast(option.widget)); const int ancestorLeft = outlinerTreeView->visualRect(index).left() + (m_prefabBorderThickness / 2) - 1; @@ -283,7 +283,7 @@ namespace AzToolsFramework void PrefabUiHandler::PaintItemForeground(QPainter* painter, const QStyleOptionViewItem& option, [[maybe_unused]] const QModelIndex& index) const { - AZ::EntityId entityId(index.data(EntityOutlinerListModel::EntityIdRole).value()); + AZ::EntityId entityId = GetEntityIdFromIndex(index); const QPoint offset = QPoint(-18, 3); QModelIndex firstColumnIndex = index.siblingAtColumn(EntityOutlinerListModel::ColumnName); const int iconSize = 16; @@ -385,7 +385,7 @@ namespace AzToolsFramework bool PrefabUiHandler::OnOutlinerItemClick(const QPoint& position, const QStyleOptionViewItem& option, const QModelIndex& index) const { - AZ::EntityId entityId(index.data(EntityOutlinerListModel::EntityIdRole).value()); + AZ::EntityId entityId = GetEntityIdFromIndex(index); const QPoint offset = QPoint(-18, 3); if (m_prefabFocusPublicInterface->IsOwningPrefabInFocusHierarchy(entityId)) @@ -411,7 +411,7 @@ namespace AzToolsFramework void PrefabUiHandler::OnOutlinerItemCollapse(const QModelIndex& index) const { - AZ::EntityId entityId(index.data(EntityOutlinerListModel::EntityIdRole).value()); + AZ::EntityId entityId = GetEntityIdFromIndex(index); if (m_prefabFocusPublicInterface->IsOwningPrefabBeingFocused(entityId)) { @@ -420,8 +420,10 @@ namespace AzToolsFramework } } - bool PrefabUiHandler::OnEntityDoubleClick(AZ::EntityId entityId) const + bool PrefabUiHandler::OnOutlinerItemDoubleClick(const QModelIndex& index) const { + AZ::EntityId entityId = GetEntityIdFromIndex(index); + if (!m_prefabFocusPublicInterface->IsOwningPrefabBeingFocused(entityId)) { // Focus on this prefab diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.h b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.h index a1c624f85a..7166629cf3 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabUiHandler.h @@ -43,8 +43,8 @@ namespace AzToolsFramework const QModelIndex& index, const QModelIndex& descendantIndex) const override; bool OnOutlinerItemClick(const QPoint& position, const QStyleOptionViewItem& option, const QModelIndex& index) const override; + bool OnOutlinerItemDoubleClick(const QModelIndex& index) const override; void OnOutlinerItemCollapse(const QModelIndex& index) const override; - bool OnEntityDoubleClick(AZ::EntityId entityId) const override; protected: Prefab::PrefabFocusPublicInterface* m_prefabFocusPublicInterface = nullptr;