use super::proxy_cpp_bridge::QListModelProxyCpp;
use crate::{call_rust_trait_impl, call_cpp_impl};
use qtbridge_runtime::{DispatchMetaCall, QObjectHolder};
use crate::genericrustproxy::GenericRustProxy;
use qtbridge_runtime::QModelItem;
use qtbridge_type_lib::{QByteArray, QHash, QModelIndex, QVariant};
#[doc(hidden)]
pub trait QListModelAdapter: DispatchMetaCall + 'static {
fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex;
fn row_count(&self, parent: &QModelIndex) -> i32;
fn data(&self, index: &QModelIndex, role: i32) -> QVariant;
fn role_names(&self) -> QHash<i32, QByteArray>;
fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool;
fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool;
fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex;
}
impl<T> QListModelAdapter for T
where
T: QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> {
fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &*proxy }.base_index(self, row, column, parent)
}
fn row_count(&self, _parent: &QModelIndex) -> i32 {
return self.len() as i32;
}
fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
let Some(item) = self.get(index.row() as usize)
else {
return QVariant::default();
};
item.get_role(role)
}
fn role_names(&self) -> QHash<i32, QByteArray> {
let names = T::Item::role_names();
let mut result = QHash::default();
names.iter()
.for_each(|(k, v)| result.insert(k, &QByteArray::from(v)));
result
}
fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
if !index.is_valid() {
return false;
}
let Some(mut item) = self.get(index.row() as usize)
.cloned()
else {
return false;
};
let updated = item.set_role(role, value);
if updated {
self.set_unnotified(index.row() as usize, item);
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &mut *proxy }.base_data_changed(&mut *self, index, index);
}
updated
}
fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool {
let first = first as usize;
let last = first + count as usize;
if last > self.len() {
return false;
}
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, parent, first as i32, (last - 1) as i32);
for index in (first..last).rev() {
self.remove_unnotified(index);
}
unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
true
}
fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &*proxy }.base_sibling(self, row, column, idx)
}
}
pub trait QListModel {
type Item: QModelItem + Default + Clone;
fn len(&self) -> usize;
fn get(&self, index: usize) -> Option<&Self::Item>;
fn set_unnotified(&mut self, _index: usize, _value: Self::Item) -> bool {
false
}
fn push_unnotified(&mut self, value: Self::Item) {
self.insert_unnotified(self.len(), value);
}
fn insert_unnotified(&mut self, _index: usize, _value: Self::Item) {
panic!("In order to use insert, implement insert_unnotified")
}
fn pop_unnotified(&mut self) -> Option<Self::Item> {
(self.len() > 0)
.then(|| self.remove_unnotified(self.len() - 1))
}
fn remove_unnotified(&mut self, _index: usize) -> Self::Item {
panic!("In order to use remove, implement remove_unnotified")
}
fn reset_unnotified(&mut self) {
panic!("In order to use reset, implement reset_unnotified")
}
}
pub trait QListModelBase : QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> {
fn set(&mut self, index: usize, value: <Self as QListModel>::Item) -> bool {
if self.set_unnotified(index, value) {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
let model_index = unsafe { &*proxy }.base_index(&*self, index as i32, 0, &QModelIndex::default());
unsafe { &mut *proxy }.base_data_changed(&mut *self, &model_index, &model_index);
true
} else {
false
}
}
fn push(&mut self, value: Self::Item) {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
let len = self.len() as i32;
unsafe { &mut *proxy }.base_begin_insert_rows(&mut *self, &QModelIndex::default(), len, len);
self.push_unnotified(value);
unsafe { &mut *proxy }.base_end_insert_rows(&mut *self);
}
fn insert(&mut self, index: usize, value: Self::Item) {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &mut *proxy }.base_begin_insert_rows(&mut *self, &QModelIndex::default(), index as i32, index as i32);
self.insert_unnotified(index, value);
unsafe { &mut *proxy }.base_end_insert_rows(&mut *self);
}
fn pop(&mut self) -> Option<Self::Item> {
if self.len() == 0 {
return None;
}
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
let len = self.len() as i32;
unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, &QModelIndex::default(), len - 1, len - 1);
let value = self.pop_unnotified();
unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
value
}
fn remove(&mut self, index: usize) -> Self::Item {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &mut *proxy }.base_begin_remove_rows(&mut *self, &QModelIndex::default(), index as i32, index as i32);
let value = self.remove_unnotified(index);
unsafe { &mut *proxy }.base_end_remove_rows(&mut *self);
value
}
fn reset(&mut self) {
let proxy = self.try_get_rust_proxy_ptr().expect("No proxy");
unsafe { &mut *proxy }.base_begin_reset_model(&mut *self);
self.reset_unnotified();
unsafe { &mut *proxy }.base_end_reset_model(&mut *self);
}
}
impl<T> QListModelBase for T
where T: QListModel + QObjectHolder<ProxyRust = QListModelProxyRust> { }
pub type QListModelProxyRust = GenericRustProxy<QListModelProxyCpp, dyn QListModelAdapter>;
impl QListModelProxyRust {
pub fn index(&self, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
call_rust_trait_impl!(self, index(row, column, parent))
}
pub fn row_count(&self, parent: &QModelIndex) -> i32 {
call_rust_trait_impl!(self, row_count(parent))
}
pub fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
call_rust_trait_impl!(self, data(index, role))
}
pub fn role_names(&self) -> QHash<i32, QByteArray> {
call_rust_trait_impl!(self, role_names())
}
pub fn set_data(&mut self, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
call_rust_trait_impl!(mut self, set_data(index, value, role))
}
pub fn remove_rows(&mut self, first: i32, count: i32, parent: &QModelIndex) -> bool {
call_rust_trait_impl!(mut self, remove_rows(first, count, parent))
}
pub fn sibling(&self, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
call_rust_trait_impl!(self, sibling(row, column, idx))
}
pub fn base_index(&self, reference: &dyn QListModelAdapter, row: i32, column: i32, parent: &QModelIndex) -> QModelIndex {
call_cpp_impl!(self, reference, base_index(row, column, parent))
}
pub fn base_role_names(&self, reference: &dyn QListModelAdapter) -> QHash<i32, QByteArray> {
call_cpp_impl!(self, reference, base_role_names())
}
pub fn base_set_data(&mut self, mut_ref: &mut dyn QListModelAdapter, index: &QModelIndex, value: &QVariant, role: i32) -> bool {
call_cpp_impl!(mut self, mut_ref, base_set_data(index, value, role))
}
pub fn base_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, first: i32, count: i32, parent: &QModelIndex) -> bool {
call_cpp_impl!(mut self, mut_ref, base_remove_rows(first, count, parent))
}
pub fn base_sibling(&self, reference: &dyn QListModelAdapter, row: i32, column: i32, idx: &QModelIndex) -> QModelIndex {
call_cpp_impl!(self, reference, base_sibling(row, column, idx))
}
pub fn base_data_changed(&mut self, mut_ref: &mut dyn QListModelAdapter, top_left: &QModelIndex, bottom_right: &QModelIndex) {
call_cpp_impl!(mut self, mut_ref, base_data_changed(top_left, bottom_right))
}
pub fn base_begin_insert_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, parent: &QModelIndex, first: i32, last: i32) {
call_cpp_impl!(mut self, mut_ref, base_begin_insert_rows(parent, first, last))
}
pub fn base_end_insert_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
call_cpp_impl!(mut self, mut_ref, base_end_insert_rows())
}
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) {
call_cpp_impl!(mut self, mut_ref, base_begin_move_rows(source_parent, source_first, source_last, destination_parent, destination_child))
}
pub fn base_end_move_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
call_cpp_impl!(mut self, mut_ref, base_end_move_rows())
}
pub fn base_begin_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter, parent: &QModelIndex, first: i32, last: i32) {
call_cpp_impl!(mut self, mut_ref, base_begin_remove_rows(parent, first, last))
}
pub fn base_end_remove_rows(&mut self, mut_ref: &mut dyn QListModelAdapter) {
call_cpp_impl!(mut self, mut_ref, base_end_remove_rows())
}
pub fn base_begin_reset_model(&mut self, mut_ref: &mut dyn QListModelAdapter) {
call_cpp_impl!(mut self, mut_ref, base_begin_reset_model())
}
pub fn base_end_reset_model(&mut self, mut_ref: &mut dyn QListModelAdapter) {
call_cpp_impl!(mut self, mut_ref, base_end_reset_model())
}
}