use crate::ffi;
use crate::signal::SignalHandle;
use crate::widget::AsWidget;
pub const NO_FRAME: i32 = 0;
pub const BOX: i32 = 1;
pub const PANEL: i32 = 2;
pub const STYLED_PANEL: i32 = 3;
pub const H_LINE: i32 = 4;
pub const V_LINE: i32 = 5;
pub const PLAIN: i32 = 16;
pub const RAISED: i32 = 32;
pub const SUNKEN: i32 = 48;
pub struct Frame {
ptr: *mut ffi::QFrame,
has_parent: bool,
signal_handles: Vec<SignalHandle>,
}
impl Frame {
pub fn new() -> Builder {
Builder::new()
}
pub fn set_frame_shape(&self, shape: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFrame_setFrameShape(self.ptr, shape); }
}
pub fn set_frame_shadow(&self, shadow: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFrame_setFrameShadow(self.ptr, shadow); }
}
pub fn set_line_width(&self, width: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFrame_setLineWidth(self.ptr, width); }
}
pub fn set_mid_line_width(&self, width: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFrame_setMidLineWidth(self.ptr, width); }
}
pub fn set_frame_style(&self, style: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QFrame_setFrameStyle(self.ptr, style); }
}
#[doc(hidden)]
#[allow(dead_code)]
pub(crate) fn from_raw(ptr: *mut ffi::QFrame, _name: &str) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, signal_handles: Vec::new() }
}
}
impl AsWidget for Frame {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::toQWidget_QFrame(self.ptr) }
}
fn set_has_parent(&mut self) { self.has_parent = true; }
}
impl Drop for Frame {
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::QFrame_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
pub struct Builder {
frame_shape: Option<i32>,
frame_shadow: Option<i32>,
line_width: Option<i32>,
mid_line_width: Option<i32>,
frame_style: Option<i32>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new() -> Self {
Self {
frame_shape: None,
frame_shadow: None,
line_width: None,
mid_line_width: None,
frame_style: None,
parent: None,
}
}
pub fn set_frame_shape(mut self, shape: i32) -> Self {
self.frame_shape = Some(shape);
self
}
pub fn set_frame_shadow(mut self, shadow: i32) -> Self {
self.frame_shadow = Some(shadow);
self
}
pub fn set_line_width(mut self, width: i32) -> Self {
self.line_width = Some(width);
self
}
pub fn set_mid_line_width(mut self, width: i32) -> Self {
self.mid_line_width = Some(width);
self
}
pub fn set_frame_style(mut self, style: i32) -> Self {
self.frame_style = Some(style);
self
}
pub fn parent(mut self, parent: &dyn AsWidget) -> Self {
self.parent = Some(parent.widget_ptr());
self
}
pub fn build(self) -> Frame {
let ptr = unsafe {
ffi::QFrame_new(self.parent.unwrap_or(std::ptr::null_mut()))
};
debug_assert!(!ptr.is_null());
let frame = Frame {
ptr,
has_parent: self.parent.is_some(),
signal_handles: Vec::new(),
};
if let Some(shape) = self.frame_shape {
unsafe { ffi::QFrame_setFrameShape(ptr, shape); }
}
if let Some(shadow) = self.frame_shadow {
unsafe { ffi::QFrame_setFrameShadow(ptr, shadow); }
}
if let Some(width) = self.line_width {
unsafe { ffi::QFrame_setLineWidth(ptr, width); }
}
if let Some(width) = self.mid_line_width {
unsafe { ffi::QFrame_setMidLineWidth(ptr, width); }
}
if let Some(style) = self.frame_style {
unsafe { ffi::QFrame_setFrameStyle(ptr, style); }
}
frame
}
}