Qt Utilities: widgets/iconbutton.cpp Source File

Qt Utilities 6.22.1 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
18
22IconButton::IconButton(QWidget *parent)
23 : QAbstractButton(parent)
24{
25 setCursor(Qt::ArrowCursor);
26 setFocusPolicy(Qt::NoFocus);
27}
28
32IconButton::~IconButton()
33{
34}
35
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
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
76QSize 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
QtUtilities::IconButton::~IconButton
~IconButton() override
Destroys the icon button.
Definition iconbutton.cpp:32
QtUtilities::IconButton::setPixmap
void setPixmap(const QPixmap &pixmap)
Sets the pixmap.
Definition iconbutton.h:54
QtUtilities::IconButton::defaultPixmapSize
static constexpr auto defaultPixmapSize
Definition iconbutton.h:28
QtUtilities::IconButton::IconButton
IconButton(QWidget *parent=nullptr)
Constructs an icon button.
Definition iconbutton.cpp:22
QtUtilities::IconButton::paintEvent
void paintEvent(QPaintEvent *event) override
Definition iconbutton.cpp:86
QtUtilities::IconButton::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Definition iconbutton.cpp:110
QtUtilities::IconButton::fromAction
static IconButton * fromAction(QAction *action, std::uintptr_t id=0)
Creates an IconButton for the specified action.
Definition iconbutton.cpp:41
QtUtilities::IconButton::sizeHint
QSize sizeHint() const override
Definition iconbutton.cpp:76
QtUtilities::IconButton::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event) override
Definition iconbutton.cpp:119
iconbutton.h
CppUtilities
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...
Definition qtconfigarguments.h:38
QtUtilities
!
Definition trylocker.h:8
text
#define text
Definition xmlparsermacros.h:18