PrismLauncher/launcher/ui/widgets/IconLabel.cpp

40 lines
846 B
C++
Raw Normal View History

2014-05-17 15:23:48 +01:00
#include "IconLabel.h"
#include <QLayout>
#include <QPainter>
#include <QRect>
#include <QStyle>
#include <QStyleOption>
2014-05-17 15:23:48 +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
}
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();
if (width < height) {
2018-07-15 13:51:05 +01:00
rect.setHeight(width);
rect.translate(0, (height - width) / 2);
} 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
}