use cxx::let_cxx_string;
use crate::ffi;
use crate::signal::{self, SignalHandle};
use crate::widget::AsWidget;
pub struct TabWidget {
ptr: *mut ffi::QTabWidget,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
pages: Vec<Box<dyn AsWidget>>,
}
impl TabWidget {
pub fn new() -> Builder {
Builder::new()
}
pub fn add_tab(&mut self, page: Box<dyn AsWidget>, label: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_label = label);
unsafe { ffi::QTabWidget_addTab(self.ptr, page.widget_ptr(), &c_label); }
let mut page = page;
page.set_has_parent();
self.pages.push(page);
}
pub fn current_index(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTabWidget_currentIndex(self.ptr) }
}
pub fn set_current_index(&self, index: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTabWidget_setCurrentIndex(self.ptr, index); }
}
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QTabWidget) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new(), pages: Vec::new() }
}
}
impl AsWidget for TabWidget {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QTabWidget(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for TabWidget {
fn drop(&mut self) {
if self.ptr.is_null() { return; }
if self.has_parent {
self.pages.clear();
self.signal_handles.clear();
} else {
for h in self.signal_handles.drain(..) { unsafe { h.reclaim(); } }
self.pages.clear();
unsafe { ffi::QTabWidget_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
on_current_changed: Option<Box<dyn Fn(i32)>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self { on_current_changed: None, parent: None }
}
pub fn on_current_changed<F: Fn(i32) + 'static>(mut self, f: F) -> Self {
self.on_current_changed = Some(Box::new(f));
self
}
pub fn parent(mut self, parent: &dyn AsWidget) -> Self {
self.parent = Some(parent.widget_ptr());
self
}
pub fn build(self) -> TabWidget {
let ptr = unsafe {
ffi::QTabWidget_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
debug_assert!(!ptr.is_null());
let mut tw = TabWidget {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
pages: Vec::new(),
};
if let Some(f) = self.on_current_changed {
let h = signal::leak_int(f);
unsafe { ffi::QTabWidget_onCurrentChanged(ptr, h.token); }
tw.signal_handles.push(h);
}
tw
}
}