Skip to content

Commit 5d6d9a8

Browse files
author
Manuel Dewald
committed
fade transitioning
1 parent d4beb46 commit 5d6d9a8

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

src/mainwindow.cpp

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <iostream>
1111
#include <QPainter>
1212
#include <QTimer>
13+
#include <QPropertyAnimation>
1314
#include <QRect>
1415
#include <QGraphicsScene>
1516
#include <QGraphicsPixmapItem>
@@ -49,13 +50,13 @@ void MainWindow::keyPressEvent(QKeyEvent* event)
4950
void MainWindow::resizeEvent(QResizeEvent* event)
5051
{
5152
QMainWindow::resizeEvent(event);
52-
updateImage();
53+
updateImage(true);
5354
}
5455

5556
void MainWindow::setImage(std::string path)
5657
{
5758
currentImage = path;
58-
updateImage();
59+
updateImage(false);
5960
}
6061

6162
int MainWindow::getImageRotation()
@@ -93,31 +94,47 @@ int MainWindow::getImageRotation()
9394
return degrees;
9495
}
9596

96-
void MainWindow::updateImage()
97+
void MainWindow::updateImage(bool immediately)
9798
{
9899
if (currentImage == "")
99-
return;
100+
return;
101+
102+
QLabel *label = this->findChild<QLabel*>("image");
103+
const QPixmap* oldImage = label->pixmap();
104+
if (oldImage != NULL && !immediately)
105+
{
106+
QPalette palette;
107+
palette.setBrush(QPalette::Background, *oldImage);
108+
this->setPalette(palette);
109+
}
100110

101111
QPixmap p( currentImage.c_str() );
102112
QPixmap rotated = getRotatedPixmap(p);
103113
QPixmap scaled = getScaledPixmap(rotated);
114+
QPixmap background = getBlurredBackground(rotated, scaled);
115+
drawForeground(background, scaled);
104116

105117
if (overlay != NULL)
106118
{
107-
drawText(scaled, overlay->getMarginTopLeft(), overlay->getFontsizeTopLeft(), overlay->getRenderedTopLeft(currentImage).c_str(), Qt::AlignTop|Qt::AlignLeft);
108-
drawText(scaled, overlay->getMarginTopRight(), overlay->getFontsizeTopRight(), overlay->getRenderedTopRight(currentImage).c_str(), Qt::AlignTop|Qt::AlignRight);
109-
drawText(scaled, overlay->getMarginBottomLeft(), overlay->getFontsizeBottomLeft(), overlay->getRenderedBottomLeft(currentImage).c_str(), Qt::AlignBottom|Qt::AlignLeft);
110-
drawText(scaled, overlay->getMarginBottomRight(), overlay->getFontsizeBottomRight(), overlay->getRenderedBottomRight(currentImage).c_str(), Qt::AlignBottom|Qt::AlignRight);
119+
drawText(background, overlay->getMarginTopLeft(), overlay->getFontsizeTopLeft(), overlay->getRenderedTopLeft(currentImage).c_str(), Qt::AlignTop|Qt::AlignLeft);
120+
drawText(background, overlay->getMarginTopRight(), overlay->getFontsizeTopRight(), overlay->getRenderedTopRight(currentImage).c_str(), Qt::AlignTop|Qt::AlignRight);
121+
drawText(background, overlay->getMarginBottomLeft(), overlay->getFontsizeBottomLeft(), overlay->getRenderedBottomLeft(currentImage).c_str(), Qt::AlignBottom|Qt::AlignLeft);
122+
drawText(background, overlay->getMarginBottomRight(), overlay->getFontsizeBottomRight(), overlay->getRenderedBottomRight(currentImage).c_str(), Qt::AlignBottom|Qt::AlignRight);
111123
}
112124

113-
QLabel *label = this->findChild<QLabel*>("image");
114-
label->setPixmap(scaled);
125+
label->setPixmap(background);
115126

116-
std::stringstream style;
117-
style << "QLabel { background-color: rgba(0, 0, 0, " << (255 - backgroundOpacity) << ");}";
118-
label->setStyleSheet(style.str().c_str());
119-
120-
drawBackground(rotated, scaled);
127+
if (oldImage != NULL && !immediately)
128+
{
129+
auto effect = new QGraphicsOpacityEffect(label);
130+
effect->setOpacity(0.0);
131+
label->setGraphicsEffect(effect);
132+
QPropertyAnimation* animation = new QPropertyAnimation(effect, "opacity");
133+
animation->setDuration(1000);
134+
animation->setStartValue(0);
135+
animation->setEndValue(1);
136+
animation->start(QAbstractAnimation::DeleteWhenStopped);
137+
}
121138

122139
update();
123140
}
@@ -135,11 +152,31 @@ void MainWindow::drawText(QPixmap& image, int margin, int fontsize, QString text
135152
pt.drawText(marginRect, alignment, text);
136153
}
137154

155+
void MainWindow::drawForeground(QPixmap& background, const QPixmap& foreground) {
156+
QPainter pt(&background);
157+
QBrush brush(QColor(0, 0, 0, 255-backgroundOpacity));
158+
pt.fillRect(0,0,background.width(), background.height(), brush);
159+
pt.drawPixmap((background.width()-foreground.width())/2, (background.height()-foreground.height())/2, foreground);
160+
}
161+
138162
void MainWindow::setOverlay(Overlay* o)
139163
{
140164
overlay = o;
141165
}
142166

167+
QPixmap MainWindow::getBlurredBackground(const QPixmap& originalSize, const QPixmap& scaled)
168+
{
169+
if (scaled.width() < width()) {
170+
QPixmap background = blur(originalSize.scaledToWidth(width(), Qt::SmoothTransformation));
171+
QRect rect(0, (background.height() - height())/2, width(), height());
172+
return background.copy(rect);
173+
} else {
174+
QPixmap background = blur(originalSize.scaledToHeight(height(), Qt::SmoothTransformation));
175+
QRect rect((background.width() - width())/2, 0, width(), height());
176+
return background.copy(rect);
177+
}
178+
}
179+
143180
QPixmap MainWindow::getRotatedPixmap(const QPixmap& p)
144181
{
145182
QMatrix matrix;
@@ -158,8 +195,8 @@ void MainWindow::drawBackground(const QPixmap& originalSize, const QPixmap& scal
158195
{
159196
QPalette palette;
160197
if (scaled.width() < width()) {
161-
QPixmap background = blur(originalSize.scaledToWidth(width()));
162-
QRect rect(0, (background.height() - height())/2, width(), height());
198+
QPixmap background = blur(originalSize.scaledToHeight(height()));
199+
QRect rect((background.width() - width())/2, 0, width(), height());
163200
background = background.copy(rect);
164201
palette.setBrush(QPalette::Background, background);
165202
} else {

src/mainwindow.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ class MainWindow : public QMainWindow
3737

3838
void drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment);
3939

40-
void updateImage();
40+
void updateImage(bool immediately);
4141
int getImageRotation();
4242

43+
QPixmap getBlurredBackground(const QPixmap& originalSize, const QPixmap& scaled);
4344
QPixmap getRotatedPixmap(const QPixmap& p);
4445
QPixmap getScaledPixmap(const QPixmap& p);
4546
void drawBackground(const QPixmap& originalSize, const QPixmap& scaled);
47+
void drawForeground(QPixmap& background, const QPixmap& foreground);
4648
QPixmap blur(const QPixmap& input);
4749
};
4850

0 commit comments

Comments
 (0)