-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageloader.h
89 lines (71 loc) · 2.36 KB
/
imageloader.h
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
#pragma once
#include <QObject>
#include <QLoggingCategory>
#include <QImage>
#include <QWaitCondition>
#include <memory>
#include <functional>
class ImageLoaderJob;
using ImageLoaderCallback = std::function<void(const ImageLoaderJob &)>;
// ImageLoaderJob is a strong reference to a pending or completed job for an ImageLoader.
// Jobs are reference counted, and will be aborted if no references remain when the job
// reaches the front of the queue.
struct ImageLoaderJobData
{
QString path;
QSize drawSize;
int priority;
ImageLoaderCallback callback;
std::shared_ptr<QImage> result;
QSize resultSize;
QString error;
};
class ImageLoaderJob
{
friend class ImageLoader;
friend class ImageLoaderPrivate;
public:
ImageLoaderJob() { }
ImageLoaderJob(const ImageLoaderJob &o) : d(o.d) { }
~ImageLoaderJob() { }
bool isNull() const { return !d; }
void reset() { d.reset(); }
QString path() const { return d ? d->path : QString(); }
QSize drawSize() const { return d ? d->drawSize : QSize(); }
int priority() const { return d ? d->priority : 0; }
ImageLoaderCallback callback() const { return d ? d->callback : ImageLoaderCallback(); }
void setDrawSize(const QSize &size)
{
if (d) d->drawSize = size;
}
bool finished() const { return d ? (d->result || !d->error.isEmpty()) : false; }
QImage result() const { return d && d->result ? *d->result : QImage(); }
QSize imageSize() const { return d ? d->resultSize : QSize(); }
QString error() const { return d ? d->error : QString(); }
private:
std::shared_ptr<ImageLoaderJobData> d;
ImageLoaderJob(const std::shared_ptr<ImageLoaderJobData> &d)
: d(d)
{
}
ImageLoaderJob(const QString &path, const QSize &drawSize, int priority, ImageLoaderCallback callback)
: d(std::make_shared<ImageLoaderJobData>())
{
d->path = path;
d->drawSize = drawSize;
d->priority = priority;
d->callback = callback;
}
};
class ImageLoaderPrivate;
class ImageLoader : public QObject
{
Q_OBJECT
public:
explicit ImageLoader(QObject *parent = nullptr);
virtual ~ImageLoader();
ImageLoaderJob enqueue(const QString &path, const QSize &drawSize, int priority, ImageLoaderCallback callback);
private:
std::shared_ptr<ImageLoaderPrivate> d;
};
Q_DECLARE_LOGGING_CATEGORY(lcImageLoad)