Skip to content

Commit 6a8b26f

Browse files
Added function internal::setButtonIcon to unify code for setting DockAreaTitleBar and DockWidgetTab icons
1 parent acb4238 commit 6a8b26f

File tree

4 files changed

+65
-50
lines changed

4 files changed

+65
-50
lines changed

src/DockAreaTitleBar.cpp

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,6 @@ struct DockAreaTitleBarPrivate
101101
{
102102
return CDockManager::configFlags().testFlag(Flag);
103103
}
104-
105-
/**
106-
* Helper function to set title bar button icons depending on operating
107-
* system and to avoid duplicated code. On windows the standard icons
108-
* are blurry since Qt 5.11 so we need to do some additional steps.
109-
* If the global IconPovider of the dockmanager provides a custom
110-
* Icon for the given CustomIconId, the this icon will be used.
111-
*/
112-
void setTitleBarButtonIcon(tTitleBarButton* Button, QStyle::StandardPixmap StandarPixmap,
113-
ads::eIcon CustomIconId)
114-
{
115-
// First we try to use custom icons if available
116-
QIcon Icon = CDockManager::iconProvider().customIcon(CustomIconId);
117-
if (!Icon.isNull())
118-
{
119-
Button->setIcon(Icon);
120-
return;
121-
}
122-
123-
#ifdef Q_OS_LINUX
124-
Button->setIcon(_this->style()->standardIcon(StandarPixmap));
125-
#else
126-
QPixmap normalPixmap = _this->style()->standardPixmap(StandarPixmap, 0, Button);
127-
Icon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
128-
Icon.addPixmap(normalPixmap, QIcon::Normal);
129-
Button->setIcon(Icon);
130-
#endif
131-
}
132104
};// struct DockAreaTitleBarPrivate
133105

134106

@@ -143,19 +115,19 @@ class CTitleBarButton : public tTitleBarButton
143115
bool Visible = true;
144116
bool HideWhenDisabled = false;
145117
public:
118+
using Super = tTitleBarButton;
146119
CTitleBarButton(bool visible = true, QWidget* parent = nullptr)
147-
: tTitleBarButton(parent)
148-
, Visible(visible)
149-
, HideWhenDisabled(DockAreaTitleBarPrivate::testConfigFlag(CDockManager::DockAreaHideDisabledButtons))
150-
{
151-
//this->setVisible(Visible); // this causes flickering and seems not to be needed TODO: investigate further and in case it IS needed fix flickering
152-
}
120+
: tTitleBarButton(parent),
121+
Visible(visible),
122+
HideWhenDisabled(DockAreaTitleBarPrivate::testConfigFlag(CDockManager::DockAreaHideDisabledButtons))
123+
{}
153124

154125

126+
/**
127+
* Adjust this visibility change request with our internal settings:
128+
*/
155129
virtual void setVisible(bool visible) override
156130
{
157-
// Adjust this visibility change request with our internal settings:
158-
159131
// 'visible' can stay 'true' if and only if this button is configured to generaly visible:
160132
visible = visible && this->Visible;
161133

@@ -165,10 +137,13 @@ class CTitleBarButton : public tTitleBarButton
165137
visible = isEnabled();
166138
}
167139

168-
tTitleBarButton::setVisible(visible);
140+
Super::setVisible(visible);
169141
}
170142

171143
protected:
144+
/**
145+
* Handle EnabledChanged signal to set button invisible if the configured
146+
*/
172147
bool event(QEvent *ev) override
173148
{
174149
if(QEvent::EnabledChange == ev->type() && HideWhenDisabled)
@@ -177,7 +152,7 @@ class CTitleBarButton : public tTitleBarButton
177152
setVisible(isEnabled());
178153
}
179154

180-
return tTitleBarButton::event(ev);;
155+
return Super::event(ev);;
181156
}
182157
};
183158

