use crate::ffi;
use crate::widget::AsWidget;
pub trait AsLayout {
fn layout_ptr(&self) -> *mut ffi::QLayout;
}
pub struct VBoxLayout {
ptr: *mut ffi::QVBoxLayout,
children: Vec<Box<dyn AsWidget>>,
}
impl VBoxLayout {
pub fn new() -> Self {
let ptr = unsafe { ffi::QVBoxLayout_new(std::ptr::null_mut()) };
assert!(!ptr.is_null(), "QVBoxLayout_new returned null");
Self {
ptr,
children: Vec::new(),
}
}
pub fn with_parent(parent: &dyn AsWidget) -> Self {
let ptr = unsafe { ffi::QVBoxLayout_new(parent.widget_ptr()) };
assert!(!ptr.is_null(), "QVBoxLayout_new returned null");
Self {
ptr,
children: Vec::new(),
}
}
pub fn add_widget(&mut self, mut widget: Box<dyn AsWidget>) {
debug_assert!(!self.ptr.is_null(), "VBoxLayout::add_widget on null pointer");
unsafe {
ffi::QVBoxLayout_addWidget(self.ptr, widget.widget_ptr());
}
widget.set_has_parent(); self.children.push(widget);
}
pub fn add<T: AsWidget + 'static>(&mut self, widget: T) {
self.add_widget(Box::new(widget));
}
pub fn layout_ptr(&self) -> *mut ffi::QVBoxLayout {
self.ptr
}
pub fn set_spacing(&self, spacing: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QVBoxLayout_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::QVBoxLayout_setContentsMargins(self.ptr, left, top, right, bottom); }
}
}
impl AsLayout for VBoxLayout {
fn layout_ptr(&self) -> *mut ffi::QLayout {
self.ptr as *mut u8 as *mut ffi::QLayout
}
}
impl Drop for VBoxLayout {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
}
self.children.clear();
unsafe { ffi::QVBoxLayout_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}
pub struct HBoxLayout {
ptr: *mut ffi::QHBoxLayout,
children: Vec<Box<dyn AsWidget>>,
}
impl HBoxLayout {
pub fn new() -> Self {
let ptr = unsafe { ffi::QHBoxLayout_new(std::ptr::null_mut()) };
assert!(!ptr.is_null(), "QHBoxLayout_new returned null");
Self {
ptr,
children: Vec::new(),
}
}
pub fn with_parent(parent: &dyn AsWidget) -> Self {
let ptr = unsafe { ffi::QHBoxLayout_new(parent.widget_ptr()) };
assert!(!ptr.is_null(), "QHBoxLayout_new returned null");
Self {
ptr,
children: Vec::new(),
}
}
pub fn add_widget(&mut self, mut widget: Box<dyn AsWidget>) {
debug_assert!(!self.ptr.is_null(), "HBoxLayout::add_widget on null pointer");
unsafe {
ffi::QHBoxLayout_addWidget(self.ptr, widget.widget_ptr());
}
widget.set_has_parent();
self.children.push(widget);
}
pub fn add<T: AsWidget + 'static>(&mut self, widget: T) {
self.add_widget(Box::new(widget));
}
pub fn layout_ptr(&self) -> *mut ffi::QHBoxLayout {
self.ptr
}
pub fn set_spacing(&self, spacing: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QHBoxLayout_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::QHBoxLayout_setContentsMargins(self.ptr, left, top, right, bottom); }
}
}
impl AsLayout for HBoxLayout {
fn layout_ptr(&self) -> *mut ffi::QLayout {
self.ptr as *mut u8 as *mut ffi::QLayout
}
}
impl Drop for HBoxLayout {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
}
self.children.clear();
unsafe { ffi::QHBoxLayout_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}