use cxx::let_cxx_string;
use crate::ffi;
use crate::signal::{self, SignalHandle};
use crate::widget::AsWidget;
pub const SINGLE_SELECTION: i32 = 0;
pub const MULTI_SELECTION: i32 = 1;
pub const EXTENDED_SELECTION: i32 = 2;
pub const CONTIGUOUS_SELECTION: i32 = 3;
pub const NO_SELECTION: i32 = 4;
pub struct ListWidget {
ptr: *mut ffi::QListWidget,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
}
impl ListWidget {
pub fn new() -> Builder { Builder::new() }
pub fn add_item(&self, text: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_text = text);
unsafe { ffi::QListWidget_addItem(self.ptr, &c_text); }
}
pub fn add_items(&self, items: &[&str]) {
debug_assert!(!self.ptr.is_null());
for text in items {
let_cxx_string!(c_text = text);
unsafe { ffi::QListWidget_addItem(self.ptr, &c_text); }
}
}
pub fn insert_item(&self, row: i32, text: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_text = text);
unsafe { ffi::QListWidget_insertItem(self.ptr, row, &c_text); }
}
pub fn clear(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_clear(self.ptr); }
}
pub fn remove_item(&self, row: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_removeItem(self.ptr, row); }
}
pub fn count(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_count(self.ptr) }
}
pub fn item_text(&self, row: i32) -> String {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_itemText(self.ptr, row) }
}
pub fn current_row(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_currentRow(self.ptr) }
}
pub fn current_text(&self) -> String {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_currentText(self.ptr) }
}
pub fn set_current_row(&self, row: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_setCurrentRow(self.ptr, row); }
}
pub fn set_selection_mode(&self, mode: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QListWidget_setSelectionMode(self.ptr, mode); }
}
pub fn connect_item_clicked<F: Fn(String) + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_string(f);
unsafe { ffi::QListWidget_onItemClicked(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
pub fn connect_item_double_clicked<F: Fn(String) + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_string(f);
unsafe { ffi::QListWidget_onItemDoubleClicked(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
pub fn connect_current_item_changed<F: Fn(String) + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_string(f);
unsafe { ffi::QListWidget_onCurrentItemChanged(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QListWidget, _name: &str) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new() }
}
}
impl AsWidget for ListWidget {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QListWidget(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for ListWidget {
fn drop(&mut self) {
if self.ptr.is_null() { return; }
if self.has_parent {
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(); } }
unsafe { ffi::QListWidget_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
items: Vec<String>,
on_item_clicked: Option<Box<dyn Fn(String)>>,
on_item_double_clicked: Option<Box<dyn Fn(String)>>,
on_current_item_changed: Option<Box<dyn Fn(String)>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self {
items: Vec::new(),
on_item_clicked: None,
on_item_double_clicked: None,
on_current_item_changed: None,
parent: None,
}
}
pub fn items(mut self, items: &[&str]) -> Self {
self.items = items.iter().map(|s| s.to_string()).collect();
self
}
pub fn on_item_clicked<F: Fn(String) + 'static>(mut self, f: F) -> Self {
self.on_item_clicked = Some(Box::new(f));
self
}
pub fn on_item_double_clicked<F: Fn(String) + 'static>(mut self, f: F) -> Self {
self.on_item_double_clicked = Some(Box::new(f));
self
}
pub fn on_current_item_changed<F: Fn(String) + 'static>(mut self, f: F) -> Self {
self.on_current_item_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) -> ListWidget {
let ptr = unsafe {
ffi::QListWidget_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
debug_assert!(!ptr.is_null());
let mut lw = ListWidget {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
};
for item in &self.items {
let_cxx_string!(c_item = item);
unsafe { ffi::QListWidget_addItem(ptr, &c_item); }
}
if let Some(f) = self.on_item_clicked {
let h = signal::leak_string(f);
unsafe { ffi::QListWidget_onItemClicked(ptr, h.token); }
lw.signal_handles.push(h);
}
if let Some(f) = self.on_item_double_clicked {
let h = signal::leak_string(f);
unsafe { ffi::QListWidget_onItemDoubleClicked(ptr, h.token); }
lw.signal_handles.push(h);
}
if let Some(f) = self.on_current_item_changed {
let h = signal::leak_string(f);
unsafe { ffi::QListWidget_onCurrentItemChanged(ptr, h.token); }
lw.signal_handles.push(h);
}
lw
}
}