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
|
|
|
{
|
|
|
|
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize IconLabel::sizeHint() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconLabel::setIcon(QIcon icon)
|
|
|
|
{
|
|
|
|
m_icon = icon;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void IconLabel::paintEvent(QPaintEvent*)
|
2014-05-17 15:23:48 +01:00
|
|
|
{
|
|
|
|
QPainter p(this);
|
2014-10-26 22:44:20 +00:00
|
|
|
QRect rect = contentsRect();
|
|
|
|
int width = rect.width();
|
|
|
|
int height = rect.height();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (width < height) {
|
2014-10-26 22:44:20 +00:00
|
|
|
rect.setHeight(width);
|
|
|
|
rect.translate(0, (height - width) / 2);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else if (width > height) {
|
2014-10-26 22:44:20 +00:00
|
|
|
rect.setWidth(height);
|
|
|
|
rect.translate((width - height) / 2, 0);
|
|
|
|
}
|
|
|
|
m_icon.paint(&p, rect);
|
2014-05-17 15:23:48 +01:00
|
|
|
}
|