-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmypixmap.h
94 lines (77 loc) · 2.04 KB
/
mypixmap.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
90
91
92
93
94
#ifndef MYPIXMAP_H
#define MYPIXMAP_H
#include "values/AsyncValueRunable.h"
#include "values/AsyncValueRunThread.h"
#include "widgets/AsyncWidget.h"
#include <QBitmap>
class MyPixmap : public AsyncValueRunableAbstract<QPixmap>
{
public:
MyPixmap()
: AsyncValueRunableAbstract<QPixmap>(AsyncInitByError{}, "Select image file path.")
{
}
QString imageUrl() const
{
QReadLocker locker(&m_urlLock);
return m_imageUrl;
}
void setImageUrl(QString url)
{
{
QWriteLocker locker(&m_urlLock);
m_imageUrl = url;
}
// reload image
run();
}
protected:
// hide run from public
using AsyncValueRunableAbstract<QPixmap>::run;
void deferImpl(RunFnType&& func) final
{
asyncValueRunThread(*this, func, "Loading image...", ASYNC_CAN_REQUEST_STOP::NO);
}
void runImpl(ProgressType& progress) final
{
auto url = imageUrl();
QImage image(url);
for (auto i : {0, 1, 2, 3})
{
if (progress.isRerunRequested())
{
// exit and retry load image with new path
return ;
}
progress.setProgress(i, 4);
// do some heavy work
QThread::sleep(1);
}
if (image.isNull())
emplaceError(QString("Cannot load image from file '%1'.").arg(url));
else
emplaceValue(QPixmap::fromImage(image));
}
private:
mutable QReadWriteLock m_urlLock;
QString m_imageUrl;
};
class MyPixmapWidget : public AsyncWidget<MyPixmap>
{
public:
MyPixmapWidget(QWidget* parent, MyPixmap* value)
: AsyncWidget<MyPixmap>(parent)
{
setValue(value);
}
protected:
QWidget* createValueWidgetImpl(ValueType& value, QWidget* parent) final
{
auto label = new QLabel(parent);
label->setAlignment(Qt::AlignCenter);
label->setPixmap(value);
label->setStyleSheet("border: 1px solid black");
return label;
}
};
#endif // MYPIXMAP_H