implemented delete card entry

This commit is contained in:
leca 2024-04-06 07:04:19 +03:00
parent fa260bb8f5
commit 193db81657
4 changed files with 42 additions and 27 deletions

View File

@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="CardEntry" parent="GtkWidget">
<property name="imagepath"></property>
<property name="hieroglyph"></property>
<property name="reading"></property>
<child>
<object class="GtkPicture" id="picture">
<property name="halign">GTK_ALIGN_CENTER</property>
@ -23,9 +20,10 @@
</object>
</child>
<child>
<object class="GtkButton" id="edit_button">
<object class="GtkButton" id="delete_button">
<property name="halign">GTK_ALIGN_END</property>
<property name="valign">GTK_ALIGN_END</property>
<property name="label">delete</property>
</object>
</child>
</template>

View File

@ -60,7 +60,6 @@ impl ObjectImpl for MemoryCardsEditScene {
}));
self.update_card_list();
}
}
impl MemoryCardsEditScene {
@ -70,33 +69,44 @@ impl MemoryCardsEditScene {
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);
let image_path: String = row.get(0).unwrap();
let image_path = get_images_store_path() + &image_path;
let hieroglyph: String = row.get(1).unwrap();
let reading: String = row.get(2).unwrap();
let entry = CardEntry::new(&image_path, &hieroglyph, &reading);
Ok(entry)
})
.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 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)));
let self_binding = self.obj();
c.append(&card_binding);
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();
let imagename = &Path::new(&imagepath).file_name().unwrap().to_str().unwrap();
fs::remove_file(get_images_store_path() + "/" + &imagename).expect("Could not remove file!");
connection.execute("DELETE FROM cards WHERE imagename = ?1", [&imagename]).unwrap();
self_binding.imp().update_card_list();
}));
card_binding.update_file_for_picture();
}
}
}
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;

View File

@ -1,9 +1,7 @@
use std::cell::RefCell;
use std::path::Path;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::gio::File;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture};
@ -18,9 +16,9 @@ pub struct CardEntry {
#[template_child]
pub reading_label: TemplateChild<Label>,
#[template_child]
pub edit_button: TemplateChild<Button>,
pub delete_button: TemplateChild<Button>,
#[property(get, set)]
imagepath: RefCell<String>,
pub imagepath: RefCell<String>,
#[property(get, set)]
hieroglyph: RefCell<String>,
#[property(get, set)]
@ -47,12 +45,6 @@ impl ObjectImpl for CardEntry {
fn constructed(&self) {
self.parent_constructed();
let picture_binding: &Picture = self.picture.as_ref();
let picture_file: File =
File::for_path(Path::new(&String::from(&*self.imagepath.borrow())));
picture_binding.set_file(glib::bitflags::__private::core::option::Option::Some(
&picture_file,
));
let hieroglyph_label_binding: &Label = self.hieroglyph_label.as_ref();
self.obj()
@ -65,6 +57,7 @@ impl ObjectImpl for CardEntry {
.bind_property("reading", reading_label_binding, "label")
.sync_create()
.build();
}
fn dispose(&self) {

View File

@ -1,7 +1,11 @@
mod imp;
use glib::Object;
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Picture};
use gtk::{
gio,
glib::{self, subclass::types::ObjectSubclassIsExt},
Button, Picture,
};
glib::wrapper! {
pub struct CardEntry(ObjectSubclass<imp::CardEntry>)
@ -22,4 +26,14 @@ impl CardEntry {
pub fn get_picture_widget(&self) -> &Picture {
self.imp().picture.as_ref()
}
pub fn get_delete_button_widget(&self) -> &Button {
self.imp().delete_button.as_ref()
}
pub fn update_file_for_picture(&self) {
let picture_binding: &Picture = self.imp().picture.as_ref();
let path: &str = &*self.imp().imagepath.borrow().clone().to_string();
picture_binding.set_file(Some(&gio::File::for_path(path)));
}
}