-
Notifications
You must be signed in to change notification settings - Fork 4
/
RealCloseButton.cpp
69 lines (60 loc) · 1.75 KB
/
RealCloseButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "RealCloseButton.h"
#include <QPainter>
#include <QStyleOption>
RealCloseButton::RealCloseButton(QWidget *parent)
: QAbstractButton(parent)
{
setFocusPolicy(Qt::NoFocus);
#ifndef QT_NO_CURSOR
setCursor(Qt::ArrowCursor);
#endif
#ifndef QT_NO_TOOLTIP
setToolTip(tr("Close Tab"));
#endif
resize(RealCloseButton::sizeHint());
}
QSize RealCloseButton::sizeHint() const
{
ensurePolished();
int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this);
return QSize(width, height);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void RealCloseButton::enterEvent(QEvent *event)
#else
void RealCloseButton::enterEvent(QEnterEvent *event)
#endif
{
if (isEnabled())
update();
QAbstractButton::enterEvent(event);
}
void RealCloseButton::leaveEvent(QEvent *event)
{
if (isEnabled())
update();
QAbstractButton::leaveEvent(event);
}
void RealCloseButton::paintEvent(QPaintEvent *)
{
QPainter p(this);
QStyleOption opt;
opt.initFrom(this);
opt.state |= QStyle::State_AutoRaise;
if (isEnabled() && underMouse() && !isChecked() && !isDown())
opt.state |= QStyle::State_Raised;
if (isChecked())
opt.state |= QStyle::State_On;
if (isDown())
opt.state |= QStyle::State_Sunken;
if (const QTabBar *tb = qobject_cast<const QTabBar *>(parent()))
{
int index = tb->currentIndex();
QTabBar::ButtonPosition position
= (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tb);
if (tb->tabButton(index, position) == this)
opt.state |= QStyle::State_Selected;
}
style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this);
}