-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
67 lines (59 loc) · 1.83 KB
/
window.cpp
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
#include "window.h"
#include "ui_window.h"
window::window(QWidget *parent)
: QWidget(parent)
, ui(new Ui::window)
{
ui->setupUi(this);
}
window::~window()
{
delete ui;
}
QImage window::blurImage(QImage imageSource, int blurValue)
{
if(imageSource.isNull()) return QImage();
QGraphicsScene scene;
QGraphicsPixmapItem item;
item.setPixmap(QPixmap::fromImage(imageSource));
auto* blur = new QGraphicsBlurEffect;
blur->setBlurRadius(blurValue);
item.setGraphicsEffect(blur);
scene.addItem(&item);
QImage result (imageSource.size(), QImage::Format_ARGB32);
result.fill(Qt::transparent);
QPainter painter(&result);
scene.render(&painter, QRectF(),
QRectF(0, 0, imageSource.width(), imageSource.height()));
return result;
}
void window::processNewImage(QString sourceFile, int blurValue)
{
ui->label->setPixmap(QPixmap::fromImage(blurImage(QImage(sourceFile), blurValue).scaled(
ui->label->width(),
ui->label->height(),
Qt::KeepAspectRatio)));
}
void window::openFile()
{
imageSourcePath = QFileDialog::getOpenFileName(nullptr,
"Open image to blur",
"",
"image files (*.jpg)"
);
if(QFileInfo::exists(imageSourcePath))
{
processNewImage(imageSourcePath, ui->horizontalSlider->value());
}
}
void window::blurImage()
{
if(QFileInfo::exists(imageSourcePath))
{
processNewImage(imageSourcePath, ui->horizontalSlider->value());
}
else
{
ui->label->setText("Image path is not set. Please press \"Open image\" button.");
}
}