Debian Codegraph @

widgets/iconbutton.cpp

raw symbols SCIP
1 #include "./iconbutton.h"
2
3 #include <c++utilities/conversion/stringbuilder.h>
4
5 #include <QKeyEvent>
6 #include <QStyle>
7 #include <QStyleOptionFocusRect>
8 #include <QStylePainter>
9
10 using namespace CppUtilities;
11
12 namespace QtUtilities {
13
14 /*!
15 * \class IconButton
16 * \brief A simple QAbstractButton implementation displaying a QPixmap.
17 */
18
19 /*!
20 * \brief Constructs an icon button.
21 */
22 IconButton::IconButton(QWidget *parent)
23 : QAbstractButton(parent)
24 {
25 setCursor(Qt::ArrowCursor);
26 setFocusPolicy(Qt::NoFocus);
27 }
28
29 /*!
30 * \brief Destroys the icon button.
31 */
32 IconButton::~IconButton()
33 {
34 }
35
36 /*!
37 * \brief Creates an IconButton for the specified \a action.
38 * \remarks Calling this function on the same action twice with the same \a id yields the
39 * same instance.
40 */
41 IconButton *IconButton::fromAction(QAction *action, std::uintptr_t id)
42 {
43 const auto propertyName = argsToString("iconButton-", id);
44 const auto existingIconButton = action->property(propertyName.data());
45 if (!existingIconButton.isNull()) {
46 return existingIconButton.value<IconButton *>();
47 }
48 auto *const iconButton = new IconButton;
49 iconButton->assignDataFromAction(action);
50 action->setProperty(propertyName.data(), QVariant::fromValue(iconButton));
51 connect(action, &QAction::changed, iconButton, &IconButton::assignDataFromActionChangedSignal);
52 connect(iconButton, &IconButton::clicked, action, &QAction::trigger);
53 return iconButton;
54 }
55
56 /*!
57 * \brief Internally called to assign data from a QAction to the icon button.
58 */
59 void IconButton::assignDataFromActionChangedSignal()
60 {
61 assignDataFromAction(qobject_cast<const QAction *>(QObject::sender()));
62 }
63
64 /*!
65 * \brief Internally called to assign data from a QAction to the icon button.
66 */
67 void IconButton::assignDataFromAction(const QAction *action)
68 {
69 auto const icon = action->icon();
70 const auto sizes = icon.availableSizes();
71 const auto text = action->text();
72 setPixmap(icon.pixmap(sizes.empty() ? defaultPixmapSize : sizes.front()));
73 setToolTip(text.isEmpty() ? action->toolTip() : text);
74 }
75
76 QSize IconButton::sizeHint() const
77 {
78 #if QT_VERSION >= 0x050100
79 const qreal pixmapRatio = m_pixmap.devicePixelRatio();
80 #else
81 const qreal pixmapRatio = 1.0;
82 #endif
83 return QSize(static_cast<int>(m_pixmap.width() / pixmapRatio), static_cast<int>(m_pixmap.height() / pixmapRatio));
84 }
85
86 void IconButton::paintEvent(QPaintEvent *)
87 {
88 #if QT_VERSION >= 0x050100
89 const qreal pixmapRatio = m_pixmap.devicePixelRatio();
90 #else
91 const qreal pixmapRatio = 1.0;
92 #endif
93 auto painter = QStylePainter(this);
94 auto pixmapRect = QRect(0, 0, static_cast<int>(m_pixmap.width() / pixmapRatio), static_cast<int>(m_pixmap.height() / pixmapRatio));
95 pixmapRect.moveCenter(rect().center());
96 painter.drawPixmap(pixmapRect, m_pixmap);
97 if (hasFocus()) {
98 auto focusOption = QStyleOptionFocusRect();
99 focusOption.initFrom(this);
100 focusOption.rect = pixmapRect;
101 #ifdef Q_OS_MAC
102 focusOption.rect.adjust(-4, -4, 4, 4);
103 painter.drawControl(QStyle::CE_FocusFrame, focusOption);
104 #else
105 painter.drawPrimitive(QStyle::PE_FrameFocusRect, focusOption);
106 #endif
107 }
108 }
109
110 void IconButton::keyPressEvent(QKeyEvent *event)
111 {
112 QAbstractButton::keyPressEvent(event);
113 if (!event->modifiers() && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
114 click();
115 }
116 event->accept();
117 }
118
119 void IconButton::keyReleaseEvent(QKeyEvent *event)
120 {
121 QAbstractButton::keyReleaseEvent(event);
122 event->accept();
123 }
124
125 } // namespace QtUtilities