|
| 1 | +// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> |
| 2 | +// SPDX-FileContributor: Ben Ford <[email protected]> |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 5 | + |
| 6 | +pub use cxx_qt::QObject; |
| 7 | +use std::pin::Pin; |
| 8 | + |
| 9 | +#[cxx_qt::bridge] |
| 10 | +mod ffi { |
| 11 | + unsafe extern "C++" { |
| 12 | + #[rust_name = "QObjectExternal"] |
| 13 | + type QObject; |
| 14 | + |
| 15 | + include!("cxx-qt-lib/qstring.h"); |
| 16 | + type QString = crate::QString; |
| 17 | + |
| 18 | + #[rust_name = "block_signals"] |
| 19 | + pub fn blockSignals(self: Pin<&mut QObjectExternal>, block: bool) -> bool; |
| 20 | + |
| 21 | + #[rust_name = "signals_blocked"] |
| 22 | + pub fn signalsBlocked(self: &QObjectExternal) -> bool; |
| 23 | + |
| 24 | + #[rust_name = "set_object_name"] |
| 25 | + pub fn setObjectName(self: Pin<&mut QObjectExternal>, name: &QString); |
| 26 | + |
| 27 | + #[rust_name = "object_name"] |
| 28 | + pub fn objectName(self: &QObjectExternal) -> QString; |
| 29 | + |
| 30 | + pub fn parent(self: &QObjectExternal) -> *mut QObjectExternal; |
| 31 | + |
| 32 | + #[rust_name = "set_parent"] |
| 33 | + pub unsafe fn setParent(self: Pin<&mut QObjectExternal>, parent: *mut QObjectExternal); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +use crate::core::qobject::ffi::QString; |
| 38 | +use ffi::QObjectExternal; |
| 39 | + |
| 40 | +/// Trait which exposes methods available on a `QObject`. |
| 41 | +/// Exposes some basic signals and methods for now, more to be added. |
| 42 | +pub trait QObjectExt { |
| 43 | + fn block_signals(self: Pin<&mut Self>, block: bool) -> bool; |
| 44 | + |
| 45 | + fn signals_blocked(&self) -> bool; |
| 46 | + |
| 47 | + fn set_object_name(self: Pin<&mut Self>, name: &QString); |
| 48 | + |
| 49 | + fn object_name(&self) -> QString; |
| 50 | + |
| 51 | + fn parent(&self) -> *mut Self; |
| 52 | + |
| 53 | + fn set_parent(self: Pin<&mut Self>, parent: &Self); |
| 54 | +} |
| 55 | + |
| 56 | +/// Used to convert the QObject type from the library type to the C++ type, as a pin |
| 57 | +fn cast_pin(obj: Pin<&mut QObject>) -> Pin<&mut QObjectExternal> { |
| 58 | + unsafe { |
| 59 | + let mut_ptr = obj.get_unchecked_mut() as *mut QObject as *mut QObjectExternal; |
| 60 | + Pin::new_unchecked(&mut *mut_ptr) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Used to convert the QObject type from the library type to the C++ type |
| 65 | +fn cast(obj: &QObject) -> &QObjectExternal { |
| 66 | + unsafe { |
| 67 | + let ptr = obj as *const QObject as *const QObjectExternal; |
| 68 | + &*ptr |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl QObjectExt for QObject { |
| 73 | + fn block_signals(self: Pin<&mut Self>, block: bool) -> bool { |
| 74 | + cast_pin(self).block_signals(block) |
| 75 | + } |
| 76 | + |
| 77 | + fn signals_blocked(&self) -> bool { |
| 78 | + cast(self).signals_blocked() |
| 79 | + } |
| 80 | + |
| 81 | + fn set_object_name(self: Pin<&mut Self>, name: &QString) { |
| 82 | + cast_pin(self).set_object_name(name) |
| 83 | + } |
| 84 | + |
| 85 | + fn object_name(&self) -> QString { |
| 86 | + cast(self).object_name() |
| 87 | + } |
| 88 | + |
| 89 | + fn parent(&self) -> *mut Self { |
| 90 | + cast(self).parent() as *mut Self |
| 91 | + } |
| 92 | + |
| 93 | + fn set_parent(self: Pin<&mut Self>, parent: &Self) { |
| 94 | + unsafe { |
| 95 | + cast_pin(self) |
| 96 | + .set_parent(cast(parent) as *const QObjectExternal as *mut QObjectExternal); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments