From 24acb4ecd14dbb54d4c87d3bb17a586fd1b1d348 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 6 Apr 2024 15:58:20 +0300 Subject: [PATCH] autoupdate on card_entry add --- src/ui/cards/edit/imp.rs | 17 ++++++++++------- src/widgets/card_entry/imp.rs | 12 +++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs index 5aa52e9..8d9cfab 100644 --- a/src/ui/cards/edit/imp.rs +++ b/src/ui/cards/edit/imp.rs @@ -14,6 +14,7 @@ use gtk::{gio, glib, Box, Button, CompositeTemplate, ScrolledWindow, SearchEntry use gtk::{prelude::*, FileDialog}; use rusqlite::Connection; use sha256::try_digest; +use gtk::prelude::WidgetExt; #[derive(CompositeTemplate, Default)] #[template(resource = "/org/foxarmy/learn-hieroglyph/cards/edit/ui.xml")] @@ -56,7 +57,11 @@ impl ObjectImpl for MemoryCardsEditScene { new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |_| { gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win))); })); + new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| { + binding.imp().update_card_list(); + })); new_win.present(); + })); self.update_card_list(); } @@ -86,14 +91,13 @@ impl MemoryCardsEditScene { .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 self_binding = self.obj(); c.append(&card_binding); + card_binding.connect_closure("created", false, closure_local!(move | | { + println!("New card created"); + })); + 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(); @@ -144,9 +148,8 @@ async fn file_choose_dialog>(window: Rc) { let conn = Connection::open(get_db_path()).unwrap(); let query = "INSERT INTO cards (imagename, hieroglyph, reading) VALUES(?1, ?2, ?3)"; - conn.execute(query, (&new_filename, &hieroglyph, &reading)).unwrap(); + conn.execute(query, (&new_filename, &hieroglyph, &reading)).unwrap(); w.close(); - }), ); } diff --git a/src/widgets/card_entry/imp.rs b/src/widgets/card_entry/imp.rs index bef5eba..e913d85 100644 --- a/src/widgets/card_entry/imp.rs +++ b/src/widgets/card_entry/imp.rs @@ -1,7 +1,9 @@ use std::cell::RefCell; +use std::sync::OnceLock; use glib::subclass::InitializingObject; use glib::Properties; +use gtk::glib::subclass::Signal; use gtk::subclass::prelude::*; use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture}; @@ -42,6 +44,15 @@ impl ObjectSubclass for CardEntry { } #[glib::derived_properties] impl ObjectImpl for CardEntry { + + fn signals() -> &'static [Signal] { + static SIGNALS: OnceLock> = OnceLock::new(); + SIGNALS.get_or_init(|| { + vec![Signal::builder("created") + .build()] + }) + } + fn constructed(&self) { self.parent_constructed(); @@ -57,7 +68,6 @@ impl ObjectImpl for CardEntry { .bind_property("reading", reading_label_binding, "label") .sync_create() .build(); - } fn dispose(&self) {