I don't have brains

This commit is contained in:
leca 2024-05-13 20:15:23 +03:00
parent 966b9d22d7
commit 04c225357c
5 changed files with 92 additions and 56 deletions

30
src/card_gobject/imp.rs Normal file
View File

@ -0,0 +1,30 @@
use std::cell::RefCell;
use glib::Properties;
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
#[derive(Properties, Default)]
#[properties(wrapper_type = super::CardGObject)]
pub struct CardGObject {
#[property(get, set)]
imagepath: RefCell<String>,
#[property(get, set)]
hieroglyph: RefCell<String>,
#[property(get, set)]
reading: RefCell<String>,
#[property(get, set)]
translation: RefCell<String>,
#[property(get, set)]
islearning: RefCell<bool>,
}
#[glib::object_subclass]
impl ObjectSubclass for CardGObject {
const NAME: &'static str = "CardGObject";
type Type = super::CardGObject;
}
#[glib::derived_properties]
impl ObjectImpl for CardGObject {}

26
src/card_gobject/mod.rs Normal file
View File

@ -0,0 +1,26 @@
mod imp;
use glib::Object;
use gtk::glib;
use crate::card::Card;
glib::wrapper! {
pub struct CardGObject(ObjectSubclass<imp::CardGObject>);
}
impl CardGObject {
pub fn new(c: Option<Card>) -> Self {
match c {
Some(c) => {
Object::builder()
.property("imagepath", c.image_path().unwrap())
.property("hieroglyph", c.hieroglyph().unwrap())
.property("reading", c.reading().unwrap())
.property("translation", c.translation().unwrap())
.property("islearning", c.is_learning().unwrap())
.build()
},
None => Object::builder().build()
}
}
}

View File

@ -6,7 +6,7 @@ mod ui;
mod widgets;
mod dictionary;
mod card;
// mod card_object;
mod card_gobject;
use crate::ui::menu::MenuScene;

View File

