Files
o3de/Code/Framework/AzQtComponents/AzQtComponents/Components/DockBarButton.cpp
T
2021-03-08 14:30:57 -08:00

204 lines
7.6 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzQtComponents/Components/DockBarButton.h>
#include <AzQtComponents/Components/ConfigHelpers.h>
#include <AzQtComponents/Components/Widgets/TabWidget.h>
#include <AzQtComponents/Components/Style.h>
#include <QApplication>
#include <QStyleOptionToolButton>
#include <QStylePainter>
namespace AzQtComponents
{
DockBarButton::Config DockBarButton::loadConfig(QSettings& settings)
{
Config config = defaultConfig();
ConfigHelpers::read<int>(settings, QStringLiteral("ButtonIconSize"), config.buttonIconSize);
ConfigHelpers::read<int>(settings, QStringLiteral("DefaultButtonMargin"), config.defaultButtonMargin);
ConfigHelpers::read<int>(settings, QStringLiteral("MenuIndicatorWidth"), config.menuIndicatorWidth);
ConfigHelpers::read<QString>(settings, QStringLiteral("MenuIndicatorIcon"), config.menuIndicatorIcon);
ConfigHelpers::read<QSize>(settings, QStringLiteral("MenuIndicatorIconSize"), config.menuIndicatorIconSize);
ConfigHelpers::read<QColor>(settings, QStringLiteral("HoverBackgroundColor"), config.hoverBackgroundColor);
ConfigHelpers::read<QColor>(settings, QStringLiteral("CloseHoverBackgroundColor"), config.closeHoverBackgroundColor);
ConfigHelpers::read<QColor>(settings, QStringLiteral("SelectedBackgroundColor"), config.selectedBackgroundColor);
ConfigHelpers::read<QColor>(settings, QStringLiteral("CloseSelectedBackgroundColor"), config.closeSelectedBackgroundColor);
return config;
}
DockBarButton::Config DockBarButton::defaultConfig()
{
Config config;
config.buttonIconSize = 16;
config.defaultButtonMargin = 1;
config.menuIndicatorWidth = 10;
config.menuIndicatorIcon = QStringLiteral(":/stylesheet/img/UI20/menu-indicator.svg");
config.menuIndicatorIconSize = QSize(6, 3);
config.hoverBackgroundColor = QStringLiteral("#11FFFFFF");
config.closeHoverBackgroundColor = QStringLiteral("#B80D1C");
config.selectedBackgroundColor = QStringLiteral("#1A1A1A");
config.closeSelectedBackgroundColor = QStringLiteral("#850914");
return config;
}
/**
* Create a dock bar button that can be shared between any kind of docking bars for common actions
*/
DockBarButton::DockBarButton(DockBarButton::WindowDecorationButton buttonType, QWidget* parent, bool darkStyle)
: QToolButton(parent)
, m_buttonType(buttonType)
, m_isDarkStyle(darkStyle)
{
Style::addClass(this, NoMargins);
switch (m_buttonType)
{
case DockBarButton::CloseButton:
Style::addClass(this, QStringLiteral("close"));
break;
case DockBarButton::MaximizeButton:
Style::addClass(this, QStringLiteral("maximize"));
break;
case DockBarButton::MinimizeButton:
Style::addClass(this, QStringLiteral("minimize"));
break;
case DockBarButton::DividerButton:
break;
}
if (m_isDarkStyle)
{
Style::addClass(this, QStringLiteral("dark"));
}
// Handle when our button is clicked
QObject::connect(this, &QToolButton::clicked, this, &DockBarButton::handleButtonClick);
// Our dock bar buttons only need click focus, they don't need to accept
// focus by tabbing
setFocusPolicy(Qt::ClickFocus);
}
void DockBarButton::paintEvent(QPaintEvent *)
{
QStylePainter p(this);
QStyleOptionToolButton option;
initStyleOption(&option);
// Set the icon based on m_buttonType. This allows the icon to be changed in a QStyle, or in
// a Qt Style Sheet by changing the titlebar-close-icon, titlebar-maximize-icon, and
// titlebar-minimize-icon properties.
// Used in combination with the close, maximize, minimize and dark classes set in the
// constructor, and :hover and :pressed selectors available to buttons, we have full control
// of the pixmap in the style sheet.
switch (m_buttonType)
{
case DockBarButton::CloseButton:
option.icon = style()->standardIcon(QStyle::SP_TitleBarCloseButton, &option, this);
break;
case DockBarButton::MaximizeButton:
option.icon = style()->standardIcon(QStyle::SP_TitleBarMaxButton, &option, this);
break;
case DockBarButton::MinimizeButton:
option.icon = style()->standardIcon(QStyle::SP_TitleBarMinButton, &option, this);
break;
default:
break;
}
p.drawComplexControl(QStyle::CC_ToolButton, option);
}
/**
* Handle our button clicks by emitting a signal with our button type
*/
void DockBarButton::handleButtonClick()
{
if (!window())
{
return;
}
emit buttonPressed(m_buttonType);
}
bool DockBarButton::drawDockBarButton(const Style* style, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget, const Config& config)
{
auto dockBarButton = qobject_cast<const DockBarButton*>(widget);
auto buttonOption = qstyleoption_cast<const QStyleOptionToolButton*>(option);
if (!dockBarButton || !buttonOption)
{
return false;
}
QRect buttonRect = style->subControlRect(QStyle::CC_ToolButton, option, QStyle::SC_ToolButton, widget);
QRect menuRect = style->subControlRect(QStyle::CC_ToolButton, option, QStyle::SC_ToolButtonMenu, widget);
painter->save();
QStyleOptionToolButton label = *buttonOption;
// Do not draw hover rect if the button is used in a Tab
auto tabBarParent = qobject_cast<const AzQtComponents::TabBar*>(widget->parent());
bool mouseOver = buttonOption->state & QStyle::State_MouseOver;
Qt::MouseButtons mouseButtons = QApplication::mouseButtons();
bool mouseDown = mouseButtons & Qt::LeftButton;
if (!tabBarParent && mouseOver)
{
painter->setPen(Qt::NoPen);
if (dockBarButton->m_buttonType == DockBarButton::CloseButton)
{
if (mouseDown)
{
painter->setBrush(config.closeSelectedBackgroundColor);
}
else
{
painter->setBrush(config.closeHoverBackgroundColor);
}
}
else
{
if (mouseDown)
{
painter->setBrush(config.selectedBackgroundColor);
}
else
{
painter->setBrush(config.hoverBackgroundColor);
}
}
const QRect highlightRect = buttonOption->rect;
painter->drawRect(highlightRect);
}
label.rect = buttonRect;
style->drawControl(QStyle::CE_ToolButtonLabel, &label, painter, widget);
painter->restore();
return true;
}
} // namespace AzQtComponents
#include "Components/moc_DockBarButton.cpp"