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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
use cpp::{cpp, cpp_class};
use crate::scenegraph::*;
use crate::*;
static HAS_ENGINE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
cpp! {{
#include <memory>
#include <QtQuick/QtQuick>
#include <QtCore/QDebug>
#include <QtWidgets/QApplication>
#include <QtQml/QQmlComponent>
struct SingleApplicationGuard {
SingleApplicationGuard() {
rust!(Rust_QmlEngineHolder_ctor[] {
HAS_ENGINE.compare_exchange(false, true, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst)
.expect("There can only be one QmlEngine in the process");
});
}
~SingleApplicationGuard() {
rust!(Rust_QmlEngineHolder_dtor[] {
HAS_ENGINE.compare_exchange(true, false, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst)
.unwrap();
});
}
};
struct QmlEngineHolder : SingleApplicationGuard {
std::unique_ptr<QApplication> app;
std::unique_ptr<QQmlApplicationEngine> engine;
std::unique_ptr<QQuickView> view;
QmlEngineHolder(int &argc, char **argv)
: app(new QApplication(argc, argv))
, engine(new QQmlApplicationEngine())
{}
};
bool qmeta_inherits(const QMetaObject *child, const QMetaObject *check) {
#if QT_VERSION <= QT_VERSION_CHECK(5, 7, 0)
do {
if (child == check) {
return true;
}
} while ((child = child->superClass()));
return false;
#else
return child->inherits(check);
#endif
}
#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
using CreatorFunction = void (*)(void *);
#else
using CreatorFunction = void (*)(void *, void*);
#endif
}}
cpp_class!(
pub unsafe struct QmlEngine as "QmlEngineHolder"
);
impl QmlEngine {
pub fn new() -> QmlEngine {
let mut arguments: Vec<*mut c_char> = std::env::args()
.map(|arg| CString::new(arg.into_bytes()).expect("argument contains invalid c-string!"))
.map(|arg| arg.into_raw())
.collect();
let argc: i32 = arguments.len() as i32;
let argv: *mut *mut c_char = arguments.as_mut_ptr();
let result = cpp!(unsafe [
argc as "int",
argv as "char **"
] -> QmlEngine as "QmlEngineHolder" {
static int _argc = argc;
static char **_argv = nullptr;
if (_argv == nullptr) {
_argv = new char *[argc + 1];
_argv[argc] = nullptr;
for (int i = 0; i < argc; ++i) {
_argv[i] = new char[strlen(argv[i]) + 1];
strcpy(_argv[i], argv[i]);
}
}
return QmlEngineHolder(_argc, _argv);
});
for arg in arguments {
let _ = unsafe { CString::from_raw(arg) };
}
result
}
pub fn load_file(&mut self, path: QString) {
cpp!(unsafe [self as "QmlEngineHolder *", path as "QString"] {
self->engine->load(path);
})
}
pub fn load_url(&mut self, url: QUrl) {
cpp!(unsafe [self as "QmlEngineHolder *", url as "QUrl"] {
self->engine->load(url);
})
}
pub fn load_data(&mut self, data: QByteArray) {
cpp!(unsafe [self as "QmlEngineHolder *", data as "QByteArray"] {
self->engine->loadData(data);
})
}
pub fn load_data_as(&mut self, data: QByteArray, url: QUrl) {
cpp!(unsafe [self as "QmlEngineHolder *", data as "QByteArray", url as "QUrl"] {
self->engine->loadData(data, url);
})
}
pub fn exec(&self) {
cpp!(unsafe [self as "QmlEngineHolder *"] {
self->app->exec();
})
}
pub fn quit(&self) {
cpp!(unsafe [self as "QmlEngineHolder *"] {
self->app->quit();
})
}
pub fn set_property(&mut self, name: QString, value: QVariant) {
cpp!(unsafe [self as "QmlEngineHolder *", name as "QString", value as "QVariant"] {
self->engine->rootContext()->setContextProperty(name, value);
})
}
pub fn set_object<T: QObject + Sized>(&mut self, obj: QObjectPinned<T>) {
let obj_ptr = obj.get_or_create_cpp_object();
cpp!(unsafe [self as "QmlEngineHolder *", obj_ptr as "QObject *"] {
self->engine->rootContext()->setContextObject(obj_ptr);
})
}
pub fn set_object_property<T: QObject + Sized>(
&mut self,
name: QString,
obj: QObjectPinned<T>,
) {
let obj_ptr = obj.get_or_create_cpp_object();
cpp!(unsafe [self as "QmlEngineHolder *", name as "QString", obj_ptr as "QObject *"] {
self->engine->rootContext()->setContextProperty(name, obj_ptr);
})
}
pub fn invoke_method(&mut self, name: QByteArray, args: &[QVariant]) -> QVariant {
let args_size = args.len();
let args_ptr = args.as_ptr();
cpp!(unsafe [
self as "QmlEngineHolder *",
name as "QByteArray",
args_size as "size_t",
args_ptr as "QVariant *"
] -> QVariant as "QVariant"
{
auto robjs = self->engine->rootObjects();
if (robjs.isEmpty()) {
return {};
}
QVariant ret;
QGenericArgument args[9] = {};
for (uint i = 0; i < args_size; ++i) {
args[i] = Q_ARG(QVariant, args_ptr[i]);
}
QMetaObject::invokeMethod(
robjs.first(),
name,
Q_RETURN_ARG(QVariant, ret),
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]
);
return ret;
})
}
pub fn new_qobject<T: QObject>(&mut self, obj: T) -> QJSValue {
let obj_ptr = into_leaked_cpp_ptr(obj);
cpp!(unsafe [
self as "QmlEngineHolder *",
obj_ptr as "QObject *"
] -> QJSValue as "QJSValue" {
return self->engine->newQObject(obj_ptr);
})
}
pub fn add_import_path(&mut self, path: QString) {
cpp!(unsafe [self as "QmlEngineHolder *", path as "QString"] {
self->engine->addImportPath(path);
})
}
pub fn cpp_ptr(&self) -> *mut c_void {
cpp!(unsafe [self as "QmlEngineHolder *"] -> *mut c_void as "QQmlEngine *" {
return self->engine.get();
})
}
}
pub struct QQuickView {
engine: QmlEngine,
}
impl QQuickView {
pub fn new() -> QQuickView {
let mut engine = QmlEngine::new();
cpp!(unsafe [mut engine as "QmlEngineHolder"] {
engine.view = std::unique_ptr<QQuickView>(new QQuickView(engine.engine.get(), nullptr));
engine.view->setResizeMode(QQuickView::SizeRootObjectToView);
});
QQuickView { engine }
}
pub fn engine(&mut self) -> &mut QmlEngine {
&mut self.engine
}
pub fn show(&mut self) {
let engine = self.engine();
cpp!(unsafe [engine as "QmlEngineHolder *"] {
engine->view->show();
});
}
pub fn set_source(&mut self, url: QString) {
let engine = self.engine();
cpp!(unsafe [engine as "QmlEngineHolder *", url as "QString"] {
engine->view->setSource(url);
});
}
}
impl Default for QQuickView {
fn default() -> Self {
Self::new()
}
}
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CompilationMode {
PreferSynchronous,
Asynchronous,
}
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ComponentStatus {
Null,
Ready,
Loading,
Error,
}
cpp! {{
struct QQmlComponentHolder {
std::unique_ptr<QQmlComponent> component;
QQmlComponentHolder(QQmlEngine *e)
: component(new QQmlComponent(e))
{}
};
}}
cpp_class!(
pub unsafe struct QmlComponent as "QQmlComponentHolder"
);
impl QmlComponent {
pub fn new(engine: &QmlEngine) -> QmlComponent {
cpp!(unsafe [engine as "QmlEngineHolder *"] -> QmlComponent as "QQmlComponentHolder" {
return QQmlComponentHolder(engine->engine.get());
})
}
pub fn get_cpp_object(&self) -> *mut c_void {
cpp!(unsafe [self as "QQmlComponentHolder *"] -> *mut c_void as "QQmlComponent *" {
return self->component.get();
})
}
pub fn load_url(&mut self, url: QUrl, compilation_mode: CompilationMode) {
cpp!(unsafe [
self as "QQmlComponentHolder *",
url as "QUrl",
compilation_mode as "QQmlComponent::CompilationMode"
] {
self->component->loadUrl(url, compilation_mode);
})
}
pub fn set_data(&mut self, data: QByteArray) {
cpp!(unsafe [self as "QQmlComponentHolder *", data as "QByteArray"] {
self->component->setData(data, QUrl());
})
}
pub fn set_data_as(&mut self, data: QByteArray, url: QUrl) {
cpp!(unsafe [self as "QQmlComponentHolder *", data as "QByteArray", url as "QUrl"] {
self->component->setData(data, url);
})
}
pub fn create(&mut self) -> *mut c_void {
cpp!(unsafe [self as "QQmlComponentHolder *"] -> *mut c_void as "QObject *" {
return self->component->create();
})
}
pub fn status(&self) -> ComponentStatus {
cpp!(unsafe [
self as "QQmlComponentHolder *"
] -> ComponentStatus as "QQmlComponent::Status" {
return self->component->status();
})
}
pub fn status_changed_signal() -> Signal<fn(status: ComponentStatus)> {
unsafe {
Signal::new(cpp!([] -> SignalInner as "SignalInner" {
return &QQmlComponent::statusChanged;
}))
}
}
}
pub fn qml_register_type<T: QObject + Default + Sized>(
uri: &CStr,
version_major: u32,
version_minor: u32,
qml_name: &CStr,
) {
let uri_ptr = uri.as_ptr();
let qml_name_ptr = qml_name.as_ptr();
let meta_object = T::static_meta_object();
extern "C" fn extra_destruct(c: *mut c_void) {
cpp!(unsafe [c as "QObject *"] {
QQmlPrivate::qdeclarativeelement_destructor(c);
})
}
extern "C" fn creator_fn<T: QObject + Default + Sized>(
c: *mut c_void,
#[cfg(qt_6_0)] _: *mut c_void,
) {
let b: Box<RefCell<T>> = Box::new(RefCell::new(T::default()));
let ed: extern "C" fn(c: *mut c_void) = extra_destruct;
unsafe {
T::qml_construct(&b, c, ed);
}
Box::leak(b);
}
let creator_fn: extern "C" fn(c: *mut c_void, #[cfg(qt_6_0)] _: *mut c_void) = creator_fn::<T>;
let size = T::cpp_size();
let type_id = <RefCell<T> as PropertyType>::register_type(Default::default());
cpp!(unsafe [
qml_name_ptr as "char *",
uri_ptr as "char *",
version_major as "int",
version_minor as "int",
meta_object as "const QMetaObject *",
creator_fn as "CreatorFunction",
size as "size_t",
type_id as "int"
] {
int parserStatusCast = meta_object && qmeta_inherits(meta_object, &QQuickItem::staticMetaObject)
? QQmlPrivate::StaticCastSelector<QQuickItem, QQmlParserStatus>::cast()
: -1;
QQmlPrivate::RegisterType api = {
0,
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
type_id,
#else
QMetaType(type_id),
#endif
{},
int(size),
creator_fn,
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
nullptr,
#endif
QString(),
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
nullptr,
#endif
uri_ptr,
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
version_major,
version_minor,
#else
QTypeRevision::fromVersion(version_major, version_minor),
#endif
qml_name_ptr,
meta_object,
nullptr,
nullptr,
parserStatusCast,
-1,
-1,
nullptr,
nullptr,
nullptr,
{}
};
QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &api);
})
}
type QmlRegisterSingletonTypeCallback =
extern "C" fn(qml_engine: *mut c_void, js_engine: *mut c_void) -> *mut c_void;
cpp! {{
using QmlRegisterSingletonTypeCallback = QObject *(*)(QQmlEngine *, QJSEngine *);
}}
pub trait QSingletonInit {
fn init(&mut self);
}
pub fn qml_register_singleton_type<T: QObject + QSingletonInit + Sized + Default>(
uri: &CStr,
version_major: u32,
version_minor: u32,
qml_name: &CStr,
) {
let uri_ptr = uri.as_ptr();
let qml_name_ptr = qml_name.as_ptr();
let meta_object = T::static_meta_object();
extern "C" fn callback_fn<T: QObject + Default + Sized + QSingletonInit>(
_qml_engine: *mut c_void,
_js_engine: *mut c_void,
) -> *mut c_void {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let obj_box: Box<RefCell<T>> = Box::new(RefCell::new(T::default()));
let obj_ptr = unsafe { T::cpp_construct(&obj_box) };
obj_box.borrow_mut().init();
Box::leak(obj_box);
obj_ptr
}));
match result {
Ok(value) => value,
Err(_panic) => {
eprintln!("qml_register_singleton_type T::default or T::init panicked.");
std::process::abort()
}
}
}
let callback_fn: QmlRegisterSingletonTypeCallback = callback_fn::<T>;
let type_id = <RefCell<T> as PropertyType>::register_type(Default::default());
cpp!(unsafe [
uri_ptr as "const char *",
version_major as "int",
version_minor as "int",
qml_name_ptr as "const char *",
meta_object as "const QMetaObject *",
callback_fn as "QmlRegisterSingletonTypeCallback",
type_id as "int"
] {
QQmlPrivate::RegisterSingletonType api = {
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
2,
#else
0,
#endif
uri_ptr,
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
version_major,
version_minor,
#else
QTypeRevision::fromVersion(version_major, version_minor),
#endif
qml_name_ptr,
nullptr,
callback_fn,
meta_object,
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
type_id,
#else
QMetaType(type_id),
#endif
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
nullptr,
nullptr,
#endif
{},
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0) && QT_VERSION < QT_VERSION_CHECK(6,0,0)
{}
#endif
};
QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api);
})
}
#[cfg(qt_5_14)]
pub fn qml_register_singleton_instance<T: QObject + Sized + Default>(
uri: &CStr,
version_major: u32,
version_minor: u32,
type_name: &CStr,
obj: T,
) {
let uri_ptr = uri.as_ptr();
let type_name_ptr = type_name.as_ptr();
let obj_box = Box::new(RefCell::new(obj));
let obj_ptr = unsafe { T::cpp_construct(&obj_box) };
Box::leak(obj_box);
cpp!(unsafe [
uri_ptr as "char *",
version_major as "int",
version_minor as "int",
type_name_ptr as "char *",
obj_ptr as "QObject *"
] {
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
qmlRegisterSingletonInstance(
uri_ptr,
version_major,
version_minor,
type_name_ptr,
obj_ptr
);
#endif
})
}
#[cfg(qt_5_8)]
pub fn qml_register_enum<T: QEnum>(
uri: &CStr,
version_major: u32,
version_minor: u32,
qml_name: &CStr,
) {
let uri_ptr = uri.as_ptr();
let qml_name_ptr = qml_name.as_ptr();
let meta_object = T::static_meta_object();
cpp!(unsafe [
qml_name_ptr as "char *",
uri_ptr as "char *",
version_major as "int",
version_minor as "int",
meta_object as "const QMetaObject *"
] {
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
qmlRegisterUncreatableMetaObject(
*meta_object,
uri_ptr,
version_major,
version_minor,
qml_name_ptr,
"Access to enums & flags only"
);
#endif
})
}
pub trait QQuickItem: QObject {
fn get_object_description() -> &'static QObjectDescriptor
where
Self: Sized,
{
unsafe {
&*cpp!([]-> *const QObjectDescriptor as "RustQObjectDescriptor const*" {
return RustQObjectDescriptor::instance<Rust_QQuickItem>();
})
}
}
fn class_begin(&mut self) {}
fn component_complete(&mut self) {}
fn release_resources(&mut self) {}
fn mouse_event(&mut self, _event: QMouseEvent) -> bool {
false
}
fn geometry_changed(&mut self, _new_geometry: QRectF, _old_geometry: QRectF) {}
fn update_paint_node(&mut self, node: SGNode<ContainerNode>) -> SGNode<ContainerNode> {
node
}
}
cpp! {{
#include <qmetaobject_rust.hpp>
#include <QtQuick/QQuickItem>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#define QT_QQUICKITEM_GEOMETRYCHANGE geometryChanged
#else
#define QT_QQUICKITEM_GEOMETRYCHANGE geometryChange
#endif
struct Rust_QQuickItem : RustObject<QQuickItem> {
void classBegin() override {
QQuickItem::classBegin();
rust!(Rust_QQuickItem_classBegin[
rust_object: QObjectPinned<dyn QQuickItem> as "TraitObject"
] {
rust_object.borrow_mut().class_begin();
});
}
void componentComplete() override {
QQuickItem::componentComplete();
rust!(Rust_QQuickItem_componentComplete[
rust_object: QObjectPinned<dyn QQuickItem> as "TraitObject"
] {
rust_object.borrow_mut().component_complete();
});
}
void mousePressEvent(QMouseEvent *event) override { handleMouseEvent(event); }
void mouseMoveEvent(QMouseEvent *event) override { handleMouseEvent(event); }
void mouseReleaseEvent(QMouseEvent *event) override { handleMouseEvent(event); }
void handleMouseEvent(QMouseEvent *event) {
if (!rust!(Rust_QQuickItem_mousePressEvent[
rust_object: QObjectPinned<dyn QQuickItem> as "TraitObject",
event: QMouseEvent as "QMouseEvent *"
] -> bool as "bool" {
rust_object.borrow_mut().mouse_event(event)
})) { event->ignore(); }
}
void QT_QQUICKITEM_GEOMETRYCHANGE (const QRectF &new_geometry, const QRectF &old_geometry) override{
rust!(Rust_QQuickItem_geometryChanged[
rust_object: QObjectPinned<dyn QQuickItem> as "TraitObject",
new_geometry: QRectF as "QRectF",
old_geometry: QRectF as "QRectF"
] {
rust_object.borrow_mut().geometry_changed(new_geometry, old_geometry);
});
QQuickItem::QT_QQUICKITEM_GEOMETRYCHANGE(new_geometry, old_geometry);
}
QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override {
return rust!(Rust_QQuickItem_updatePaintNode[
rust_object: QObjectPinned<dyn QQuickItem> as "TraitObject",
node: *mut c_void as "QSGNode *"
] -> SGNode<ContainerNode> as "QSGNode *" {
rust_object.borrow_mut().update_paint_node(unsafe {
SGNode::<ContainerNode>::from_raw(node)
})
});
}
void releaseResources() override {
QQuickItem::releaseResources();
rust!(Rust_QQuickItem_releaseResources[
rust_object: QObjectPinned<dyn QQuickItem> as "TraitObject"
] {
rust_object.borrow_mut().release_resources();
});
}
};
}}
impl<'a> dyn QQuickItem + 'a {
pub fn bounding_rect(&self) -> QRectF {
let obj = self.get_cpp_object();
cpp!(unsafe [obj as "Rust_QQuickItem *"] -> QRectF as "QRectF" {
return obj ? obj->boundingRect() : QRectF();
})
}
pub fn update(&self) {
let obj = self.get_cpp_object();
cpp!(unsafe [obj as "Rust_QQuickItem *"] {
if (obj) obj->update();
});
}
}
#[repr(u32)]
#[non_exhaustive]
pub enum QMouseEventType {
MouseButtonPress = 2,
MouseButtonRelease = 3,
MouseMove = 5,
}
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct QMouseEvent<'a>(*const c_void, std::marker::PhantomData<&'a u32>);
impl<'a> QMouseEvent<'a> {
pub fn event_type(self) -> QMouseEventType {
cpp!(unsafe [self as "QMouseEvent *"] -> QMouseEventType as "int" {
return self->type();
})
}
pub fn position(self) -> QPointF {
cpp!(unsafe [self as "QMouseEvent *"] -> QPointF as "QPointF" {
return self->localPos();
})
}
}
cpp_class!(
pub unsafe struct QJSValue as "QJSValue"
);
impl QJSValue {
pub fn to_string(&self) -> QString {
cpp!(unsafe [self as "const QJSValue *"] -> QString as "QString" {
return self->toString();
})
}
pub fn to_bool(&self) -> bool {
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
return self->toBool();
})
}
pub fn to_number(&self) -> f64 {
cpp!(unsafe [self as "const QJSValue *"] -> f64 as "double" {
return self->toNumber();
})
}
pub fn to_variant(&self) -> QVariant {
cpp!(unsafe [self as "const QJSValue *"] -> QVariant as "QVariant" {
return self->toVariant();
})
}
pub fn to_qobject<'a, T: QObject + 'a>(&'a self) -> Option<QObjectPinned<'a, T>> {
let mo = T::static_meta_object();
let obj = cpp!(unsafe [
self as "const QJSValue *",
mo as "const QMetaObject *"
] -> *mut c_void as "QObject *" {
QObject *obj = self->toQObject();
return obj && qmeta_inherits(obj->metaObject(), mo) ? obj : nullptr;
});
if obj.is_null() {
return None;
}
Some(unsafe { T::get_from_cpp(obj) })
}
}
impl From<QString> for QJSValue {
fn from(a: QString) -> QJSValue {
cpp!(unsafe [a as "QString"] -> QJSValue as "QJSValue" {
return QJSValue(a);
})
}
}
impl From<i32> for QJSValue {
fn from(a: i32) -> QJSValue {
cpp!(unsafe [a as "int"] -> QJSValue as "QJSValue" {
return QJSValue(a);
})
}
}
impl From<u32> for QJSValue {
fn from(a: u32) -> QJSValue {
cpp!(unsafe [a as "uint"] -> QJSValue as "QJSValue" {
return QJSValue(a);
})
}
}
impl From<f64> for QJSValue {
fn from(a: f64) -> QJSValue {
cpp!(unsafe [a as "double"] -> QJSValue as "QJSValue" {
return QJSValue(a);
})
}
}
impl From<bool> for QJSValue {
fn from(a: bool) -> QJSValue {
cpp!(unsafe [a as "bool"] -> QJSValue as "QJSValue" {
return QJSValue(a);
})
}
}
impl QMetaType for QJSValue {
fn register(_name: Option<&CStr>) -> i32 {
cpp!(unsafe [] -> i32 as "int" { return qMetaTypeId<QJSValue>(); })
}
}
#[cfg(test)]
mod qjsvalue_tests {
use super::*;
#[test]
fn test_qjsvalue() {
let foo = QJSValue::from(45);
assert_eq!(foo.to_number(), 45 as f64);
assert_eq!(foo.to_string(), "45".into());
assert_eq!(foo.to_variant().to_qbytearray(), "45".into());
}
#[test]
fn test_qvariantlist_from_iter() {
let v = vec![1u32, 2u32, 3u32];
let qvl: QVariantList = v.iter().collect();
assert_eq!(qvl.len(), 3);
assert_eq!(qvl[1].to_qbytearray().to_string(), "2");
}
}
pub trait QQmlExtensionPlugin: QObject {
#[doc(hidden)]
fn get_object_description() -> &'static QObjectDescriptor
where
Self: Sized,
{
unsafe {
&*cpp!([]-> *const QObjectDescriptor as "RustQObjectDescriptor const *" {
return RustQObjectDescriptor::instance<Rust_QQmlExtensionPlugin>();
})
}
}
fn register_types(&mut self, uri: &CStr);
}
cpp! {{
#include <qmetaobject_rust.hpp>
#include <QtQml/QQmlExtensionPlugin>
struct Rust_QQmlExtensionPlugin : RustObject<QQmlExtensionPlugin> {
void registerTypes(const char *uri) override {
rust!(Rust_QQmlExtensionPlugin_registerTypes[
rust_object: QObjectPinned<dyn QQmlExtensionPlugin> as "TraitObject",
uri: *const c_char as "const char *"
] {
rust_object.borrow_mut().register_types(unsafe { CStr::from_ptr(uri) });
});
}
};
}}
cpp! {{
#include <qmetaobject_rust.hpp>
#include <QtQuick/QQuickItem>
#include <QtQuick/QQuickPaintedItem>
#include <QtGui/QPainter>
struct Rust_QQuickPaintedItem : RustObject<QQuickPaintedItem> {
void classBegin() override {
QQuickPaintedItem::classBegin();
rust!(Rust_QQuickPaintedItem_classBegin[
rust_object: QObjectPinned<dyn QQuickPaintedItem> as "TraitObject"
] {
rust_object.borrow_mut().class_begin();
});
}
void componentComplete() override {
QQuickPaintedItem::componentComplete();
rust!(Rust_QQuickPaintedItem_componentComplete[
rust_object: QObjectPinned<dyn QQuickPaintedItem> as "TraitObject"
] {
rust_object.borrow_mut().component_complete();
});
}
void mousePressEvent(QMouseEvent *event) override { handleMouseEvent(event); }
void mouseMoveEvent(QMouseEvent *event) override { handleMouseEvent(event); }
void mouseReleaseEvent(QMouseEvent *event) override { handleMouseEvent(event); }
void handleMouseEvent(QMouseEvent *event) {
if (!rust!(Rust_QQuickPaintedItem_mousePressEvent[
rust_object: QObjectPinned<dyn QQuickPaintedItem> as "TraitObject",
event: QMouseEvent as "QMouseEvent *"
] -> bool as "bool" {
rust_object.borrow_mut().mouse_event(event)
})) { event->ignore(); }
}
void QT_QQUICKITEM_GEOMETRYCHANGE (const QRectF &new_geometry, const QRectF &old_geometry) override{
rust!(Rust_QQuickPaintedItem_geometryChanged[
rust_object: QObjectPinned<dyn QQuickPaintedItem> as "TraitObject",
new_geometry: QRectF as "QRectF",
old_geometry: QRectF as "QRectF"
] {
rust_object.borrow_mut().geometry_changed(new_geometry, old_geometry);
});
QQuickPaintedItem::QT_QQUICKITEM_GEOMETRYCHANGE(new_geometry, old_geometry);
}
void releaseResources() override {
QQuickPaintedItem::releaseResources();
rust!(Rust_QQuickPaintedItem_releaseResources[
rust_object: QObjectPinned<dyn QQuickPaintedItem> as "TraitObject"
] {
rust_object.borrow_mut().release_resources();
});
}
void paint(QPainter *p) override {
rust!(Rust_QQuickPaintedItem_paint[
rust_object: QObjectPinned<dyn QQuickPaintedItem> as "TraitObject",
p: *mut QPainter as "QPainter*"
] {
rust_object.borrow_mut().paint(&mut *p);
});
}
};
}}
pub trait QQuickPaintedItem: QQuickItem {
fn get_object_description() -> &'static QObjectDescriptor
where
Self: Sized,
{
unsafe {
&*cpp!([]-> *const QObjectDescriptor as "RustQObjectDescriptor const*" {
return RustQObjectDescriptor::instance<Rust_QQuickPaintedItem>();
})
}
}
fn paint(&mut self, _p: &mut QPainter) {}
}