-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageprocessor.cpp
104 lines (81 loc) · 2.68 KB
/
imageprocessor.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
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
#include "imageprocessor.h"
#include <QPainter>
#include <QPainterPath>
#include <QBrush>
#include <QBitmap>
bool ImageProcessor::grayscale = false;
// ctor
ImageProcessor::ImageProcessor(QObject *parent) : QObject(parent)
{
networkManager = new QNetworkAccessManager(this);
connect(networkManager, &QNetworkAccessManager::finished, this, &ImageProcessor::onDownloadFinished);
}
/////////////////////////////////////
// Public Slots
/////////////////////////////////////
// Triggered upon new album art URL
void ImageProcessor::onNewUrl(const QString &url) {
startImageDownload(url);
}
// TODO: change to onMusicPaused and onMusicStarted
void ImageProcessor::onMusicToggled(const bool on)
{
grayscale = !on;
qDebug() << "Set grayscale to " << grayscale;
}
/////////////////////////////////////
// Private Slots
/////////////////////////////////////
//TODO: split out into helper fxns
// slot to process image and update UI upon new album art download
void ImageProcessor::onDownloadFinished(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Error in album art download";
return;
}
QPixmap pixmap;
pixmap.loadFromData(reply->readAll());
pixmap = roundImageCorners(pixmap);
QImage roundedImage = pixmap.toImage();
if(grayscale)
{
roundedImage = roundedImage.convertToFormat(QImage::Format_Grayscale8);
}
QString path = saveImage(roundedImage);
emit imageReady(path);
}
/////////////////////////////////////
// Helpers
/////////////////////////////////////
// fxn to round image corners using QPainter
QPixmap ImageProcessor::roundImageCorners(const QPixmap &image)
{
QPixmap roundedImage(image.size());
roundedImage.fill(Qt::transparent);
QPainter painter(&roundedImage);
painter.setRenderHint(QPainter::Antialiasing);
QPainterPath path;
path.addRoundedRect(image.rect(), 50, 50);
painter.setClipPath(path);
painter.drawPixmap(0, 0, image);
return roundedImage;
}
// fxn to save image to file system
QString ImageProcessor::saveImage(const QImage &img)
{
QString tempPath = "/tmp/image.png";
img.save(tempPath, "PNG");
QString imagePath = QUrl::fromLocalFile(tempPath).toString();
// A little hacky:
// everything after ? is ignored by the system, but emitted image path
// appears to change so the GUI refreshes the image
QString dynamicImagePath = imagePath + "?" + QString::number(QDateTime::currentMSecsSinceEpoch());
return dynamicImagePath;
}
// fxn to launch NetworkManager image download
void ImageProcessor::startImageDownload(const QString &url)
{
networkManager->get(QNetworkRequest(QUrl(url)));
}