fix switch and some changes

This commit is contained in:
leca 2024-04-23 18:20:03 +03:00
parent 3d0543931d
commit f30bc31d46
6 changed files with 72 additions and 85 deletions

View File

@ -3,7 +3,7 @@
<template class="MemoryCardsEditScene" parent="GtkApplicationWindow"> <template class="MemoryCardsEditScene" parent="GtkApplicationWindow">
<property name="title">Memory cards edit</property> <property name="title">Memory cards edit</property>
<property name="height-request">1000</property> <property name="height-request">1000</property>
<!-- <property name="width-request">500</property> --> <property name="width-request">1000</property>
<child> <child>
<object class="GtkBox" id="content"> <object class="GtkBox" id="content">
<property name="orientation">vertical</property> <property name="orientation">vertical</property>

View File

@ -14,6 +14,7 @@ mod card;
use crate::ui::menu::MenuScene; use crate::ui::menu::MenuScene;
use gtk::glib::closure_local; use gtk::glib::closure_local;
use gtk::glib::subclass::types::ObjectSubclassIsExt;
use gtk::{gio, glib, Application, Button}; use gtk::{gio, glib, Application, Button};
use gtk::{prelude::*, Window}; use gtk::{prelude::*, Window};
use ui::cards::edit::MemoryCardsEditScene; use ui::cards::edit::MemoryCardsEditScene;
@ -124,8 +125,10 @@ fn build_ui(app: &Application) {
"clicked", "clicked",
false, false,
closure_local!( closure_local!(
@strong app => move |_b: &Button| { @strong app, @strong memory_cards_edit => move |_b: &Button| {
switch_to(&app, AppWindow::MemoryCardsEditScene); switch_to(&app, AppWindow::MemoryCardsEditScene);
memory_cards_edit.imp().query_cards(None);
memory_cards_edit.imp().update_state();
} }
), ),
); );

View File

@ -10,9 +10,7 @@ use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry; 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::ffi::GString;
use gtk::glib::object::ObjectExt; use gtk::glib::object::ObjectExt;
use gtk::glib::GString;
use gtk::glib::{clone, closure_local}; use gtk::glib::{clone, closure_local};
use gtk::prelude::WidgetExt; use gtk::prelude::WidgetExt;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
@ -21,7 +19,7 @@ use gtk::{
Window, Window,
}; };
use gtk::{prelude::*, FileDialog}; use gtk::{prelude::*, FileDialog};
use gtk::{ListBox, ListItem, SignalListItemFactory}; use gtk::{ListItem, SignalListItemFactory};
use rusqlite::Connection; use rusqlite::Connection;
use sha256::try_digest; use sha256::try_digest;
@ -73,6 +71,7 @@ impl ObjectImpl for MemoryCardsEditScene {
})); }));
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);
binding.imp().update_state();
})); }));
new_win.present(); new_win.present();
})); }));
@ -88,51 +87,18 @@ impl ObjectImpl for MemoryCardsEditScene {
} }
} }
); );
binding.imp().update_state();
})); }));
self.query_cards(None); // self.query_cards(None);
self.update_state();
println!("Constructed");
} }
} }
impl MemoryCardsEditScene { impl MemoryCardsEditScene {
pub fn query_cards(&self, options: Option<String>) {
let conn = Connection::open(get_db_path()).unwrap();
let selector = match options {
Some(s) => "WHERE ".to_owned() + &s,
None => "".to_owned(),
};
let sql = format!(
"SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}"
);
let mut stmt = conn.prepare(sql.as_str()).unwrap();
let cards_iter = stmt
.query_map([], |row| {
let image_path: String = match row.get(0) {
Ok(path) => path,
Err(_) => String::from(""),
};
let c = Card::new(
Some(get_images_store_path() + &image_path),
Some(row.get(1).unwrap()),
Some(row.get(2).unwrap()),
Some(row.get(3).unwrap()),
Some(row.get(4).unwrap()),
);
// println!("{c:?} ");
let entry = CardEntry::new(Some(&c));
entry.update_state();
Ok(entry)
})
.unwrap();
self.displaying_cards.borrow_mut().clear();
println!("pos1");
for c in cards_iter {
self.displaying_cards.borrow_mut().push(c.unwrap());
}
println!("pos2");
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());
@ -160,16 +126,6 @@ impl MemoryCardsEditScene {
.and_downcast::<CardEntry>() .and_downcast::<CardEntry>()
.unwrap(); .unwrap();
// let path =
// // card_object_to_display.get_image_widget().set_file(Some(
// // // &card_object.get_image_widget().file().unwrap()
// // match card_object.get_image_widget().file() {
// // None => "",
// // Some(t) => {
// // t.to_string().to_owned().as_str()
// // }
// // }
// // ));
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());
@ -177,12 +133,48 @@ impl MemoryCardsEditScene {
card_object_to_display.set_islearning(card_object.islearning()); card_object_to_display.set_islearning(card_object.islearning());
}); });
println!("pos3");
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));
self.cards_scrolled_window.set_child(Some(&list_view)); self.cards_scrolled_window.set_child(Some(&list_view));
println!("pos4"); }
pub fn query_cards(&self, options: Option<String>) {
let conn = Connection::open(get_db_path()).unwrap();
let selector = match options {
Some(s) => "WHERE ".to_owned() + &s,
None => "".to_owned(),
};
let sql = format!(
"SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}"
);
let mut stmt = conn.prepare(sql.as_str()).unwrap();
let cards_iter = stmt
.query_map([], |row| {
let image_path: String = match row.get(0) {
Ok(path) => path,
Err(_) => String::from(""),
};
let c = Card::new(
Some(get_images_store_path() + &image_path),
Some(row.get(1).unwrap()),
Some(row.get(2).unwrap()),
Some(row.get(3).unwrap()),
Some(row.get(4).unwrap()),
);
if c.is_learning().unwrap() {
println!("{c:?}");
}
let entry = CardEntry::new(Some(&c));
entry.update_state();
Ok(entry)
})
.unwrap();
self.displaying_cards.borrow_mut().clear();
for c in cards_iter {
self.displaying_cards.borrow_mut().push(c.unwrap());
}
} }
} }
@ -204,7 +196,6 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
let path: String = path.as_path().to_str().unwrap().to_owned(); let path: String = path.as_path().to_str().unwrap().to_owned();
picture_widget.set_file(Some(&gio::File::for_path(&path))); picture_widget.set_file(Some(&gio::File::for_path(&path)));
println!("Image path has changed");
let images_store_path = get_program_home_path() + "/images"; let images_store_path = get_program_home_path() + "/images";
@ -215,6 +206,7 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
_ => panic!("Could not create directory for storing images!"), _ => panic!("Could not create directory for storing images!"),
}, },
}; };
w.get_done_button().connect_closure( w.get_done_button().connect_closure(
"clicked", "clicked",
false, false,
@ -246,8 +238,10 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
) )
)", (&new_filename, &hieroglyph, &reading, &translation)).unwrap(); )", (&new_filename, &hieroglyph, &reading, &translation)).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();
w.close();
}), }),
); );
} }

