forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainers.rs
209 lines (179 loc) · 7.02 KB
/
containers.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
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
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//! This example shows how Qt container types can be used
/// A CXX-Qt bridge which shows how to use Qt container types
#[cxx_qt::bridge(cxx_file_stem = "rust_containers")]
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qhash.h");
/// QHash<QString, QVariant> from cxx_qt_lib
type QHash_QString_QVariant = cxx_qt_lib::QHash<cxx_qt_lib::QHashPair_QString_QVariant>;
include!("cxx-qt-lib/qlist.h");
/// QList<i32> from cxx_qt_lib
type QList_i32 = cxx_qt_lib::QList<i32>;
include!("cxx-qt-lib/qmap.h");
/// QMap<QString, QVariant> from cxx_qt_lib
type QMap_QString_QVariant = cxx_qt_lib::QMap<cxx_qt_lib::QMapPair_QString_QVariant>;
include!("cxx-qt-lib/qset.h");
/// QSet<i32> from cxx_qt_lib
type QSet_i32 = cxx_qt_lib::QSet<i32>;
include!("cxx-qt-lib/qstring.h");
/// QString from cxx_qt_lib
type QString = cxx_qt_lib::QString;
include!("cxx-qt-lib/qvariant.h");
/// QVariant from cxx_qt_lib
type QVariant = cxx_qt_lib::QVariant;
include!("cxx-qt-lib/qvector.h");
/// QVector<i32> from cxx_qt_lib
type QVector_i32 = cxx_qt_lib::QVector<i32>;
}
unsafe extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
#[qproperty(QString, string_hash)]
#[qproperty(QString, string_list)]
#[qproperty(QString, string_map)]
#[qproperty(QString, string_set)]
#[qproperty(QString, string_vector)]
// Expose as a Q_PROPERTY so that QML tests can ensure that QVariantMap works
#[qproperty(QMap_QString_QVariant, map)]
type RustContainers = super::RustContainersRust;
/// Reset all the containers
#[qinvokable]
fn reset(self: Pin<&mut qobject::RustContainers>);
/// Append the given number to the vector container
#[qinvokable]
fn append_vector(self: Pin<&mut qobject::RustContainers>, value: i32);
/// Append the given number to the list container
#[qinvokable]
fn append_list(self: Pin<&mut qobject::RustContainers>, value: i32);
/// Insert the given number into the set container
#[qinvokable]
fn insert_set(self: Pin<&mut qobject::RustContainers>, value: i32);
/// Insert the given string and variant to the hash container
#[qinvokable]
fn insert_hash(self: Pin<&mut qobject::RustContainers>, key: QString, value: QVariant);
/// Insert the given string and variant to the map container
#[qinvokable]
fn insert_map(self: Pin<&mut qobject::RustContainers>, key: QString, value: QVariant);
}
}
use core::pin::Pin;
use cxx_qt::CxxQtType;
use cxx_qt_lib::{
QHash, QHashPair_QString_QVariant, QList, QMap, QMapPair_QString_QVariant, QSet, QString,
QVariant, QVector,
};
/// A QObject which stores container types internally
///
/// It has Q_PROPERTYs which expose a string with the container's contents to show in QML
#[derive(Default)]
pub struct RustContainersRust {
string_hash: QString,
string_list: QString,
string_map: QString,
string_set: QString,
string_vector: QString,
pub(crate) hash: QHash<QHashPair_QString_QVariant>,
pub(crate) list: QList<i32>,
pub(crate) map: QMap<QMapPair_QString_QVariant>,
pub(crate) set: QSet<i32>,
pub(crate) vector: QVector<i32>,
}
impl qobject::RustContainers {
/// Reset all the containers
fn reset(mut self: Pin<&mut Self>) {
// Update the private rust fields via the rust_mut
{
let mut rust_mut = self.as_mut().rust_mut();
rust_mut.hash = QHash::<QHashPair_QString_QVariant>::default();
rust_mut.list = QList::<i32>::default();
rust_mut.set = QSet::<i32>::default();
rust_mut.vector = QVector::<i32>::default();
}
self.as_mut()
.set_map(QMap::<QMapPair_QString_QVariant>::default());
self.update_strings();
}
/// Append the given number to the vector container
fn append_vector(mut self: Pin<&mut Self>, value: i32) {
self.as_mut().rust_mut().vector.append(value);
self.update_strings();
}
/// Append the given number to the list container
fn append_list(mut self: Pin<&mut Self>, value: i32) {
self.as_mut().rust_mut().list.append(value);
self.update_strings();
}
/// Insert the given number into the set container
fn insert_set(mut self: Pin<&mut Self>, value: i32) {
self.as_mut().rust_mut().set.insert(value);
self.update_strings();
}
/// Insert the given string and variant to the hash container
fn insert_hash(mut self: Pin<&mut Self>, key: QString, value: QVariant) {
self.as_mut().rust_mut().hash.insert(key, value);
self.update_strings();
}
/// Insert the given string and variant to the map container
fn insert_map(mut self: Pin<&mut Self>, key: QString, value: QVariant) {
// Note: map is a Q_PROPERTY so ensure we manually trigger changed
self.as_mut().rust_mut().map.insert(key, value);
self.as_mut().map_changed();
self.update_strings();
}
fn update_strings(mut self: Pin<&mut Self>) {
let hash_items = self
.as_ref()
.rust()
.hash
.iter()
.map(|(key, value)| {
let value = value.value::<i32>().unwrap_or(0);
format!("{key} => {value}")
})
.collect::<Vec<String>>()
.join(", ");
self.as_mut().set_string_hash(QString::from(&hash_items));
let list_items = self
.as_ref()
.rust()
.list
.iter()
.map(|value| value.to_string())
.collect::<Vec<String>>()
.join(", ");
self.as_mut().set_string_list(QString::from(&list_items));
let map_items = self
.as_ref()
.rust()
.map
.iter()
.map(|(key, value)| {
let value = value.value::<i32>().unwrap_or(0);
format!("{key} => {value}")
})
.collect::<Vec<String>>()
.join(", ");
self.as_mut().set_string_map(QString::from(&map_items));
let set_items = self
.as_ref()
.rust()
.set
.iter()
.map(|value| value.to_string())
.collect::<Vec<String>>()
.join(", ");
self.as_mut().set_string_set(QString::from(&set_items));
let vector_items = self
.as_ref()
.rust()
.vector
.iter()
.map(|value| value.to_string())
.collect::<Vec<String>>()
.join(", ");
self.set_string_vector(QString::from(&vector_items));
}
}