@ -1,13 +1,12 @@
use std::borrow::Borrow;
use std::cell::RefCell;
use std::fs;
use std::sync::{Arc, Mutex};
use std::io::ErrorKind;
use std::path::Path;
use std::rc::Rc;
use crate::card::Card;
use crate::card_gobject::CardGObject;
use crate::db::*;
use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry;
@ -19,8 +18,8 @@ use gtk::prelude::WidgetExt;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{
gio, glib, Button, CompositeTemplate, FileChooserAction, ListView,
NoSelection, ResponseType, ScrolledWindow, SearchEntry, Switch, Window,
gio, glib, Button, CompositeTemplate, FileChooserAction, ListView, NoSelection, ResponseType,
ScrolledWindow, SearchEntry, Switch, Window,
};
use gtk::{ListItem, SignalListItemFactory};
use rusqlite::Connection;
@ -40,7 +39,7 @@ pub struct MemoryCardsEditScene {
#[template_child]
pub back_button: TemplateChild<Button>,
displaying_cards: Arc<Mutex<RefCell<Vec<CardEntry>>>>,
displaying_cards: RefCell<Vec<CardGObject>>,
}
#[glib::object_subclass]
@ -63,10 +62,6 @@ impl ObjectImpl for MemoryCardsEditScene {
self.parent_constructed();
let binding = self.obj();
// glib::spawn_future_local(clone!(@weak binding => async move {
// binding.imp().query_cards(None);
// }));
self.add_button.connect_closure("clicked",
false,
closure_local!(@strong binding => move |_b: &Button| {
@ -74,7 +69,6 @@ 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);
@ -96,15 +90,14 @@ impl ObjectImpl for MemoryCardsEditScene {
);
binding.imp().update_state();
}));
// self.query_cards(None);
self.update_state();
}
}
impl MemoryCardsEditScene {
pub fn update_state(&self) {
let model = ListStore::new::<CardEntry>();
model.extend_from_slice(&*(*self.displaying_cards.lock().unwrap()).borrow());
let model = ListStore::new::<CardGObject>();
model.extend_from_slice(&self.displaying_cards.borrow());
let factory = SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| {
@ -118,11 +111,11 @@ impl MemoryCardsEditScene {
//setting up every card's buttons behavior: editing, switch flipping and deleting.
factory.connect_closure("bind", false, closure_local!(@strong self_binding => move |_f: &SignalListItemFactory, list_item: &Object| {
let card_object = &list_item
let card_gobject = &list_item
.downcast_ref::<ListItem>()
.unwrap()
.item()
.and_downcast::<CardEntry>()
.and_downcast::<CardGObject>()
.unwrap();
let card_object_to_display = list_item
@ -131,7 +124,7 @@ impl MemoryCardsEditScene {
.child()
.and_downcast::<CardEntry>()
.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| {
card_object_to_display.get_edit_button_widget().connect_closure("clicked", false, closure_local!(@strong self_binding as binding, @strong card_gobject as card => move |_b: &Button| {
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
//setting corresponding properties
@ -172,7 +165,6 @@ impl MemoryCardsEditScene {
card.set_hieroglyph(hieroglyph);
card.set_reading(reading);
card.set_translation(translation);
card.update_file_for_image();
binding.imp().update_state();
w.close();
@ -180,7 +172,7 @@ impl MemoryCardsEditScene {
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| {
card_object_to_display.get_is_learning_switch_widget().connect_closure("state-set", false, closure_local!(@strong card_gobject 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();
@ -188,7 +180,7 @@ impl MemoryCardsEditScene {
glib::Propagation::Proceed
}));
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| {
card_object_to_display.get_delete_button_widget().connect_closure("clicked", false, closure_local!(@strong card_gobject 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();
@ -197,17 +189,16 @@ impl MemoryCardsEditScene {
Err(_) => ()
};
connection.execute("DELETE FROM cards WHERE hieroglyph = ?1", [&card.hieroglyph()]).unwrap();
let displaying_cards_lock = self_binding.imp().displaying_cards.lock().unwrap();
let position = (**displaying_cards_lock.borrow()).borrow().iter().position(|c| *c == card).unwrap();
displaying_cards_lock.borrow_mut().remove(position);
let position = self_binding.imp().displaying_cards.borrow().iter().position(|c| *c == card).unwrap();
self_binding.imp().displaying_cards.borrow_mut().remove(position);
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.set_hieroglyph(card_object.hieroglyph());
card_object_to_display.set_reading(card_object.reading());
card_object_to_display.set_translation(card_object.translation());
card_object_to_display.set_islearning(card_object.islearning());
card_gobject.bind_property("imagepath", card_object_to_display.get_image_widget(), "file").sync_create().build();
card_gobject.bind_property("hieroglyph", &card_object_to_display, "hieroglyph").sync_create().build();
card_gobject.bind_property("reading", &card_object_to_display, "reading").sync_create().build();
card_gobject.bind_property("translation", &card_object_to_display, "translation").sync_create().build();
card_gobject.bind_property("islearning", &card_object_to_display, "islearning").sync_create().build();
}));
let no_selection_model = NoSelection::new(Some(model));
@ -242,18 +233,13 @@ impl MemoryCardsEditScene {
Some(row.get(3).unwrap()),
Some(row.get(4).unwrap()),
);
let entry = CardEntry::new(Some(&c));
entry.update_state();
let entry = CardGObject::new(Some(c));
Ok(entry)
})
.unwrap();
self.displaying_cards.lock().unwrap().borrow_mut().clear();
self.displaying_cards.borrow_mut().clear();
for c in cards_iter {
self.displaying_cards
.lock()
.unwrap()
.borrow_mut()
.push(c.unwrap());
self.displaying_cards.borrow_mut().push(c.unwrap());
}
}
}
@ -276,15 +262,18 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
let path = RefCell::new(String::from(""));
dialog.run_async(clone!( @strong path, @strong w, @strong picture_widget => move |a, _b| {
let c = a.file().unwrap().path().unwrap().as_os_str().to_owned().to_str().unwrap().to_string();
println!("{c}");
let c = a.file().unwrap()
.path().unwrap()
.as_os_str().to_owned()
.to_str().unwrap()
.to_string(); // 0_0
*path.borrow_mut() = c;
a.close();
picture_widget.set_file(Some(&gio::File::for_path(Path::new(path.borrow().as_str()))));
let images_store_path = get_program_home_path() + "/images";
match fs::create_dir_all(&images_store_path) {
Ok(_) => {}
Err(error) => match error.kind() {
@ -292,7 +281,7 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
_ => panic!("Could not create directory for storing images!"),
},
};
w.get_done_button().connect_closure(
"clicked",
false,
@ -304,28 +293,15 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
let new_filename: String = hash.as_str().to_owned() + "." + extenstion;
let stored_image_path = Path::new(&images_store_path).join(&new_filename);
fs::copy(&p, stored_image_path).expect("Error copying image to store");
let hieroglyph = w.get_hieroglyph_input();
let conn = Connection::open(get_db_path()).unwrap();
conn.execute("UPDATE cards SET imagename = ?1 WHERE hieroglyph = ?2", (&new_filename, &hieroglyph)).unwrap();
conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap();
}),
);
}));
// let dialog: FileDialog = gtk::FileDialog::builder().build();
// let answer = Ok("");
// let answer = dialog.open_future(Some(&*window)).await;
// let path = match answer {
// Ok(p) => p,
// Err(_) => return,
// };
// .path()
// .unwrap();
// let path: String = path.as_path().to_str().unwrap().to_owned();
}
impl WidgetImpl for MemoryCardsEditScene {}

View File

@ -1,6 +1,10 @@
mod imp;
use glib::Object;
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button};
use gtk::{
gio,
glib::{self, subclass::types::ObjectSubclassIsExt},
Application, Button,
};
glib::wrapper! {
pub struct MemoryCardsEditScene(ObjectSubclass<imp::MemoryCardsEditScene>)