fix edit button

This commit is contained in:
leca 2024-04-25 02:01:48 +03:00
parent 78b2a1a272
commit fbe63367d2
1 changed files with 67 additions and 24 deletions

View File

@ -15,7 +15,8 @@ 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::{
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::{prelude::*, FileDialog};
use gtk::{ListItem, SignalListItemFactory}; 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| { new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |b: &Button| {
b.set_visible(false); b.set_visible(false);
gtk::glib::MainContext::default().spawn_local(new_card_setup(Rc::clone(&new_win))); 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| { new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| {
binding.imp().query_cards(None); binding.imp().query_cards(None);
@ -94,7 +96,6 @@ impl ObjectImpl for MemoryCardsEditScene {
} }
impl MemoryCardsEditScene { impl MemoryCardsEditScene {
pub fn update_state(&self) { pub fn update_state(&self) {
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());
@ -123,8 +124,48 @@ impl MemoryCardsEditScene {
.child() .child()
.and_downcast::<CardEntry>() .and_downcast::<CardEntry>()
.unwrap(); .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 connection = Connection::open(get_db_path()).unwrap();
let is_learning = is_enabled; let is_learning = is_enabled;
let hieroglyph = card.hieroglyph(); let hieroglyph = card.hieroglyph();
@ -232,30 +273,32 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
fs::copy(&path, stored_image_path).expect("Error copying image to store"); fs::copy(&path, stored_image_path).expect("Error copying image to store");
let hieroglyph = w.get_hieroglyph_input(); let hieroglyph = w.get_hieroglyph_input();
let reading = w.get_reading_input(); // let reading = w.get_reading_input();
let translation = w.get_translation_input(); // let translation = w.get_translation_input();
// println!("Input: {hieroglyph}, {reading}, {translation}");
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
conn.execute("INSERT OR REPLACE INTO cards ( id, // conn.execute("INSERT OR REPLACE INTO cards ( id,
imagename, // imagename,
hieroglyph, // hieroglyph,
reading, // reading,
translation, // translation,
is_learning // is_learning
) VALUES ( // ) VALUES (
(SELECT id FROM cards WHERE hieroglyph = ?2), // (SELECT id FROM cards WHERE hieroglyph = ?2),
?1, // ?1,
?2, // ?2,
?3, // ?3,
?4, // ?4,
(SELECT is_learning FROM cards WHERE hieroglyph = ?2 // (SELECT is_learning FROM cards WHERE hieroglyph = ?2
) // )
)", (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); // )", (&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(); conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap();
w.close();
}), }),
); );
} }