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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! Single-line text input widget.
//!
//! Wraps [`QLineEdit`](https://doc.qt.io/qt-6/qlineedit.html).
use cxx::let_cxx_string;
use crate::ffi;
use crate::signal;
use crate::widget::AsWidget;
/// A single-line text input field.
///
/// `LineEdit` uses a **builder pattern**: call [`LineEdit::new`] to obtain
/// a [`Builder`], chain `.on_return_pressed(f)`, `.parent(w)`, then call
/// `.build()`.
///
/// # Signals
///
/// | Method | Qt signal | When |
/// |---|---|---|
/// | [`Builder::on_return_pressed`] | `QLineEdit::returnPressed` | User presses Enter/Return |
///
/// # Text caching
///
/// The widget stores a local copy of the text. It is updated when you
/// call [`set_text`](Self::set_text) or [`refresh_text`](Self::refresh_text).
/// The getter [`text`](Self::text) returns the cached copy without any
/// FFI call.
///
/// # Memory safety
///
/// See [`PushButton`] for signal-closure lifecycle rules — the same
/// parent/no-parent disconnect/reclaim logic applies here.
///
/// [`PushButton`]: crate::PushButton
///
/// # Example
///
/// ```no_run
/// use qtrs::LineEdit;
///
/// let edit = LineEdit::new("type here...")
/// .on_return_pressed(|| println!("Enter pressed!"))
/// .build();
/// ```
pub struct LineEdit {
ptr: *mut ffi::QLineEdit,
has_parent: bool,
#[allow(dead_code)]
text: String,
signal_handles: Vec<crate::signal::SignalHandle>,
}
impl LineEdit {
/// Start building a new `QLineEdit`.
///
/// Returns a [`Builder`]. Set placeholder text (via the initial value),
/// optional callbacks, and an optional parent, then call `.build()`.
pub fn new(text: impl Into<String>) -> Builder {
Builder::new(text.into())
}
/// Get the cached text.
///
/// To read the live value from the Qt widget, call
/// [`refresh_text`](Self::refresh_text) first.
pub fn text(&self) -> &str {
&self.text
}
/// Re-read the current text from the Qt widget.
///
/// This fetches `QLineEdit::text()` via FFI and updates the local
/// cache returned by [`text`](Self::text).
pub fn refresh_text(&mut self) {
debug_assert!(!self.ptr.is_null(), "LineEdit::refresh_text on null pointer");
self.text = unsafe { ffi::QLineEdit_text(self.ptr) };
}
/// Set the text at runtime.
pub fn set_text(&mut self, text: impl Into<String>) {
debug_assert!(!self.ptr.is_null(), "LineEdit::set_text on null pointer");
self.text = text.into();
let_cxx_string!(c_text = &self.text);
unsafe { ffi::QLineEdit_setText(self.ptr, &c_text); }
}
/// Clear the text content.
pub fn clear(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_clear(self.ptr); }
}
/// Select all text.
pub fn select_all(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_selectAll(self.ptr); }
}
/// Copy selected text to clipboard.
pub fn copy(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_copy(self.ptr); }
}
/// Cut selected text to clipboard.
pub fn cut(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_cut(self.ptr); }
}
/// Paste text from clipboard.
pub fn paste(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_paste(self.ptr); }
}
/// Undo the last edit operation.
pub fn undo(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_undo(self.ptr); }
}
/// Redo the last undone operation.
pub fn redo(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_redo(self.ptr); }
}
/// Set whether the text is read-only.
pub fn set_read_only(&self, ro: bool) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_setReadOnly(self.ptr, ro); }
}
/// Returns `true` if the line edit is read-only.
pub fn is_read_only(&self) -> bool {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_isReadOnly(self.ptr) }
}
/// Set the echo mode (e.g. password mode).
pub fn set_echo_mode(&self, mode: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_setEchoMode(self.ptr, mode); }
}
/// Set the maximum input length.
pub fn set_max_length(&self, len: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_setMaxLength(self.ptr, len); }
}
/// Get the maximum input length.
pub fn max_length(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_maxLength(self.ptr) }
}
/// Get the current cursor position.
pub fn cursor_position(&self) -> i32 {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_cursorPosition(self.ptr) }
}
/// Set the cursor position.
pub fn set_cursor_position(&self, pos: i32) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QLineEdit_setCursorPosition(self.ptr, pos); }
}
/// Connect a return-pressed callback to an already-existing widget.
pub fn connect_return_pressed<F: Fn()>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_void(f);
unsafe { ffi::QLineEdit_onReturnPressed(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QLineEdit, text: &str) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr, has_parent: true, text: text.to_string(), signal_handles: Vec::new() }
}
}
impl AsWidget for LineEdit {
fn widget_ptr(&self) -> *mut ffi::QWidget {
debug_assert!(!self.ptr.is_null(), "LineEdit::widget_ptr on null pointer");
unsafe { ffi::toQWidget_QLineEdit(self.ptr) }
}
fn set_has_parent(&mut self) {
self.has_parent = true;
}
}
impl Drop for LineEdit {
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::QLineEdit_delete(self.ptr) };
}
self.ptr = std::ptr::null_mut();
}
}
// ============================================================
// Builder
// ============================================================
/// Builder for [`LineEdit`].
///
/// Collects initial text, signal callbacks, and parent, then creates the
/// C++ `QLineEdit` (and connects signals) in [`build`](Self::build).
pub struct Builder {
text: String,
read_only: bool,
echo_mode: Option<i32>,
max_length: Option<i32>,
on_return_pressed: Option<Box<dyn Fn()>>,
parent: Option<*mut ffi::QWidget>,
}
impl Builder {
fn new(text: String) -> Self {
Self { text, read_only: false, echo_mode: None, max_length: None, on_return_pressed: None, parent: None }
}
/// Make the line edit read-only.
pub fn read_only(mut self, ro: bool) -> Self { self.read_only = ro; self }
/// Set the echo mode (e.g. password mode).
pub fn echo_mode(mut self, mode: i32) -> Self { self.echo_mode = Some(mode); self }
/// Set the maximum input length.
pub fn max_length(mut self, len: i32) -> Self { self.max_length = Some(len); self }
/// Set the callback for when the user presses Enter/Return.
///
/// The closure is stored on the heap and reclaimed when the widget
/// is dropped (only if the widget has no Qt parent).
pub fn on_return_pressed<F: Fn() + 'static>(mut self, f: F) -> Self {
self.on_return_pressed = Some(Box::new(f));
self
}
/// Set the parent widget.
///
/// The parent manages the line-edit's C++ lifetime.
pub fn parent(mut self, parent: &dyn AsWidget) -> Self {
self.parent = Some(parent.widget_ptr());
self
}
/// Create the C++ `QLineEdit`, connect signals, and return the Rust
/// wrapper.
///
/// This is the terminal method of the builder pattern.
pub fn build(self) -> LineEdit {
let_cxx_string!(c_text = &self.text);
let ptr = unsafe {
ffi::QLineEdit_new(
&c_text,
self.parent.unwrap_or(std::ptr::null_mut()),
)
};
assert!(!ptr.is_null(), "QLineEdit_new returned null");
let has_parent = self.parent.is_some();
let mut signal_handles = Vec::new();
if self.read_only { unsafe { ffi::QLineEdit_setReadOnly(ptr, true); } }
if let Some(mode) = self.echo_mode { unsafe { ffi::QLineEdit_setEchoMode(ptr, mode); } }
if let Some(len) = self.max_length { unsafe { ffi::QLineEdit_setMaxLength(ptr, len); } }
if let Some(cb) = self.on_return_pressed {
let handle = signal::leak_void(cb);
unsafe { ffi::QLineEdit_onReturnPressed(ptr, handle.token); }
signal_handles.push(handle);
}
LineEdit {
ptr,
has_parent,
text: self.text,
signal_handles,
}
}
/// Build and show the widget.
pub fn show(self) -> LineEdit {
self.build()
}
}