From 193db81657d60149b98d45a72fd46a09b1a19f9b Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 6 Apr 2024 07:04:19 +0300 Subject: [PATCH] implemented delete card entry --- resources/widgets/card_entry/template.ui.xml | 6 ++-- src/ui/cards/edit/imp.rs | 34 +++++++++++++------- src/widgets/card_entry/imp.rs | 13 ++------ src/widgets/card_entry/mod.rs | 16 ++++++++- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/resources/widgets/card_entry/template.ui.xml b/resources/widgets/card_entry/template.ui.xml index f4676b7..6d8fffe 100644 --- a/resources/widgets/card_entry/template.ui.xml +++ b/resources/widgets/card_entry/template.ui.xml @@ -1,9 +1,6 @@ diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs index bcc3595..5aa52e9 100644 --- a/src/ui/cards/edit/imp.rs +++ b/src/ui/cards/edit/imp.rs @@ -60,7 +60,6 @@ impl ObjectImpl for MemoryCardsEditScene { })); self.update_card_list(); } - } impl MemoryCardsEditScene { @@ -70,33 +69,44 @@ impl MemoryCardsEditScene { while c.first_child() != None { c.remove(&c.first_child().unwrap()); } - + let conn = Connection::open(get_db_path()).unwrap(); let mut stmt = conn .prepare("SELECT imagename, hieroglyph, reading FROM cards") .unwrap(); let cards_iter = stmt .query_map([], |row| { - let a: String = row.get(0).unwrap(); - let b: String = row.get(1).unwrap(); - let c: String = row.get(2).unwrap(); - let entry = CardEntry::new(&a, &b, &c); + let image_path: String = row.get(0).unwrap(); + let image_path = get_images_store_path() + &image_path; + let hieroglyph: String = row.get(1).unwrap(); + let reading: String = row.get(2).unwrap(); + let entry = CardEntry::new(&image_path, &hieroglyph, &reading); Ok(entry) }) .unwrap(); - + for card in cards_iter { + // let card_binding = card.unwrap(); + // let path = &card_binding.imagepath(); + // println!("path: {}", path); + // card_binding.set_imagepath(&*path); + // card_binding.get_picture_widget().set_file(Some(&gio::File::for_path(&path))); let card_binding = card.unwrap(); - let path = get_images_store_path() + &card_binding.imagepath(); - card_binding.set_imagepath(&*path); - card_binding.get_picture_widget().set_file(Some(&gio::File::for_path(&path))); + let self_binding = self.obj(); c.append(&card_binding); + card_binding.get_delete_button_widget().connect_closure("clicked", false, closure_local!(@strong card_binding, @strong self_binding => move |_b: &Button| { + let connection = Connection::open(get_db_path()).unwrap(); + let imagepath = card_binding.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().update_card_list(); + })); + card_binding.update_file_for_picture(); } } } - - async fn file_choose_dialog>(window: Rc) { let dialog: FileDialog = gtk::FileDialog::builder().build(); let answer = dialog.open_future(Some(&*window)).await; diff --git a/src/widgets/card_entry/imp.rs b/src/widgets/card_entry/imp.rs index d68c94b..bef5eba 100644 --- a/src/widgets/card_entry/imp.rs +++ b/src/widgets/card_entry/imp.rs @@ -1,9 +1,7 @@ use std::cell::RefCell; -use std::path::Path; use glib::subclass::InitializingObject; use glib::Properties; -use gtk::gio::File; use gtk::subclass::prelude::*; use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture}; @@ -18,9 +16,9 @@ pub struct CardEntry { #[template_child] pub reading_label: TemplateChild