2025-09-21 19:28:17 +02:00
|
|
|
import os
|
2025-09-11 18:59:22 +02:00
|
|
|
import sys
|
2025-09-21 19:28:17 +02:00
|
|
|
import subprocess
|
2025-09-08 13:22:10 +02:00
|
|
|
import tkinter as tk
|
2025-09-24 14:30:23 +02:00
|
|
|
from tkinter import ttk, simpledialog, messagebox, filedialog
|
|
|
|
|
from pathlib import Path
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
from src.core.image import load_icon
|
2025-09-24 06:58:13 +02:00
|
|
|
from src.core.file_manager import FileManager
|
2025-09-24 14:30:23 +02:00
|
|
|
from src.core.tag_manager import TagManager
|
|
|
|
|
from src.core.file import File
|
|
|
|
|
from src.core.tag import Tag
|
2025-09-08 13:22:10 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
class TagSelectionDialog(tk.Toplevel):
|
|
|
|
|
def __init__(self, parent, tags: list[str]):
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
self.title("Vyber tagy")
|
|
|
|
|
self.selected_tags = []
|
|
|
|
|
self.vars = {}
|
|
|
|
|
|
|
|
|
|
tk.Label(self, text="Vyber tagy k přiřazení:").pack(pady=5)
|
|
|
|
|
|
|
|
|
|
frame = tk.Frame(self)
|
|
|
|
|
frame.pack(padx=10, pady=5)
|
|
|
|
|
|
|
|
|
|
for tag in tags:
|
|
|
|
|
var = tk.BooleanVar(value=False)
|
|
|
|
|
chk = tk.Checkbutton(frame, text=tag, variable=var)
|
|
|
|
|
chk.pack(anchor="w")
|
|
|
|
|
self.vars[tag] = var
|
|
|
|
|
|
|
|
|
|
btn_frame = tk.Frame(self)
|
|
|
|
|
btn_frame.pack(pady=5)
|
|
|
|
|
tk.Button(btn_frame, text="OK", command=self.on_ok).pack(side="left", padx=5)
|
|
|
|
|
tk.Button(btn_frame, text="Cancel", command=self.destroy).pack(side="left", padx=5)
|
|
|
|
|
|
|
|
|
|
self.transient(parent)
|
|
|
|
|
self.grab_set()
|
|
|
|
|
parent.wait_window(self)
|
|
|
|
|
|
|
|
|
|
def on_ok(self):
|
|
|
|
|
self.selected_tags = [tag for tag, var in self.vars.items() if var.get()]
|
|
|
|
|
self.destroy()
|
2025-09-17 06:46:05 +02:00
|
|
|
|
2025-09-21 19:28:17 +02:00
|
|
|
class App:
|
2025-09-24 14:30:23 +02:00
|
|
|
def __init__(self, filehandler: FileManager, tagmanager: TagManager):
|
|
|
|
|
self.states = {} # tree states (checkboxy)
|
|
|
|
|
self.listbox_map = {} # filename -> list[File]
|
2025-09-21 19:28:17 +02:00
|
|
|
self.selected_tree_item_for_context = None
|
|
|
|
|
self.selected_list_index_for_context = None
|
2025-09-24 06:58:13 +02:00
|
|
|
self.filehandler = filehandler
|
2025-09-24 14:30:23 +02:00
|
|
|
self.tagmanager = tagmanager
|
|
|
|
|
|
|
|
|
|
# callback z FileManageru
|
|
|
|
|
self.filehandler.on_files_changed = self.update_files_from_manager
|
2025-09-21 19:28:17 +02:00
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# MAIN GUI
|
|
|
|
|
# ==================================================
|
2025-09-24 14:30:23 +02:00
|
|
|
def main(self):
|
2025-09-17 06:46:05 +02:00
|
|
|
root = tk.Tk()
|
2025-09-24 06:58:13 +02:00
|
|
|
root.title("Tagger")
|
2025-09-21 19:28:17 +02:00
|
|
|
root.geometry("900x600")
|
|
|
|
|
self.root = root
|
|
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
# ---- Ikony
|
2025-09-21 19:28:17 +02:00
|
|
|
unchecked = load_icon("src/resources/images/32/32_unchecked.png")
|
|
|
|
|
checked = load_icon("src/resources/images/32/32_checked.png")
|
|
|
|
|
self.icons = {"unchecked": unchecked, "checked": checked}
|
|
|
|
|
root.unchecked_img = unchecked
|
|
|
|
|
root.checked_img = checked
|
2025-09-17 06:46:05 +02:00
|
|
|
|
2025-09-21 19:28:17 +02:00
|
|
|
# ---- Layout
|
2025-09-17 06:46:05 +02:00
|
|
|
menu_bar = tk.Menu(root)
|
|
|
|
|
root.config(menu=menu_bar)
|
|
|
|
|
file_menu = tk.Menu(menu_bar, tearoff=0)
|
2025-09-24 14:30:23 +02:00
|
|
|
file_menu.add_command(label="Open Folder...", command=self.open_folder_dialog)
|
|
|
|
|
file_menu.add_separator()
|
2025-09-21 19:28:17 +02:00
|
|
|
file_menu.add_command(label="Exit", command=root.quit)
|
|
|
|
|
menu_bar.add_cascade(label="File", menu=file_menu)
|
2025-09-17 06:46:05 +02:00
|
|
|
|
|
|
|
|
main_frame = tk.Frame(root)
|
|
|
|
|
main_frame.pack(fill="both", expand=True)
|
|
|
|
|
main_frame.columnconfigure(0, weight=1)
|
|
|
|
|
main_frame.columnconfigure(1, weight=2)
|
|
|
|
|
main_frame.rowconfigure(0, weight=1)
|
|
|
|
|
|
2025-09-21 19:28:17 +02:00
|
|
|
# ---- Tree (left)
|
|
|
|
|
self.tree = ttk.Treeview(main_frame)
|
|
|
|
|
self.tree.grid(row=0, column=0, sticky="nsew", padx=4, pady=4)
|
|
|
|
|
self.tree.bind("<Button-1>", self.on_tree_left_click)
|
|
|
|
|
self.tree.bind("<Button-3>", self.on_tree_right_click)
|
|
|
|
|
|
|
|
|
|
# ---- Listbox (right)
|
2025-09-24 14:30:23 +02:00
|
|
|
self.listbox = tk.Listbox(main_frame, selectmode="extended")
|
2025-09-21 19:28:17 +02:00
|
|
|
self.listbox.grid(row=0, column=1, sticky="nsew", padx=4, pady=4)
|
|
|
|
|
self.listbox.bind("<Double-1>", self.on_list_double)
|
|
|
|
|
self.listbox.bind("<Button-3>", self.on_list_right_click)
|
|
|
|
|
|
|
|
|
|
# ---- Status bar
|
|
|
|
|
self.status_bar = tk.Label(root, text="Připraven", anchor="w", relief="sunken")
|
|
|
|
|
self.status_bar.pack(side="bottom", fill="x")
|
|
|
|
|
|
|
|
|
|
# ---- Context menus
|
|
|
|
|
self.tree_menu = tk.Menu(root, tearoff=0)
|
|
|
|
|
self.tree_menu.add_command(label="Nový tag zde", command=self.tree_add_tag)
|
|
|
|
|
self.tree_menu.add_command(label="Smazat tag", command=self.tree_delete_tag)
|
|
|
|
|
|
|
|
|
|
self.list_menu = tk.Menu(root, tearoff=0)
|
|
|
|
|
self.list_menu.add_command(label="Otevřít soubor", command=self.list_open_file)
|
|
|
|
|
self.list_menu.add_command(label="Smazat z indexu", command=self.list_remove_file)
|
2025-09-24 14:30:23 +02:00
|
|
|
self.list_menu.add_command(label="Assign Tag", command=self.assign_tag_to_selected)
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
# ---- Root node
|
|
|
|
|
root_id = self.tree.insert("", "end", text="Štítky")
|
2025-09-21 19:28:17 +02:00
|
|
|
self.states[root_id] = False
|
|
|
|
|
self.tree.item(root_id, open=True)
|
2025-09-24 14:30:23 +02:00
|
|
|
self.root_id = root_id
|
|
|
|
|
|
|
|
|
|
# ⚡ refresh listbox při startu
|
|
|
|
|
self.update_files_from_manager(self.filehandler.filelist)
|
2025-09-21 19:28:17 +02:00
|
|
|
|
|
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
2025-09-24 14:30:23 +02:00
|
|
|
# FILE REFRESH + MAP
|
2025-09-21 19:28:17 +02:00
|
|
|
# ==================================================
|
2025-09-24 14:30:23 +02:00
|
|
|
def update_files_from_manager(self, filelist):
|
|
|
|
|
self.listbox.delete(0, "end")
|
|
|
|
|
self.listbox_map = {}
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
checked_tags = self.get_checked_full_tags()
|
|
|
|
|
for f in filelist:
|
|
|
|
|
# ignoruj metadata soubory
|
|
|
|
|
if f.file_path.name.endswith(".!tag") or f.file_path.name.startswith("."):
|
|
|
|
|
continue
|
|
|
|
|
if not checked_tags or checked_tags.issubset(set(f.tags)):
|
|
|
|
|
filename = f.file_path.name
|
|
|
|
|
self.listbox.insert("end", filename)
|
|
|
|
|
if filename not in self.listbox_map:
|
|
|
|
|
self.listbox_map[filename] = []
|
|
|
|
|
self.listbox_map[filename].append(f)
|
|
|
|
|
self.status_bar.config(text=f"Zobrazeno {self.listbox.size()} položek")
|
|
|
|
|
|
|
|
|
|
def get_selected_files_objects(self):
|
|
|
|
|
indices = self.listbox.curselection()
|
|
|
|
|
files = []
|
|
|
|
|
for idx in indices:
|
|
|
|
|
filename = self.listbox.get(idx)
|
|
|
|
|
files.extend(self.listbox_map.get(filename, []))
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# ASSIGN TAG
|
|
|
|
|
# ==================================================
|
|
|
|
|
def assign_tag_to_selected(self):
|
|
|
|
|
files = self.get_selected_files_objects()
|
|
|
|
|
if not files:
|
|
|
|
|
self.status_bar.config(text="Nebyly vybrány žádné soubory")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# generujeme seznam tagů přímo z TagManageru (aktuální)
|
|
|
|
|
all_tags = []
|
|
|
|
|
for category in self.tagmanager.get_categories():
|
|
|
|
|
for tag in self.tagmanager.get_tags_in_category(category):
|
|
|
|
|
all_tags.append(f"{category}/{tag.name}") # vždy aktuální seznam
|
|
|
|
|
|
|
|
|
|
if not all_tags:
|
|
|
|
|
messagebox.showwarning("Chyba", "Žádné tagy nejsou definovány")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# vytvoříme dialog **po načtení všech tagů**
|
|
|
|
|
dialog = TagSelectionDialog(self.root, all_tags)
|
|
|
|
|
selected_tags = dialog.selected_tags
|
|
|
|
|
|
|
|
|
|
if not selected_tags:
|
|
|
|
|
self.status_bar.config(text="Nebyl vybrán žádný tag")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# přiřazení tagů souborům
|
|
|
|
|
for full_tag in selected_tags:
|
|
|
|
|
if "/" in full_tag:
|
|
|
|
|
category, name = full_tag.split("/", 1)
|
|
|
|
|
self.filehandler.assign_tag_to_file_objects(files, Tag(category, name))
|
|
|
|
|
|
|
|
|
|
self.update_files_from_manager(self.filehandler.filelist)
|
|
|
|
|
self.status_bar.config(text=f"Přiřazeny tagy: {', '.join(selected_tags)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# DOUBLE CLICK OPEN
|
|
|
|
|
# ==================================================
|
|
|
|
|
def on_list_double(self, event):
|
|
|
|
|
for f in self.get_selected_files_objects():
|
|
|
|
|
self.open_file(f.file_path)
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# OPEN FILE
|
|
|
|
|
# ==================================================
|
|
|
|
|
def open_file(self, path):
|
|
|
|
|
try:
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
|
os.startfile(path)
|
|
|
|
|
elif sys.platform.startswith("darwin"):
|
|
|
|
|
subprocess.call(["open", path])
|
|
|
|
|
else:
|
|
|
|
|
subprocess.call(["xdg-open", path])
|
|
|
|
|
self.status_bar.config(text=f"Otevírám: {path}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
messagebox.showerror("Chyba", f"Nelze otevřít {path}: {e}")
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# LIST CONTEXT MENU
|
|
|
|
|
# ==================================================
|
|
|
|
|
def on_list_right_click(self, event):
|
|
|
|
|
idx = self.listbox.nearest(event.y)
|
|
|
|
|
if idx is None:
|
|
|
|
|
return
|
|
|
|
|
self.selected_list_index_for_context = idx
|
|
|
|
|
self.listbox.selection_clear(0, "end")
|
|
|
|
|
self.listbox.selection_set(idx)
|
|
|
|
|
self.list_menu.tk_popup(event.x_root, event.y_root)
|
|
|
|
|
|
|
|
|
|
def list_open_file(self):
|
|
|
|
|
for f in self.get_selected_files_objects():
|
|
|
|
|
self.open_file(f.file_path)
|
|
|
|
|
|
|
|
|
|
def list_remove_file(self):
|
|
|
|
|
files = self.get_selected_files_objects()
|
|
|
|
|
if not files:
|
|
|
|
|
return
|
|
|
|
|
ans = messagebox.askyesno("Smazat z indexu", f"Odstranit {len(files)} souborů z indexu?")
|
|
|
|
|
if ans:
|
|
|
|
|
for f in files:
|
|
|
|
|
if f in self.filehandler.filelist:
|
|
|
|
|
self.filehandler.filelist.remove(f)
|
|
|
|
|
self.update_files_from_manager(self.filehandler.filelist)
|
|
|
|
|
self.status_bar.config(text=f"Odstraněno {len(files)} souborů z indexu")
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# OPEN FOLDER
|
|
|
|
|
# ==================================================
|
|
|
|
|
def open_folder_dialog(self):
|
|
|
|
|
folder = filedialog.askdirectory(title="Vyber složku pro sledování")
|
|
|
|
|
if not folder:
|
|
|
|
|
return
|
|
|
|
|
folder_path = Path(folder)
|
|
|
|
|
try:
|
|
|
|
|
self.filehandler.append(folder_path)
|
|
|
|
|
# po načtení propíš tagy z metadat do TagManageru
|
|
|
|
|
for f in self.filehandler.filelist:
|
|
|
|
|
if f.tags and f.tagmanager:
|
|
|
|
|
for t in f.tags: # t je Tag
|
|
|
|
|
f.tagmanager.add_tag(t.category, t.name)
|
|
|
|
|
self.status_bar.config(text=f"Přidána složka: {folder_path}")
|
|
|
|
|
self.refresh_tree_tags()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
messagebox.showerror("Chyba", f"Nelze přidat složku {folder}: {e}")
|
2025-09-21 19:28:17 +02:00
|
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
|
# TREE EVENTS
|
|
|
|
|
# ==================================================
|
|
|
|
|
def on_tree_left_click(self, event):
|
|
|
|
|
region = self.tree.identify("region", event.x, event.y)
|
2025-09-24 14:30:23 +02:00
|
|
|
if region not in ("tree", "icon"):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
item_id = self.tree.identify_row(event.y)
|
|
|
|
|
if not item_id:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
item_text = self.tree.item(item_id, "text")
|
|
|
|
|
parent_id = self.tree.parent(item_id)
|
|
|
|
|
if parent_id == "":
|
|
|
|
|
# root nebo kategorie → jen toggle open/close
|
|
|
|
|
is_open = self.tree.item(item_id, "open")
|
|
|
|
|
self.tree.item(item_id, open=not is_open)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# pokud je to tag → toggle checkbox
|
|
|
|
|
self.states[item_id] = not self.states.get(item_id, False)
|
|
|
|
|
self.tree.item(item_id, image=self.icons["checked"] if self.states[item_id] else self.icons["unchecked"])
|
|
|
|
|
self.status_bar.config(text=f"Tag {'checked' if self.states[item_id] else 'unchecked'}: {self.build_full_tag(item_id)}")
|
|
|
|
|
self.update_files_from_manager(self.filehandler.filelist)
|
2025-09-21 19:28:17 +02:00
|
|
|
|
|
|
|
|
def on_tree_right_click(self, event):
|
|
|
|
|
item_id = self.tree.identify_row(event.y)
|
|
|
|
|
if item_id:
|
|
|
|
|
self.selected_tree_item_for_context = item_id
|
|
|
|
|
self.tree.selection_set(item_id)
|
|
|
|
|
self.tree_menu.tk_popup(event.x_root, event.y_root)
|
|
|
|
|
else:
|
|
|
|
|
menu = tk.Menu(self.root, tearoff=0)
|
|
|
|
|
menu.add_command(label="Nový top-level tag", command=lambda: self.tree_add_tag(background=True))
|
|
|
|
|
menu.tk_popup(event.x_root, event.y_root)
|
|
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
# ==================================================
|
|
|
|
|
# TREE TAG CRUD
|
|
|
|
|
# ==================================================
|
2025-09-21 19:28:17 +02:00
|
|
|
def tree_add_tag(self, background=False):
|
|
|
|
|
name = simpledialog.askstring("Nový tag", "Název tagu:")
|
|
|
|
|
if not name:
|
|
|
|
|
return
|
2025-09-24 14:30:23 +02:00
|
|
|
parent = self.selected_tree_item_for_context if not background else self.root_id
|
|
|
|
|
new_id = self.tree.insert(parent, "end", text=name)
|
2025-09-21 19:28:17 +02:00
|
|
|
self.states[new_id] = False
|
2025-09-24 14:30:23 +02:00
|
|
|
|
|
|
|
|
# ⚡ aktualizace TagManageru
|
|
|
|
|
if parent == self.root_id:
|
|
|
|
|
category = name
|
|
|
|
|
self.tagmanager.add_category(category)
|
|
|
|
|
else:
|
|
|
|
|
# tag pod existující kategorií
|
|
|
|
|
category = self.tree.item(parent, "text")
|
|
|
|
|
self.tagmanager.add_tag(category, name)
|
|
|
|
|
|
2025-09-21 19:28:17 +02:00
|
|
|
self.status_bar.config(text=f"Vytvořen tag: {self.build_full_tag(new_id)}")
|
|
|
|
|
|
|
|
|
|
def tree_delete_tag(self):
|
|
|
|
|
item = self.selected_tree_item_for_context
|
|
|
|
|
if not item:
|
|
|
|
|
return
|
|
|
|
|
full = self.build_full_tag(item)
|
|
|
|
|
ans = messagebox.askyesno("Smazat tag", f"Opravdu chcete smazat '{full}'?")
|
|
|
|
|
if not ans:
|
|
|
|
|
return
|
2025-09-24 14:30:23 +02:00
|
|
|
tag_name = self.tree.item(item, "text")
|
|
|
|
|
parent_id = self.tree.parent(item)
|
2025-09-21 19:28:17 +02:00
|
|
|
self.tree.delete(item)
|
|
|
|
|
self.states.pop(item, None)
|
2025-09-24 14:30:23 +02:00
|
|
|
|
|
|
|
|
# odebrání z TagManageru
|
|
|
|
|
if parent_id == self.root_id:
|
|
|
|
|
self.tagmanager.remove_category(tag_name)
|
|
|
|
|
else:
|
|
|
|
|
category = self.tree.item(parent_id, "text")
|
|
|
|
|
self.tagmanager.remove_tag(category, tag_name)
|
|
|
|
|
|
|
|
|
|
self.update_files_from_manager(self.filehandler.filelist)
|
2025-09-21 19:28:17 +02:00
|
|
|
self.status_bar.config(text=f"Smazán tag: {full}")
|
|
|
|
|
|
|
|
|
|
# ==================================================
|
2025-09-24 14:30:23 +02:00
|
|
|
# TREE HELPERS
|
2025-09-21 19:28:17 +02:00
|
|
|
# ==================================================
|
2025-09-24 14:30:23 +02:00
|
|
|
def build_full_tag(self, item_id):
|
|
|
|
|
parts = []
|
|
|
|
|
cur = item_id
|
|
|
|
|
while cur:
|
|
|
|
|
parts.append(self.tree.item(cur, "text"))
|
|
|
|
|
cur = self.tree.parent(cur)
|
|
|
|
|
parts.reverse()
|
|
|
|
|
return "/".join(parts) if parts else ""
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
def get_checked_full_tags(self):
|
|
|
|
|
return {self.build_full_tag(i) for i, v in self.states.items() if v}
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
def refresh_tree_tags(self):
|
|
|
|
|
# smažeme všechny pod root
|
|
|
|
|
for child in self.tree.get_children(self.root_id):
|
|
|
|
|
self.tree.delete(child)
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
# projdeme všechny kategorie a tagy
|
|
|
|
|
for category in self.tagmanager.get_categories():
|
|
|
|
|
cat_id = self.tree.insert(self.root_id, "end", text=category)
|
|
|
|
|
self.states[cat_id] = False
|
|
|
|
|
for tag in self.tagmanager.get_tags_in_category(category):
|
|
|
|
|
tag_id = self.tree.insert(cat_id, "end", text=tag.name, image=self.icons["unchecked"])
|
|
|
|
|
self.states[tag_id] = False
|
2025-09-21 19:28:17 +02:00
|
|
|
|
2025-09-24 14:30:23 +02:00
|
|
|
# otevřeme root
|
|
|
|
|
self.tree.item(self.root_id, open=True)
|