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