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
use qvariant::*;
use types::*;
use qurl::*;
extern "C" {
fn dos_qapplication_create();
fn dos_qapplication_exec();
fn dos_qapplication_quit();
fn dos_qapplication_delete();
fn dos_qqmlapplicationengine_create() -> DosQmlApplicationEngine;
fn dos_qqmlapplicationengine_load(vptr: DosQmlApplicationEngine, filename: DosCStr);
fn dos_qqmlapplicationengine_load_url(vptr: DosQmlApplicationEngine, url: DosQUrl);
fn dos_qqmlapplicationengine_load_data(vptr: DosQmlApplicationEngine, data: DosCStr);
fn dos_qqmlapplicationengine_context(vptr: DosQmlApplicationEngine) -> DosQQmlContext;
fn dos_qqmlapplicationengine_delete(vptr: DosQmlApplicationEngine);
fn dos_qqmlcontext_setcontextproperty(vptr: DosQQmlContext,
name: DosCStr,
value: DosQVariant);
}
pub struct QmlEngine {
ptr: DosQmlApplicationEngine,
stored: Vec<QVariant>,
}
impl QmlEngine {
pub fn new() -> Self {
unsafe {
dos_qapplication_create();
QmlEngine {
ptr: dos_qqmlapplicationengine_create(),
stored: Vec::new(),
}
}
}
pub fn load_file(&mut self, path: &str) {
let path_raw = ::std::env::current_dir().unwrap().join(path);
let path = if cfg!(windows) {
format!("file:///{}", path_raw.display())
} else {
format!("file://{}", path_raw.display())
};
unsafe { dos_qqmlapplicationengine_load_url(self.ptr, construct_qurl(&path)) }
}
pub fn load_url(&mut self, uri: &str) {
unsafe { dos_qqmlapplicationengine_load_url(self.ptr, construct_qurl(uri)) }
}
pub fn load_data(&mut self, data: &str) {
unsafe { dos_qqmlapplicationengine_load_data(self.ptr, stoptr(data)) }
}
pub fn exec(&mut self) {
unsafe {
dos_qapplication_exec();
}
}
pub fn quit(&mut self) {
unsafe {
dos_qapplication_quit();
}
}
pub fn set_and_store_property<T: Into<QVariant>>(&mut self, name: &str, value: T) {
let val = value.into();
unsafe {
let context = dos_qqmlapplicationengine_context(self.ptr);
dos_qqmlcontext_setcontextproperty(context, stoptr(name), get_private_variant(&val));
}
self.stored.push(val);
}
pub fn set_property(&mut self, name: &str, value: &QVariant) {
unsafe {
let context = dos_qqmlapplicationengine_context(self.ptr);
dos_qqmlcontext_setcontextproperty(context, stoptr(name), get_private_variant(value));
}
}
}
use utils::*;
impl Default for QmlEngine {
fn default() -> Self {
Self::new()
}
}
impl Drop for QmlEngine {
fn drop(&mut self) {
unsafe {
dos_qapplication_quit();
dos_qqmlapplicationengine_delete(self.ptr);
dos_qapplication_delete();
}
}
}