Add switch to enable cards

This commit is contained in:
leca 2024-04-18 14:14:19 +03:00
parent d0d2aaeb60
commit 9fa5524c9d
13 changed files with 139 additions and 50 deletions

View File

@ -1,11 +1,11 @@
<mxfile host="app.diagrams.net" modified="2024-04-14T07:14:49.035Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" etag="fvixqLn8GSK1Vul1DARw" version="24.2.5" type="device">
<mxfile host="app.diagrams.net" modified="2024-04-17T17:31:09.965Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" etag="OH14bMBbwCJqqmGBNy2U" version="24.2.5" type="device">
<diagram id="R2lEEEUBdFMjLlhIrx00" name="Page-1">
<mxGraphModel dx="1434" dy="748" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0" extFonts="Permanent Marker^https://fonts.googleapis.com/css?family=Permanent+Marker">
<mxGraphModel dx="683" dy="356" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0" extFonts="Permanent Marker^https://fonts.googleapis.com/css?family=Permanent+Marker">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="C-vyLk0tnHw3VtMMgP7b-23" value="cards&#xa;" style="shape=table;startSize=30;container=1;collapsible=1;childLayout=tableLayout;fixedRows=1;rowLines=0;fontStyle=1;align=center;resizeLast=1;" parent="1" vertex="1">
<mxGeometry x="125" y="111" width="330" height="180" as="geometry" />
<mxGeometry x="125" y="111" width="330" height="210" as="geometry" />
</mxCell>
<mxCell id="C-vyLk0tnHw3VtMMgP7b-24" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=1;" parent="C-vyLk0tnHw3VtMMgP7b-23" vertex="1">
<mxGeometry y="30" width="330" height="30" as="geometry" />
@ -46,15 +46,15 @@
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="BE118a502lDT9OyVwVR6-1" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=0;" vertex="1" parent="C-vyLk0tnHw3VtMMgP7b-23">
<mxCell id="BE118a502lDT9OyVwVR6-1" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=0;" parent="C-vyLk0tnHw3VtMMgP7b-23" vertex="1">
<mxGeometry y="120" width="330" height="30" as="geometry" />
</mxCell>
<mxCell id="BE118a502lDT9OyVwVR6-2" value="                    " style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;" vertex="1" parent="BE118a502lDT9OyVwVR6-1">
<mxCell id="BE118a502lDT9OyVwVR6-2" value="                    " style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;" parent="BE118a502lDT9OyVwVR6-1" vertex="1">
<mxGeometry width="30" height="30" as="geometry">
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="BE118a502lDT9OyVwVR6-3" value="reading VARCHAR(64)" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;" vertex="1" parent="BE118a502lDT9OyVwVR6-1">
<mxCell id="BE118a502lDT9OyVwVR6-3" value="reading VARCHAR(64)" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;" parent="BE118a502lDT9OyVwVR6-1" vertex="1">
<mxGeometry x="30" width="300" height="30" as="geometry">
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
@ -72,6 +72,19 @@
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="vElE6OK4QyMOiGnmp26Q-1" value="" style="shape=partialRectangle;collapsible=0;dropTarget=0;pointerEvents=0;fillColor=none;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;top=0;left=0;right=0;bottom=0;" vertex="1" parent="C-vyLk0tnHw3VtMMgP7b-23">
<mxGeometry y="180" width="330" height="30" as="geometry" />
</mxCell>
<mxCell id="vElE6OK4QyMOiGnmp26Q-2" value="" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;" vertex="1" parent="vElE6OK4QyMOiGnmp26Q-1">
<mxGeometry width="30" height="30" as="geometry">
<mxRectangle width="30" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="vElE6OK4QyMOiGnmp26Q-3" value="is_learning BOOLEAN DEFAULT FALSE" style="shape=partialRectangle;overflow=hidden;connectable=0;fillColor=none;top=0;left=0;bottom=0;right=0;align=left;spacingLeft=6;" vertex="1" parent="vElE6OK4QyMOiGnmp26Q-1">
<mxGeometry x="30" width="300" height="30" as="geometry">
<mxRectangle width="300" height="30" as="alternateBounds" />
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>