@@ -200,7 +175,7 @@ void DockAreaTitleBarPrivate::createButtons()
200175
TabsMenuButton->setObjectName("tabsMenuButton");
201176
TabsMenuButton->setAutoRaise(true);
202177
TabsMenuButton->setPopupMode(QToolButton::InstantPopup);
203-
setTitleBarButtonIcon(TabsMenuButton, QStyle::SP_TitleBarUnshadeButton, ads::DockAreaMenuIcon);
178+
internal::setButtonIcon(TabsMenuButton, QStyle::SP_TitleBarUnshadeButton, ads::DockAreaMenuIcon);
204179
QMenu* TabsMenu = new QMenu(TabsMenuButton);
205180
#ifndef QT_NO_TOOLTIP
206181
TabsMenu->setToolTipsVisible(true);
@@ -219,7 +194,7 @@ void DockAreaTitleBarPrivate::createButtons()
219194
UndockButton->setObjectName("undockButton");
220195
UndockButton->setAutoRaise(true);
221196
internal::setToolTip(UndockButton, QObject::tr("Detach Group"));
222-
setTitleBarButtonIcon(UndockButton, QStyle::SP_TitleBarNormalButton, ads::DockAreaUndockIcon);
197+
internal::setButtonIcon(UndockButton, QStyle::SP_TitleBarNormalButton, ads::DockAreaUndockIcon);
223198
UndockButton->setSizePolicy(ButtonSizePolicy);
224199
TopLayout->addWidget(UndockButton, 0);
225200
_this->connect(UndockButton, SIGNAL(clicked()), SLOT(onUndockButtonClicked()));
@@ -228,7 +203,7 @@ void DockAreaTitleBarPrivate::createButtons()
228203
CloseButton = new CTitleBarButton(testConfigFlag(CDockManager::DockAreaHasCloseButton));
229204
CloseButton->setObjectName("closeButton");
230205
CloseButton->setAutoRaise(true);
231-
setTitleBarButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, ads::DockAreaCloseIcon);
206+
internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, ads::DockAreaCloseIcon);
232207
if (testConfigFlag(CDockManager::DockAreaCloseButtonClosesTab))
233208
{
234209
internal::setToolTip(CloseButton, QObject::tr("Close Active Tab"));

src/DockWidgetTab.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,7 @@ void DockWidgetTabPrivate::createLayout()
173173

174174
CloseButton = createCloseButton();
175175
CloseButton->setObjectName("tabCloseButton");
176-
QIcon CloseIcon = CDockManager::iconProvider().customIcon(TabCloseIcon);
177-
if (CloseIcon.isNull())
178-
{
179-
// The standard icons do does not look good on high DPI screens
180-
QPixmap normalPixmap = _this->style()->standardPixmap(QStyle::SP_TitleBarCloseButton, 0, CloseButton);
181-
CloseIcon.addPixmap(normalPixmap, QIcon::Normal);
182-
CloseIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
183-
}
184-
CloseButton->setIcon(CloseIcon);
176+
internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon);
185177
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
186178
_this->onDockWidgetFeaturesChanged();
187179
internal::setToolTip(CloseButton, QObject::tr("Close Tab"));

src/ads_globals.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
//============================================================================
3131
#include <QVariant>
3232
#include <QPainter>
33+
#include <QAbstractButton>
3334

3435
#include "DockSplitter.h"
36+
#include "DockManager.h"
37+
#include "IconProvider.h"
3538
#include "ads_globals.h"
3639

3740

@@ -90,6 +93,31 @@ void hideEmptyParentSplitters(CDockSplitter* Splitter)
9093
}
9194
}
9295

96+
97+
//============================================================================
98+
void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandarPixmap,
99+
ads::eIcon CustomIconId)
100+
{
101+
// First we try to use custom icons if available
102+
QIcon Icon = CDockManager::iconProvider().customIcon(CustomIconId);
103+
if (!Icon.isNull())
104+
{
105+
Button->setIcon(Icon);
106+
return;
107+
}
108+
109+
#ifdef Q_OS_LINUX
110+
Button->setIcon(Button->style()->standardIcon(StandarPixmap));
111+
#else
112+
// The standard icons does not look good on high DPI screens so we create
113+
// our own "standard" icon here.
114+
QPixmap normalPixmap = Button->style()->standardPixmap(StandarPixmap, 0, Button);
115+
Icon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
116+
Icon.addPixmap(normalPixmap, QIcon::Normal);
117+
Button->setIcon(Icon);
118+
#endif
119+
}
120+
93121
} // namespace internal
94122
} // namespace ads
95123

src/ads_globals.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include <QPixmap>
3636
#include <QWidget>
3737
#include <QDebug>
38+
#include <QStyle>
39+
40+
class QAbstractButton;
3841

3942
#ifndef ADS_STATIC
4043
#ifdef ADS_SHARED_EXPORT
@@ -231,6 +234,23 @@ void setToolTip(QObjectPtr obj, const QString &tip)
231234
#endif
232235
}
233236

237+
238+
/**
239+
* Helper function to set the icon of a certain button.
240+
* Use this function to set the icons for the dock area and dock widget buttons.
241+
* The function first uses the CustomIconId to get an icon from the
242+
* CIconProvider. You can register your custom icons with the icon provider, if
243+
* you do not want to use the default buttons and if you do not want to use
244+
* stylesheets.
245+
* If the IconProvider does not return a valid icon (icon is null), the function
246+
* fetches the given standard pixmap from the QStyle.
247+
* param[in] Button The button whose icons are to be set
248+
* param[in] StandardPixmap The standard pixmap to be used for the button
249+
* param[in] CustomIconId The identifier for the custom icon.
250+
*/
251+
void setButtonIcon(QAbstractButton* Button, QStyle::StandardPixmap StandarPixmap,
252+
ads::eIcon CustomIconId);
253+
234254
} // namespace internal
235255
} // namespace ads
236256

0 commit comments

Comments
 (0)