use cxx::let_cxx_string;
use crate::ffi;
use crate::signal::{self, SignalHandle};
use crate::widget::AsWidget;
pub struct ToolBox {
ptr: *mut ffi::QToolBox,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
pages: Vec<Box<dyn AsWidget>>,
}
impl ToolBox {
pub fn new() -> Builder { Builder::new() }
pub fn add_item(&mut self, widget: Box<dyn AsWidget>, text: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_text = text);
let mut widget = widget;
widget.set_has_parent();
unsafe {
ffi::QToolBox_addItem(self.ptr, widget.widget_ptr(), &c_text);
}
self.pages.push(widget);
}
pub fn add<T: AsWidget + 'static>(&mut self, widget: T, text: &str) {
self.add_item(Box::new(widget), text);
}
pub fn insert_item(&mut self, index: i32, widget: Box<dyn AsWidget>, text: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_text = text);
let mut widget = widget;
widget.set_has_parent();
unsafe {
ffi::QToolBox_insertItem(self.ptr, index, widget.widget_ptr(), &c_text);
}
self.pages.push(widget);
}
pub fn remove_item(&self, index: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_removeItem(self.ptr, index); }
}
pub fn set_item_text(&self, index: i32, text: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_text = text);
unsafe { ffi::QToolBox_setItemText(self.ptr, index, &c_text); }
}
pub fn item_text(&self, index: i32) -> String {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_itemText(self.ptr, index) }
}
pub fn set_item_icon(&self, index: i32, icon_path: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_path = icon_path);
unsafe { ffi::QToolBox_setItemIcon(self.ptr, index, &c_path); }
}
pub fn set_item_enabled(&self, index: i32, enabled: bool) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_setItemEnabled(self.ptr, index, enabled); }
}
pub fn is_item_enabled(&self, index: i32) -> bool {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_isItemEnabled(self.ptr, index) }
}
pub fn current_index(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_currentIndex(self.ptr) }
}
pub fn set_current_index(&self, index: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_setCurrentIndex(self.ptr, index); }
}
pub fn count(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_count(self.ptr) }
}
#[doc(hidden)]
pub fn widget_ptr(&self, index: i32) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QToolBox_widget(self.ptr, index) }
}
pub fn connect_current_changed<F: Fn()>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QToolBox_onCurrentChanged(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
#[doc(hidden)]
#[allow(dead_code)]
pub(crate) fn from_raw(ptr: *mut ffi::QToolBox) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new(), pages: Vec::new() }
}
}
impl AsWidget for ToolBox {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QToolBox(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for ToolBox {
fn drop(&mut self) {
if self.ptr.is_null() { return; }
if self.has_parent {
self.pages.clear();
unsafe { ffi::QWidget_disconnectAll(self.ptr as *mut _); }
for h in self.signal_handles.drain(..) { unsafe { h.reclaim(); } }
} else {
for h in self.signal_handles.drain(..) { unsafe { h.reclaim(); } }
self.pages.clear();
unsafe { ffi::QToolBox_delete(self.ptr); }
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
on_current_changed: Option<Box<dyn Fn()>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self { on_current_changed: None, parent: None }
}
pub fn on_current_changed<F: Fn() + '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) -> ToolBox {
let ptr = unsafe {
ffi::QToolBox_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
assert!(!ptr.is_null(), "QToolBox_new returned null");
let mut tb = ToolBox {
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_void(f);
unsafe { ffi::QToolBox_onCurrentChanged(ptr, h.token); }
tb.signal_handles.push(h);
}
tb
}
}