40 lines
972 B
C++
40 lines
972 B
C++
#include <QPainter>
|
|
|
|
#include "imagewidget.h"
|
|
|
|
void ImageWidget::paintEvent(QPaintEvent *event)
|
|
{
|
|
QLabel::paintEvent(event);
|
|
displayImage();
|
|
}
|
|
|
|
void ImageWidget::setPixmap(QPixmap image)
|
|
{
|
|
sourceImage = image;
|
|
currentImage = image;
|
|
repaint();
|
|
}
|
|
|
|
void ImageWidget::displayImage()
|
|
{
|
|
if (sourceImage.isNull())
|
|
return;
|
|
|
|
float cw = width(), ch = height();
|
|
float pw = currentImage.width(), ph = currentImage.height();
|
|
|
|
if(pw > cw && ph > ch && pw/cw > ph/ch ||
|
|
pw > cw && ph <= ch ||
|
|
pw < cw && ph < ch && cw/pw < ch/ph)
|
|
currentImage = sourceImage.scaledToWidth(cw, Qt::TransformationMode::FastTransformation);
|
|
else if(pw > cw && ph > ch && pw/cw <= ph/ch ||
|
|
ph > ch && pw <= cw ||
|
|
pw < cw && ph < ch && cw/pw > ch/ph)
|
|
currentImage = sourceImage.scaledToHeight(ch, Qt::TransformationMode::FastTransformation);
|
|
|
|
int x = (cw - currentImage.width())/2, y = (ch - currentImage.height())/2;
|
|
|
|
QPainter paint(this);
|
|
paint.drawPixmap(x, y, currentImage);
|
|
}
|