From 50015f85999d3c175e8931c3977ab1b0cc6b6a15 Mon Sep 17 00:00:00 2001 From: Danilo Aimini <82231674+AMZN-daimini@users.noreply.github.com> Date: Tue, 25 Jan 2022 12:00:30 -0800 Subject: [PATCH] Add icon support for the Breadcrumbs UI Component, use it in the Prefab Focus Mode breadcrumbs. (#7136) * Add icon support for the Breadcrumbs UI Component, use it in the prefab focus mode breadcrumb. Signed-off-by: Danilo Aimini <82231674+AMZN-daimini@users.noreply.github.com> * Simplified iconAt logic Signed-off-by: Danilo Aimini <82231674+AMZN-daimini@users.noreply.github.com> --- .../Components/Widgets/BreadCrumbs.cpp | 65 +++++++++++++++++-- .../Components/Widgets/BreadCrumbs.h | 16 +++++ .../Prefab/PrefabViewportFocusPathHandler.cpp | 6 ++ 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.cpp index 7ea06c0632..fae6340cec 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.cpp @@ -12,13 +12,12 @@ AZ_PUSH_DISABLE_WARNING(4244 4251, "-Wunknown-warning-option") // 4251: 'QLayoutItem::align': class 'QFlags' needs to have dll-interface to be used by clients of class 'QLayoutItem' #include -#include -#include #include -#include -#include #include #include +#include +#include +#include AZ_POP_DISABLE_WARNING namespace AzQtComponents @@ -46,7 +45,7 @@ namespace AzQtComponents m_label = new QLabel(this); m_label->setObjectName(g_labelName); boxLayout->addWidget(m_label); - m_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); + m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); connect(m_label, &QLabel::linkActivated, this, &BreadCrumbs::onLinkActivated); } @@ -82,9 +81,38 @@ namespace AzQtComponents // clean up the path to use all the first separator in the list of separators m_currentPath.replace(g_windowsSeparator, g_separator); + // update internals + m_currentPathSize = m_currentPath.split(g_separator, Qt::SkipEmptyParts).size(); + m_currentPathIcons.resize(m_currentPathSize); + fillLabel(); } + void BreadCrumbs::setDefaultIcon(const QString& icon) + { + m_defaultIcon = icon; + } + + void BreadCrumbs::setIconAt(int index, const QString& icon) + { + if (index < 0 || index >= m_currentPathSize) + { + return; + } + + m_currentPathIcons[index] = icon; + } + + QIcon BreadCrumbs::iconAt(int index) + { + if (index < 0 || index >= m_currentPathSize || m_currentPathIcons[index].isNull()) + { + return QIcon(m_defaultIcon); + } + + return QIcon(m_currentPathIcons[index]); + } + bool BreadCrumbs::getPushPathOnLinkActivation() const { return m_pushPathOnLinkActivation; @@ -220,6 +248,26 @@ namespace AzQtComponents QWidget::resizeEvent(event); } + QString BreadCrumbs::generateIconHtml(int index) + { + QString imagePath; + + if (index >= 0 && index < m_currentPathIcons.size()) + { + imagePath = m_currentPathIcons[index]; + } + + if (imagePath.isEmpty()) + { + imagePath = m_defaultIcon; + } + + return !imagePath.isEmpty() ? QStringLiteral("%2%2") + .arg(imagePath) + .arg(" ") + : ""; + } + void BreadCrumbs::fillLabel() { QString htmlString = ""; @@ -247,7 +295,10 @@ namespace AzQtComponents plainTextPath = m_truncatedPaths.takeLast(); } - htmlString.prepend(plainTextPath); + int index = m_currentPathSize - 1; + + htmlString.prepend(generateIconHtml(index) + plainTextPath); + --index; while (!m_truncatedPaths.isEmpty()) { @@ -262,7 +313,7 @@ namespace AzQtComponents const QString linkPath = buildPathFromList(fullPath, m_truncatedPaths.size()); const QString& part = m_truncatedPaths.takeLast(); - htmlString.prepend(QString("%1").arg(formatLink(linkPath, part))); + htmlString.prepend(QString("%1%2").arg(generateIconHtml(index--)).arg(formatLink(linkPath, part))); } m_label->setText(htmlString); diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.h b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.h index 4ecbb59647..14391883d5 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.h +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/BreadCrumbs.h @@ -78,6 +78,13 @@ namespace AzQtComponents //! Sets the current breadcrumb path without updating the navigation stack. void setCurrentPath(const QString& newPath); + //! Sets a default icon for path elements. + void setDefaultIcon(const QString& iconPath); + //! Sets an icon for the path element at index. + void setIconAt(int index, const QString& iconPath); + //! Gets the icon for the path element at index. + QIcon iconAt(int index); + //! Returns true if activating a link should automatically push a new path to the navigation stack. bool getPushPathOnLinkActivation() const; //! Sets whether activating a link should automatically push a new path to the navigation stack. @@ -134,6 +141,7 @@ namespace AzQtComponents void onLinkActivated(const QString& link); private: + QString generateIconHtml(int index); void fillLabel(); void changePath(const QString& newPath); @@ -155,6 +163,14 @@ namespace AzQtComponents QStringList m_truncatedPaths; AZ_POP_DISABLE_WARNING bool m_pushPathOnLinkActivation = true; + int m_currentPathSize = 0; + + QString m_defaultIcon; + AZ_PUSH_DISABLE_WARNING( + 4251, "-Wunknown-warning-option") // 4251: 'AzQtComponents::BreadCrumbs::m_currentPathIcons': class 'QVector' needs to have + // dll-interface to be used by clients of class 'AzQtComponents::BreadCrumbs' + QVector m_currentPathIcons; + AZ_POP_DISABLE_WARNING friend class Style; diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabViewportFocusPathHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabViewportFocusPathHandler.cpp index 275079371a..5cff5127df 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabViewportFocusPathHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Prefab/PrefabViewportFocusPathHandler.cpp @@ -44,6 +44,9 @@ namespace AzToolsFramework::Prefab m_breadcrumbsWidget = breadcrumbsWidget; m_backButton = backButton; + // Add icons to the widget + m_breadcrumbsWidget->setDefaultIcon(QString(":/Entity/prefab_edit.svg")); + // If a part of the path is clicked, focus on that instance connect(m_breadcrumbsWidget, &AzQtComponents::BreadCrumbs::linkClicked, this, [&](const QString&, int linkIndex) @@ -74,6 +77,9 @@ namespace AzToolsFramework::Prefab // Push new Path m_breadcrumbsWidget->pushPath(m_prefabFocusPublicInterface->GetPrefabFocusPath(m_editorEntityContextId).c_str()); + // Set root icon + m_breadcrumbsWidget->setIconAt(0, QString(":/Level/level.svg")); + // If root instance is focused, disable the back button; else enable it. m_backButton->setEnabled(m_prefabFocusPublicInterface->GetPrefabFocusPathLength(m_editorEntityContextId) > 1); }