2015-09-06 20:19:21 +02:00
|
|
|
#include "./iconbutton.h"
|
2015-04-22 18:57:44 +02:00
|
|
|
|
2020-06-09 23:24:24 +02:00
|
|
|
#include <c++utilities/conversion/stringbuilder.h>
|
|
|
|
|
2015-04-22 18:57:44 +02:00
|
|
|
#include <QKeyEvent>
|
2018-06-05 22:49:08 +02:00
|
|
|
#include <QStyle>
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QStyleOptionFocusRect>
|
|
|
|
#include <QStylePainter>
|
2015-04-22 18:57:44 +02:00
|
|
|
|
2020-06-09 23:24:24 +02:00
|
|
|
using namespace CppUtilities;
|
|
|
|
|
2019-06-10 21:57:46 +02:00
|
|
|
namespace QtUtilities {
|
2015-04-22 18:57:44 +02:00
|
|
|
|
|
|
|
/*!
|
2019-06-10 21:57:46 +02:00
|
|
|
* \class IconButton
|
2015-04-22 18:57:44 +02:00
|
|
|
* \brief A simple QAbstractButton implementation displaying a QPixmap.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs an icon button.
|
|
|
|
*/
|
2017-05-01 03:16:25 +02:00
|
|
|
IconButton::IconButton(QWidget *parent)
|
|
|
|
: QAbstractButton(parent)
|
2015-04-22 18:57:44 +02:00
|
|
|
{
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Destroys the icon button.
|
|
|
|
*/
|
|
|
|
IconButton::~IconButton()
|
2017-05-01 03:16:25 +02:00
|
|
|
{
|
|
|
|
}
|
2015-04-22 18:57:44 +02:00
|
|
|
|
2020-06-09 23:24:24 +02:00
|
|
|
/*!
|
|
|
|
* \brief Creates an IconButton for the specified \a action.
|
|
|
|
* \remarks Calling this function on the same action twice with the same \a id yields the
|
|
|
|
* same instance.
|
|
|
|
*/
|
|
|
|
IconButton *IconButton::fromAction(QAction *action, std::uintptr_t id)
|
|
|
|
{
|
|
|
|
const auto propertyName = argsToString("iconButton-", id);
|
|
|
|
const auto existingIconButton = action->property(propertyName.data());
|
|
|
|
if (!existingIconButton.isNull()) {
|
|
|
|
return existingIconButton.value<IconButton *>();
|
|
|
|
}
|
|
|
|
auto *const iconButton = new IconButton;
|
|
|
|
iconButton->assignDataFromAction(action);
|
|
|
|
action->setProperty(propertyName.data(), QVariant::fromValue(iconButton));
|
|
|
|
connect(action, &QAction::changed, iconButton, &IconButton::assignDataFromActionChangedSignal);
|
|
|
|
connect(iconButton, &IconButton::clicked, action, &QAction::trigger);
|
|
|
|
return iconButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Internally called to assign data from a QAction to the icon button.
|
|
|
|
*/
|
|
|
|
void IconButton::assignDataFromActionChangedSignal()
|
|
|
|
{
|
|
|
|
assignDataFromAction(qobject_cast<const QAction *>(QObject::sender()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Internally called to assign data from a QAction to the icon button.
|
|
|
|
*/
|
|
|
|
void IconButton::assignDataFromAction(const QAction *action)
|
|
|
|
{
|
|
|
|
auto const icon = action->icon();
|
|
|
|
const auto sizes = icon.availableSizes();
|
|
|
|
const auto text = action->text();
|
2020-06-14 14:56:03 +02:00
|
|
|
setPixmap(icon.pixmap(sizes.empty() ? defaultPixmapSize : sizes.front()));
|
2020-06-09 23:24:24 +02:00
|
|
|
setToolTip(text.isEmpty() ? action->toolTip() : text);
|
|
|
|
}
|
|
|
|
|
2015-04-22 18:57:44 +02:00
|
|
|
QSize IconButton::sizeHint() const
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= 0x050100
|
|
|
|
const qreal pixmapRatio = m_pixmap.devicePixelRatio();
|
|
|
|
#else
|
|
|
|
const qreal pixmapRatio = 1.0;
|
|
|
|
#endif
|
2021-03-20 21:02:00 +01:00
|
|
|
return QSize(static_cast<int>(m_pixmap.width() / pixmapRatio), static_cast<int>(m_pixmap.height() / pixmapRatio));
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= 0x050100
|
|
|
|
const qreal pixmapRatio = m_pixmap.devicePixelRatio();
|
|
|
|
#else
|
|
|
|
const qreal pixmapRatio = 1.0;
|
|
|
|
#endif
|
2021-03-20 21:02:00 +01:00
|
|
|
auto painter = QStylePainter(this);
|
|
|
|
auto pixmapRect = QRect(0, 0, static_cast<int>(m_pixmap.width() / pixmapRatio), static_cast<int>(m_pixmap.height() / pixmapRatio));
|
2015-04-22 18:57:44 +02:00
|
|
|
pixmapRect.moveCenter(rect().center());
|
|
|
|
painter.drawPixmap(pixmapRect, m_pixmap);
|
2017-05-01 03:16:25 +02:00
|
|
|
if (hasFocus()) {
|
2021-03-20 21:02:00 +01:00
|
|
|
auto focusOption = QStyleOptionFocusRect();
|
2015-04-22 18:57:44 +02:00
|
|
|
focusOption.initFrom(this);
|
|
|
|
focusOption.rect = pixmapRect;
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
focusOption.rect.adjust(-4, -4, 4, 4);
|
|
|
|
painter.drawControl(QStyle::CE_FocusFrame, focusOption);
|
|
|
|
#else
|
|
|
|
painter.drawPrimitive(QStyle::PE_FrameFocusRect, focusOption);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractButton::keyPressEvent(event);
|
|
|
|
if (!event->modifiers() && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
|
|
|
|
click();
|
|
|
|
}
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractButton::keyReleaseEvent(event);
|
|
|
|
event->accept();
|
|
|
|
}
|
2020-06-09 23:24:24 +02:00
|
|
|
|
2019-06-10 21:57:46 +02:00
|
|
|
} // namespace QtUtilities
|