forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvokables.rs
107 lines (91 loc) · 3.09 KB
/
invokables.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
// 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 a Q_INVOKABLE can be used
/// A CXX-Qt bridge which shows how a Q_INVOKABLE can be used
// ANCHOR: book_macro_code
#[cxx_qt::bridge(cxx_file_stem = "rust_invokables")]
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qcolor.h");
/// QColor from cxx_qt_lib
type QColor = cxx_qt_lib::QColor;
}
unsafe extern "RustQt" {
#[qobject]
#[qml_element]
type RustInvokables = super::RustInvokablesRust;
}
// ANCHOR: book_invokable_signature
unsafe extern "RustQt" {
/// Immutable invokable method that returns the QColor
#[qinvokable]
fn load_color(self: &RustInvokables) -> Result<QColor>;
/// Mutable invokable method that stores a color
#[qinvokable]
fn store_color(self: Pin<&mut RustInvokables>, red: f32, green: f32, blue: f32);
/// Mutable invokable method with no parameters that resets the color
#[qinvokable]
fn reset(self: Pin<&mut RustInvokables>);
/// C++ only method which returns the red value
fn red_value(self: &RustInvokables) -> f32;
}
// ANCHOR_END: book_invokable_signature
}
use core::pin::Pin;
use cxx_qt::CxxQtType;
use cxx_qt_lib::QColor;
/// A QObject which has Q_INVOKABLEs
pub struct RustInvokablesRust {
pub(crate) red: f32,
pub(crate) green: f32,
pub(crate) blue: f32,
}
impl Default for RustInvokablesRust {
fn default() -> Self {
Self {
red: 0.0,
green: 0.4667,
blue: 0.7843,
}
}
}
// ANCHOR: book_invokable_impl
impl qobject::RustInvokables {
/// Immutable invokable method that returns the QColor
pub fn load_color(&self) -> Result<QColor, i32> {
Ok(self.as_qcolor())
}
/// Mutable invokable method that stores a color
pub fn store_color(self: Pin<&mut Self>, red: f32, green: f32, blue: f32) {
self.store_helper(red, green, blue);
}
/// Mutable invokable method with no parameters that resets the color
pub fn reset(self: Pin<&mut Self>) {
self.store_helper(0.0, 0.4667, 0.7843);
}
/// C++ only method which returns the red value
pub fn red_value(&self) -> f32 {
self.red
}
/// Mutable C++ context method that helps to store the color
fn store_helper(mut self: Pin<&mut Self>, red: f32, green: f32, blue: f32) {
let mut rust_mut = self.as_mut().rust_mut();
rust_mut.red = red;
rust_mut.green = green;
rust_mut.blue = blue;
}
}
// ANCHOR_END: book_invokable_impl
impl RustInvokablesRust {
/// Immutable Rust context method that returns the QColor
pub fn as_qcolor(&self) -> QColor {
QColor::from_rgb(
(self.red * 255.0).round() as i32,
(self.green * 255.0).round() as i32,
(self.blue * 255.0).round() as i32,
)
}
}
// ANCHOR_END: book_macro_code