forked from NautiluX/slide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
209 lines (184 loc) · 6.42 KB
/
mainwindow.cpp
File metadata and controls
209 lines (184 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "mainwindow.h"
#include "overlay.h"
#include "ui_mainwindow.h"
#include "exifhelper.h"
#include <QLabel>
#include <QPixmap>
#include <QBitmap>
#include <QKeyEvent>
#include <QGraphicsBlurEffect>
#include <iostream>
#include <QPainter>
#include <QTimer>
#include <QPropertyAnimation>
#include <QRect>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <sstream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_exifHelper(new ExifHelper())
{
ui->setupUi(this);
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
QTimer::singleShot(5, this, SLOT(showFullScreen()));
QApplication::setOverrideCursor(Qt::BlankCursor);
QLabel *label = this->findChild<QLabel*>("image");
setCentralWidget(label);
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
update();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_Escape)
{
QCoreApplication::quit();
}
else
QWidget::keyPressEvent(event);
}
void MainWindow::resizeEvent(QResizeEvent* event)
{
QMainWindow::resizeEvent(event);
updateImage(true);
}
void MainWindow::setImage(std::string path)
{
currentImage = path;
m_exifHelper->setImage(path);
updateImage(false);
}
void MainWindow::updateImage(bool immediately)
{
if (currentImage == "")
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 rotated = getRotatedPixmap(p);
QPixmap scaled = getScaledPixmap(rotated);
QPixmap background = getBlurredBackground(rotated, scaled);
drawForeground(background, scaled);
if (overlay != NULL)
{
drawText(background, overlay->getMarginTopLeft(), overlay->getFontsizeTopLeft(), overlay->getRenderedTopLeft(currentImage).c_str(), Qt::AlignTop|Qt::AlignLeft);
drawText(background, overlay->getMarginTopRight(), overlay->getFontsizeTopRight(), overlay->getRenderedTopRight(currentImage).c_str(), Qt::AlignTop|Qt::AlignRight);
drawText(background, overlay->getMarginBottomLeft(), overlay->getFontsizeBottomLeft(), overlay->getRenderedBottomLeft(currentImage).c_str(), Qt::AlignBottom|Qt::AlignLeft);
drawText(background, overlay->getMarginBottomRight(), overlay->getFontsizeBottomRight(), overlay->getRenderedBottomRight(currentImage).c_str(), Qt::AlignBottom|Qt::AlignRight);
}
label->setPixmap(background);
if (oldImage != NULL && !immediately)
{
auto effect = new QGraphicsOpacityEffect(label);
effect->setOpacity(0.0);
label->setGraphicsEffect(effect);
QPropertyAnimation* animation = new QPropertyAnimation(effect, "opacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
update();
}
void MainWindow::drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment) {
//std::cout << "text: " << text.toStdString() << " margin: " << margin << " fontsize: " << fontsize<< std::endl;
QPainter pt(&image);
pt.setPen(QPen(Qt::white));
pt.setFont(QFont("Sans", fontsize, QFont::Bold));
QRect marginRect = image.rect().adjusted(
margin,
margin,
margin*-1,
margin*-1);
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)
{
overlay = o;
overlay->setExifHelper(m_exifHelper);
}
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)
{
QMatrix matrix;
matrix.rotate(m_exifHelper->getImageRotation());
return p.transformed(matrix);
}
QPixmap MainWindow::getScaledPixmap(const QPixmap& p)
{
int w = width();
int h = height();
return p.scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
void MainWindow::drawBackground(const QPixmap& originalSize, const QPixmap& scaled)
{
QPalette palette;
if (scaled.width() < width()) {
QPixmap background = blur(originalSize.scaledToHeight(height()));
QRect rect((background.width() - width())/2, 0, width(), height());
background = background.copy(rect);
palette.setBrush(QPalette::Background, background);
} else {
QPixmap background = blur(originalSize.scaledToHeight(height()));
QRect rect((background.width() - width())/2, 0, width(), height());
background = background.copy(rect);
palette.setBrush(QPalette::Background, background);
}
this->setPalette(palette);
}
QPixmap MainWindow::blur(const QPixmap& input)
{
QGraphicsScene scene;
QGraphicsPixmapItem item;
item.setPixmap(input);
QGraphicsBlurEffect effect;
effect.setBlurRadius(blurRadius);
item.setGraphicsEffect(&effect);
scene.addItem(&item);
QImage res(input.size(), QImage::Format_ARGB32);
res.fill(Qt::transparent);
QPainter ptr(&res);
scene.render(&ptr, QRectF(), QRectF( 0, 0, input.width(), input.height()) );
return QPixmap::fromImage(res);
}
void MainWindow::setBlurRadius(unsigned int blurRadius)
{
this->blurRadius = blurRadius;
}
void MainWindow::setBackgroundOpacity(unsigned int backgroundOpacity)
{
this->backgroundOpacity = backgroundOpacity;
}
void MainWindow::warn(std::string text)
{
QLabel *label = this->findChild<QLabel*>("image");
label->setText(text.c_str());
}