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 const SELECT_ITEMS: i32 = 0;
pub const SELECT_ROWS: i32 = 1;
pub const SELECT_COLUMNS: i32 = 2;
pub struct TableWidget {
ptr: *mut ffi::QTableWidget,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
}
impl TableWidget {
pub fn new(rows: i32, cols: i32) -> Builder { Builder::new(rows, cols) }
pub fn set_row_count(&self, rows: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setRowCount(self.ptr, rows); }
}
pub fn set_column_count(&self, cols: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setColumnCount(self.ptr, cols); }
}
pub fn set_item(&self, row: i32, col: i32, text: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_text = text);
unsafe { ffi::QTableWidget_setItem(self.ptr, row, col, &c_text); }
}
pub fn item_text(&self, row: i32, col: i32) -> String {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_itemText(self.ptr, row, col) }
}
pub fn set_horizontal_header_labels(&self, labels: &[&str]) {
debug_assert!(!self.ptr.is_null());
let vec: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
unsafe { ffi::QTableWidget_setHorizontalHeaderLabels(self.ptr, vec); }
}
pub fn set_vertical_header_labels(&self, labels: &[&str]) {
debug_assert!(!self.ptr.is_null());
let vec: Vec<String> = labels.iter().map(|s| s.to_string()).collect();
unsafe { ffi::QTableWidget_setVerticalHeaderLabels(self.ptr, vec); }
}
pub fn set_current_cell(&self, row: i32, col: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setCurrentCell(self.ptr, row, col); }
}
pub fn current_row(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_currentRow(self.ptr) }
}
pub fn current_column(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_currentColumn(self.ptr) }
}
pub fn selected_rows(&self) -> Vec<i32> {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_selectedRows(self.ptr) }
}
pub fn clear(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_clear(self.ptr); }
}
pub fn clear_contents(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_clearContents(self.ptr); }
}
pub fn set_selection_mode(&self, mode: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setSelectionMode(self.ptr, mode); }
}
pub fn set_selection_behavior(&self, behavior: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setSelectionBehavior(self.ptr, behavior); }
}
pub fn remove_row(&self, row: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_removeRow(self.ptr, row); }
}
pub fn insert_row(&self, row: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_insertRow(self.ptr, row); }
}
pub fn set_column_width(&self, col: i32, width: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setColumnWidth(self.ptr, col, width); }
}
pub fn set_row_height(&self, row: i32, height: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTableWidget_setRowHeight(self.ptr, row, height); }
}
pub fn connect_cell_clicked<F: Fn() + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QTableWidget_onCellClicked(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
pub fn connect_cell_double_clicked<F: Fn() + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QTableWidget_onCellDoubleClicked(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
pub fn connect_current_cell_changed<F: Fn() + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QTableWidget_onCurrentCellChanged(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QTableWidget) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new() }
}
}
impl AsWidget for TableWidget {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QTableWidget(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for TableWidget {
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::QTableWidget_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
rows: i32,
cols: i32,
on_cell_clicked: Option<Box<dyn Fn()>>,
on_cell_double_clicked: Option<Box<dyn Fn()>>,
on_current_cell_changed: Option<Box<dyn Fn()>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new(rows: i32, cols: i32) -> Self {
Self {
rows,
cols,
on_cell_clicked: None,
on_cell_double_clicked: None,
on_current_cell_changed: None,
parent: None,
}
}
pub fn on_cell_clicked<F: Fn() + 'static>(mut self, f: F) -> Self {
self.on_cell_clicked = Some(Box::new(f));
self
}
pub fn on_cell_double_clicked<F: Fn() + 'static>(mut self, f: F) -> Self {
self.on_cell_double_clicked = Some(Box::new(f));
self
}
pub fn on_current_cell_changed<F: Fn() + 'static>(mut self, f: F) -> Self {
self.on_current_cell_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) -> TableWidget {
let ptr = unsafe {
ffi::QTableWidget_new(
self.rows,
self.cols,
self.parent.unwrap_or(std::ptr::null_mut()),
)
};
debug_assert!(!ptr.is_null());
let mut tw = TableWidget {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
};
if let Some(f) = self.on_cell_clicked {
let h = signal::leak_void(f);
unsafe { ffi::QTableWidget_onCellClicked(ptr, h.token); }
tw.signal_handles.push(h);
}
if let Some(f) = self.on_cell_double_clicked {
let h = signal::leak_void(f);
unsafe { ffi::QTableWidget_onCellDoubleClicked(ptr, h.token); }
tw.signal_handles.push(h);
}
if let Some(f) = self.on_current_cell_changed {
let h = signal::leak_void(f);
unsafe { ffi::QTableWidget_onCurrentCellChanged(ptr, h.token); }
tw.signal_handles.push(h);
}
tw
}
}