Added function internal::setButtonIcon to unify code for setting DockAreaTitleBar and DockWidgetTab icons

This commit is contained in:
Uwe Kindler 2020-02-05 08:57:57 +01:00
parent acb423872a
commit 6a8b26f415
4 changed files with 65 additions and 50 deletions

View File

@ -101,34 +101,6 @@ struct DockAreaTitleBarPrivate
{ {
return CDockManager::configFlags().testFlag(Flag); return CDockManager::configFlags().testFlag(Flag);
} }
/**
* Helper function to set title bar button icons depending on operating
* system and to avoid duplicated code. On windows the standard icons
* are blurry since Qt 5.11 so we need to do some additional steps.
* If the global IconPovider of the dockmanager provides a custom
* Icon for the given CustomIconId, the this icon will be used.
*/
void setTitleBarButtonIcon(tTitleBarButton* Button, QStyle::StandardPixmap StandarPixmap,
ads::eIcon CustomIconId)
{
// First we try to use custom icons if available
QIcon Icon = CDockManager::iconProvider().customIcon(CustomIconId);
if (!Icon.isNull())
{
Button->setIcon(Icon);
return;
}
#ifdef Q_OS_LINUX
Button->setIcon(_this->style()->standardIcon(StandarPixmap));
#else
QPixmap normalPixmap = _this->style()->standardPixmap(StandarPixmap, 0, Button);
Icon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
Icon.addPixmap(normalPixmap, QIcon::Normal);
Button->setIcon(Icon);
#endif
}
};// struct DockAreaTitleBarPrivate };// struct DockAreaTitleBarPrivate
@ -143,19 +115,19 @@ class CTitleBarButton : public tTitleBarButton
bool Visible = true; bool Visible = true;
bool HideWhenDisabled = false; bool HideWhenDisabled = false;
public: public:
using Super = tTitleBarButton;
CTitleBarButton(bool visible = true, QWidget* parent = nullptr) CTitleBarButton(bool visible = true, QWidget* parent = nullptr)
: tTitleBarButton(parent) : tTitleBarButton(parent),
, Visible(visible) Visible(visible),
, HideWhenDisabled(DockAreaTitleBarPrivate::testConfigFlag(CDockManager::DockAreaHideDisabledButtons)) HideWhenDisabled(DockAreaTitleBarPrivate::testConfigFlag(CDockManager::DockAreaHideDisabledButtons))
{ {}
//this->setVisible(Visible); // this causes flickering and seems not to be needed TODO: investigate further and in case it IS needed fix flickering
}
/**
* Adjust this visibility change request with our internal settings:
*/
virtual void setVisible(bool visible) override virtual void setVisible(bool visible) override
{ {
// Adjust this visibility change request with our internal settings:
// 'visible' can stay 'true' if and only if this button is configured to generaly visible: // 'visible' can stay 'true' if and only if this button is configured to generaly visible:
visible = visible && this->Visible; visible = visible && this->Visible;
@ -165,10 +137,13 @@ public:
visible = isEnabled(); visible = isEnabled();
} }
tTitleBarButton::setVisible(visible); Super::setVisible(visible);
} }
protected: protected:
/**
* Handle EnabledChanged signal to set button invisible if the configured
*/
bool event(QEvent *ev) override bool event(QEvent *ev) override
{ {
if(QEvent::EnabledChange == ev->type() && HideWhenDisabled) if(QEvent::EnabledChange == ev->type() && HideWhenDisabled)
@ -177,7 +152,7 @@ protected:
setVisible(isEnabled()); setVisible(isEnabled());
} }
return tTitleBarButton::event(ev);; return Super::event(ev);;
} }
}; };
@ -200,7 +175,7 @@ void DockAreaTitleBarPrivate::createButtons()
TabsMenuButton->setObjectName("tabsMenuButton"); TabsMenuButton->setObjectName("tabsMenuButton");
TabsMenuButton->setAutoRaise(true); TabsMenuButton->setAutoRaise(true);
TabsMenuButton->setPopupMode(QToolButton::InstantPopup); TabsMenuButton->setPopupMode(QToolButton::InstantPopup);
setTitleBarButtonIcon(TabsMenuButton, QStyle::SP_TitleBarUnshadeButton, ads::DockAreaMenuIcon); internal::setButtonIcon(TabsMenuButton, QStyle::SP_TitleBarUnshadeButton, ads::DockAreaMenuIcon);
QMenu* TabsMenu = new QMenu(TabsMenuButton); QMenu* TabsMenu = new QMenu(TabsMenuButton);
#ifndef QT_NO_TOOLTIP #ifndef QT_NO_TOOLTIP
TabsMenu->setToolTipsVisible(true); TabsMenu->setToolTipsVisible(true);
@ -219,7 +194,7 @@ void DockAreaTitleBarPrivate::createButtons()
UndockButton->setObjectName("undockButton"); UndockButton->setObjectName("undockButton");
UndockButton->setAutoRaise(true); UndockButton->setAutoRaise(true);
internal::setToolTip(UndockButton, QObject::tr("Detach Group")); internal::setToolTip(UndockButton, QObject::tr("Detach Group"));
setTitleBarButtonIcon(UndockButton, QStyle::SP_TitleBarNormalButton, ads::DockAreaUndockIcon); internal::setButtonIcon(UndockButton, QStyle::SP_TitleBarNormalButton, ads::DockAreaUndockIcon);
UndockButton->setSizePolicy(ButtonSizePolicy); UndockButton->setSizePolicy(ButtonSizePolicy);
TopLayout->addWidget(UndockButton, 0); TopLayout->addWidget(UndockButton, 0);
_this->connect(UndockButton, SIGNAL(clicked()), SLOT(onUndockButtonClicked())); _this->connect(UndockButton, SIGNAL(clicked()), SLOT(onUndockButtonClicked()));
@ -228,7 +203,7 @@ void DockAreaTitleBarPrivate::createButtons()
CloseButton = new CTitleBarButton(testConfigFlag(CDockManager::DockAreaHasCloseButton)); CloseButton = new CTitleBarButton(testConfigFlag(CDockManager::DockAreaHasCloseButton));
CloseButton->setObjectName("closeButton"); CloseButton->setObjectName("closeButton");
CloseButton->setAutoRaise(true); CloseButton->setAutoRaise(true);
setTitleBarButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, ads::DockAreaCloseIcon); internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, ads::DockAreaCloseIcon);
if (testConfigFlag(CDockManager::DockAreaCloseButtonClosesTab)) if (testConfigFlag(CDockManager::DockAreaCloseButtonClosesTab))
{ {
internal::setToolTip(CloseButton, QObject::tr("Close Active Tab")); internal::setToolTip(CloseButton, QObject::tr("Close Active Tab"));

View File

@ -173,15 +173,7 @@ void DockWidgetTabPrivate::createLayout()
CloseButton = createCloseButton(); CloseButton = createCloseButton();
CloseButton->setObjectName("tabCloseButton"); CloseButton->setObjectName("tabCloseButton");
QIcon CloseIcon = CDockManager::iconProvider().customIcon(TabCloseIcon); internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon);
if (CloseIcon.isNull())
{
// The standard icons do does not look good on high DPI screens
QPixmap normalPixmap = _this->style()->standardPixmap(QStyle::SP_TitleBarCloseButton, 0, CloseButton);
CloseIcon.addPixmap(normalPixmap, QIcon::Normal);
CloseIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
}
CloseButton->setIcon(CloseIcon);
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
_this->onDockWidgetFeaturesChanged(); _this->onDockWidgetFeaturesChanged();
internal::setToolTip(CloseButton, QObject::tr("Close Tab")); internal::setToolTip(CloseButton, QObject::tr("Close Tab"));

View File

@ -30,8 +30,11 @@
//============================================================================ //============================================================================
#include <QVariant> #include <QVariant>
#include <QPainter> #include <QPainter>
#include <QAbstractButton>
#include "DockSplitter.h" #include "DockSplitter.h"
#include "DockManager.h"
#include "IconProvider.h"
#include "ads_globals.h" #include "ads_globals.h"
@ -90,6 +93,31 @@ void hideEmptyParentSplitters(CDockSplitter* Splitter)
} }
} }
//============================================================================
void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandarPixmap,
ads::eIcon CustomIconId)
{
// First we try to use custom icons if available
QIcon Icon = CDockManager::iconProvider().customIcon(CustomIconId);
if (!Icon.isNull())
{
Button->setIcon(Icon);
return;
}
#ifdef Q_OS_LINUX
Button->setIcon(Button->style()->standardIcon(StandarPixmap));
#else
// The standard icons does not look good on high DPI screens so we create
// our own "standard" icon here.
QPixmap normalPixmap = Button->style()->standardPixmap(StandarPixmap, 0, Button);
Icon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
Icon.addPixmap(normalPixmap, QIcon::Normal);
Button->setIcon(Icon);
#endif
}
} // namespace internal } // namespace internal
} // namespace ads } // namespace ads

View File

@ -35,6 +35,9 @@
#include <QPixmap> #include <QPixmap>
#include <QWidget> #include <QWidget>
#include <QDebug> #include <QDebug>
#include <QStyle>
class QAbstractButton;
#ifndef ADS_STATIC #ifndef ADS_STATIC
#ifdef ADS_SHARED_EXPORT #ifdef ADS_SHARED_EXPORT
@ -231,6 +234,23 @@ void setToolTip(QObjectPtr obj, const QString &tip)
#endif #endif
} }
/**
* Helper function to set the icon of a certain button.
* Use this function to set the icons for the dock area and dock widget buttons.
* The function first uses the CustomIconId to get an icon from the
* CIconProvider. You can register your custom icons with the icon provider, if
* you do not want to use the default buttons and if you do not want to use
* stylesheets.
* If the IconProvider does not return a valid icon (icon is null), the function
* fetches the given standard pixmap from the QStyle.
* param[in] Button The button whose icons are to be set
* param[in] StandardPixmap The standard pixmap to be used for the button
* param[in] CustomIconId The identifier for the custom icon.
*/
void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandarPixmap,
ads::eIcon CustomIconId);
} // namespace internal } // namespace internal
} // namespace ads } // namespace ads