forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
106 lines (87 loc) · 2.96 KB
/
lib.rs
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
// SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
// SPDX-FileContributor: Gerhard de Clercq <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
// Note: keep any changes here in sync with the main README.md
use serde::{Deserialize, Serialize};
// Represent the data within the QObject below with serde friendly types, so we can (de)serialize it
#[derive(Deserialize, Serialize)]
pub struct DataSerde {
number: i32,
string: String,
}
impl From<&MyObjectRust> for DataSerde {
fn from(value: &MyObjectRust) -> DataSerde {
DataSerde {
number: value.number,
string: value.string.to_string(),
}
}
}
const DEFAULT_STR: &str = r#"{"number": 1, "string": "Hello World!"}"#;
#[cxx_qt::bridge(cxx_file_stem = "my_object", namespace = "core")]
mod qobject {
#[namespace = ""]
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}
unsafe extern "RustQt" {
#[cxx_qt::qobject]
#[qproperty(i32, number)]
#[qproperty(QString, string)]
type MyObject = super::MyObjectRust;
#[qinvokable]
pub fn increment(self: Pin<&mut qobject::MyObject>);
#[qinvokable]
pub fn reset(self: Pin<&mut qobject::MyObject>);
#[qinvokable]
pub fn serialize(self: &qobject::MyObject) -> QString;
#[qinvokable]
pub fn grab_values(self: Pin<&mut qobject::MyObject>);
}
}
use core::pin::Pin;
use cxx_qt::CxxQtType;
use cxx_qt_lib::QString;
pub struct MyObjectRust {
pub number: i32,
pub string: QString,
}
impl Default for MyObjectRust {
fn default() -> Self {
let data_serde: DataSerde = serde_json::from_str(DEFAULT_STR).unwrap();
data_serde.into()
}
}
impl From<DataSerde> for MyObjectRust {
fn from(value: DataSerde) -> Self {
Self {
number: value.number,
string: QString::from(&value.string),
}
}
}
impl qobject::MyObject {
pub fn increment(self: Pin<&mut Self>) {
let new_number = self.number() + 1;
self.set_number(new_number);
}
pub fn reset(mut self: Pin<&mut Self>) {
let data: DataSerde = serde_json::from_str(DEFAULT_STR).unwrap();
self.as_mut().set_number(data.number);
self.as_mut().set_string(QString::from(&data.string));
}
pub fn serialize(&self) -> QString {
let data_serde = DataSerde::from(self.rust());
let data_string = serde_json::to_string(&data_serde).unwrap();
QString::from(&data_string)
}
pub fn grab_values(mut self: Pin<&mut Self>) {
let string = r#"{"number": 2, "string": "Goodbye!"}"#;
let data: DataSerde = serde_json::from_str(string).unwrap();
self.as_mut().set_number(data.number);
self.as_mut().set_string(QString::from(&data.string));
}
}