use crate::ffi;
use crate::layout::AsLayout;
use crate::widget::AsWidget;
pub struct GridLayout {
ptr: *mut ffi::QGridLayout,
children: Vec<Box<dyn AsWidget>>,
}
impl GridLayout {
pub fn new() -> Self {
let ptr = unsafe { ffi::QGridLayout_new(std::ptr::null_mut()) };
debug_assert!(!ptr.is_null());
Self { ptr, children: Vec::new() }
}
pub fn with_parent(parent: &dyn AsWidget) -> Self {
let ptr = unsafe { ffi::QGridLayout_new(parent.widget_ptr()) };
debug_assert!(!ptr.is_null());
Self { ptr, children: Vec::new() }
}
pub fn add_widget(
&mut self, mut widget: Box<dyn AsWidget>,
row: i32, col: i32, row_span: i32, col_span: i32,
) {
debug_assert!(!self.ptr.is_null());
unsafe {
ffi::QGridLayout_addWidget(self.ptr, widget.widget_ptr(), row, col, row_span, col_span);
}
widget.set_has_parent();
self.children.push(widget);
}
pub fn layout_ptr(&self) -> *mut ffi::QGridLayout { self.ptr }
}
impl AsLayout for GridLayout {
fn layout_ptr(&self) -> *mut ffi::QLayout {
self.ptr as *mut u8 as *mut ffi::QLayout
}
}
impl Drop for GridLayout {
fn drop(&mut self) {
if self.ptr.is_null() { return; }
self.children.clear();
unsafe { ffi::QGridLayout_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}