pollymc/launcher/ui/widgets/IconLabel.cpp

44 lines
862 B
C++
Raw Normal View History

2014-05-17 19:53:48 +05:30
#include "IconLabel.h"
#include <QStyle>
#include <QStyleOption>
#include <QLayout>
#include <QPainter>
#include <QRect>
IconLabel::IconLabel(QWidget *parent, QIcon icon, QSize size)
2018-07-15 18:21:05 +05:30
: QWidget(parent), m_size(size), m_icon(icon)
2014-05-17 19:53:48 +05:30
{
2018-07-15 18:21:05 +05:30
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
2014-05-17 19:53:48 +05:30
}
QSize IconLabel::sizeHint() const
{
2018-07-15 18:21:05 +05:30
return m_size;
2014-05-17 19:53:48 +05:30
}
void IconLabel::setIcon(QIcon icon)
{
2018-07-15 18:21:05 +05:30
m_icon = icon;
update();
2014-05-17 19:53:48 +05:30
}
void IconLabel::paintEvent(QPaintEvent *)
{
2018-07-15 18:21:05 +05:30
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 19:53:48 +05:30
}