| 1 |
/* This file is part of the KDE project |
| 2 |
SPDX-FileCopyrightText: 2007 Marijn Kruisselbrink <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2007 Thomas Zander <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
// clazy:excludeall=qstring-arg |
| 9 |
#include "KoDockWidgetTitleBarButton.h" |
| 10 |
|
| 11 |
#include <WidgetsDebug.h> |
| 12 |
|
| 13 |
#include <QAbstractButton> |
| 14 |
#include <QAction> |
| 15 |
#include <QLabel> |
| 16 |
#include <QLayout> |
| 17 |
#include <QStyle> |
| 18 |
#include <QStylePainter> |
| 19 |
#include <QStyleOptionFrame> |
| 20 |
|
| 21 |
class Q_DECL_HIDDEN KoDockWidgetTitleBarButton::Private |
| 22 |
{ |
| 23 |
public: |
| 24 |
Private() : styleSize(0, 0), iconSize(0) {} |
| 25 |
QSize styleSize; |
| 26 |
int iconSize; |
| 27 |
}; |
| 28 |
|
| 29 |
KoDockWidgetTitleBarButton::KoDockWidgetTitleBarButton(QWidget *parent) |
| 30 |
: QAbstractButton(parent), d(new Private()) |
| 31 |
{ |
| 32 |
setFocusPolicy(Qt::NoFocus); |
| 33 |
} |
| 34 |
|
| 35 |
KoDockWidgetTitleBarButton::~KoDockWidgetTitleBarButton() |
| 36 |
{ |
| 37 |
delete d; |
| 38 |
} |
| 39 |
|
| 40 |
QSize KoDockWidgetTitleBarButton::sizeHint() const |
| 41 |
{ |
| 42 |
ensurePolished(); |
| 43 |
|
| 44 |
const int margin = style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin, nullptr, this); |
| 45 |
if (icon().isNull()) { |
| 46 |
return QSize(18, 18); // TODO: was QSize(margin, margin), hardcoded values seem bad |
| 47 |
} |
| 48 |
|
| 49 |
int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this); |
| 50 |
if (iconSize != d->iconSize) { |
| 51 |
const_cast<KoDockWidgetTitleBarButton*>(this)->d->iconSize = iconSize; |
| 52 |
const QPixmap pm = icon().pixmap(iconSize); |
| 53 |
const_cast<KoDockWidgetTitleBarButton*>(this)->d->styleSize = QSize(pm.width() + margin, pm.height() + margin); |
| 54 |
} |
| 55 |
return d->styleSize; |
| 56 |
} |
| 57 |
|
| 58 |
QSize KoDockWidgetTitleBarButton::minimumSizeHint() const |
| 59 |
{ |
| 60 |
return sizeHint(); |
| 61 |
} |
| 62 |
|
| 63 |
// redraw the button when the mouse enters or leaves it |
| 64 |
void KoDockWidgetTitleBarButton::enterEvent(QEnterEvent *event) |
| 65 |
{ |
| 66 |
if (isEnabled()) |
| 67 |
update(); |
| 68 |
QAbstractButton::enterEvent(event); |
| 69 |
} |
| 70 |
|
| 71 |
void KoDockWidgetTitleBarButton::leaveEvent(QEvent *event) |
| 72 |
{ |
| 73 |
if (isEnabled()) |
| 74 |
update(); |
| 75 |
QAbstractButton::leaveEvent(event); |
| 76 |
} |
| 77 |
|
| 78 |
void KoDockWidgetTitleBarButton::paintEvent(QPaintEvent *) |
| 79 |
{ |
| 80 |
QPainter p(this); |
| 81 |
|
| 82 |
QStyleOptionToolButton opt; |
| 83 |
opt.initFrom(this); |
| 84 |
opt.state |= QStyle::State_AutoRaise; |
| 85 |
|
| 86 |
if (isEnabled() && underMouse() && !isChecked() && !isDown()) |
| 87 |
opt.state |= QStyle::State_Raised; |
| 88 |
if (isChecked()) |
| 89 |
opt.state |= QStyle::State_On; |
| 90 |
if (isDown()) |
| 91 |
opt.state |= QStyle::State_Sunken; |
| 92 |
style()->drawPrimitive(QStyle::PE_PanelButtonTool, &opt, &p, this); |
| 93 |
|
| 94 |
opt.icon = icon(); |
| 95 |
opt.subControls = {}; |
| 96 |
opt.activeSubControls = {}; |
| 97 |
opt.features = QStyleOptionToolButton::None; |
| 98 |
opt.arrowType = Qt::NoArrow; |
| 99 |
int size = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this); |
| 100 |
opt.iconSize = QSize(size, size); |
| 101 |
style()->drawComplexControl(QStyle::CC_ToolButton, &opt, &p, this); |
| 102 |
} |