-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageprovider.cpp
120 lines (97 loc) · 3.22 KB
/
imageprovider.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright 2023 Sergey Levin <serjth@levz.dev>
This file is part of Image Finder.
Image Finder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Image Finder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Image Finder. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
*/
#include "imageprovider.h"
#include "logging.h"
#include "imagecache.h"
// Ref. https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quick/imageresponseprovider?h=6.3
namespace {
class AsyncImageResponseRunnable : public QObject, public QRunnable
{
Q_OBJECT
public:
AsyncImageResponseRunnable(const QString &id, const QSize &requestedSize)
: mId(id),
mRequestedSize(requestedSize)
{
setAutoDelete(true);
}
void run() override
{
QImage image(mId);
if (image.isNull()) {
qCDebug(Log::main) << "Unable to open image:" << mId;
image.load(QString(":/images/error%1x%2.png").arg(mRequestedSize.width()).arg(mRequestedSize.height()));
} else {
image = image.scaled(mRequestedSize, Qt::KeepAspectRatio);
}
emit done(image);
}
signals:
void done(QImage image);
private:
QString mId;
QSize mRequestedSize;
};
class AsyncImageResponse : public QQuickImageResponse
{
Q_OBJECT
public:
AsyncImageResponse(const QString &id, const QSize &requestedSize, QThreadPool *pool):
mId(id),
mRequestedSize(requestedSize)
{
auto runnable = new AsyncImageResponseRunnable(id, requestedSize);
connect(runnable, &AsyncImageResponseRunnable::done, this, &AsyncImageResponse::handleDone);
pool->start(runnable);
}
AsyncImageResponse(const QImage &image):
mImage(image)
{
emit finished();
}
void handleDone(QImage image) {
mImage = image;
emit finished();
emit imageLoaded(mId, mRequestedSize, mImage);
}
QQuickTextureFactory *textureFactory() const override
{
return QQuickTextureFactory::textureFactoryForImage(mImage);
}
signals:
void imageLoaded(const QString &, const QSize &, const QImage &);
private:
QString mId;
QSize mRequestedSize;
QImage mImage;
};
} // namespace
QQuickImageResponse *ImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
{
auto image = ImageCache::instance().get(id, requestedSize);
AsyncImageResponse *response = nullptr;
if (image.isNull()) {
response = new AsyncImageResponse(id, requestedSize, &mPool);
QObject::connect(response, &AsyncImageResponse::imageLoaded, &ImageCache::instance(), &ImageCache::add);
} else {
response = new AsyncImageResponse(image);
}
return response;
}
void ImageProvider::resetCache()
{
ImageCache::instance().reset();
}
#include "imageprovider.moc"