View File

@ -1,9 +1,6 @@
use crate::ui::cards::{edit::MemoryCardsEditScene, game::MemoryCardsGameScene};
use glib::subclass::InitializingObject; use glib::subclass::InitializingObject;
use gtk::glib::closure_local;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate}; use gtk::{glib, Button, CompositeTemplate};
#[derive(CompositeTemplate, Default)] #[derive(CompositeTemplate, Default)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/setup/ui.xml")] #[template(resource = "/org/foxarmy/learn-hieroglyph/cards/setup/ui.xml")]
@ -34,20 +31,6 @@ impl ObjectSubclass for MemoryCardsSetupScene {
impl ObjectImpl for MemoryCardsSetupScene { impl ObjectImpl for MemoryCardsSetupScene {
fn constructed(&self) { fn constructed(&self) {
self.parent_constructed(); self.parent_constructed();
let binding = self.obj();
self.edit_button.connect_closure("clicked",false, closure_local!(@strong binding => move |_b: &Button| {
let new_win: MemoryCardsEditScene = MemoryCardsEditScene::new(&binding.application().unwrap());
new_win.present();
})
);
self.start_button.connect_closure("clicked", false, closure_local!(@strong binding => move |_b: &Button| {
let new_win: MemoryCardsGameScene = MemoryCardsGameScene::new(&binding.application().unwrap());
new_win.present();
}),
);
} }
} }

View File

@ -73,6 +73,12 @@ impl ObjectImpl for CardEntry {
.bind_property("translation", translation_label_binding, "label") .bind_property("translation", translation_label_binding, "label")
.sync_create() .sync_create()
.build(); .build();
let is_learning_switch_binding: &Switch = self.is_learning_switch.as_ref();
self.obj()
.bind_property("islearning", is_learning_switch_binding, "active")
.sync_create()
.build();
} }
fn dispose(&self) { fn dispose(&self) {

View File

@ -20,13 +20,14 @@ 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) => Object::builder() Some(card) =>
.property("imagepath", card.image_path().unwrap()) Object::builder()
.property("hieroglyph", card.hieroglyph().unwrap()) .property("imagepath", card.image_path().unwrap())
.property("reading", card.reading().unwrap()) .property("hieroglyph", card.hieroglyph().unwrap())
.property("translation", card.translation().unwrap()) .property("reading", card.reading().unwrap())
.property("islearning", card.is_learning().unwrap()) .property("translation", card.translation().unwrap())
.build(), .property("islearning", card.is_learning().unwrap())
.build(),
None => Object::builder() None => Object::builder()
.property("imagepath", "") .property("imagepath", "")
.property("hieroglyph", "") .property("hieroglyph", "")