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>
This commit is contained in:
Danilo Aimini
2022-01-25 12:00:30 -08:00
committed by GitHub
parent aab3d8df67
commit 50015f8599
3 changed files with 80 additions and 7 deletions
@@ -12,13 +12,12 @@
AZ_PUSH_DISABLE_WARNING(4244 4251, "-Wunknown-warning-option") // 4251: 'QLayoutItem::align': class 'QFlags<Qt::AlignmentFlag>' needs to have dll-interface to be used by clients of class 'QLayoutItem'
#include <QHBoxLayout>
#include <QToolButton>
#include <QToolBar>
#include <QLabel>
#include <QHBoxLayout>
#include <QSettings>
#include <QMenu>
#include <QResizeEvent>
#include <QSettings>
#include <QToolBar>
#include <QToolButton>
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("<img width=\"16\" height=\"16\" style=\"vertical-align: middle\" src=\"%1\">%2%2")
.arg(imagePath)
.arg("&nbsp;")
: "";
}
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);
@@ -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<QIcon>' needs to have
// dll-interface to be used by clients of class 'AzQtComponents::BreadCrumbs'
QVector<QString> m_currentPathIcons;
AZ_POP_DISABLE_WARNING
friend class Style;
@@ -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);
}