sneedmc/launcher/ui/widgets/IconLabel.cpp

44 lines
862 B
C++
Raw Normal View History

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