cards preview are now displaying, some improvements and renaming

This commit is contained in:
2024-04-06 02:06:25 +03:00
parent 8d17e4e474
commit fa260bb8f5
10 changed files with 158 additions and 70 deletions

View File

@@ -1,15 +1,18 @@
use rusqlite::Connection;
use std::path::{Path, PathBuf};
use std::fs;
use std::io::ErrorKind;
use std::path::Path;
use std::rc::Rc;
use crate::db::*;
use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry;
use glib::subclass::InitializingObject;
use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local};
use gtk::subclass::prelude::*;
use gtk::{gio, glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{gio, glib, Box, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{prelude::*, FileDialog};
use rusqlite::Connection;
use sha256::try_digest;
#[derive(CompositeTemplate, Default)]
@@ -20,9 +23,9 @@ pub struct MemoryCardsEditScene {
#[template_child]
pub add_button: TemplateChild<Button>,
#[template_child]
pub first: TemplateChild<CardEntry>,
pub cards: TemplateChild<Box>,
#[template_child]
pub cards: TemplateChild<ScrolledWindow>,
pub cards_scrolled_window: TemplateChild<ScrolledWindow>,
}
#[glib::object_subclass]
@@ -55,14 +58,45 @@ impl ObjectImpl for MemoryCardsEditScene {
}));
new_win.present();
}));
self.update_card_list();
}
let c: &ScrolledWindow = self.cards.as_ref();
c.set_child(glib::bitflags::__private::core::option::Option::Some(
&CardEntry::new(&"a".to_string(), &"a".to_string()),
));
}
impl MemoryCardsEditScene {
pub fn update_card_list(&self) {
let c: &Box = self.cards.as_ref();
while c.first_child() != None {
c.remove(&c.first_child().unwrap());
}
let conn = Connection::open(get_db_path()).unwrap();
let mut stmt = conn
.prepare("SELECT imagename, hieroglyph, reading FROM cards")
.unwrap();
let cards_iter = stmt
.query_map([], |row| {
let a: String = row.get(0).unwrap();
let b: String = row.get(1).unwrap();
let c: String = row.get(2).unwrap();
let entry = CardEntry::new(&a, &b, &c);
Ok(entry)
})
.unwrap();
for card in cards_iter {
let card_binding = card.unwrap();
let path = get_images_store_path() + &card_binding.imagepath();
card_binding.set_imagepath(&*path);
card_binding.get_picture_widget().set_file(Some(&gio::File::for_path(&path)));
c.append(&card_binding);
}
}
}
async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
let dialog: FileDialog = gtk::FileDialog::builder().build();
let answer = dialog.open_future(Some(&*window)).await;
@@ -71,21 +105,40 @@ async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref())
.downcast_ref()
.unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
// w.get_image_widget().set_file(Some(path));
w.get_image_widget().set_file(Some(&gio::File::for_path(path)));
let binding = Path::new(path);
let hash = try_digest(binding).unwrap();
w.get_done_button().connect_closure("clicked", false, closure_local!(@strong w => move |_b: &Button| {
let hieroglyph = w.get_hieroglyph_input();
let reading = w.get_reading_input();
let program_home_path = &*crate::db::get_program_home_path().read().unwrap().clone();
let conn = Connection::open(PathBuf::new().join(program_home_path).join("db.sqlite")).unwrap();
w.get_picture_widget()
.set_file(Some(&gio::File::for_path(path)));
let query = "INSERT INTO cards (image_hash, hieroglyph, translation) VALUES(?1, ?2, ?3)";
conn.execute(query, (&hash, &hieroglyph, &reading)).unwrap();
w.close();
}));
let images_store_path = get_program_home_path() + "/images";
match fs::create_dir_all(&images_store_path) {
Ok(_) => {}
Err(error) => match error.kind() {
ErrorKind::AlreadyExists => {}
_ => panic!("Could not create directory for storing pictures!"),
},
};
let hash = try_digest(path).unwrap();
let extenstion = Path::new(path).extension().unwrap().to_str().unwrap();
let new_filename: String = hash.as_str().to_owned() + "." + extenstion;
let stored_image_path = Path::new(&images_store_path).join(&new_filename);
fs::copy(path, stored_image_path).expect("Error copying image to store");
w.get_done_button().connect_closure(
"clicked",
false,
closure_local!(@strong w => move |_b: &Button| {
let hieroglyph = w.get_hieroglyph_input();
let reading = w.get_reading_input();
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();
w.close();
}),
);
}
impl WidgetImpl for MemoryCardsEditScene {}

View File

@@ -6,7 +6,7 @@ use gtk::{glib, Button, CompositeTemplate, Entry, Picture};
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/new/ui.xml")]
pub struct MemoryCardsNewScene {
#[template_child]
pub image: TemplateChild<Picture>,
pub picture: TemplateChild<Picture>,
#[template_child]
pub file_choose_button: TemplateChild<Button>,
#[template_child]

View File

@@ -21,8 +21,8 @@ impl MemoryCardsNewScene {
self.imp().file_choose_button.as_ref()
}
pub fn get_image_widget(&self) -> &Picture {
self.imp().image.as_ref()
pub fn get_picture_widget(&self) -> &Picture {
self.imp().picture.as_ref()
}
pub fn get_hieroglyph_input(&self) -> String {