1
0
mirror of https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System.git synced 2025-04-01 02:42:39 +08:00

Allow setting custom icon for auto hide vs dock.

This commit is contained in:
Cynthia Pang 2023-10-20 14:21:43 +08:00
parent 420baeedfe
commit 9d081593d0
8 changed files with 163 additions and 11 deletions

View File

@ -182,7 +182,7 @@ void DockAreaTitleBarPrivate::createButtons()
TabsMenuButton->setObjectName("tabsMenuButton");
TabsMenuButton->setAutoRaise(true);
TabsMenuButton->setPopupMode(QToolButton::InstantPopup);
internal::setButtonIcon(TabsMenuButton, QStyle::SP_TitleBarUnshadeButton, ads::DockAreaMenuIcon);
internal::setButtonIcon(TabsMenuButton, QStyle::SP_TitleBarUnshadeButton, _this->titleBarButtonCustomIcon(TitleBarButtonTabsMenu));
QMenu* TabsMenu = new QMenu(TabsMenuButton);
#ifndef QT_NO_TOOLTIP
TabsMenu->setToolTipsVisible(true);
@ -200,7 +200,7 @@ void DockAreaTitleBarPrivate::createButtons()
UndockButton->setObjectName("detachGroupButton");
UndockButton->setAutoRaise(true);
internal::setToolTip(UndockButton, QObject::tr("Detach Group"));
internal::setButtonIcon(UndockButton, QStyle::SP_TitleBarNormalButton, ads::DockAreaUndockIcon);
internal::setButtonIcon(UndockButton, QStyle::SP_TitleBarNormalButton, _this->titleBarButtonCustomIcon(TitleBarButtonUndock));
UndockButton->setSizePolicy(ButtonSizePolicy);
Layout->addWidget(UndockButton, 0);
_this->connect(UndockButton, SIGNAL(clicked()), SLOT(onUndockButtonClicked()));
@ -211,7 +211,7 @@ void DockAreaTitleBarPrivate::createButtons()
AutoHideButton->setObjectName("dockAreaAutoHideButton");
AutoHideButton->setAutoRaise(true);
internal::setToolTip(AutoHideButton, _this->titleBarButtonToolTip(TitleBarButtonAutoHide));
internal::setButtonIcon(AutoHideButton, QStyle::SP_DialogOkButton, ads::AutoHideIcon);
internal::setButtonIcon(AutoHideButton, QStyle::SP_DialogOkButton, _this->titleBarButtonCustomIcon(TitleBarButtonAutoHide));
AutoHideButton->setSizePolicy(ButtonSizePolicy);
AutoHideButton->setCheckable(testAutoHideConfigFlag(CDockManager::AutoHideButtonCheckable));
AutoHideButton->setChecked(false);
@ -222,7 +222,7 @@ void DockAreaTitleBarPrivate::createButtons()
CloseButton = new CTitleBarButton(testConfigFlag(CDockManager::DockAreaHasCloseButton));
CloseButton->setObjectName("dockAreaCloseButton");
CloseButton->setAutoRaise(true);
internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, ads::DockAreaCloseIcon);
internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, _this->titleBarButtonCustomIcon(TitleBarButtonClose));
internal::setToolTip(CloseButton, _this->titleBarButtonToolTip(TitleBarButtonClose));
CloseButton->setSizePolicy(ButtonSizePolicy);
CloseButton->setIconSize(QSize(16, 16));
@ -831,6 +831,74 @@ QString CDockAreaTitleBar::titleBarButtonToolTip(TitleBarButton Button) const
return QString();
}
//============================================================================
QIcon getAutoHideCustomIcon(TitleBarButton Button)
{
switch (Button)
{
case TitleBarButtonTabsMenu:
{
return CDockManager::iconProvider().customIcon(AutoHideDockAreaMenuIcon);
}
case TitleBarButtonUndock:
{
return CDockManager::iconProvider().customIcon(AutoHideDockAreaUndockIcon);
}
case TitleBarButtonClose:
{
return CDockManager::iconProvider().customIcon(AutoHideDockAreaCloseIcon);
}
case TitleBarButtonAutoHide:
{
return CDockManager::iconProvider().customIcon(AutoHideAutoHideIcon);
}
}
return {};
}
//============================================================================
QIcon getCustomIcon(TitleBarButton Button)
{
switch (Button)
{
case TitleBarButtonTabsMenu:
{
return CDockManager::iconProvider().customIcon(DockAreaMenuIcon);
}
case TitleBarButtonUndock:
{
return CDockManager::iconProvider().customIcon(DockAreaUndockIcon);
}
case TitleBarButtonClose:
{
return CDockManager::iconProvider().customIcon(DockAreaCloseIcon);
}
case TitleBarButtonAutoHide:
{
return CDockManager::iconProvider().customIcon(AutoHideIcon);
}
}
return {};
}
//============================================================================
QIcon CDockAreaTitleBar::titleBarButtonCustomIcon(TitleBarButton Button) const
{
QIcon icon;
if (d->DockArea != nullptr && d->DockArea->isAutoHide())
{
icon = getAutoHideCustomIcon(Button);
}
if (icon.isNull())
{
icon = getCustomIcon(Button);
}
return icon;
}
//============================================================================
CTitleBarButton::CTitleBarButton(bool showInTitleBar, QWidget* parent)
: tTitleBarButton(parent),

View File

