aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/fancyiconbutton.cpp
blob: 975dffa1e84cba9dcb882ede6701cb507d8918f3 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "fancyiconbutton.h"
#include "hostosinfo.h"
#include "stylehelper.h"

#include <QApplication>
#include <QKeyEvent>
#include <QPainter>
#include <QPropertyAnimation>
#include <QStyle>
#include <QStyleOptionFocusRect>
#include <QStylePainter>
#include <QWindow>

namespace Utils {

FancyIconButton::FancyIconButton(QWidget *parent)
    : QAbstractButton(parent)
{
    setCursor(Qt::ArrowCursor);
    setFocusPolicy(Qt::NoFocus);
}

void FancyIconButton::paintEvent(QPaintEvent *)
{
    const qreal pixelRatio = window()->windowHandle()->devicePixelRatio();
    const QPixmap iconPixmap = icon().pixmap(sizeHint(), pixelRatio,
                                             isEnabled() ? QIcon::Normal : QIcon::Disabled);
    QStylePainter painter(this);
    QRect pixmapRect(QPoint(), iconPixmap.size() / iconPixmap.devicePixelRatio());
    pixmapRect.moveCenter(rect().center());

    if (m_autoHide)
        painter.setOpacity(m_iconOpacity);

    painter.drawPixmap(pixmapRect, iconPixmap);

    if (hasFocus()) {
        QStyleOptionFocusRect focusOption;
        focusOption.initFrom(this);
        focusOption.rect = pixmapRect;
        if (HostOsInfo::isMacHost()) {
            focusOption.rect.adjust(-4, -4, 4, 4);
            painter.drawControl(QStyle::CE_FocusFrame, focusOption);
        } else {
            painter.drawPrimitive(QStyle::PE_FrameFocusRect, focusOption);
        }
    }
}

void FancyIconButton::animateShow(bool visible)
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
    animation->setDuration(StyleHelper::defaultFadeAnimationDuration);
    animation->setEndValue(visible ? 1.0 : 0.0);
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

QSize FancyIconButton::sizeHint() const
{
    return icon().actualSize(QSize(32, 16)); // Find flags icon can be wider than 16px
}

void FancyIconButton::keyPressEvent(QKeyEvent *ke)
{
    QAbstractButton::keyPressEvent(ke);
    if (!ke->modifiers() && (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return))
        click();
    // do not forward to line edit
    ke->accept();
}

void FancyIconButton::keyReleaseEvent(QKeyEvent *ke)
{
    QAbstractButton::keyReleaseEvent(ke);
    // do not forward to line edit
    ke->accept();
}

} // namespace Utils