View File

@ -7,22 +7,22 @@
<property name="orientation">vertical</property>
<child>
<object class="LabledSwitch" id="hiragana_enable">
<property name="label_text">Enable hiragana?</property>
<property name="label">Enable hiragana?</property>
</object>
</child>
<child>
<object class="LabledSwitch" id="katakana_enable">
<property name="label_text">Enable katakana?</property>
<property name="label">Enable katakana?</property>
</object>
</child>
<child>
<object class="LabledSwitch" id="ktr_enable">
<property name="label_text">Enable kana to romaji?</property>
<property name="label">Enable kana to romaji?</property>
</object>
</child>
<child>
<object class="LabledSwitch" id="rtk_enable">
<property name="label_text">Enable romaji to kana?</property>
<property name="label">Enable romaji to kana?</property>
</object>
</child>
<child>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="CardEntry" parent="GtkWidget">
<!-- <property name="is_learning">false</property> -->
<child>
<object class="GtkImage" id="image">
<property name="halign">GTK_ALIGN_START</property>
@ -18,6 +19,13 @@
<property name="margin-start">50</property>
</object>
</child>
<child>
<object class="GtkLabel" id="reading_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="translation_label">
<property name="halign">GTK_ALIGN_START</property>
@ -25,6 +33,14 @@
<property name="margin-end">50</property>
<property name="hexpand-set">true</property>
<property name="hexpand">true</property>
<property name="width-chars">16</property>
<property name="max-width-chars">16</property>
<property name="wrap">true</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="is_learning_switch">
<property name="valign">GTK_ALIGN_CENTER</property>
</object>
</child>
<child>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="LabledSwitch" parent="GtkWidget">
<property name="label_text">placeholder</property>
<child>
<object class="GtkSwitch" id="switch_obj">
</object>

View File

@ -17,7 +17,7 @@ impl Card {
self.image_path.clone()
}
pub fn hieroglyp(&self) -> Option<String> {
pub fn hieroglyph(&self) -> Option<String> {
self.hieroglyph.clone()
}

View File

@ -45,7 +45,8 @@ pub fn init() {
imagename CHAR(64),
hieroglyph VARCHAR(64),
reading VARCHAR(64),
translation VARCHAR(128)
translation VARCHAR(128),
is_learning BOOLEAN DEFAULT FALSE
)",()).unwrap();
}

View File

