fade transitioning

This commit is contained in:
Manuel Dewald
2021-03-04 16:22:53 +01:00
parent d4beb46f28
commit 5d6d9a804d
2 changed files with 57 additions and 18 deletions

View File

@@ -10,6 +10,7 @@
#include <iostream> #include <iostream>
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
#include <QPropertyAnimation>
#include <QRect> #include <QRect>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsPixmapItem> #include <QGraphicsPixmapItem>
@@ -49,13 +50,13 @@ void MainWindow::keyPressEvent(QKeyEvent* event)
void MainWindow::resizeEvent(QResizeEvent* event) void MainWindow::resizeEvent(QResizeEvent* event)
{ {
QMainWindow::resizeEvent(event); QMainWindow::resizeEvent(event);
updateImage(); updateImage(true);
} }
void MainWindow::setImage(std::string path) void MainWindow::setImage(std::string path)
{ {
currentImage = path; currentImage = path;
updateImage(); updateImage(false);
} }
int MainWindow::getImageRotation() int MainWindow::getImageRotation()
@@ -93,31 +94,47 @@ int MainWindow::getImageRotation()
return degrees; return degrees;
} }
void MainWindow::updateImage() void MainWindow::updateImage(bool immediately)
{ {
if (currentImage == "") if (currentImage == "")
return; return;
QLabel *label = this->findChild<QLabel*>("image");
const QPixmap* oldImage = label->pixmap();
if (oldImage != NULL && !immediately)
{
QPalette palette;
palette.setBrush(QPalette::Background, *oldImage);
this->setPalette(palette);
}
QPixmap p( currentImage.c_str() ); QPixmap p( currentImage.c_str() );
QPixmap rotated = getRotatedPixmap(p); QPixmap rotated = getRotatedPixmap(p);
QPixmap scaled = getScaledPixmap(rotated); QPixmap scaled = getScaledPixmap(rotated);
QPixmap background = getBlurredBackground(rotated, scaled);
drawForeground(background, scaled);
if (overlay != NULL) if (overlay != NULL)
{ {
drawText(scaled, overlay->getMarginTopLeft(), overlay->getFontsizeTopLeft(), overlay->getRenderedTopLeft(currentImage).c_str(), Qt::AlignTop|Qt::AlignLeft); drawText(background, overlay->getMarginTopLeft(), overlay->getFontsizeTopLeft(), overlay->getRenderedTopLeft(currentImage).c_str(), Qt::AlignTop|Qt::AlignLeft);
drawText(scaled, overlay->getMarginTopRight(), overlay->getFontsizeTopRight(), overlay->getRenderedTopRight(currentImage).c_str(), Qt::AlignTop|Qt::AlignRight); drawText(background, overlay->getMarginTopRight(), overlay->getFontsizeTopRight(), overlay->getRenderedTopRight(currentImage).c_str(), Qt::AlignTop|Qt::AlignRight);
drawText(scaled, overlay->getMarginBottomLeft(), overlay->getFontsizeBottomLeft(), overlay->getRenderedBottomLeft(currentImage).c_str(), Qt::AlignBottom|Qt::AlignLeft); drawText(background, overlay->getMarginBottomLeft(), overlay->getFontsizeBottomLeft(), overlay->getRenderedBottomLeft(currentImage).c_str(), Qt::AlignBottom|Qt::AlignLeft);
drawText(scaled, overlay->getMarginBottomRight(), overlay->getFontsizeBottomRight(), overlay->getRenderedBottomRight(currentImage).c_str(), Qt::AlignBottom|Qt::AlignRight); drawText(background, overlay->getMarginBottomRight(), overlay->getFontsizeBottomRight(), overlay->getRenderedBottomRight(currentImage).c_str(), Qt::AlignBottom|Qt::AlignRight);
} }
QLabel *label = this->findChild<QLabel*>("image"); label->setPixmap(background);
label->setPixmap(scaled);
std::stringstream style; if (oldImage != NULL && !immediately)
style << "QLabel { background-color: rgba(0, 0, 0, " << (255 - backgroundOpacity) << ");}"; {
label->setStyleSheet(style.str().c_str()); auto effect = new QGraphicsOpacityEffect(label);
effect->setOpacity(0.0);
drawBackground(rotated, scaled); label->setGraphicsEffect(effect);
QPropertyAnimation* animation = new QPropertyAnimation(effect, "opacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
update(); update();
} }
@@ -135,11 +152,31 @@ void MainWindow::drawText(QPixmap& image, int margin, int fontsize, QString text
pt.drawText(marginRect, alignment, text); pt.drawText(marginRect, alignment, text);
} }
void MainWindow::drawForeground(QPixmap& background, const QPixmap& foreground) {
QPainter pt(&background);
QBrush brush(QColor(0, 0, 0, 255-backgroundOpacity));
pt.fillRect(0,0,background.width(), background.height(), brush);
pt.drawPixmap((background.width()-foreground.width())/2, (background.height()-foreground.height())/2, foreground);
}
void MainWindow::setOverlay(Overlay* o) void MainWindow::setOverlay(Overlay* o)
{ {
overlay = o; overlay = o;
} }
QPixmap MainWindow::getBlurredBackground(const QPixmap& originalSize, const QPixmap& scaled)
{
if (scaled.width() < width()) {
QPixmap background = blur(originalSize.scaledToWidth(width(), Qt::SmoothTransformation));
QRect rect(0, (background.height() - height())/2, width(), height());
return background.copy(rect);
} else {
QPixmap background = blur(originalSize.scaledToHeight(height(), Qt::SmoothTransformation));
QRect rect((background.width() - width())/2, 0, width(), height());
return background.copy(rect);
}
}
QPixmap MainWindow::getRotatedPixmap(const QPixmap& p) QPixmap MainWindow::getRotatedPixmap(const QPixmap& p)
{ {
QMatrix matrix; QMatrix matrix;
@@ -158,8 +195,8 @@ void MainWindow::drawBackground(const QPixmap& originalSize, const QPixmap& scal
{ {
QPalette palette; QPalette palette;
if (scaled.width() < width()) { if (scaled.width() < width()) {
QPixmap background = blur(originalSize.scaledToWidth(width())); QPixmap background = blur(originalSize.scaledToHeight(height()));
QRect rect(0, (background.height() - height())/2, width(), height()); QRect rect((background.width() - width())/2, 0, width(), height());
background = background.copy(rect); background = background.copy(rect);
palette.setBrush(QPalette::Background, background); palette.setBrush(QPalette::Background, background);
} else { } else {

View File

@@ -37,12 +37,14 @@ private:
void drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment); void drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment);
void updateImage(); void updateImage(bool immediately);
int getImageRotation(); int getImageRotation();
QPixmap getBlurredBackground(const QPixmap& originalSize, const QPixmap& scaled);
QPixmap getRotatedPixmap(const QPixmap& p); QPixmap getRotatedPixmap(const QPixmap& p);
QPixmap getScaledPixmap(const QPixmap& p); QPixmap getScaledPixmap(const QPixmap& p);
void drawBackground(const QPixmap& originalSize, const QPixmap& scaled); void drawBackground(const QPixmap& originalSize, const QPixmap& scaled);
void drawForeground(QPixmap& background, const QPixmap& foreground);
QPixmap blur(const QPixmap& input); QPixmap blur(const QPixmap& input);
}; };