use cxx::let_cxx_string;
use crate::ffi;
use crate::signal::{self, SignalHandle};
use crate::widget::AsWidget;
pub struct DockWidget {
ptr: *mut ffi::QDockWidget,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
}
impl DockWidget {
pub fn new() -> Builder { Builder::new() }
pub fn set_window_title(&self, title: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_title = title);
unsafe { ffi::QDockWidget_setWindowTitle(self.ptr, &c_title); }
}
pub fn set_widget(&self, w: &dyn AsWidget) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_setWidget(self.ptr, w.widget_ptr()); }
}
#[doc(hidden)]
pub fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_widget(self.ptr) }
}
pub fn set_features(&self, features: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_setFeatures(self.ptr, features); }
}
pub fn features(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_features(self.ptr) }
}
pub fn set_allowed_areas(&self, areas: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_setAllowedAreas(self.ptr, areas); }
}
pub fn allowed_areas(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_allowedAreas(self.ptr) }
}
pub fn set_floating(&self, floating: bool) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_setFloating(self.ptr, floating); }
}
pub fn is_floating(&self) -> bool {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_isFloating(self.ptr) }
}
pub fn show(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_show(self.ptr); }
}
pub fn hide(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QDockWidget_hide(self.ptr); }
}
pub fn connect_visibility_changed<F: Fn()>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QDockWidget_onVisibilityChanged(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
pub fn connect_features_changed<F: Fn()>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QDockWidget_onFeaturesChanged(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
#[doc(hidden)]
#[allow(dead_code)]
pub(crate) fn from_raw(ptr: *mut ffi::QDockWidget) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new() }
}
}
impl AsWidget for DockWidget {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QDockWidget(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for DockWidget {
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::QDockWidget_delete(self.ptr); }
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
window_title: Option<String>,
features: Option<i32>,
allowed_areas: Option<i32>,
floating: Option<bool>,
on_visibility_changed: Option<Box<dyn Fn()>>,
on_features_changed: Option<Box<dyn Fn()>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self {
window_title: None,
features: None,
allowed_areas: None,
floating: None,
on_visibility_changed: None,
on_features_changed: None,
parent: None,
}
}
pub fn window_title(mut self, title: impl Into<String>) -> Self {
self.window_title = Some(title.into());
self
}
pub fn features(mut self, features: i32) -> Self {
self.features = Some(features);
self
}
pub fn allowed_areas(mut self, areas: i32) -> Self {
self.allowed_areas = Some(areas);
self
}
pub fn floating(mut self, floating: bool) -> Self {
self.floating = Some(floating);
self
}
pub fn on_visibility_changed<F: Fn() + 'static>(mut self, f: F) -> Self {
self.on_visibility_changed = Some(Box::new(f));
self
}
pub fn on_features_changed<F: Fn() + 'static>(mut self, f: F) -> Self {
self.on_features_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) -> DockWidget {
let ptr = unsafe {
ffi::QDockWidget_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
assert!(!ptr.is_null(), "QDockWidget_new returned null");
let mut dw = DockWidget {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
};
if let Some(ref title) = self.window_title {
dw.set_window_title(title);
}
if let Some(features) = self.features {
unsafe { ffi::QDockWidget_setFeatures(ptr, features); }
}
if let Some(areas) = self.allowed_areas {
unsafe { ffi::QDockWidget_setAllowedAreas(ptr, areas); }
}
if let Some(floating) = self.floating {
unsafe { ffi::QDockWidget_setFloating(ptr, floating); }
}
if let Some(f) = self.on_visibility_changed {
let h = signal::leak_void(f);
unsafe { ffi::QDockWidget_onVisibilityChanged(ptr, h.token); }
dw.signal_handles.push(h);
}
if let Some(f) = self.on_features_changed {
let h = signal::leak_void(f);
unsafe { ffi::QDockWidget_onFeaturesChanged(ptr, h.token); }
dw.signal_handles.push(h);
}
dw
}
}