1use super::proxy_cpp_bridge::QListModelProxyCpp;
5use crate::{call_rust_trait_impl, call_cpp_impl};
6use qtbridge_runtime::{DispatchMetaCall, QObjectHolder};
7use crate::genericrustproxy::GenericRustProxy;
8use qtbridge_runtime::QModelItem;
9use qtbridge_type_lib::{QByteArray, QHash_i32_QByteArray, QModelIndex, QVariant};
10
11#[doc(hidden)]
12pub trait QListModelAdapter: DispatchMetaCall + 'static {
13 fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex;
14 fn row_count(&self, parent: &QModelIndex) -> i32;
15 fn data(&self, index: &QModelIndex, role: i32) -> QVariant;
16 fn role_names(&self) -> QHash_i32_QByteArray;
17 fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool;
18 fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool;
19 fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex;
20}
21
22impl<T> QListModelAdapter for T
23where
24 T: QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> {
25
26 fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
27 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
28 unsafe { &*proxy }.base_index(self, row, column, parent)
29 }
30
31 fn row_count(&self, _parent: &QModelIndex) -> i32 {
32 self.len() as i32
33 }
34
35 fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
36 let Some(item) = self.get(index.row() as usize)
37 else {
38 return QVariant::default();
39 };
40 item.get_role(role)
41 }
42
43 fn role_names(&self) -> QHash_i32_QByteArray {
44 let names = T::Item::role_names();
45 let mut result = QHash_i32_QByteArray::default();
46 names.into_iter()
47 .for_each(|(k, v)| result.insert(k, QByteArray::from(&v)));
48 result
49 }
50
51 fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
52 if !index.is_valid() {
53 return false;
54 }
55 let Some(mut item) = self.get(index.row() as usize)
56 .cloned()
57 else {
58 return false;
59 };
60 let updated = item.set_role(role, value);
61 if updated {
62 self.set_unnotified(index.row() as usize, item);
63 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
64 unsafe { &mut *proxy }.base_data_changed(&mut *self, index, index);
65 }
66 updated
67 }
68
69 fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool {
70 let first = first as usize;
71 let last = first + count as usize;
72 if last > self.len() {
73 return false;
74 }
75 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
76 unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, parent, first as i32, (last - 1) as i32);
77 for index in (first..last).rev() {
78 self.remove_unnotified(index);
79 }
80 unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
81 true
82 }
83
84 fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
85 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
86 unsafe { &*proxy }.base_sibling(self, row, column, idx)
87 }
88}
89
90#[allow(clippy::len_without_is_empty)]
160pub trait QListModel {
161 type Item: QModelItem + Default + Clone;
168
169 fn len(&self) -> usize;
171
172 fn get(&self, index: usize) -> Option<&Self::Item>;
175
176 fn set_unnotified(&mut self, _index: usize, _value: Self::Item) -> bool {
185 false
186 }
187
188 fn push_unnotified(&mut self, value: Self::Item) {
198 self.insert_unnotified(self.len(), value);
199 }
200
201 fn insert_unnotified(&mut self, _index: usize, _value: Self::Item) {
211 panic!("In order to use insert, implement insert_unnotified")
212 }
213
214 fn pop_unnotified(&mut self) -> Option<Self::Item> {
224 (self.len() > 0)
225 .then(|| self.remove_unnotified(self.len() - 1))
226 }
227
228 fn remove_unnotified(&mut self, _index: usize) -> Self::Item {
238 panic!("In order to use remove, implement remove_unnotified")
239 }
240
241 fn reset_unnotified(&mut self) {
251 panic!("In order to use reset, implement reset_unnotified")
252 }
253}
254
255pub trait QListModelBase : QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> {
283 fn set(&mut self, index: usize, value: <Self as QListModel>::Item) -> bool {
292 if self.set_unnotified(index, value) {
293 if let Some(proxy) = self.try_get_rust_proxy_ptr() {
294 let model_index = unsafe { &*proxy }.base_index(&*self, index as i32, 0, &QModelIndex::default());
295 unsafe { &mut *proxy }.base_data_changed(&mut *self, &model_index, &model_index);
296 }
297 true
298 } else {
299 false
300 }
301 }
302
303 fn push(&mut self, value: Self::Item) {
308 let Some(proxy) = self.try_get_rust_proxy_ptr() else {
309 return self.push_unnotified(value);
310 };
311 let len = self.len() as i32;
312 unsafe { &mut *proxy }.base_begin_insert_rows(&mut *self, &QModelIndex::default(), len, len);
313 self.push_unnotified(value);
314 unsafe { &mut *proxy }.base_end_insert_rows(&mut *self);
315 }
316
317 fn insert(&mut self, index: usize, value: Self::Item) {
322 let Some(proxy) = self.try_get_rust_proxy_ptr() else {
323 return self.insert_unnotified(index, value);
324 };
325 unsafe { &mut *proxy }.base_begin_insert_rows(&mut *self, &QModelIndex::default(), index as i32, index as i32);
326 self.insert_unnotified(index, value);
327 unsafe { &mut *proxy }.base_end_insert_rows(&mut *self);
328 }
329
330 fn pop(&mut self) -> Option<Self::Item> {
338 if self.len() == 0 {
339 return None;
340 }
341 let Some(proxy) = self.try_get_rust_proxy_ptr() else {
342 return self.pop_unnotified();
343 };
344 let len = self.len() as i32;
345 unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, &QModelIndex::default(), len - 1, len - 1);
346 let value = self.pop_unnotified();
347 unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
348 value
349 }
350
351 fn remove(&mut self, index: usize) -> Self::Item {
356 let Some(proxy) = self.try_get_rust_proxy_ptr() else {
357 return self.remove_unnotified(index);
358 };
359 unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, &QModelIndex::default(), index as i32, index as i32);
360 let value = self.remove_unnotified(index);
361 unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
362 value
363 }
364
365 fn reset(&mut self) {
369 let Some(proxy) = self.try_get_rust_proxy_ptr() else {
370 return self.reset_unnotified();
371 };
372 unsafe { &mut *proxy }.base_begin_reset_model(&mut *self);
373 self.reset_unnotified();
374 unsafe { &mut *proxy }.base_end_reset_model(&mut *self);
375 }
376}
377
378impl<T> QListModelBase for T
379where T: QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> { }
380
381pub type QListModelProxyRust = GenericRustProxy<QListModelProxyCpp, dyn QListModelAdapter>;
382
383impl<T: QListModelAdapter + 'static> qtbridge_runtime::qproxies::AdapterUpcast<T> for QListModelProxyRust {
384 fn upcast(rc: std::rc::Rc<std::cell::RefCell<T>>) -> std::rc::Rc<std::cell::RefCell<dyn QListModelAdapter>> {
385 rc
386 }
387}
388
389impl QListModelProxyRust {
390 pub fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
391 call_rust_trait_impl!(self, index(row, column, parent))
392 }
393 pub fn row_count(&self, parent: &QModelIndex) -> i32 {
394 call_rust_trait_impl!(self, row_count(parent))
395 }
396 pub fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
397 call_rust_trait_impl!(self, data(index, role))
398 }
399 pub fn role_names(&self) -> QHash_i32_QByteArray {
400 call_rust_trait_impl!(self, role_names())
401 }
402 pub fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
403 call_rust_trait_impl!(mut self, set_data(index, value, role))
404 }
405 pub fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool {
406 call_rust_trait_impl!(mut self, remove_rows(first, count, parent))
407 }
408 pub fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
409 call_rust_trait_impl!(self, sibling(row, column, idx))
410 }
411
412 pub fn base_index(&self, reference: &dyn QListModelAdapter, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
413 call_cpp_impl!(self, reference, base_index(row, column, parent))
414 }
415 pub fn base_role_names(&self, reference: &dyn QListModelAdapter) -> QHash_i32_QByteArray {
416 call_cpp_impl!(self, reference, base_role_names())
417 }
418 pub fn base_set_data(&mut self, mut_ref: &mut dyn QListModelAdapter, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
419 call_cpp_impl!(mut self, mut_ref, base_set_data(index, value, role))
420 }
421 pub fn base_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, first: i32, count: i32, parent: &QModelIndex) -> bool {
422 call_cpp_impl!(mut self, mut_ref, base_remove_rows(first, count, parent))
423 }
424 pub fn base_sibling(&self, reference: &dyn QListModelAdapter, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
425 call_cpp_impl!(self, reference, base_sibling(row, column, idx))
426 }
427 pub fn base_data_changed(&mut self, mut_ref: &mut dyn QListModelAdapter, top_left: &QModelIndex, bottom_right: &QModelIndex) {
428 call_cpp_impl!(mut self, mut_ref, base_data_changed(top_left, bottom_right))
429 }
430 pub fn base_begin_insert_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, parent: &QModelIndex, first: i32, last: i32) {
431 call_cpp_impl!(mut self, mut_ref, base_begin_insert_rows(parent, first, last))
432 }
433 pub fn base_end_insert_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
434 call_cpp_impl!(mut self, mut_ref, base_end_insert_rows())
435 }
436 pub fn base_begin_move_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, source_parent: &QModelIndex, source_first: i32, source_last: i32, destination_parent: &QModelIndex, destination_child: i32) {
437 call_cpp_impl!(mut self, mut_ref, base_begin_move_rows(source_parent, source_first, source_last, destination_parent, destination_child))
438 }
439 pub fn base_end_move_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
440 call_cpp_impl!(mut self, mut_ref, base_end_move_rows())
441 }
442 pub fn base_begin_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, parent: &QModelIndex, first: i32, last: i32) {
443 call_cpp_impl!(mut self, mut_ref, base_begin_remove_rows(parent, first, last))
444 }
445 pub fn base_end_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
446 call_cpp_impl!(mut self, mut_ref, base_end_remove_rows())
447 }
448 pub fn base_begin_reset_model(&mut self, mut_ref: &mut dyn QListModelAdapter) {
449 call_cpp_impl!(mut self, mut_ref, base_begin_reset_model())
450 }
451 pub fn base_end_reset_model(&mut self, mut_ref: &mut dyn QListModelAdapter) {
452 call_cpp_impl!(mut self, mut_ref, base_end_reset_model())
453 }
454}