Delete button
This commit is contained in:
		| @@ -11,7 +11,7 @@ use crate::widgets::card_entry::CardEntry; | |||||||
| use glib::subclass::InitializingObject; | use glib::subclass::InitializingObject; | ||||||
| use gtk::gio::ListStore; | use gtk::gio::ListStore; | ||||||
| use gtk::glib::object::ObjectExt; | use gtk::glib::object::ObjectExt; | ||||||
| use gtk::glib::{clone, closure_local}; | use gtk::glib::{clone, closure_local, Object}; | ||||||
| use gtk::prelude::WidgetExt; | use gtk::prelude::WidgetExt; | ||||||
| use gtk::subclass::prelude::*; | use gtk::subclass::prelude::*; | ||||||
| use gtk::{ | use gtk::{ | ||||||
| @@ -91,14 +91,12 @@ impl ObjectImpl for MemoryCardsEditScene { | |||||||
|         })); |         })); | ||||||
|         // self.query_cards(None); |         // self.query_cards(None); | ||||||
|         self.update_state(); |         self.update_state(); | ||||||
|         println!("Constructed"); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| impl MemoryCardsEditScene { | impl MemoryCardsEditScene { | ||||||
|  |  | ||||||
|     pub fn update_state(&self) { |     pub fn update_state(&self) { | ||||||
|         println!("Updating state"); |  | ||||||
|         let model = ListStore::new::<CardEntry>(); |         let model = ListStore::new::<CardEntry>(); | ||||||
|         model.extend_from_slice(&*self.displaying_cards.borrow()); |         model.extend_from_slice(&*self.displaying_cards.borrow()); | ||||||
|  |  | ||||||
| @@ -110,8 +108,9 @@ impl MemoryCardsEditScene { | |||||||
|                 .unwrap() |                 .unwrap() | ||||||
|                 .set_child(Some(&card_entry)); |                 .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 |             let card_object = &list_item | ||||||
|                 .downcast_ref::<ListItem>() |                 .downcast_ref::<ListItem>() | ||||||
|                 .unwrap() |                 .unwrap() | ||||||
| @@ -126,12 +125,24 @@ impl MemoryCardsEditScene { | |||||||
|                 .and_downcast::<CardEntry>() |                 .and_downcast::<CardEntry>() | ||||||
|                 .unwrap(); |                 .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.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_hieroglyph(card_object.hieroglyph()); | ||||||
|             card_object_to_display.set_reading(card_object.reading()); |             card_object_to_display.set_reading(card_object.reading()); | ||||||
|             card_object_to_display.set_translation(card_object.translation()); |             card_object_to_display.set_translation(card_object.translation()); | ||||||
|             card_object_to_display.set_islearning(card_object.islearning()); |             card_object_to_display.set_islearning(card_object.islearning()); | ||||||
|         }); |         })); | ||||||
|  |  | ||||||
|  |         // factory.connect_bind(); | ||||||
|  |  | ||||||
|         let no_selection_model = NoSelection::new(Some(model)); |         let no_selection_model = NoSelection::new(Some(model)); | ||||||
|         let list_view = ListView::new(Some(no_selection_model), Some(factory)); |         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(3).unwrap()), | ||||||
|                     Some(row.get(4).unwrap()), |                     Some(row.get(4).unwrap()), | ||||||
|                 ); |                 ); | ||||||
|                 if c.is_learning().unwrap() { |  | ||||||
|                     println!("{c:?}"); |  | ||||||
|                 } |  | ||||||
|                 let entry = CardEntry::new(Some(&c)); |                 let entry = CardEntry::new(Some(&c)); | ||||||
|                 entry.update_state(); |                 entry.update_state(); | ||||||
|                 Ok(entry) |                 Ok(entry) | ||||||
|   | |||||||
| @@ -20,14 +20,13 @@ glib::wrapper! { | |||||||
| impl CardEntry { | impl CardEntry { | ||||||
|     pub fn new(card: Option<&Card>) -> Self { |     pub fn new(card: Option<&Card>) -> Self { | ||||||
|         match card { |         match card { | ||||||
|             Some(card) => |             Some(card) => Object::builder() | ||||||
|                 Object::builder() |                 .property("imagepath", card.image_path().unwrap()) | ||||||
|                     .property("imagepath", card.image_path().unwrap()) |                 .property("hieroglyph", card.hieroglyph().unwrap()) | ||||||
|                     .property("hieroglyph", card.hieroglyph().unwrap()) |                 .property("reading", card.reading().unwrap()) | ||||||
|                     .property("reading", card.reading().unwrap()) |                 .property("translation", card.translation().unwrap()) | ||||||
|                     .property("translation", card.translation().unwrap()) |                 .property("islearning", card.is_learning().unwrap()) | ||||||
|                     .property("islearning", card.is_learning().unwrap()) |                 .build(), | ||||||
|                     .build(), |  | ||||||
|             None => Object::builder() |             None => Object::builder() | ||||||
|                 .property("imagepath", "") |                 .property("imagepath", "") | ||||||
|                 .property("hieroglyph", "") |                 .property("hieroglyph", "") | ||||||
| @@ -60,30 +59,6 @@ impl CardEntry { | |||||||
|         image_binding.set_file(Some(&path)); |         image_binding.set_file(Some(&path)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // pub fn get_hieroglyph(&self) -> String { |  | ||||||
|     //     self.imp().hieroglyph.borrow().clone() |  | ||||||
|     // } |  | ||||||
|  |  | ||||||
|     // pub fn set_hieroglyph(&self, h: &String) { |  | ||||||
|     //     self.imp().hieroglyph.borrow_mut() = h; |  | ||||||
|     // } |  | ||||||
|  |  | ||||||
|     // pub fn get_reading(&self) -> String { |  | ||||||
|     //     self.imp().reading.borrow().clone() |  | ||||||
|     // } |  | ||||||
|  |  | ||||||
|     // pub fn set_reading(&self, r: &String) { |  | ||||||
|     //     self.imp().reading.borrow_mut() = r; |  | ||||||
|     // } |  | ||||||
|  |  | ||||||
|     // pub fn get_translation(&self) -> String { |  | ||||||
|     //     self.imp().translation.borrow().clone() |  | ||||||
|     // } |  | ||||||
|  |  | ||||||
|     // pub fn set_translation(&self, t: &String) { |  | ||||||
|     //     self.imp().translation.borrow_mut() = t; |  | ||||||
|     // } |  | ||||||
|  |  | ||||||
|     pub fn update_state(&self) { |     pub fn update_state(&self) { | ||||||
|         let conn = Connection::open(get_db_path()).unwrap(); |         let conn = Connection::open(get_db_path()).unwrap(); | ||||||
|         let is_learning_switch_binding: &Switch = self.imp().is_learning_switch.as_ref(); |         let is_learning_switch_binding: &Switch = self.imp().is_learning_switch.as_ref(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user