update ui

This commit is contained in:
leca 2024-04-09 16:42:13 +03:00
parent eb26ca7418
commit c376dbc01a
8 changed files with 80 additions and 76 deletions

View File

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

View File

@ -2,9 +2,15 @@
<interface>
<template class="CardDisplay" parent="GtkWidget">
<child>
<object class="GtkPicture" id="picture">
<object class="GtkImage" id="image">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="vexpand-set">true</property>
<property name="vexpand">true</property>
<property name="hexpand-set">true</property>
<property name="hexpand">true</property>
<property name="width-request">512</property>
<property name="height-request">512</property>
</object>
</child>
<child>
@ -17,7 +23,7 @@
<object class="GtkEntry" id="answer_entry">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_START</property>
<property name="placeholder-text">Type kana that picture means</property>
<property name="placeholder-text">Type kana that image means</property>
</object>
</child>
</template>

View File

@ -2,46 +2,38 @@
<interface>
<template class="CardEntry" parent="GtkWidget">
<child>
<object class="GtkGrid">
<child>
<object class="GtkPicture" id="picture">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<layout>
<property name="column">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="hieroglyph_label">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<layout>
<property name="column">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel" id="reading_label">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<layout>
<property name="column">2</property>
</layout>
</object>
</child>
<child>
<object class="GtkButton" id="delete_button">
<property name="halign">GTK_ALIGN_END</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="label">delete</property>
<property name="hexpand">true</property>
<property name="hexpand-set">true</property>
<layout>
<property name="column">3</property>
</layout>
</object>
</child>
<object class="GtkImage" id="image">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="vexpand">true</property>
<property name="vexpand">true</property>
<property name="height-request">128</property>
<property name="width-request">128</property>
</object>
</child>
<child>
<object class="GtkLabel" id="hieroglyph_label">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="margin-start">50</property>
</object>
</child>
<child>
<object class="GtkLabel" id="reading_label">
<property name="halign">GTK_ALIGN_END</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="margin-end">50</property>
<property name="hexpand-set">true</property>
<property name="hexpand">true</property>
</object>
</child>
<child>
<object class="GtkButton" id="delete_button">
<property name="halign">GTK_ALIGN_END</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="label">delete</property>
<property name="hexpand">true</property>
<property name="hexpand-set">true</property>
</object>
</child>
</template>

View File

