This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
qaimagewriter.cpp
116 lines (94 loc) · 2.39 KB
/
qaimagewriter.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
#include <QtCore>
#include <QRunnable>
#include <QImageWriter>
#include "qaimagewriter.h"
class QAImageWriterRunnable : public QRunnable {
public:
QPointer<QAImageWriter> owner;
QImage image;
QString fileUrl;
void run() {
if (fileUrl.isNull()) { // Save as temp
QTemporaryFile tmp;
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
QString tmpPath = paths.at(0);
tmp.setFileTemplate(tmpPath + "/XXXXXX.jpg");
tmp.open();
fileUrl = tmp.fileName();
tmp.close();
}
image.save(fileUrl);
QImageWriter writer;
writer.setFileName(fileUrl);
if (!writer.write(image)) {
qWarning() << QString("Failed to save %1 : %2").arg(fileUrl).arg(writer.errorString());
}
if (!owner.isNull()) {
QMetaObject::invokeMethod(owner.data(),"endSave",Qt::QueuedConnection,
Q_ARG(QString,fileUrl));
}
}
};
QAImageWriter::QAImageWriter(QObject *parent) : QObject(parent)
{
m_running = false;
}
bool QAImageWriter::running() const
{
return m_running;
}
void QAImageWriter::setRunning(bool running)
{
m_running = running;
emit runningChanged();
}
QString QAImageWriter::fileUrl() const
{
return m_fileUrl;
}
void QAImageWriter::setFileUrl(const QString &fileUrl)
{
m_fileUrl = fileUrl;
emit fileUrlChanged();
}
QImage QAImageWriter::image() const
{
return m_image;
}
void QAImageWriter::setImage(const QImage &image)
{
m_image = image;
emit imageChanged();
}
void QAImageWriter::save(QString fileUrl)
{
if (!fileUrl.isNull()) {
setFileUrl(fileUrl);
}
if (m_running) {
qWarning() << "ImageWriter::save() - It is already running.";
return;
}
if (m_image.isNull()) {
qWarning() << "ImageWriter::save() - image is null.";
return;
}
setRunning(true);
QAImageWriterRunnable* saver = new QAImageWriterRunnable();
saver->setAutoDelete(true);
saver->owner = this;
saver->fileUrl = m_fileUrl;
saver->image = m_image;
QThreadPool::globalInstance()->start(saver);
}
void QAImageWriter::clear()
{
setImage(QImage());
setFileUrl("");
}
void QAImageWriter::endSave(QString fileUrl)
{
setFileUrl(fileUrl);
setRunning(false);
emit finished();
}