2018-03-11 07:57:57 +00:00
|
|
|
#include "aspectratiopixmaplabel.h"
|
|
|
|
|
|
|
|
AspectRatioPixmapLabel::AspectRatioPixmapLabel(QWidget *parent) :
|
|
|
|
QLabel(parent)
|
|
|
|
{
|
|
|
|
this->setMinimumSize(1,1);
|
|
|
|
setScaledContents(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AspectRatioPixmapLabel::setPixmap ( const QPixmap & p)
|
|
|
|
{
|
|
|
|
pix = p;
|
|
|
|
QLabel::setPixmap(scaledPixmap());
|
|
|
|
}
|
|
|
|
|
|
|
|
int AspectRatioPixmapLabel::heightForWidth( int width ) const
|
|
|
|
{
|
|
|
|
return pix.isNull() ? this->height() : ((qreal)pix.height()*width)/pix.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize AspectRatioPixmapLabel::sizeHint() const
|
|
|
|
{
|
|
|
|
int w = this->width();
|
|
|
|
return QSize( w, heightForWidth(w) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap AspectRatioPixmapLabel::scaledPixmap() const
|
|
|
|
{
|
|
|
|
return pix.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AspectRatioPixmapLabel::resizeEvent(QResizeEvent * e)
|
|
|
|
{
|
2018-03-31 20:15:01 +00:00
|
|
|
Q_UNUSED(e);
|
2018-03-11 07:57:57 +00:00
|
|
|
if(!pix.isNull())
|
|
|
|
QLabel::setPixmap(scaledPixmap());
|
|
|
|
}
|