Delete button

This commit is contained in:
2024-04-24 19:40:13 +03:00
parent f30bc31d46
commit 6a6ecd79d4
2 changed files with 23 additions and 40 deletions

View File

@@ -11,7 +11,7 @@ use crate::widgets::card_entry::CardEntry;
use glib::subclass::InitializingObject;
use gtk::gio::ListStore;
use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local};
use gtk::glib::{clone, closure_local, Object};
use gtk::prelude::WidgetExt;
use gtk::subclass::prelude::*;
use gtk::{
@@ -91,14 +91,12 @@ impl ObjectImpl for MemoryCardsEditScene {
}));
// self.query_cards(None);
self.update_state();
println!("Constructed");
}
}
impl MemoryCardsEditScene {
pub fn update_state(&self) {
println!("Updating state");
let model = ListStore::new::<CardEntry>();
model.extend_from_slice(&*self.displaying_cards.borrow());
@@ -110,8 +108,9 @@ impl MemoryCardsEditScene {
.unwrap()
.set_child(Some(&card_entry));
});
let self_binding = self.obj();
factory.connect_bind(move |_, list_item| {
factory.connect_closure("bind", false, closure_local!(@strong self_binding => move |_f: &SignalListItemFactory, list_item: &Object| {
let card_object = &list_item
.downcast_ref::<ListItem>()
.unwrap()
@@ -126,12 +125,24 @@ impl MemoryCardsEditScene {
.and_downcast::<CardEntry>()
.unwrap();
card_object_to_display.get_delete_button_widget().connect_closure("clicked", false, closure_local!(@strong card_object as card, @strong self_binding => move |_b: &Button| {
let connection = Connection::open(get_db_path()).unwrap();
let imagepath = &card.imagepath();
let imagename = &Path::new(&imagepath).file_name().unwrap().to_str().unwrap();
fs::remove_file(get_images_store_path() + "/" + &imagename).expect("Could not remove file!");
connection.execute("DELETE FROM cards WHERE imagename = ?1", [&imagename]).unwrap();
self_binding.imp().query_cards(None);
self_binding.imp().update_state();
}));
card_object_to_display.get_image_widget().set_file(Some(card_object.get_image_file_path().as_str()));
card_object_to_display.set_hieroglyph(card_object.hieroglyph());
card_object_to_display.set_reading(card_object.reading());
card_object_to_display.set_translation(card_object.translation());
card_object_to_display.set_islearning(card_object.islearning());
});
}));
// factory.connect_bind();
let no_selection_model = NoSelection::new(Some(model));
let list_view = ListView::new(Some(no_selection_model), Some(factory));
@@ -163,9 +174,6 @@ impl MemoryCardsEditScene {
Some(row.get(3).unwrap()),
Some(row.get(4).unwrap()),
);
if c.is_learning().unwrap() {
println!("{c:?}");
}
let entry = CardEntry::new(Some(&c));
entry.update_state();
Ok(entry)