Qt Utilities 6.14.4
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
iconbutton.cpp
Go to the documentation of this file.
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
10using namespace CppUtilities;
11
12namespace QtUtilities {
13
22IconButton::IconButton(QWidget *parent)
23 : QAbstractButton(parent)
24{
25 setCursor(Qt::ArrowCursor);
26 setFocusPolicy(Qt::NoFocus);
27}
28
35
41IconButton *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
59void IconButton::assignDataFromActionChangedSignal()
60{
61 assignDataFromAction(qobject_cast<const QAction *>(QObject::sender()));
62}
63
67void 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
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
86void 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
110void 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
119void IconButton::keyReleaseEvent(QKeyEvent *event)
120{
121 QAbstractButton::keyReleaseEvent(event);
122 event->accept();
123}
124
125} // namespace QtUtilities
A simple QAbstractButton implementation displaying a QPixmap.
Definition iconbutton.h:15
~IconButton() override
Destroys the icon button.
void setPixmap(const QPixmap &pixmap)
Sets the pixmap.
Definition iconbutton.h:54
static constexpr auto defaultPixmapSize
Definition iconbutton.h:28
IconButton(QWidget *parent=nullptr)
Constructs an icon button.
void paintEvent(QPaintEvent *event) override
void keyPressEvent(QKeyEvent *event) override
static IconButton * fromAction(QAction *action, std::uintptr_t id=0)
Creates an IconButton for the specified action.
QSize sizeHint() const override
void keyReleaseEvent(QKeyEvent *event) override
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...
#define text