| 1 |
/* Copyright (c) 2024, DyssolTEC GmbH. |
| 2 |
* All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */ |
| 3 |
|
| 4 |
#include "QtPushButton.h" |
| 5 |
#include <QPainter> |
| 6 |
|
| 7 |
CQtPushButton::CQtPushButton(QWidget* _parent) |
| 8 |
: QPushButton{ _parent } |
| 9 |
{ |
| 10 |
} |
| 11 |
|
| 12 |
QIcon CQtPushButton::GetIcon() |
| 13 |
{ |
| 14 |
return m_icon; |
| 15 |
} |
| 16 |
|
| 17 |
void CQtPushButton::setIcon(const QIcon& _icon) |
| 18 |
{ |
| 19 |
m_icon = _icon; |
| 20 |
m_pixmap = _icon.pixmap(iconSize()); |
| 21 |
} |
| 22 |
|
| 23 |
void CQtPushButton::setIconSize(const QSize& _size) |
| 24 |
{ |
| 25 |
QPushButton::setIconSize(_size); |
| 26 |
if (!m_icon.isNull()) |
| 27 |
m_pixmap = m_icon.pixmap(iconSize()); |
| 28 |
} |
| 29 |
|
| 30 |
QSize CQtPushButton::sizeHint() const |
| 31 |
{ |
| 32 |
const auto parentHint = QPushButton::sizeHint(); |
| 33 |
const auto sz = iconSize(); |
| 34 |
return { parentHint.width() + 2 * sz.width() + 2 * c_margin, std::max(parentHint.height(), sz.height() + 2 * c_margin) }; |
| 35 |
} |
| 36 |
|
| 37 |
void CQtPushButton::paintEvent(QPaintEvent* _event) |
| 38 |
{ |
| 39 |
QPushButton::paintEvent(_event); |
| 40 |
if (!m_pixmap.isNull()) |
| 41 |
{ |
| 42 |
const int y = (height() - m_pixmap.height()) / 2; |
| 43 |
QPainter painter{ this }; |
| 44 |
painter.drawPixmap(c_margin, y, m_pixmap); |
| 45 |
} |
| 46 |
} |