3#include <c++utilities/conversion/stringbuilder.h>
7#include <QStyleOptionFocusRect>
8#include <QStylePainter>
23 : QAbstractButton(parent)
25 setCursor(Qt::ArrowCursor);
26 setFocusPolicy(Qt::NoFocus);
43 const auto propertyName = argsToString(
"iconButton-",
id);
44 const auto existingIconButton = action->property(propertyName.data());
45 if (!existingIconButton.isNull()) {
46 return existingIconButton.value<
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);
59void IconButton::assignDataFromActionChangedSignal()
61 assignDataFromAction(qobject_cast<const QAction *>(QObject::sender()));
67void IconButton::assignDataFromAction(
const QAction *action)
69 auto const icon = action->icon();
70 const auto sizes = icon.availableSizes();
71 const auto text = action->text();
73 setToolTip(
text.isEmpty() ? action->toolTip() :
text);
78#if QT_VERSION >= 0x050100
79 const qreal pixmapRatio = m_pixmap.devicePixelRatio();
81 const qreal pixmapRatio = 1.0;
83 return QSize(
static_cast<int>(m_pixmap.width() / pixmapRatio),
static_cast<int>(m_pixmap.height() / pixmapRatio));
88#if QT_VERSION >= 0x050100
89 const qreal pixmapRatio = m_pixmap.devicePixelRatio();
91 const qreal pixmapRatio = 1.0;
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);
98 auto focusOption = QStyleOptionFocusRect();
99 focusOption.initFrom(
this);
100 focusOption.rect = pixmapRect;
102 focusOption.rect.adjust(-4, -4, 4, 4);
103 painter.drawControl(QStyle::CE_FocusFrame, focusOption);
105 painter.drawPrimitive(QStyle::PE_FrameFocusRect, focusOption);
112 QAbstractButton::keyPressEvent(event);
113 if (!event->modifiers() && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
121 QAbstractButton::keyReleaseEvent(event);
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...