From fbe63367d21b128c7d165ee408283c0c1acf58d2 Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 25 Apr 2024 02:01:48 +0300 Subject: [PATCH] fix edit button --- src/ui/cards/edit/imp.rs | 91 +++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 24 deletions(-) diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs index 256933f..f2ac20a 100644 --- a/src/ui/cards/edit/imp.rs +++ b/src/ui/cards/edit/imp.rs @@ -15,7 +15,8 @@ use gtk::glib::{clone, closure_local, Object}; use gtk::prelude::WidgetExt; use gtk::subclass::prelude::*; use gtk::{ - gio, glib, Button, CompositeTemplate, ListView, NoSelection, ScrolledWindow, SearchEntry, Switch, Window + gio, glib, Button, CompositeTemplate, ListView, NoSelection, ScrolledWindow, SearchEntry, + Switch, Window, }; use gtk::{prelude::*, FileDialog}; use gtk::{ListItem, SignalListItemFactory}; @@ -67,6 +68,7 @@ impl ObjectImpl for MemoryCardsEditScene { new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |b: &Button| { b.set_visible(false); gtk::glib::MainContext::default().spawn_local(new_card_setup(Rc::clone(&new_win))); + println!("test"); })); new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| { binding.imp().query_cards(None); @@ -94,7 +96,6 @@ impl ObjectImpl for MemoryCardsEditScene { } impl MemoryCardsEditScene { - pub fn update_state(&self) { let model = ListStore::new::(); model.extend_from_slice(&*self.displaying_cards.borrow()); @@ -123,8 +124,48 @@ impl MemoryCardsEditScene { .child() .and_downcast::() .unwrap(); + card_object_to_display.get_edit_button_widget().connect_closure("clicked", false, closure_local!(@strong self_binding as binding, @strong card_object as card => move |_b: &Button| { + let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap())); - card_object_to_display.get_is_learning_switch_widget().connect_closure("state-set", false, closure_local!(@strong card_object as card => move |s: &Switch, is_enabled: bool| { + //setting corresponding properties + new_win.get_picture_widget().set_file(Some(&gio::File::for_path(card.imagepath()))); + new_win.get_hieroglyph_entry().set_text(&card.hieroglyph()); + new_win.get_reading_entry().set_text(&card.reading()); + new_win.get_translation_entry().set_text(&card.translation()); + + new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win, @strong card => move |b: &Button| { + b.set_visible(false); + gtk::glib::MainContext::default().spawn_local(new_card_setup(Rc::clone(&new_win))); + })); + new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding, @strong new_win as w => move |_w: &Button| { + let conn = Connection::open(get_db_path()).unwrap(); + + let hieroglyph = w.get_hieroglyph_input(); + let reading = w.get_reading_input(); + let translation = w.get_translation_input(); + + conn.execute("INSERT OR REPLACE INTO cards ( id, + hieroglyph, + reading, + translation, + is_learning, + imagename + ) VALUES ( + (SELECT id FROM cards WHERE hieroglyph = ?1), + ?1, + ?2, + ?3, + (SELECT is_learning FROM cards WHERE hieroglyph = ?1), + (SELECT imagename FROM cards WHERE hieroglyph = ?1) + )", (&hieroglyph, &reading, &translation)).unwrap(); + + binding.imp().query_cards(None); + binding.imp().update_state(); + w.close(); + })); + new_win.present(); + })); + card_object_to_display.get_is_learning_switch_widget().connect_closure("state-set", false, closure_local!(@strong card_object as card => move |_s: &Switch, is_enabled: bool| { let connection = Connection::open(get_db_path()).unwrap(); let is_learning = is_enabled; let hieroglyph = card.hieroglyph(); @@ -232,30 +273,32 @@ async fn new_card_setup>(window: Rc) { fs::copy(&path, stored_image_path).expect("Error copying image to store"); let hieroglyph = w.get_hieroglyph_input(); - let reading = w.get_reading_input(); - let translation = w.get_translation_input(); + // let reading = w.get_reading_input(); + // let translation = w.get_translation_input(); + + // println!("Input: {hieroglyph}, {reading}, {translation}"); let conn = Connection::open(get_db_path()).unwrap(); - conn.execute("INSERT OR REPLACE INTO cards ( id, - imagename, - hieroglyph, - reading, - translation, - is_learning - ) VALUES ( - (SELECT id FROM cards WHERE hieroglyph = ?2), - ?1, - ?2, - ?3, - ?4, - (SELECT is_learning FROM cards WHERE hieroglyph = ?2 - ) - )", (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); + // conn.execute("INSERT OR REPLACE INTO cards ( id, + // imagename, + // hieroglyph, + // reading, + // translation, + // is_learning + // ) VALUES ( + // (SELECT id FROM cards WHERE hieroglyph = ?2), + // ?1, + // ?2, + // ?3, + // ?4, + // (SELECT is_learning FROM cards WHERE hieroglyph = ?2 + // ) + // )", (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); + // println!("") + + conn.execute("UPDATE cards SET imagename = ?1 WHERE hieroglyph = ?2", (&new_filename, &hieroglyph)).unwrap(); + // conn.execute("INSERT OR REPLACE INTO cards (id, imagename) VALUES ((SELECT id FROM cards WHERE hieroglyph = ?1), ?2)", (&hieroglyph, &new_filename)).unwrap(); conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap(); - - - - w.close(); }), ); }