@ -60,7 +60,8 @@ impl ObjectImpl for MemoryCardsEditScene {
false,
closure_local!(@strong binding => move |_b: &Button| {
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |_| {
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(file_choose_dialog(Rc::clone(&new_win)));
}));
new_win.get_done_button().connect_closure("clicked", true, closure_local!(@strong binding => move |_w: &Button| {
@ -144,7 +145,7 @@ impl MemoryCardsEditScene {
self_binding.imp().query_cards(None);
self_binding.imp().update_card_list();
}));
card.update_file_for_picture();
card.update_file_for_image();
}
}
}
@ -152,14 +153,17 @@ impl MemoryCardsEditScene {
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;
let path = answer.unwrap().path().unwrap();
let path: &str = path.as_path().to_str().unwrap();
let path = match answer {
Ok(p) => p,
Err(_) => return
}.path().unwrap();
let path: String = path.as_path().to_str().unwrap().to_owned();
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref())
.downcast_ref()
.unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
w.get_picture_widget()
.set_file(Some(&gio::File::for_path(path)));
.set_file(Some(&gio::File::for_path(&path)));
let images_store_path = get_program_home_path() + "/images";
@ -167,24 +171,26 @@ async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
Ok(_) => {}
Err(error) => match error.kind() {
ErrorKind::AlreadyExists => {}
_ => panic!("Could not create directory for storing pictures!"),
_ => panic!("Could not create directory for storing images!"),
},
};
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");
println!("Setting handler");
w.get_done_button().connect_closure(
"clicked",
false,
closure_local!(@strong w => move |_b: &Button| {
closure_local!(@strong w, @strong path => move |_b: &Button| {
let hash = try_digest(&path).unwrap();
let extenstion = Path::new(path.as_str()).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");
let hieroglyph = w.get_hieroglyph_input();
let reading = w.get_reading_input();
let conn = Connection::open(get_db_path()).unwrap();
println!("INSERTING NEW IMAGE");
let query = "INSERT INTO cards (imagename, hieroglyph, reading) VALUES(?1, ?2, ?3)";
conn.execute(query, (&new_filename, &hieroglyph, &reading)).unwrap();
w.close();

View File

@ -3,14 +3,14 @@ use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, BoxLayout, CompositeTemplate, Entry, Label, Picture};
use gtk::{glib, prelude::*, BoxLayout, CompositeTemplate, Entry, Label, Image};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardDisplay)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_display/template.ui.xml")]
pub struct CardDisplay {
#[template_child]
pub picture: TemplateChild<Picture>,
pub image: TemplateChild<Image>,
#[template_child]
pub answer_entry: TemplateChild<Entry>,
#[template_child]
@ -44,10 +44,10 @@ impl ObjectImpl for CardDisplay {
let layout_manager: BoxLayout = BoxLayout::new(gtk::Orientation::Vertical);
self.obj().set_layout_manager(Some(layout_manager));
self.picture.set_width_request(256);
self.picture.set_height_request(256);
// self.picture.
// self.picture.set
self.image.set_width_request(256);
self.image.set_height_request(256);
// self.image.
// self.image.set
}
fn dispose(&self) {

View File

@ -3,7 +3,7 @@ mod imp;
use std::cell::RefCell;
use glib::Object;
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, prelude::*, Entry, Picture};
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, prelude::*, Entry, Image};
use rusqlite::Connection;
use crate::db::{get_db_path, get_images_store_path};
@ -29,14 +29,14 @@ impl CardDisplay {
.build()
}
pub fn update_file_for_picture(&self) {
let picture_binding: &Picture = self.imp().picture.as_ref();
pub fn update_file_for_image(&self) {
let image_binding: &Image = self.imp().image.as_ref();
let path: &str = &*self.imp().imagepath.borrow().clone().to_string();
if path == "" {
self.imp().error_message.set_visible(true);
return;
}
picture_binding.set_file(Some(&gio::File::for_path(path)));
image_binding.set_file(Some(&path));
}
pub fn generate_card(&self) -> Option<(&RefCell<String>, &RefCell<String>)> {
let connection = Connection::open(get_db_path()).unwrap();
@ -63,7 +63,7 @@ impl CardDisplay {
*self.imp().imagepath.borrow_mut() = get_images_store_path() + "/" + random_card.imagename.as_str();
*self.imp().hieroglyph.borrow_mut() = random_card.hieroglyph;
self.update_file_for_picture();
self.update_file_for_image();
Some((&self.imp().imagepath, &self.imp().hieroglyph))
}

View File

@ -3,14 +3,14 @@ use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture};
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Image};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardEntry)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_entry/template.ui.xml")]
pub struct CardEntry {
#[template_child]
pub picture: TemplateChild<Picture>,
pub image: TemplateChild<Image>,
#[template_child]
pub hieroglyph_label: TemplateChild<Label>,
#[template_child]

View File

@ -4,7 +4,7 @@ use glib::Object;
use gtk::{
gio,
glib::{self, subclass::types::ObjectSubclassIsExt},
Button, Picture,
Button, Image,
};
glib::wrapper! {
@ -23,17 +23,17 @@ impl CardEntry {
.build()
}
pub fn get_picture_widget(&self) -> &Picture {
self.imp().picture.as_ref()
pub fn get_image_widget(&self) -> &Image {
self.imp().image.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();
pub fn update_file_for_image(&self) {
let image_binding: &Image = self.imp().image.as_ref();
let path: &str = &*self.imp().imagepath.borrow().clone().to_string();
picture_binding.set_file(Some(&gio::File::for_path(path)));
image_binding.set_file(Some(&path));
}
}