-
Notifications
You must be signed in to change notification settings - Fork 0
/
cover_image_provider.cpp
42 lines (34 loc) · 1.17 KB
/
cover_image_provider.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
#include "cover_image_provider.h"
#include <QImage>
#include <QDir>
#include <QFileInfo>
CoverImageProvider::CoverImageProvider() : QQuickImageProvider(QQuickImageProvider::Image)
{
}
QImage CoverImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
int width = requestedSize.isValid() ? requestedSize.width() : 1024;
int height = requestedSize.isValid() ? requestedSize.height() : 1024;
QFileInfo fileInfo(id);
QStringList coverFileNames{"cover.jpg", "folder.jpg", "cover.png", "cover.jpeg"};
QString imagePath;
for (const QString &fileName : coverFileNames) {
imagePath = fileInfo.path() + "/" + fileName;
if (QFile::exists(imagePath)) {
break;
}
}
if (m_cache.contains(imagePath)) {
return m_cache.value(imagePath);
}
QImage image;
if (QFile::exists(imagePath)) {
image.load(imagePath);
image = image.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
} else {
image = QImage(width, height, QImage::Format_RGB32);
image.fill(Qt::black);
}
m_cache.insert(imagePath, image);
return image;
}