@ -204,6 +204,11 @@ public:
*/
QString titleBarButtonToolTip(TitleBarButton Button) const;
/**
* Custom icon based on the current state
*/
QIcon titleBarButtonCustomIcon(TitleBarButton Button) const;
/**
* Moves the dock area into its own floating widget if the area
* DockWidgetFloatable flag is true

View File

@ -467,6 +467,7 @@ void CDockAreaWidget::setAutoHideDockContainer(CAutoHideDockContainer* AutoHideD
d->AutoHideDockContainer = AutoHideDockContainer;
updateAutoHideButtonCheckState();
updateTitleBarButtonsToolTips();
updateTitleBarButtonsIcons();
d->TitleBar->button(TitleBarButtonAutoHide)->setShowInTitleBar(true);
}
@ -891,6 +892,21 @@ void CDockAreaWidget::updateTitleBarButtonsToolTips()
titleBar()->titleBarButtonToolTip(TitleBarButtonAutoHide));
}
void CDockAreaWidget::updateTitleBarButtonsIcons()
{
internal::setButtonIcon(titleBarButton(TitleBarButtonTabsMenu),
QStyle::SP_TitleBarUnshadeButton,
titleBar()->titleBarButtonCustomIcon(TitleBarButtonTabsMenu));
internal::setButtonIcon(titleBarButton(TitleBarButtonUndock),
QStyle::SP_TitleBarNormalButton,
titleBar()->titleBarButtonCustomIcon(TitleBarButtonUndock));
internal::setButtonIcon(titleBarButton(TitleBarButtonClose),
QStyle::SP_TitleBarCloseButton,
titleBar()->titleBarButtonCustomIcon(TitleBarButtonClose));
internal::setButtonIcon(titleBarButton(TitleBarButtonAutoHide),
QStyle::SP_DialogOkButton,
titleBar()->titleBarButtonCustomIcon(TitleBarButtonAutoHide));
}
//============================================================================
void CDockAreaWidget::saveState(QXmlStreamWriter& s) const

View File

@ -89,6 +89,11 @@ private Q_SLOTS:
*/
void updateTitleBarButtonsToolTips();
/*
* Update the title bar button icons
*/
void updateTitleBarButtonsIcons();
/**
* Calculate the auto hide side bar location depending on the dock area
* widget position in the container

View File

@ -20,6 +20,7 @@ struct IconProviderPrivate
{
CIconProvider *_this;
QVector<QIcon> UserIcons{IconCount, QIcon()};
QVector<QIcon> UserAutoHideIcons{AutoHideIconCount, QIcon()};
/**
* Private data constructor
@ -57,6 +58,14 @@ QIcon CIconProvider::customIcon(eIcon IconId) const
}
//============================================================================
QIcon CIconProvider::customIcon(eAutoHideIcon IconId) const
{
Q_ASSERT(IconId < d->UserAutoHideIcons.size());
return d->UserAutoHideIcons[IconId];
}
//============================================================================
void CIconProvider::registerCustomIcon(eIcon IconId, const QIcon &icon)
{
@ -64,6 +73,14 @@ void CIconProvider::registerCustomIcon(eIcon IconId, const QIcon &icon)
d->UserIcons[IconId] = icon;
}
//============================================================================
void CIconProvider::registerCustomIcon(eAutoHideIcon IconId, const QIcon &icon)
{
Q_ASSERT(IconId < d->UserAutoHideIcons.size());
d->UserAutoHideIcons[IconId] = icon;
}
} // namespace ads

View File

@ -47,11 +47,13 @@ public:
* if no custom icon is registered
*/
QIcon customIcon(eIcon IconId) const;
QIcon customIcon(eAutoHideIcon IconId) const;
/**
/**
* Registers a custom icon for the given IconId
*/
void registerCustomIcon(eIcon IconId, const QIcon &icon);
void registerCustomIcon(eAutoHideIcon IconId, const QIcon& icon);
}; // class IconProvider
} // namespace ads

View File

@ -412,17 +412,35 @@ void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandarPixmap
return;
}
Button->setIcon(getStandardPixmapIcon(Button, StandarPixmap));
}
void setButtonIcon(QAbstractButton *Button, QStyle::StandardPixmap StandardPixmap, QIcon Icon)
{
// First we try to use custom icons if available
if (!Icon.isNull())
{
Button->setIcon(Icon);
return;
}
Button->setIcon(getStandardPixmapIcon(Button, StandardPixmap));
}
//============================================================================
QIcon getStandardPixmapIcon(QAbstractButton* Button, QStyle::StandardPixmap Pixmap)
{
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
Button->setIcon(Button->style()->standardIcon(StandarPixmap));
return Button->style()->standardIcon(Pixmap);
#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);
const QPixmap normalPixmap = Button->style()->standardPixmap(Pixmap, 0, Button);
QIcon icon;
icon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
icon.addPixmap(normalPixmap, QIcon::Normal);
return icon;
#endif
}
}
//============================================================================

View File

@ -135,6 +135,19 @@ enum eIcon
IconCount, //!< just a delimiter for range checks
};
/**
* The different icons used in the UI for Auto Hide
*/
enum eAutoHideIcon
{
AutoHideAutoHideIcon, //!< AutoHideIcon
AutoHideDockAreaMenuIcon, //!< DockAreaMenuIcon
AutoHideDockAreaUndockIcon,//!< DockAreaUndockIcon
AutoHideDockAreaCloseIcon, //!< DockAreaCloseIcon
AutoHideIconCount, //!< just a delimiter for range checks
};
/**
* For bitwise combination of dock wdget features
*/
@ -344,6 +357,14 @@ void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandarPixmap
ads::eIcon CustomIconId);
void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandardPixmap, QIcon Icon);
/**
* Return the icon of standard pixmap for button.
*/
QIcon getStandardPixmapIcon(QAbstractButton* Button, QStyle::StandardPixmap Pixmap);
enum eRepolishChildOptions
{
RepolishIgnoreChildren,