-
Notifications
You must be signed in to change notification settings - Fork 87
/
PictureImageProvider.cpp
53 lines (43 loc) · 1.73 KB
/
PictureImageProvider.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
#include "PictureImageProvider.h"
#include "PictureModel.h"
const QString PICTURE_SIZE_FULL = "full";
const QString PICTURE_SIZE_THUMBNAIL = "thumbnail";
const QSize PictureImageProvider::THUMBNAIL_SIZE = QSize(350, 350);
PictureImageProvider::PictureImageProvider(PictureModel* pictureModel) :
QQuickImageProvider(QQuickImageProvider::Pixmap),
mPictureModel(pictureModel),
mPicturesCache()
{
}
QPixmap PictureImageProvider::requestPixmap(const QString& id, QSize* /*size*/, const QSize& /*requestedSize*/)
{
QStringList query = id.split('/');
if (!mPictureModel || query.size() < 2) {
return QPixmap();
}
int rowId = query[0].toInt();
QString pictureSize = query[1];
QUrl fileUrl = mPictureModel->data(mPictureModel->index(rowId, 0), PictureModel::Roles::UrlRole).toUrl();
return *pictureFromCache(fileUrl.toLocalFile(), pictureSize);
}
QPixmap* PictureImageProvider::pictureFromCache(const QString& filepath, const QString& pictureSize)
{
QString key = QStringList{ pictureSize, filepath }
.join("-");
QPixmap* cachePicture = nullptr;
if (!mPicturesCache.contains(key)) {
QPixmap originalPicture(filepath);
if (pictureSize == PICTURE_SIZE_THUMBNAIL) {
cachePicture = new QPixmap(originalPicture
.scaled(THUMBNAIL_SIZE,
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
} else if (pictureSize == PICTURE_SIZE_FULL) {
cachePicture = new QPixmap(originalPicture);
}
mPicturesCache.insert(key, cachePicture);
} else {
cachePicture = mPicturesCache[key];
}
return cachePicture;
}