-
Notifications
You must be signed in to change notification settings - Fork 0
/
browse_flashcards.py
437 lines (346 loc) · 13.1 KB
/
browse_flashcards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from functions import *
from view_scheme import view_scheme
from preview import preview_card
from update_source import update_source
from add_card import add_card
def update_flashcards_inputs(window: sg.Window, front: str, back: str):
"""
It updates the text of the flashcards inner the InputText.
"""
visible_flashcars_inputs = False
if len(get_flashcards_array()) > 0:
visible_flashcars_inputs = True
window["flashcard_inputs"].update(visible=visible_flashcars_inputs)
window["front"].update(value=front)
window["back"].update(value=back)
def update_selected_source(window: sg.Window, row: int):
"""
It highlights the selected source.
"""
source_id = None
sources_arr = get_sources_array()
if row is not None and sources_arr != []:
source_id = sources_arr[row][0]
window["table_sources"].update(select_rows=[row])
set_row_sources(row=row)
set_selected_source_id(source_id)
update_flashcards_table(window)
update_selected_flashcards(window, 0)
def update_flashcards_table(window: sg.Window):
"""
It changes the values of flashcards table with the flashcards
of the selected source.
"""
source_id = get_selected_source_id()
flashcards_arr = []
if source_id is not None:
flashcards_arr = get_flashcards_for_table(source_id)
set_flashcards_array(flashcards_arr)
window["table_flashcards"].update(values=flashcards_arr)
def update_sources_table(window: sg.Window):
sources_names = all_sources_names()
set_sources_array(sources_names)
window["table_sources"].update(values=sources_names)
def update_selected_flashcards(window: sg.Window, row: int):
"""
It highlights the selected flashcard.
"""
flashcards_arr = get_flashcards_array()
front = str()
back = str()
sl_flashcard_id = None
if flashcards_arr != []:
if row > len(flashcards_arr) and row <= 1:
row = 0
sl_flashcard_id = flashcards_arr[row][0]
front = flashcards_arr[row][1]
back = flashcards_arr[row][2]
window["table_flashcards"].update(select_rows=[row])
set_row_flashcards(row=row)
set_selected_flashcard_id(sl_flashcard_id)
update_flashcards_inputs(window=window, front=front, back=back)
def update_tables(window):
update_sources_table(window)
update_selected_source(window, 0)
update_selected_flashcards(window, 0)
def browse_flashcards():
"""
It shows all decks and all the flashcards of selected deck.
"""
# If there are flashcards to display I will display it
cond_layout_flashcards_visible = True
sources_names = all_sources_names() # List of dicts with IDs and names
# Initial values for selected_source_id, row_sources, selected_flashcard_id, row_flashcards
set_selected_source_id(None)
set_row_sources(None)
set_selected_flashcard_id(None)
set_row_flashcards(None)
set_flashcards_array([])
if sources_names != []:
# Exists almost one source
# To default I set the first source as selected
first_source_id = sources_names[0][0]
set_selected_source_id(first_source_id)
set_row_sources(0)
flashcards_arr = get_flashcards_for_table(
first_source_id
) # Gets the flashcards of the first source
set_flashcards_array(flashcards_arr)
# To default I set the next values to None because
# it's not guaranteed the existence of flashcards
first_flashcard_id = None
row_flashcard = None
if flashcards_arr != []:
# To default if the array of flashcards is not void
# I set the first flashcard as selected
first_flashcard_id = flashcards_arr[0][0]
row_flashcard = 0
else:
cond_layout_flashcards_visible = False
set_selected_flashcard_id(first_flashcard_id)
set_row_flashcards(row_flashcard)
else:
cond_layout_flashcards_visible = False
set_sources_array(sources_names)
right_click_sources = [
"Sources",
["Add source", "Open source", "Change file", "Modify source", "Delete source"],
]
values_table_sources = [[]]
if sources_names is not None:
values_table_sources = sources_names
sources_table = sg.Table(
values=values_table_sources,
headings=["ID", "Name"],
key="table_sources",
enable_click_events=True,
justification="l",
hide_vertical_scroll=True,
col_widths=[3, 10],
auto_size_columns=False,
expand_x=False,
num_rows=5,
row_height=40,
right_click_menu=right_click_sources,
)
right_click_flashcards = [
"Flashcards",
["Add card", "Delete card", "Reschedule expired cards"],
]
values_table_flashcards = [[]]
if get_flashcards_array() is not None:
values_table_flashcards = get_flashcards_array()
flashcards_table = sg.Table(
values=values_table_flashcards,
headings=["id", "Front", "Back", "Deadline", "Box"],
key="table_flashcards",
justification="l",
hide_vertical_scroll=True,
enable_click_events=True,
col_widths=[3, 30, 30, 8, 3],
auto_size_columns=False,
expand_x=False,
num_rows=10,
row_height=20,
right_click_menu=right_click_flashcards,
)
menu_bar = [
["File", "Exit"],
right_click_sources,
right_click_flashcards,
["&Help", "&About..."],
]
front_default_text = ""
back_default_text = ""
if get_flashcards_array() != []:
front_default_text = flashcards_arr[0][1]
back_default_text = flashcards_arr[0][2]
flashcard_inputs = [
[sg.Column([[sg.Button("Latex", key="add_latex")]], justification="right")],
[
sg.Text("Front"),
sg.InputText(
default_text=front_default_text,
key="front",
expand_x=True,
enable_events=True,
size=(100, 1),
),
],
[
sg.Text("Back"),
sg.Multiline(
default_text=back_default_text,
key="back",
expand_x=True,
enable_events=True,
size=(100, 8),
),
],
[
sg.Button("Save", key="save_flashcard"),
sg.Button("View Scheme", key="view_scheme"),
sg.Button("Preview", key="preview_card"),
],
]
layout = [
[
[sg.Menu(menu_bar)],
[sources_table, flashcards_table],
[
sg.Column(
flashcard_inputs,
key="flashcard_inputs",
visible=cond_layout_flashcards_visible,
)
],
]
]
window = sg.Window(
title="Decks & Flashcards", layout=layout, modal=True, finalize=True
)
window["front"].bind("<Return>", "_Enter")
window["back"].bind("<Return>", "_Enter")
window["front"].bind("<Button-1>", "_LClick")
window["front"].bind("<Tab>", "_Tab")
window["back"].bind("<Button-1>", "_LClick")
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, "Exit"):
break
if event is not None:
### EVENTS FOR CLICK IN TABLES
if (
event[1] == "+CLICKED+"
and (event[2][0] is not None and event[2][0] >= 0)
and get_sources_array() != []
):
row = event[2][0]
set_front_input_selected(False)
set_back_input_selected(False)
match event[0]:
case "table_sources":
update_selected_source(window=window, row=row)
case "table_flashcards":
update_selected_flashcards(window=window, row=row)
###
### EVENTS FOR VIEWING AND LOADING SCHEME
if event == "view_scheme":
view_scheme(flashcard_id=get_selected_flashcard_id())
###
### EVENTS FOR FLASHCARD CHANGES
if event in ["front_Enter", "back_Enter", "save_flashcard"]:
update_flashcard(
flashcard_id=get_selected_flashcard_id(),
front=values["front"],
back=values["back"],
)
# To change only one flashcards I have to redraw all array.
update_flashcards_table(window)
check_input_click(event)
if event == "add_latex":
add_latex_to_input_field(window)
if event == "preview_card":
preview_card(text_front=values["front"], text_back=values["back"])
###
### MENU BAR EVENTS
## BOOK BAR EVENTS
### !!! CONTINUE WITH THE COMMENTS :D -C
if event in ["Add source", "Modify source"]:
if event == "Add source":
update_source("NEW")
if event == "Modify source":
update_source("MODIFY")
update_tables(window=window)
if event == "Delete source":
response = sg.popup_yes_no(
f"Do you want to delete the source and his content?"
)
if response == "Yes":
cursor.execute(
"DELETE FROM sources WHERE id=?", (get_selected_source_id(),)
)
con.commit()
cursor.execute(
"DELETE FROM flashcards WHERE source_id=?",
(get_selected_source_id(),),
)
con.commit()
update_tables(window=window)
if event == "Open source":
filename = cursor.execute(
"SELECT filename FROM sources where id=?",
(get_selected_source_id(),),
).fetchone()[0]
if filename in [None, ""]:
filename = sg.popup_get_file(
"Please, select a file",
default_path="",
title="File selector",
file_types=(("Portable Document Format", "PDF"),),
keep_on_top=True,
modal=True,
)
cursor.execute(
"UPDATE sources SET filename=? WHERE id=?",
(
filename,
get_selected_source_id(),
),
)
con.commit()
else:
import webbrowser
webbrowser.open_new(filename)
if event == "Change file":
old_filename = cursor.execute(
"SELECT filename FROM sources where id=?",
(get_selected_source_id(),),
).fetchone()[0]
filename = sg.popup_get_file(
"Please, select a file",
default_path=old_filename,
title="File selector",
file_types=(("Portable Document Format", "PDF"),),
keep_on_top=True,
modal=True,
)
if exists_filename(filename=filename):
cursor.execute(
"UPDATE sources SET filename=? WHERE id=?",
(
filename,
get_selected_source_id(),
),
)
con.commit()
##
## CARD BAR EVENTS
if event == "Add card":
add_card()
update_flashcards_table(window)
update_selected_flashcards(window, len(get_flashcards_array()) - 1)
window["table_flashcards"].Widget.see(len(get_flashcards_array()))
if not cond_layout_flashcards_visible:
cond_layout_flashcards_visible = True
if event == "Delete card":
cursor.execute(
"DELETE FROM flashcards WHERE id=?", (get_selected_flashcard_id(),)
)
con.commit()
update_flashcards_table(window)
update_selected_flashcards(window, 0)
window["table_flashcards"].Widget.see(1)
if event == "Reschedule expired cards":
today_str = datetime.now().strftime("%Y-%m-%d")
cursor.execute(
"UPDATE flashcards SET deadline = ? WHERE deadline < ?",
(today_str, today_str),
)
con.commit()
update_flashcards_table(window)
update_selected_flashcards(window, 0)
window["table_flashcards"].Widget.see(1)
##
###
window.close()