use cxx_qt::casting::Upcast;
pub use cxx_qt::QObject;
use std::pin::Pin;
use std::ptr;
#[cxx::bridge]
pub mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;
}
unsafe extern "C++" {
include!("cxx-qt-lib/qobject.h");
#[rust_name = "QObjectExternal"]
type QObject;
#[rust_name = "block_signals"]
pub fn blockSignals(self: Pin<&mut Self>, block: bool) -> bool;
#[rust_name = "signals_blocked"]
pub fn signalsBlocked(&self) -> bool;
#[rust_name = "set_object_name"]
pub fn setObjectName(self: Pin<&mut Self>, name: &QString);
#[rust_name = "object_name"]
pub fn objectName(&self) -> QString;
pub fn parent(&self) -> *mut QObjectExternal;
#[rust_name = "set_parent"]
pub unsafe fn setParent(self: Pin<&mut Self>, parent: *mut QObjectExternal);
}
}
use ffi::{QObjectExternal, QString};
pub trait QObjectExt {
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool;
fn signals_blocked(&self) -> bool;
fn set_object_name(self: Pin<&mut Self>, name: &QString);
fn object_name(&self) -> QString;
fn parent(&self) -> *const QObject;
fn set_parent<P: Upcast<QObject>>(self: Pin<&mut Self>, parent: Pin<&mut P>);
fn unset_parent(self: Pin<&mut Self>);
}
fn cast_pin(obj: Pin<&mut QObject>) -> Pin<&mut QObjectExternal> {
unsafe { Pin::new_unchecked(&mut *ptr::from_mut(obj.get_unchecked_mut()).cast()) }
}
fn cast(obj: &QObject) -> &QObjectExternal {
unsafe { &*(ptr::from_ref(obj).cast()) }
}
fn uncast(obj_extern: *mut QObjectExternal) -> *mut QObject {
obj_extern.cast()
}
impl<T> QObjectExt for T
where
T: Upcast<QObject>,
{
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool {
cast_pin(self.upcast_pin()).block_signals(block)
}
fn signals_blocked(&self) -> bool {
cast(self.upcast()).signals_blocked()
}
fn set_object_name(self: Pin<&mut Self>, name: &QString) {
cast_pin(self.upcast_pin()).set_object_name(name)
}
fn object_name(&self) -> QString {
cast(self.upcast()).object_name()
}
fn parent(&self) -> *const QObject {
uncast(cast(self.upcast()).parent())
}
fn set_parent<P: Upcast<QObject>>(self: Pin<&mut Self>, parent: Pin<&mut P>) {
let parent = cast_pin(parent.upcast_pin());
unsafe { cast_pin(self.upcast_pin()).set_parent(parent.get_unchecked_mut()) }
}
fn unset_parent(self: Pin<&mut Self>) {
unsafe { cast_pin(self.upcast_pin()).set_parent(ptr::null_mut()) }
}
}