use cxx::let_cxx_string;
use crate::ffi;
use crate::widget::AsWidget;
use crate::layout::AsLayout;
pub struct FormLayout {
ptr: *mut ffi::QFormLayout,
children: Vec<Box<dyn AsWidget>>,
}
impl FormLayout {
pub fn new() -> Self {
let ptr = unsafe { ffi::QFormLayout_new(std::ptr::null_mut()) };
assert!(!ptr.is_null(), "QFormLayout_new returned null");
Self {
ptr,
children: Vec::new(),
}
}
pub fn with_parent(parent: &dyn AsWidget) -> Self {
let ptr = unsafe { ffi::QFormLayout_new(parent.widget_ptr()) };
assert!(!ptr.is_null(), "QFormLayout_new returned null");
Self {
ptr,
children: Vec::new(),
}
}
pub fn add_row<T: AsWidget + 'static>(&mut self, label: impl Into<String>, widget: T) {
debug_assert!(!self.ptr.is_null(), "FormLayout::add_row on null pointer");
let_cxx_string!(c_label = label.into());
unsafe {
ffi::QFormLayout_addRow(self.ptr, &c_label.to_string(), widget.widget_ptr());
}
self.children.push(Box::new(widget));
}
pub fn add_widget<T: AsWidget + 'static>(&mut self, widget: T) {
debug_assert!(!self.ptr.is_null(), "FormLayout::add_widget on null pointer");
unsafe {
ffi::QFormLayout_addRowWidget(self.ptr, widget.widget_ptr());
}
self.children.push(Box::new(widget));
}
pub fn set_spacing(&self, spacing: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFormLayout_setSpacing(self.ptr, spacing); }
}
pub fn set_contents_margins(&self, left: i32, top: i32, right: i32, bottom: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFormLayout_setContentsMargins(self.ptr, left, top, right, bottom); }
}
pub fn layout_ptr(&self) -> *mut ffi::QFormLayout {
self.ptr
}
}
impl AsLayout for FormLayout {
fn layout_ptr(&self) -> *mut ffi::QLayout {
self.ptr as *mut u8 as *mut ffi::QLayout
}
}
impl Drop for FormLayout {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
}
self.children.clear();
unsafe { ffi::QFormLayout_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}