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, 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 return 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::default();
46 names.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
90pub trait QListModel {
160 type Item: QModelItem + Default + Clone;
167
168 fn len(&self) -> usize;
170
171 fn get(&self, index: usize) -> Option<&Self::Item>;
174
175 fn set_unnotified(&mut self, _index: usize, _value: Self::Item) -> bool {
184 false
185 }
186
187 fn push_unnotified(&mut self, value: Self::Item) {
197 self.insert_unnotified(self.len(), value);
198 }
199
200 fn insert_unnotified(&mut self, _index: usize, _value: Self::Item) {
210 panic!("In order to use insert, implement insert_unnotified")
211 }
212
213 fn pop_unnotified(&mut self) -> Option<Self::Item> {
223 (self.len() > 0)
224 .then(|| self.remove_unnotified(self.len() - 1))
225 }
226
227 fn remove_unnotified(&mut self, _index: usize) -> Self::Item {
237 panic!("In order to use remove, implement remove_unnotified")
238 }
239
240 fn reset_unnotified(&mut self) {
250 panic!("In order to use reset, implement reset_unnotified")
251 }
252}
253
254pub trait QListModelBase : QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> {
282 fn set(&mut self, index: usize, value: <Self as QListModel>::Item) -> bool {
291 if self.set_unnotified(index, value) {
292 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
293 let model_index = unsafe { &*proxy }.base_index(&*self, index as i32, 0, &QModelIndex::default());
294 unsafe { &mut *proxy }.base_data_changed(&mut *self, &model_index, &model_index);
295 true
296 } else {
297 false
298 }
299 }
300
301 fn push(&mut self, value: Self::Item) {
306 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
307 let len = self.len() as i32;
308 unsafe { &mut *proxy }.base_begin_insert_rows(&mut *self, &QModelIndex::default(), len, len);
309 self.push_unnotified(value);
310 unsafe { &mut *proxy }.base_end_insert_rows(&mut *self);
311 }
312
313 fn insert(&mut self, index: usize, value: Self::Item) {
318 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
319 unsafe { &mut *proxy }.base_begin_insert_rows(&mut *self, &QModelIndex::default(), index as i32, index as i32);
320 self.insert_unnotified(index, value);
321 unsafe { &mut *proxy }.base_end_insert_rows(&mut *self);
322 }
323
324 fn pop(&mut self) -> Option<Self::Item> {
332 if self.len() == 0 {
333 return None;
334 }
335 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
336 let len = self.len() as i32;
337 unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, &QModelIndex::default(), len - 1, len - 1);
338 let value = self.pop_unnotified();
339 unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
340 value
341 }
342
343 fn remove(&mut self, index: usize) -> Self::Item {
348 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
349 unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, &QModelIndex::default(), index as i32, index as i32);
350 let value = self.remove_unnotified(index);
351 unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
352 value
353 }
354
355 fn reset(&mut self) {
359 let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
360 unsafe { &mut *proxy }.base_begin_reset_model(&mut *self);
361 self.reset_unnotified();
362 unsafe { &mut *proxy }.base_end_reset_model(&mut *self);
363 }
364}
365
366impl<T> QListModelBase for T
367where T: QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> { }
368
369pub type QListModelProxyRust = GenericRustProxy<QListModelProxyCpp, dyn QListModelAdapter>;
370
371impl QListModelProxyRust {
372 pub fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
373 call_rust_trait_impl!(self, index(row, column, parent))
374 }
375 pub fn row_count(&self, parent: &QModelIndex) -> i32 {
376 call_rust_trait_impl!(self, row_count(parent))
377 }
378 pub fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
379 call_rust_trait_impl!(self, data(index, role))
380 }
381 pub fn role_names(&self) -> QHash<i32, QByteArray> {
382 call_rust_trait_impl!(self, role_names())
383 }
384 pub fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
385 call_rust_trait_impl!(mut self, set_data(index, value, role))
386 }
387 pub fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool {
388 call_rust_trait_impl!(mut self, remove_rows(first, count, parent))
389 }
390 pub fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
391 call_rust_trait_impl!(self, sibling(row, column, idx))
392 }
393
394 pub fn base_index(&self, reference: &dyn QListModelAdapter, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
395 call_cpp_impl!(self, reference, base_index(row, column, parent))
396 }
397 pub fn base_role_names(&self, reference: &dyn QListModelAdapter) -> QHash<i32, QByteArray> {
398 call_cpp_impl!(self, reference, base_role_names())
399 }
400 pub fn base_set_data(&mut self, mut_ref: &mut dyn QListModelAdapter, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
401 call_cpp_impl!(mut self, mut_ref, base_set_data(index, value, role))
402 }
403 pub fn base_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, first: i32, count: i32, parent: &QModelIndex) -> bool {
404 call_cpp_impl!(mut self, mut_ref, base_remove_rows(first, count, parent))
405 }
406 pub fn base_sibling(&self, reference: &dyn QListModelAdapter, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
407 call_cpp_impl!(self, reference, base_sibling(row, column, idx))
408 }
409 pub fn base_data_changed(&mut self, mut_ref: &mut dyn QListModelAdapter, top_left: &QModelIndex, bottom_right: &QModelIndex) {
410 call_cpp_impl!(mut self, mut_ref, base_data_changed(top_left, bottom_right))
411 }
412 pub fn base_begin_insert_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, parent: &QModelIndex, first: i32, last: i32) {
413 call_cpp_impl!(mut self, mut_ref, base_begin_insert_rows(parent, first, last))
414 }
415 pub fn base_end_insert_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
416 call_cpp_impl!(mut self, mut_ref, base_end_insert_rows())
417 }
418 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) {
419 call_cpp_impl!(mut self, mut_ref, base_begin_move_rows(source_parent, source_first, source_last, destination_parent, destination_child))
420 }
421 pub fn base_end_move_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
422 call_cpp_impl!(mut self, mut_ref, base_end_move_rows())
423 }
424 pub fn base_begin_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, parent: &QModelIndex, first: i32, last: i32) {
425 call_cpp_impl!(mut self, mut_ref, base_begin_remove_rows(parent, first, last))
426 }
427 pub fn base_end_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
428 call_cpp_impl!(mut self, mut_ref, base_end_remove_rows())
429 }
430 pub fn base_begin_reset_model(&mut self, mut_ref: &mut dyn QListModelAdapter) {
431 call_cpp_impl!(mut self, mut_ref, base_begin_reset_model())
432 }
433 pub fn base_end_reset_model(&mut self, mut_ref: &mut dyn QListModelAdapter) {
434 call_cpp_impl!(mut self, mut_ref, base_end_reset_model())
435 }
436}