1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Dialog support.
//!
//! Provides a builder-pattern API for creating modal dialogs with
//! custom content.
use cxx::let_cxx_string;
use crate::ffi;
use crate::widget::AsWidget;
use crate::layout::VBoxLayout;
/// A custom dialog window.
///
/// Use the builder pattern: call [`Dialog::new`] to get a [`DialogBuilder`],
/// configure with methods like `.title()`, `.modal()`, `.size()`, and
/// finally `.build()` to create the dialog.
///
/// # Example
///
/// ```no_run
/// use qtrs::{Dialog, Label, PushButton, VBoxLayout};
///
/// let dialog = Dialog::new()
/// .title("Configuration")
/// .modal(true)
/// .size(400, 300)
/// .build(|layout| {
/// layout.add(Label::new("Hello, world!").build());
/// });
/// ```
pub struct Dialog {
ptr: *mut ffi::QDialog,
has_parent: bool,
}
impl Dialog {
/// Start building a new dialog.
pub fn new() -> DialogBuilder {
DialogBuilder::new()
}
/// Get the raw dialog pointer.
pub fn dialog_ptr(&self) -> *mut ffi::QDialog {
self.ptr
}
/// Show the dialog (non-modal).
pub fn show(&self) {
unsafe { ffi::QDialog_show(self.ptr); }
}
/// Show the dialog as a modal (blocks until closed).
pub fn exec(&self) {
unsafe { ffi::QDialog_exec(self.ptr); }
}
/// Close the dialog with accept.
pub fn accept(&self) {
unsafe { ffi::QDialog_accept(self.ptr); }
}
/// Close the dialog with reject.
pub fn reject(&self) {
unsafe { ffi::QDialog_reject(self.ptr); }
}
}
impl AsWidget for Dialog {
fn widget_ptr(&self) -> *mut ffi::QWidget {
self.ptr as *mut ffi::QWidget
}
fn set_has_parent(&mut self) {
self.has_parent = true;
}
}
/// Builder for [`Dialog`].
pub struct DialogBuilder {
title: Option<String>,
modal: bool,
size: Option<(i32, i32)>,
parent: Option<*mut ffi::QWidget>,
}
impl DialogBuilder {
fn new() -> Self {
Self {
title: None,
modal: false,
size: None,
parent: None,
}
}
/// Set the dialog title.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Make the dialog modal (blocks parent window).
pub fn modal(mut self, modal: bool) -> Self {
self.modal = modal;
self
}
/// Set the dialog size.
pub fn size(mut self, width: i32, height: i32) -> Self {
self.size = Some((width, height));
self
}
/// Set the parent widget.
pub fn parent(mut self, parent: &dyn AsWidget) -> Self {
self.parent = Some(parent.widget_ptr());
self
}
/// Build the dialog with a callback that populates the content.
///
/// The callback receives a `&mut VBoxLayout` that you can use to
/// add widgets to the dialog.
pub fn build<F>(self, content: F) -> Dialog
where
F: FnOnce(&mut VBoxLayout),
{
let parent = self.parent.unwrap_or(std::ptr::null_mut());
let ptr = unsafe { ffi::QDialog_new(parent) };
assert!(!ptr.is_null(), "QDialog_new returned null");
if let Some(title) = &self.title {
let_cxx_string!(c_title = title);
unsafe { ffi::QDialog_setWindowTitle(ptr, &c_title); }
}
if self.modal {
unsafe { ffi::QDialog_setModal(ptr, true); }
}
if let Some((w, h)) = self.size {
unsafe { ffi::QDialog_setMinimumSize(ptr, w, h); }
unsafe { ffi::QDialog_resize(ptr, w, h); }
}
let dialog = Dialog {
ptr,
has_parent: self.parent.is_some(),
};
// Create main layout
let mut layout = VBoxLayout::new();
unsafe { ffi::QDialog_setLayout(ptr, layout.layout_ptr() as *mut ffi::QLayout); }
// Call content callback
content(&mut layout);
dialog
}
}
impl Drop for Dialog {
fn drop(&mut self) {
if self.ptr.is_null() || self.has_parent {
return;
}
unsafe { ffi::QDialog_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}