forked from benlau/quickios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqisystemutils.mm
398 lines (299 loc) · 13 KB
/
qisystemutils.mm
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <QCoreApplication>
#include <UIKit/UIKit.h>
#include <QPointer>
#include <QtCore>
#include <QImage>
#include "qisystemdispatcher.h"
#include "qiviewdelegate.h"
static bool isPad() {
return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
}
static QImage fromUIImage(UIImage* image) {
QImage::Format format = QImage::Format_RGB32;
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat width = image.size.width;
CGFloat height = image.size.height;
int orientation = [image imageOrientation];
int degree = 0;
switch (orientation) {
case UIImageOrientationLeft:
degree = -90;
break;
case UIImageOrientationDown: // Down
degree = 180;
break;
case UIImageOrientationRight:
degree = 90;
break;
}
if (degree == 90 || degree == -90) {
CGFloat tmp = width;
width = height;
height = tmp;
}
QSize size(width,height);
QImage result = QImage(size,format);
CGContextRef contextRef = CGBitmapContextCreate(result.bits(), // Pointer to data
width, // Width of bitmap
height, // Height of bitmap
8, // Bits per component
result.bytesPerLine(), // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipFirst |
kCGBitmapByteOrder32Little); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), image.CGImage);
CGContextRelease(contextRef);
if (degree != 0) {
QTransform myTransform;
myTransform.rotate(degree);
result = result.transformed(myTransform,Qt::SmoothTransformation);
}
return result;
}
static QString fromNSUrl(NSURL* url) {
QString result = QString::fromNSString([url absoluteString]);
return result;
}
static UIViewController* rootViewController() {
UIApplication* app = [UIApplication sharedApplication];
return app.keyWindow.rootViewController;
}
static UIAlertView* alertView = nil;
static QIViewDelegate* alertViewDelegate = nil;
static void alertViewDismiss(int buttonIndex) {
QString name = "alertViewClickedButtonAtIndex";
QVariantMap data;
data["buttonIndex"] = buttonIndex;
QISystemDispatcher* m_instance = QISystemDispatcher::instance();
QMetaObject::invokeMethod(m_instance,"dispatched",Qt::DirectConnection,
Q_ARG(QString , name),
Q_ARG(QVariantMap,data));
alertViewDelegate = nil;
alertView = nil;
}
static bool alertViewCreate(QVariantMap& data) {
Q_UNUSED(data);
alertViewDelegate = [QIViewDelegate alloc];
alertViewDelegate->alertViewClickedButtonAtIndex = ^(int buttonIndex) {
alertViewDismiss(buttonIndex);
};
NSString* title = data["title"].toString().toNSString();
NSString* message = data["message"].toString().toNSString();
QStringList buttons = data["buttons"].toStringList();
UIAlertView *alert = [UIAlertView alloc];
alertView = alert;
[alert initWithTitle:title
message:message
delegate:alertViewDelegate
cancelButtonTitle:nil
otherButtonTitles:nil
];
for (int i = 0 ; i < buttons.size();i++) {
NSString *btn = buttons.at(i).toNSString();
[alert addButtonWithTitle:btn];
}
[alert show];
return true;
}
static bool alertViewDismissWithClickedButtonIndex(QVariantMap& message) {
if (alertView == nil) {
return false;
}
int index = message["index"].toInt();
bool animated = message["animated"].toBool();
[alertView dismissWithClickedButtonIndex:index animated:animated];
alertViewDismiss(index);
return true;
}
static bool actionSheetCreate(QVariantMap& data) {
static QIViewDelegate *delegate;
delegate = [QIViewDelegate alloc];
delegate->actionSheetClickedButtonAtIndex = ^(int buttonIndex) {
QString name = "actionSheetClickedButtonAtIndex";
QVariantMap data;
data["buttonIndex"] = buttonIndex;
QISystemDispatcher* m_instance = QISystemDispatcher::instance();
QMetaObject::invokeMethod(m_instance,"dispatched",Qt::DirectConnection,
Q_ARG(QString , name),
Q_ARG(QVariantMap,data));
};
delegate->actionSheetDidDismissWithButtonIndex = ^(int buttonIndex) {
QString name = "actionSheetDidDismissWithButtonIndex";
QVariantMap data;
data["buttonIndex"] = buttonIndex;
QISystemDispatcher* m_instance = QISystemDispatcher::instance();
m_instance->dispatch(name,data);
delegate = nil;
};
NSString* title = nil;
QString qTitle = data["title"].toString();
if (!qTitle.isEmpty()) {
title = qTitle.toNSString();
}
NSString* cancelButtonTitle = data["cancelButtonTitle"].toString().toNSString();
QStringList buttons = data["otherButtonTitles"].toStringList();
QRect rect = data["sourceRect"].value<QRect>();
UIActionSheet* actionSheet = [UIActionSheet alloc];
[actionSheet initWithTitle:title
delegate:delegate
cancelButtonTitle: nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (int i = 0 ; i < buttons.size();i++) {
NSString *btn = buttons.at(i).toNSString();
[actionSheet addButtonWithTitle:btn];
}
// Reference: http://stackoverflow.com/questions/1602214/use-nsarray-to-specify-otherbuttontitles
[actionSheet addButtonWithTitle:cancelButtonTitle];
actionSheet.cancelButtonIndex = buttons.size();
if (isPad()) {
[actionSheet showFromRect:CGRectMake(rect.x(),rect.y(),rect.width(),rect.height())
inView:[rootViewController() view] animated:YES];
} else {
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
return true;
}
static UIImagePickerController* imagePickerController = 0;
static UIActivityIndicatorView* imagePickerControllerActivityIndicator = 0;
static bool imagePickerControllerPresent(QVariantMap& data) {
UIApplication* app = [UIApplication sharedApplication];
if (app.windows.count <= 0)
return false;
UIWindow* rootWindow = app.windows[0];
UIViewController* rootViewController = rootWindow.rootViewController;
int sourceType = data["sourceType"].toInt();
bool animated = data["animated"].toBool();
if (![UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceType) sourceType]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"The operation is not supported in this device"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
// [myAlertView release];
return false;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
imagePickerController = picker;
picker.sourceType = (UIImagePickerControllerSourceType) sourceType;
static QIViewDelegate *delegate = 0;
delegate = [QIViewDelegate alloc];
delegate->imagePickerControllerDidFinishPickingMediaWithInfo = ^(UIImagePickerController *picker,
NSDictionary* info) {
Q_UNUSED(picker);
QString name = "imagePickerControllerDisFinishPickingMetaWithInfo";
QVariantMap data;
data["mediaType"] = QString::fromNSString(info[UIImagePickerControllerMediaType]);
data["mediaUrl"] = fromNSUrl(info[UIImagePickerControllerMediaURL]);
data["referenceUrl"] = fromNSUrl(info[UIImagePickerControllerReferenceURL]);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if (!chosenImage) {
chosenImage = info[UIImagePickerControllerOriginalImage];
}
if (!chosenImage) {
qWarning() << "Image Picker: Failed to take image";
name = "imagePickerControllerDidCancel";
} else {
QImage chosenQImage = fromUIImage(chosenImage);
data["image"] = QVariant::fromValue<QImage>(chosenQImage);
}
QISystemDispatcher* m_instance = QISystemDispatcher::instance();
QMetaObject::invokeMethod(m_instance,"dispatched",Qt::DirectConnection,
Q_ARG(QString , name),
Q_ARG(QVariantMap,data));
delegate = nil;
};
delegate->imagePickerControllerDidCancel = ^(UIImagePickerController *picker) {
Q_UNUSED(picker);
QString name = "imagePickerControllerDidCancel";
QVariantMap data;
QISystemDispatcher* m_instance = QISystemDispatcher::instance();
QMetaObject::invokeMethod(m_instance,"dispatched",Qt::DirectConnection,
Q_ARG(QString , name),
Q_ARG(QVariantMap,data));
delegate = nil;
};
picker.delegate = delegate;
imagePickerControllerActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
imagePickerControllerActivityIndicator.center = picker.view.center;
[picker.view addSubview:imagePickerControllerActivityIndicator];
[rootViewController presentViewController:picker animated:animated completion:NULL];
return true;
}
bool imagePickerControllerDismiss(QVariantMap& data) {
Q_UNUSED(data);
if (!imagePickerController)
return false;
bool animated = data["animated"].toBool();
[imagePickerController dismissViewControllerAnimated:animated completion:NULL];
// [imagePickerController release];
// [imagePickerControllerActivityIndicator release];
imagePickerController = 0;
imagePickerControllerActivityIndicator = 0;
return true;
}
bool imagePickerControllerSetIndicator(QVariantMap& data) {
if (!imagePickerControllerActivityIndicator)
return false;
bool active = data["active"].toBool();
if (active) {
[imagePickerControllerActivityIndicator startAnimating];
} else {
[imagePickerControllerActivityIndicator stopAnimating];
}
return true;
}
static bool applicationSetStatusBarStyle(QVariantMap& data) {
if (!data.contains("style")) {
qWarning() << "applicationSetStatusBarStyle: Missing argument";
return false;
}
int style = data["style"].toInt();
[[UIApplication sharedApplication] setStatusBarStyle:(UIStatusBarStyle) style];
return true;
}
static bool applicationSetStatusBarHidden(QVariantMap& data) {
bool hidden = data["hidden"].toBool();
int animation = data["animation"].toInt();
[[UIApplication sharedApplication] setStatusBarHidden:(bool) hidden withAnimation:(UIStatusBarAnimation) animation];
return true;
}
static UIActivityIndicatorView* activityIndicator = 0;
static bool activityIndicatorStartAniamtion(QVariantMap& data) {
Q_UNUSED(data);
if (!activityIndicator) {
int style = data["style"].toInt();
UIViewController* rootView = rootViewController();
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle) style];
activityIndicator.center = rootView.view.center;
activityIndicator.hidesWhenStopped = YES;
[rootView.view addSubview:activityIndicator];
}
[activityIndicator startAnimating];
return true;
}
static bool activityIndicatorStopAnimation(QVariantMap& data) {
Q_UNUSED(data);
if (!activityIndicator) {
return false;
}
[activityIndicator stopAnimating];
// [activityIndicator release];
activityIndicator = 0;
return true;
}
static void registerTypes() {
QISystemDispatcher* dispatcher = QISystemDispatcher::instance();
dispatcher->addListener("alertViewCreate",alertViewCreate);
dispatcher->addListener("alertViewDismissWithClickedButtonIndex", alertViewDismissWithClickedButtonIndex);
dispatcher->addListener("applicationSetStatusBarStyle",applicationSetStatusBarStyle);
dispatcher->addListener("applicationSetStatusBarHidden",applicationSetStatusBarHidden);
dispatcher->addListener("actionSheetCreate",actionSheetCreate);
dispatcher->addListener("imagePickerControllerPresent",imagePickerControllerPresent);
dispatcher->addListener("imagePickerControllerDismiss",imagePickerControllerDismiss);
dispatcher->addListener("imagePickerControllerSetIndicator",imagePickerControllerSetIndicator);
dispatcher->addListener("activityIndicatorStartAnimation",activityIndicatorStartAniamtion);
dispatcher->addListener("activityIndicatorStopAnimation",activityIndicatorStopAnimation);
}
Q_COREAPP_STARTUP_FUNCTION(registerTypes)