-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck_editor.py
More file actions
340 lines (278 loc) · 15 KB
/
deck_editor.py
File metadata and controls
340 lines (278 loc) · 15 KB
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
import gi
import data_engine as db
import os
import html
import re
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, Gdk, Gio, GObject, GLib
class DeckEditor(Gtk.Box):
def __init__(self, filename, back_callback=None, highlight_card_id=None):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
self.filename = filename
self.back_callback = back_callback
self.highlight_card_id = highlight_card_id # <--- Store it
# 0 = Default (Creation Date), 1 = A-Z, 2 = Z-A
self.sort_mode = 0
# --- 1. Compact Header ---
header_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
# Mobile-friendly margins (12px)
header_box.set_margin_top(12); header_box.set_margin_bottom(12)
header_box.set_margin_start(12); header_box.set_margin_end(12)
if self.back_callback:
btn_back = Gtk.Button(icon_name="go-previous-symbolic")
btn_back.add_css_class("flat"); btn_back.set_tooltip_text("Return")
btn_back.connect("clicked", lambda x: self.back_callback())
header_box.append(btn_back)
title_text = filename.replace('.json', '').replace('_', ' ').title()
title = Gtk.Label(label=f"Edit: {title_text}")
title.add_css_class("title-2")
title.set_ellipsize(3)
header_box.append(title)
# Spacer to push Add button to the right
header_box.append(Gtk.Label(hexpand=True))
# Sort Button
# Logic: Current sort is A-Z (Ascending), so the button shows the option to switch to Z-A (Descending)
self.btn_sort = Gtk.Button(icon_name="view-sort-descending-symbolic")
self.btn_sort.add_css_class("flat")
self.btn_sort.set_tooltip_text("Sort: Z-A")
self.btn_sort.connect("clicked", self.toggle_sort)
header_box.append(self.btn_sort)
# Add "New Card" Button
btn_add = Gtk.Button(icon_name="list-add-symbolic")
btn_add.add_css_class("suggested-action")
btn_add.set_tooltip_text("Add New Card")
btn_add.connect("clicked", lambda x: self.show_card_dialog("add"))
header_box.append(btn_add)
self.append(header_box)
self.append(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL))
# --- 2. Scrollable List ---
self.scroll = Gtk.ScrolledWindow()
self.scroll.set_hexpand(True); self.scroll.set_vexpand(True)
self.append(self.scroll)
self.clamp = Adw.Clamp(maximum_size=800)
self.clamp.set_margin_top(12); self.clamp.set_margin_bottom(12)
self.clamp.set_margin_start(12); self.clamp.set_margin_end(12)
self.scroll.set_child(self.clamp)
self.list_box = Gtk.ListBox()
self.list_box.add_css_class("boxed-list")
self.list_box.set_selection_mode(Gtk.SelectionMode.NONE)
self.clamp.set_child(self.list_box)
self.refresh_list()
def toggle_sort(self, btn):
# Toggle 0 <-> 1
self.sort_mode = 1 - self.sort_mode
if self.sort_mode == 0: # Current View is A-Z
# Button offers Z-A
self.btn_sort.set_icon_name("view-sort-descending-symbolic")
self.btn_sort.set_tooltip_text("Sort: Z-A")
else: # Current View is Z-A
# Button offers A-Z
self.btn_sort.set_icon_name("view-sort-ascending-symbolic")
self.btn_sort.set_tooltip_text("Sort: A-Z")
self.refresh_list()
def get_clean_text(self, card):
"""
Robust cleaner:
1. Removes ALL punctuation/markdown (**, ?, (), #, etc).
2. Strips whitespace.
3. Converts to lowercase.
"""
raw = card.get("front", "")
# [^\w\s] matches anything that is NOT a word char (a-z, 0-9) or whitespace
clean = re.sub(r'[^\w\s]', '', raw).strip().lower()
return clean
def refresh_list(self):
while child := self.list_box.get_first_child(): self.list_box.remove(child)
cards = db.load_deck(self.filename)
# --- 2-STATE SORTING ---
if self.sort_mode == 0: # A -> Z
cards.sort(key=self.get_clean_text)
else: # Z -> A
cards.sort(key=self.get_clean_text, reverse=True)
# -----------------------
if not cards:
status = Adw.StatusPage(icon_name="folder-open-symbolic", title="No Cards", description="Click '+' to add a card.")
status.set_vexpand(True)
self.list_box.append(status)
return
target_row = None # <--- Tracker
for card in cards:
f_raw = card.get("front", "???")
b_raw = card.get("back", "???")
f_txt = html.escape(f_raw)
b_txt = html.escape(b_raw)
row = Adw.ActionRow(title=f_txt, subtitle=b_txt)
row.set_title_lines(2)
row.set_subtitle_lines(2)
# --- HIGHLIGHT LOGIC ---
if self.highlight_card_id and card.get("id") == self.highlight_card_id:
row.add_css_class("flash-row") # We will define this CSS in main.py
target_row = row
# -----------------------
icon_box = Gtk.Box(spacing=5)
if card.get("image"): icon_box.append(Gtk.Image.new_from_icon_name("image-x-generic-symbolic"))
if card.get("audio"): icon_box.append(Gtk.Image.new_from_icon_name("audio-x-generic-symbolic"))
if card.get("suspended"):
lbl = Gtk.Label(label="⚠️"); lbl.set_tooltip_text("Leech (Suspended)")
icon_box.append(lbl)
if icon_box.get_first_child(): row.add_prefix(icon_box)
# Edit Button triggers the Unified Dialog
btn_edit = Gtk.Button(icon_name="document-edit-symbolic")
btn_edit.add_css_class("flat")
btn_edit.set_tooltip_text("Edit")
btn_edit.connect("clicked", lambda b, c=card: self.show_card_dialog("edit", c))
row.add_suffix(btn_edit)
btn_del = Gtk.Button(icon_name="user-trash-symbolic")
btn_del.add_css_class("flat"); btn_del.add_css_class("destructive-action")
btn_del.set_tooltip_text("Delete")
btn_del.connect("clicked", lambda b, c=card: self.confirm_delete(c))
row.add_suffix(btn_del)
self.list_box.append(row)
# --- SCROLL LOGIC ---
if target_row:
# We use a small timeout to let the UI layout settle before scrolling/focusing
def scroll_to_target():
target_row.grab_focus()
return False
GLib.timeout_add(100, scroll_to_target)
# Clear the ID so it doesn't flash again on next refresh
self.highlight_card_id = None
def on_delete_clicked(self, card_id):
if card_id: db.delete_card(self.filename, card_id); self.refresh_list()
# FIX: Unified Dialog for ADD and EDIT with Media Support
def show_card_dialog(self, mode, card=None):
title = "Add Card" if mode == "add" else "Edit Card"
dialog = Adw.MessageDialog(heading=title, transient_for=self.get_root())
dialog.set_modal(True)
dialog.add_response("cancel", "Cancel")
dialog.add_response("save", "Save")
dialog.set_response_appearance("save", Adw.ResponseAppearance.SUGGESTED)
# Responsive Height Logic
win_width = self.get_root().get_width()
is_narrow = win_width < 500
box_height = 90 if is_narrow else 120
# Main Layout
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
# Front
box.append(Gtk.Label(label="Front", xalign=0, css_classes=["heading"]))
tf = Gtk.TextView(); tf.set_wrap_mode(3)
tf.set_left_margin(10); tf.set_right_margin(10); tf.set_top_margin(10); tf.set_bottom_margin(10)
if card: tf.get_buffer().set_text(card.get("front", ""))
scroll_f = Gtk.ScrolledWindow(min_content_height=box_height)
scroll_f.set_propagate_natural_height(True); scroll_f.set_hexpand(True)
scroll_f.set_child(tf)
box.append(Gtk.Frame(child=scroll_f))
# Back
box.append(Gtk.Label(label="Back", xalign=0, css_classes=["heading"]))
tb = Gtk.TextView(); tb.set_wrap_mode(3)
tb.set_left_margin(10); tb.set_right_margin(10); tb.set_top_margin(10); tb.set_bottom_margin(10)
if card: tb.get_buffer().set_text(card.get("back", ""))
scroll_b = Gtk.ScrolledWindow(min_content_height=box_height)
scroll_b.set_propagate_natural_height(True); scroll_b.set_hexpand(True)
scroll_b.set_child(tb)
box.append(Gtk.Frame(child=scroll_b))
# Metadata
box.append(Gtk.Label(label="Hint", xalign=0, css_classes=["heading"]))
th = Gtk.Entry()
if card: th.set_text(card.get("hint", ""))
box.append(th)
box.append(Gtk.Label(label="Tags", xalign=0, css_classes=["heading"]))
ent_tags = Gtk.Entry(); ent_tags.set_placeholder_text("tag1, tag2")
if card: ent_tags.set_text(", ".join(card.get("tags", [])))
box.append(ent_tags)
# --- MEDIA CONTROLS ---
self.temp_img = card.get("image") if card else None
self.temp_aud = card.get("audio") if card else None
# FIX: The create_media_row function now correctly appends items to the row!
def create_media_row(label_text, current_val, type_hint):
row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
lbl = Gtk.Label(label=os.path.basename(current_val) if current_val else f"No {label_text}", hexpand=True, xalign=0)
lbl.set_ellipsize(3)
btn_del = Gtk.Button(icon_name="user-trash-symbolic", visible=bool(current_val))
btn_add = Gtk.Button(icon_name="list-add-symbolic")
if type_hint == "image": btn_add.set_icon_name("insert-image-symbolic")
if type_hint == "audio": btn_add.set_icon_name("audio-volume-high-symbolic")
# CRITICAL FIX: Append the widgets to the row!
row.append(lbl)
row.append(btn_add)
row.append(btn_del)
return row, lbl, btn_add, btn_del
# Image Row
row_img, lbl_img, btn_img_add, btn_img_del = create_media_row("Image", self.temp_img, "image")
def on_img_del(b): self.temp_img = None; lbl_img.set_label("No Image"); btn_img_del.set_visible(False)
def on_img_pick(b): self.pick_file("image", lambda p: setattr(self, 'temp_img', p) or lbl_img.set_label(os.path.basename(p)) or btn_img_del.set_visible(True))
btn_img_del.connect("clicked", on_img_del); btn_img_add.connect("clicked", on_img_pick)
box.append(row_img)
# Audio Row
row_aud, lbl_aud, btn_aud_add, btn_aud_del = create_media_row("Audio", self.temp_aud, "audio")
def on_aud_del(b): self.temp_aud = None; lbl_aud.set_label("No Audio"); btn_aud_del.set_visible(False)
def on_aud_pick(b): self.pick_file("audio", lambda p: setattr(self, 'temp_aud', p) or lbl_aud.set_label(os.path.basename(p)) or btn_aud_del.set_visible(True))
btn_aud_del.connect("clicked", on_aud_del); btn_aud_add.connect("clicked", on_aud_pick)
box.append(row_aud)
# Unsuspend (Only for Edit Mode)
self.unsuspend_flag = False
if mode == "edit" and card.get("suspended"):
btn_unsus = Gtk.Button(label="Unsuspend Leech"); box.append(Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)); box.append(btn_unsus)
def unsus(b): self.unsuspend_flag = True; b.set_sensitive(False)
btn_unsus.connect("clicked", unsus)
# Layout Inversion (Scroll -> Clamp -> Content)
scroll_main = Gtk.ScrolledWindow()
scroll_main.set_propagate_natural_height(True)
scroll_main.set_max_content_height(500)
clamp = Adw.Clamp(maximum_size=600)
clamp.set_margin_start(12); clamp.set_margin_end(12); clamp.set_margin_bottom(12)
clamp.set_child(box)
scroll_main.set_child(clamp)
dialog.set_extra_child(scroll_main)
def on_response(d, r):
if r == "save":
buf_f = tf.get_buffer(); buf_b = tb.get_buffer()
s, e = buf_f.get_bounds(); new_f = buf_f.get_text(s, e, True).strip()
s, e = buf_b.get_bounds(); new_b = buf_b.get_text(s, e, True).strip()
tags = [t.strip() for t in ent_tags.get_text().split(",") if t.strip()]
hint_val = th.get_text().strip()
if new_f and new_b:
if mode == "add":
db.add_card_to_deck(self.filename, new_f, new_b, self.temp_img, self.temp_aud, tags, hint_val)
else:
is_suspended = not self.unsuspend_flag and card.get("suspended", False)
db.edit_card(self.filename, card["id"], new_f, new_b, self.temp_img, self.temp_aud, tags, is_suspended, hint_val)
self.refresh_list()
dialog.connect("response", on_response); dialog.present()
def pick_file(self, type_hint, callback):
if hasattr(Gtk, "FileDialog"):
d = Gtk.FileDialog(); f = Gtk.FileFilter()
if type_hint == "image": f.set_name("Images"); f.add_pixbuf_formats()
elif type_hint == "audio": f.set_name("Audio"); f.add_mime_type("audio/*")
filters = Gio.ListStore.new(Gtk.FileFilter); filters.append(f); d.set_filters(filters); d.set_default_filter(f)
def on_o(f, r):
try:
res = f.open_finish(r)
if res: callback(res.get_path())
except: pass
d.open(self.get_root(), None, on_o)
def confirm_delete(self, card):
# 1. Create the Dialog
# We use the card content in the body to be specific about what is being deleted
front_text = card.get("front", "this card")
if len(front_text) > 30: front_text = front_text[:30] + "..."
dialog = Adw.MessageDialog(
heading="Delete Card?",
body=f"Are you sure you want to delete '{front_text}'? This cannot be undone.",
transient_for=self.get_root()
)
# 2. Add Responses
dialog.add_response("cancel", "Cancel")
dialog.add_response("delete", "Delete")
# 3. Style the "Delete" button as destructive (red)
dialog.set_response_appearance("delete", Adw.ResponseAppearance.DESTRUCTIVE)
dialog.set_default_response("cancel")
dialog.set_close_response("cancel")
# 4. Handle Response
def on_response(d, response):
if response == "delete":
self.on_delete_clicked(card.get("id"))
d.close()
dialog.connect("response", on_response)
dialog.present()