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 37
/
qiimagepicker.cpp
270 lines (215 loc) · 5.8 KB
/
qiimagepicker.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <QStandardPaths>
#include <QFileDialog>
#include <QTemporaryFile>
#include <QUrl>
#include <QRunnable>
#include <QPointer>
#include <QThreadPool>
#include <QImageWriter>
#include <QImageReader>
#include "qisystemdispatcher.h"
#include "qiimagepicker.h"
class QIImagePickerSaver : public QRunnable {
public:
QPointer<QIImagePicker> owner;
QImage image;
QString fileName;
void run() {
if (fileName.isNull()) { // Save as temp
QTemporaryFile tmp;
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
QString tmpPath = paths.at(0);
tmp.setFileTemplate(tmpPath + "/XXXXXX.jpg");
tmp.open();
fileName = tmp.fileName();
tmp.close();
}
image.save(fileName);
QImageWriter writer;
writer.setFileName(fileName);
if (!writer.write(image)) {
qWarning() << QString("Failed to save %1 : %2").arg(fileName).arg(writer.errorString());
}
if (!owner.isNull()) {
QMetaObject::invokeMethod(owner.data(),"endSave",Qt::QueuedConnection,
Q_ARG(QString,fileName));
}
}
};
QIImagePicker::QIImagePicker(QQuickItem *parent) : QQuickItem(parent)
{
m_sourceType = PhotoLibrary;
m_status = Null;
m_busy = false;
}
QIImagePicker::~QIImagePicker()
{
}
void QIImagePicker::show(bool animated)
{
if (m_status == Running || m_status == Saving) {
return;
}
#ifdef Q_OS_IOS
QISystemDispatcher* system = QISystemDispatcher::instance();
QVariantMap data;
data["sourceType"] = m_sourceType;
data["animated"] = animated;
connect(system,SIGNAL(dispatched(QString,QVariantMap)),
this,SLOT(onReceived(QString,QVariantMap)));
bool res = system->dispatch("imagePickerControllerPresent",data);
if (res) {
setStatus(Running);
}
#else
Q_UNUSED(animated);
// For desktop preview
setStatus(Running);
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
QString file = QFileDialog::getOpenFileName (0,
tr("Import Image"),
paths.at(0),
"Images (*.png *.xpm *.jpg)");
if (file.isNull()) {
setStatus(Null);
} else {
QImageReader reader;
reader.setFileName(file);
QImage image = reader.read();
if (image.isNull()) {
setStatus(Null);
} else {
QUrl url = QUrl::fromLocalFile(file);
setImage(image);
setMediaType("public.image");
setReferenceUrl(url.toString());
setStatus(Ready);
emit ready();
}
}
#endif
}
void QIImagePicker::close(bool animated)
{
QISystemDispatcher* system = QISystemDispatcher::instance();
QVariantMap data;
data["animated"] = animated;
system->dispatch("imagePickerControllerDismiss",data);
}
void QIImagePicker::save(QString fileName)
{
QIImagePickerSaver* saver = new QIImagePickerSaver();
saver->setAutoDelete(true);
saver->owner = this;
saver->fileName = fileName;
saver->image = m_image;
QThreadPool::globalInstance()->start(saver);
}
void QIImagePicker::saveAsTemp()
{
save(QString());
}
void QIImagePicker::clear()
{
setImage(QImage());
setStatus(Null);
}
QIImagePicker::SourceType QIImagePicker::sourceType() const
{
return m_sourceType;
}
void QIImagePicker::setSourceType(const SourceType &sourceType)
{
m_sourceType = sourceType;
emit sourceTypeChanged();
}
QImage QIImagePicker::image() const
{
return m_image;
}
void QIImagePicker::setImage(const QImage &image)
{
m_image = image;
}
QIImagePicker::Status QIImagePicker::status() const
{
return m_status;
}
void QIImagePicker::setStatus(const Status &status)
{
if (m_status == status)
return;
m_status = status;
emit statusChanged();
}
void QIImagePicker::onReceived(QString name, QVariantMap data)
{
QISystemDispatcher* system = QISystemDispatcher::instance();
if (name == "imagePickerControllerDidCancel") {
setStatus(Null);
system->disconnect(this);
close();
}
if (name != "imagePickerControllerDisFinishPickingMetaWithInfo")
return;
system->disconnect(this);
QImage image = data["image"].value<QImage>();
setImage(image);
setMediaType(data["mediaType"].toString());
setMediaUrl(data["mediaUrl"].toString());
setReferenceUrl(data["referenceUrl"].toString());
setStatus(Ready);
emit ready();
}
void QIImagePicker::endSave(QString fileName)
{
QUrl url = QUrl::fromLocalFile(fileName);
emit saved(url.toString());
if (m_image.isNull()) {
setStatus(Null);
} else {
setStatus(Ready);
}
}
QString QIImagePicker::referenceUrl() const
{
return m_referenceUrl;
}
void QIImagePicker::setReferenceUrl(const QString &referenceUrl)
{
m_referenceUrl = referenceUrl;
emit referenceUrlChanged();
}
QString QIImagePicker::mediaUrl() const
{
return m_mediaUrl;
}
void QIImagePicker::setMediaUrl(const QString &mediaUrl)
{
m_mediaUrl = mediaUrl;
emit mediaUrlChanged();
}
QString QIImagePicker::mediaType() const
{
return m_mediaType;
}
void QIImagePicker::setMediaType(const QString &mediaType)
{
m_mediaType = mediaType;
emit mediaTypeChanged();
}
bool QIImagePicker::busy() const
{
return m_busy;
}
void QIImagePicker::setBusy(bool busy)
{
if (m_busy == busy)
return;
m_busy = busy;
QISystemDispatcher* system = QISystemDispatcher::instance();
QVariantMap data;
data["active"] = m_busy;
system->dispatch("imagePickerControllerSetIndicator",data);
emit busyChanged();
}