use cxx::let_cxx_string;
use crate::ffi;
use crate::signal::{self, SignalHandle};
use crate::widget::AsWidget;
pub struct TimeEdit {
ptr: *mut ffi::QTimeEdit,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
}
impl TimeEdit {
pub fn new() -> Builder { Builder::new() }
pub fn set_time(&self, time: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_time = time);
unsafe { ffi::QTimeEdit_setTime(self.ptr, &c_time); }
}
pub fn time(&self) -> String {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QTimeEdit_time(self.ptr) }
}
pub fn set_display_format(&self, format: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_format = format);
unsafe { ffi::QTimeEdit_setDisplayFormat(self.ptr, &c_format); }
}
pub fn connect_time_changed<F: Fn(String) + 'static>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_string(f);
unsafe { ffi::QTimeEdit_onTimeChanged(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QTimeEdit, _name: &str) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new() }
}
}
impl AsWidget for TimeEdit {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QTimeEdit(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for TimeEdit {
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::QTimeEdit_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
on_time_changed: Option<Box<dyn Fn(String)>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self { on_time_changed: None, parent: None }
}
pub fn on_time_changed<F: Fn(String) + 'static>(mut self, f: F) -> Self {
self.on_time_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) -> TimeEdit {
let ptr = unsafe {
ffi::QTimeEdit_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
debug_assert!(!ptr.is_null());
let mut te = TimeEdit {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
};
if let Some(f) = self.on_time_changed {
let h = signal::leak_string(f);
unsafe { ffi::QTimeEdit_onTimeChanged(ptr, h.token); }
te.signal_handles.push(h);
}
te
}
}