redai/imagelabel.cpp
2023-04-15 16:29:10 -04:00

29 lines
No EOL
712 B
C++

#include "imagelabel.h"
ImageLabel::ImageLabel(QWidget *parent) : QLabel(parent) {
this->setMinimumSize(1, 1);
setScaledContents(false);
}
void ImageLabel::setPixmap(const QPixmap &p) {
pix = p;
QLabel::setPixmap(scaledPixmap());
}
int ImageLabel::heightForWidth(int width) const {
return pix.isNull() ? this->height() : ((qreal) pix.height() * width) / pix.width();
}
QSize ImageLabel::sizeHint() const {
int w = this->width();
return QSize(w, heightForWidth(w));
}
QPixmap ImageLabel::scaledPixmap() const {
return pix.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
void ImageLabel::resizeEvent(QResizeEvent *e) {
if (!pix.isNull())
QLabel::setPixmap(scaledPixmap());
}