2014-05-17 15:23:48 +01:00
|
|
|
#include "IconLabel.h"
|
|
|
|
|
|
|
|
#include <QLayout>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QRect>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOption>
|
2014-05-17 15:23:48 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
IconLabel::IconLabel(QWidget* parent, QIcon icon, QSize size) : QWidget(parent), m_size(size), m_icon(icon)
|
2014-05-17 15:23:48 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
2014-05-17 15:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize IconLabel::sizeHint() const
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_size;
|
2014-05-17 15:23:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconLabel::setIcon(QIcon icon)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_icon = icon;
|
|
|
|
update();
|
2014-05-17 15:23:48 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconLabel::paintEvent(QPaintEvent*)
|
2014-05-17 15:23:48 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QPainter p(this);
|
|
|
|
QRect rect = contentsRect();
|
|
|
|
int width = rect.width();
|
|
|
|
int height = rect.height();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (width < height) {
|
2018-07-15 13:51:05 +01:00
|
|
|
rect.setHeight(width);
|
|
|
|
rect.translate(0, (height - width) / 2);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else if (width > height) {
|
2018-07-15 13:51:05 +01:00
|
|
|
rect.setWidth(height);
|
|
|
|
rect.translate((width - height) / 2, 0);
|
|
|
|
}
|
|
|
|
m_icon.paint(&p, rect);
|
2014-05-17 15:23:48 +01:00
|
|
|
}
|