@ -108,7 +108,7 @@ fn write_to_database(dict: &Vec<DictionaryEntry>) {
let mut conn = Connection::open(get_db_path()).unwrap();
let tx = conn.transaction().unwrap();
for line in dict.iter() {
match tx.execute("INSERT OR REPLACE INTO cards (id, hieroglyph, reading, translation) VALUES ((SELECT id FROM cards WHERE hieroglyph = ?1), ?1, ?2, ?3)", [&line.hieroglyph, &line.reading, &line.translation]) {
match tx.execute("INSERT OR IGNORE INTO cards (id, hieroglyph, reading, translation) VALUES ((SELECT id FROM cards WHERE hieroglyph = ?1), ?1, ?2, ?3)", [&line.hieroglyph, &line.reading, &line.translation]) {
Ok(_) => (),
Err(e) => {
println!("{e}" );

View File

@ -1,21 +1,20 @@
use std::borrow::Borrow;
use std::cell::RefCell;
use std::fs;
use std::io::ErrorKind;
use std::path::Path;
use std::rc::Rc;
use crate::card::*;
use crate::db::*;
use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry;
use crate::card::Card;
use glib::subclass::InitializingObject;
use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local};
use gtk::prelude::WidgetExt;
use gtk::subclass::prelude::*;
use gtk::ListBox;
use gtk::{gio, glib, Box, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{gio, glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{prelude::*, FileDialog};
use rusqlite::Connection;
use sha256::try_digest;
@ -111,7 +110,7 @@ impl MemoryCardsEditScene {
};
let sql =
format!("SELECT imagename, hieroglyph, reading, translation FROM cards {selector}");
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| {
@ -119,12 +118,15 @@ impl MemoryCardsEditScene {
Ok(path) => path,
Err(_) => String::from(""),
};
let image_path: String = get_images_store_path() + &image_path;
let hieroglyph: String = row.get(1).unwrap();
let reading: String = row.get(2).unwrap();
let hieroglyph = format!("{} ({})", hieroglyph, reading);
let translation: String = row.get(3).unwrap();
let entry = CardEntry::new(&image_path, &hieroglyph, &translation);
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())
);
let entry = CardEntry::new(&c);
entry.update_state();
Ok(entry)
})
.unwrap();
@ -156,21 +158,12 @@ impl MemoryCardsEditScene {
card.get_edit_button_widget().connect_closure("clicked", false, closure_local!(@strong self_binding as binding, @strong card => move |_b: &Button| {
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
//parsing hieroglyph and its reading from a card
let hieroglyph_and_reading: String = card.hieroglyph();
let reading_start = hieroglyph_and_reading.find("(").unwrap();
let reading_end = hieroglyph_and_reading.find(")").unwrap();
let hieroglyph = hieroglyph_and_reading[..reading_start-1].to_owned();
let reading = hieroglyph_and_reading[reading_start+1..reading_end].to_owned();
let translation = card.translation();
//setting corresponding properties
new_win.get_picture_widget().set_file(Some(&gio::File::for_path(card.imagepath())));
new_win.get_hieroglyph_entry().set_text(&hieroglyph);
new_win.get_reading_entry().set_text(&reading);
new_win.get_translation_entry().set_text(&translation);
new_win.get_hieroglyph_entry().set_text(&card.hieroglyph());
new_win.get_reading_entry().set_text(&card.reading());
new_win.get_translation_entry().set_text(&card.translation());
new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win, @strong card => move |b: &Button| {
b.set_visible(false);
@ -230,7 +223,7 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
let translation = w.get_translation_input();
let conn = Connection::open(get_db_path()).unwrap();
let query = "INSERT OR REPLACE INTO cards (id, imagename, hieroglyph, reading, translation) VALUES((SELECT id FROM cards WHERE hieroglyph = ?2), ?1, ?2, ?3, ?4)";
let query = "INSERT OR REPLACE INTO cards (id, imagename, hieroglyph, reading, translation, is_learning) VALUES((SELECT id FROM cards WHERE hieroglyph = ?2), ?1, ?2, ?3, ?4, (SELECT is_learning FROM cards WHERE hieroglyph = ?2))";
conn.execute(query, (&new_filename, &hieroglyph, &reading, &translation)).unwrap();
println!("new imagepath: {new_filename}");
w.close();

View File

@ -41,7 +41,7 @@ impl CardDisplay {
pub fn generate_card(&self) -> Option<(&RefCell<String>, &RefCell<String>)> {
let connection = Connection::open(get_db_path()).unwrap();
let mut stmt = connection.prepare("SELECT imagename, hieroglyph FROM cards ORDER BY RANDOM() LIMIT 1").unwrap();
let mut stmt = connection.prepare("SELECT imagename, hieroglyph FROM cards WHERE is_learning = TRUE ORDER BY RANDOM() LIMIT 1").unwrap();
let random_card_iter = stmt.query_map((), |row| {
Ok(
Card {

View File

@ -3,7 +3,8 @@ use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Image};
use gtk::{glib, prelude::*, Button, CompositeTemplate, Image, Label, Switch};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardEntry)]
@ -14,17 +15,25 @@ pub struct CardEntry {
#[template_child]
pub hieroglyph_label: TemplateChild<Label>,
#[template_child]
pub reading_label: TemplateChild<Label>,
#[template_child]
pub translation_label: TemplateChild<Label>,
#[template_child]
pub edit_button: TemplateChild<Button>,
#[template_child]
pub delete_button: TemplateChild<Button>,
#[template_child]
pub is_learning_switch: TemplateChild<Switch>,
#[property(get, set)]
pub imagepath: RefCell<String>,
#[property(get, set)]
hieroglyph: RefCell<String>,
pub hieroglyph: RefCell<String>,
#[property(get, set)]
translation: RefCell<String>,
pub reading: RefCell<String>,
#[property(get, set)]
pub translation: RefCell<String>,
#[property(get, set)]
pub islearning: RefCell<bool>,
}
#[glib::object_subclass]
@ -53,6 +62,12 @@ impl ObjectImpl for CardEntry {
.sync_create()
.build();
let reading_label_binding: &Label = self.reading_label.as_ref();
self.obj()
.bind_property("reading", reading_label_binding, "label")
.sync_create()
.build();
let translation_label_binding: &Label = self.translation_label.as_ref();
self.obj()
.bind_property("translation", translation_label_binding, "label")

View File

@ -3,9 +3,12 @@ mod imp;
use glib::Object;
use gtk::{
gio,
glib::{self, subclass::types::ObjectSubclassIsExt},
Button, Image,
glib::{self, clone, subclass::types::ObjectSubclassIsExt},
Button, Image, Switch,
};
use rusqlite::Connection;
use crate::{card::Card, db::get_db_path};
glib::wrapper! {
pub struct CardEntry(ObjectSubclass<imp::CardEntry>)
@ -15,11 +18,13 @@ glib::wrapper! {
}
impl CardEntry {
pub fn new(image_path: &String, hieroglyph: &String, translation: &String) -> Self {
pub fn new(card: &Card) -> Self {
Object::builder()
.property("imagepath", image_path)
.property("hieroglyph", hieroglyph)
.property("translation", translation)
.property("imagepath", card.image_path().unwrap())
.property("hieroglyph", card.hieroglyph().unwrap())
.property("reading", card.reading().unwrap())
.property("translation", card.translation().unwrap())
.property("islearning", card.is_learning().unwrap())
.build()
}
@ -40,4 +45,40 @@ impl CardEntry {
let path: &str = &*self.imp().imagepath.borrow().clone().to_string();
image_binding.set_file(Some(&path));
}
pub fn get_hieroglyph(&self) -> String {
self.imp().hieroglyph.borrow().clone()
}
pub fn update_state(&self) {
let conn = Connection::open(get_db_path()).unwrap();
let is_learning_switch_binding: &Switch = self.imp().is_learning_switch.as_ref();
let h = &self.get_hieroglyph();
let sql = "SELECT is_learning FROM cards WHERE hieroglyph=:h;";
let mut stmt = conn.prepare(sql).unwrap();
let iter = stmt
.query_map(&[(":h", h.as_str())], |row| {
Ok(row.get(0).unwrap())
})
.unwrap();
for res in iter {
is_learning_switch_binding.set_state(res.unwrap());
}
let self_binding = self;
is_learning_switch_binding.connect_state_set(
clone!(@strong self_binding => move |_, is_enabled| {
let conn = Connection::open(get_db_path()).unwrap();
let hieroglyph = &*self_binding.imp().hieroglyph.borrow().clone();
println!("UPDATE cards SET is_learning = {is_enabled} WHERE hieroglyph = {hieroglyph}",);
conn.execute(
"UPDATE cards SET is_learning = ?1 WHERE hieroglyph = ?2",
(&is_enabled, &hieroglyph),
)
.unwrap();
glib::Propagation::Proceed
}),
);
}
}

View File

@ -14,7 +14,9 @@ pub struct LabledSwitch {
#[template_child]
pub label_obj: TemplateChild<Label>,
#[property(get, set)]
label_text: RefCell<String>,
label: RefCell<String>,
#[property(get, set)]
active: RefCell<bool>,
}
#[glib::object_subclass]
@ -38,8 +40,14 @@ impl ObjectImpl for LabledSwitch {
self.parent_constructed();
let l: &Label = self.label_obj.as_ref();
let s: &Switch = self.switch_obj.as_ref();
self.obj()
.bind_property("label_text", l, "label")
.bind_property("label", l, "label")
.sync_create()
.build();
self.obj()
.bind_property("active", s, "active")
.sync_create()
.build();
}

View File

@ -16,7 +16,10 @@ glib::wrapper! {
impl LabledSwitch {
pub fn new(label: &String) -> Self {
Object::builder().property("label_text", label).build()
Object::builder()
.property("label", label)
.property("active", false)
.build()
}
pub fn get_switch(&self) -> &Switch {