delete all selected items

This commit is contained in:
Chris Cochrun 2024-06-17 16:27:10 -05:00
parent dead3a16bc
commit 3d09157be8
2 changed files with 25 additions and 19 deletions

View file

@ -288,7 +288,7 @@ Item {
} }
Kirigami.Action { Kirigami.Action {
text: "Delete" text: "Delete"
onTriggered: removeItem(index) onTriggered: removeItems()
} }
Controls.MenuSeparator {} Controls.MenuSeparator {}

View file

@ -113,7 +113,7 @@ mod service_item_model {
fn clear(self: Pin<&mut ServiceItemModel>); fn clear(self: Pin<&mut ServiceItemModel>);
#[qinvokable] #[qinvokable]
fn remove_item(self: Pin<&mut ServiceItemModel>, index: i32); fn remove_items(self: Pin<&mut ServiceItemModel>);
#[qinvokable] #[qinvokable]
fn add_item( fn add_item(
@ -411,25 +411,31 @@ impl service_item_model::ServiceItemModel {
self.as_mut().cleared(); self.as_mut().cleared();
} }
pub fn remove_item(mut self: Pin<&mut Self>, index: i32) { pub fn remove_items(mut self: Pin<&mut Self>) {
if index < 0 || (index as usize) >= self.service_items.len() { let mut indices = vec![];
return; let mut items = self.service_items.clone();
for (index, item) in items.iter_mut().enumerate().filter(|(y, x)| x.selected) {
let index = index as i32;
indices.push(index);
} }
debug!(vec = ?indices);
unsafe { for index in indices.iter().rev() {
self.as_mut().begin_remove_rows( debug!(index);
&QModelIndex::default(), unsafe {
index, self.as_mut().begin_remove_rows(
index, &QModelIndex::default(),
); *index,
self.as_mut() *index,
.rust_mut() );
.service_items self.as_mut()
.remove(index as usize); .rust_mut()
self.as_mut().end_remove_rows(); .service_items
.remove(*index as usize);
self.as_mut().end_remove_rows();
}
let item = self.as_mut().get_item(*index);
self.as_mut().item_removed(&index, &item);
} }
let item = self.as_mut().get_item(index);
self.as_mut().item_removed(&index, &item);
} }
pub fn add_item( pub fn add_item(