Refactor app layout and load defaults from config

This commit is contained in:
lm
2025-10-15 18:16:18 +02:00
parent fc821f64aa
commit 02255f5dee
14 changed files with 109 additions and 29 deletions
+14
View File
@@ -0,0 +1,14 @@
"""GUI-related mixins and helpers for the application."""
from .color_picker import ColorPickerMixin
from .exclusions import ExclusionMixin
from .theme import ThemeMixin, HAS_TTKBOOTSTRAP
from .ui import UIBuilderMixin
__all__ = [
"ColorPickerMixin",
"ExclusionMixin",
"ThemeMixin",
"HAS_TTKBOOTSTRAP",
"UIBuilderMixin",
]
+60
View File
@@ -0,0 +1,60 @@
"""Color selection utilities."""
from __future__ import annotations
import colorsys
from tkinter import colorchooser, messagebox
class ColorPickerMixin:
"""Handles colour selection from dialogs and mouse clicks."""
ref_hue: float | None
def choose_color(self):
rgb, hex_colour = colorchooser.askcolor(title="Farbe wählen")
if rgb is None:
return
r, g, b = [int(channel) for channel in rgb]
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
self.ref_hue = h * 360.0
span = 30
self.hue_min.set((self.ref_hue - span) % 360)
self.hue_max.set((self.ref_hue + span) % 360)
self.update_preview()
self.status.config(text=f"Farbe gewählt: {hex_colour} (Hue {self.ref_hue:.1f}°)")
def enable_pick_mode(self):
if self.preview_img is None:
messagebox.showinfo("Info", "Bitte zuerst ein Bild laden.")
return
self.pick_mode = True
self.status.config(text="Pick-Modus: Klicke links ins Bild, um Farbe zu wählen (Esc beendet)")
def disable_pick_mode(self, event=None):
if self.pick_mode:
self.pick_mode = False
self.status.config(text="Pick-Modus beendet.")
def on_canvas_click(self, event):
if not self.pick_mode or self.preview_img is None:
return
x = int(event.x)
y = int(event.y)
if x < 0 or y < 0 or x >= self.preview_img.width or y >= self.preview_img.height:
return
r, g, b, a = self.preview_img.getpixel((x, y))
if a == 0:
return
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
self.ref_hue = h * 360.0
span = 30
self.hue_min.set((self.ref_hue - span) % 360)
self.hue_max.set((self.ref_hue + span) % 360)
self.disable_pick_mode()
self.update_preview()
self.status.config(text=f"Farbe vom Bild gewählt: Hue {self.ref_hue:.1f}°")
__all__ = ["ColorPickerMixin"]
+57
View File
@@ -0,0 +1,57 @@
"""Mouse handlers for exclusion rectangles."""
from __future__ import annotations
class ExclusionMixin:
"""Manage exclusion rectangles drawn on the preview canvas."""
def _exclude_start(self, event):
if self.preview_img is None:
return
x = max(0, min(self.preview_img.width - 1, int(event.x)))
y = max(0, min(self.preview_img.height - 1, int(event.y)))
self._rubber_start = (x, y)
if self._rubber_id:
try:
self.canvas_orig.delete(self._rubber_id)
except Exception:
pass
self._rubber_id = self.canvas_orig.create_rectangle(x, y, x, y, outline="yellow", width=2)
def _exclude_drag(self, event):
if not self._rubber_start:
return
x0, y0 = self._rubber_start
x1 = max(0, min(self.preview_img.width - 1, int(event.x)))
y1 = max(0, min(self.preview_img.height - 1, int(event.y)))
self.canvas_orig.coords(self._rubber_id, x0, y0, x1, y1)
def _exclude_end(self, event):
if not self._rubber_start:
return
x0, y0 = self._rubber_start
x1 = max(0, min(self.preview_img.width - 1, int(event.x)))
y1 = max(0, min(self.preview_img.height - 1, int(event.y)))
rx0, rx1 = sorted((x0, x1))
ry0, ry1 = sorted((y0, y1))
if (rx1 - rx0) > 0 and (ry1 - ry0) > 0:
self.exclude_rects.append((rx0, ry0, rx1, ry1))
self._rubber_start = None
self._rubber_id = None
self.update_preview()
def clear_excludes(self):
self.exclude_rects = []
self.canvas_orig.delete("all")
if self.preview_tk:
self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)
self.update_preview()
def undo_exclude(self):
if self.exclude_rects:
self.exclude_rects.pop()
self.update_preview()
__all__ = ["ExclusionMixin"]
+104
View File
@@ -0,0 +1,104 @@
"""Theme and window helpers."""
from __future__ import annotations
import platform
from tkinter import ttk
try:
import ttkbootstrap as tb # type: ignore
HAS_TTKBOOTSTRAP = True
except Exception: # pragma: no cover - optional dependency
tb = None
HAS_TTKBOOTSTRAP = False
try:
import winreg
except Exception: # pragma: no cover - platform-specific
winreg = None # type: ignore
class ThemeMixin:
"""Provides theme handling utilities for the main application."""
theme: str
style: ttk.Style
using_tb: bool
scale_style: str
def init_theme(self) -> None:
"""Initialise ttk style handling and apply the detected theme."""
if HAS_TTKBOOTSTRAP:
self.style = tb.Style()
self.using_tb = True
else:
self.style = ttk.Style()
self.style.theme_use("clam")
self.using_tb = False
self.theme = "light"
self.apply_theme(self.detect_system_theme())
def apply_theme(self, mode: str) -> None:
"""Apply light/dark theme including widget palette."""
mode = (mode or "light").lower()
self.theme = "dark" if mode == "dark" else "light"
if HAS_TTKBOOTSTRAP:
try:
theme_name = "darkly" if self.theme == "dark" else "flatly"
self.style.theme_use(theme_name)
except Exception:
pass
self.scale_style = (
"info.Horizontal.TScale" if self.theme == "dark" else "primary.Horizontal.TScale"
)
else:
self.scale_style = "Horizontal.TScale"
bg, fg = ("#0f0f10", "#f1f1f1") if self.theme == "dark" else ("#f2f2f7", "#202020")
self.root.configure(bg=bg) # type: ignore[attr-defined]
s = self.style
s.configure("TFrame", background=bg)
s.configure("TLabel", background=bg, foreground=fg, font=("Segoe UI", 10))
if not HAS_TTKBOOTSTRAP:
s.configure(
"TButton", padding=8, relief="flat", background="#e0e0e0", foreground=fg, font=("Segoe UI", 10)
)
s.map("TButton", background=[("active", "#d0d0d0")])
def detect_system_theme(self) -> str:
"""Best-effort detection of the OS theme preference."""
try:
if platform.system() == "Windows" and winreg is not None:
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
)
value, _ = winreg.QueryValueEx(key, "AppsUseLightTheme")
return "light" if int(value) == 1 else "dark"
except Exception:
pass
return "light"
def bring_to_front(self) -> None:
"""Try to focus the window and raise it to the foreground."""
try:
self.root.lift()
self.root.focus_force()
self.root.attributes("-topmost", True)
self.root.update()
self.root.attributes("-topmost", False)
except Exception:
pass
def toggle_theme(self) -> None:
"""Toggle between light and dark themes."""
next_mode = "dark" if self.theme == "light" else "light"
self.apply_theme(next_mode)
self.update_preview() # type: ignore[attr-defined]
__all__ = ["ThemeMixin", "HAS_TTKBOOTSTRAP"]
+151
View File
@@ -0,0 +1,151 @@
"""UI helpers and reusable Tk callbacks."""
from __future__ import annotations
import tkinter as tk
from tkinter import ttk
from .theme import HAS_TTKBOOTSTRAP
class UIBuilderMixin:
"""Constructs the Tkinter UI and common widgets."""
def setup_ui(self) -> None:
toolbar = ttk.Frame(self.root)
toolbar.pack(fill=tk.X, padx=12, pady=8)
buttons = [
("📂 Bild laden", self.load_image),
("🎨 Farbe wählen", self.choose_color),
("🖱️Farbe aus Bild klicken", self.enable_pick_mode),
("💾 Overlay speichern", self.save_overlay),
("🧹 Excludes löschen", self.clear_excludes),
("↩️ Letztes Exclude entfernen", self.undo_exclude),
("🔄 Slider zurücksetzen", self.reset_sliders),
("🌓 Theme umschalten", self.toggle_theme),
]
for text, command in buttons:
if HAS_TTKBOOTSTRAP:
from ttkbootstrap import Button # type: ignore
Button(toolbar, text=text, command=command, bootstyle="secondary").pack(side=tk.LEFT, padx=6)
else:
ttk.Button(toolbar, text=text, command=command).pack(side=tk.LEFT, padx=6)
sliders_frame = ttk.Frame(self.root)
sliders_frame.pack(fill=tk.X, padx=12, pady=4)
sliders = [
("Hue Min (°)", self.hue_min, 0, 360),
("Hue Max (°)", self.hue_max, 0, 360),
("Sättigung Min (%)", self.sat_min, 0, 100),
("Helligkeit Min (%)", self.val_min, 0, 100),
("Helligkeit Max (%)", self.val_max, 0, 100),
("Overlay Alpha", self.alpha, 0, 255),
]
for index, (label, variable, minimum, maximum) in enumerate(sliders):
self.add_slider_with_value(sliders_frame, label, variable, minimum, maximum, column=index)
sliders_frame.grid_columnconfigure(index, weight=1)
main = ttk.Frame(self.root)
main.pack(fill=tk.BOTH, expand=True, padx=12, pady=12)
self.canvas_orig = tk.Canvas(main, bg="#1e1e1e", highlightthickness=0, relief="flat")
self.canvas_orig.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 6))
self.canvas_orig.bind("<Button-1>", self.on_canvas_click)
self.canvas_orig.bind("<ButtonPress-3>", self._exclude_start)
self.canvas_orig.bind("<B3-Motion>", self._exclude_drag)
self.canvas_orig.bind("<ButtonRelease-3>", self._exclude_end)
self.canvas_overlay = tk.Canvas(main, bg="#1e1e1e", highlightthickness=0, relief="flat")
self.canvas_overlay.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=(6, 0))
status_frame = ttk.Frame(self.root)
status_frame.pack(fill=tk.X, padx=12, pady=6)
self.status = ttk.Label(status_frame, text="Keine Datei geladen.")
self.status.pack(anchor="w")
self._attach_copy_menu(self.status)
self.ratio_label = ttk.Label(status_frame, text="Purple (mit Excludes): —")
self.ratio_label.pack(anchor="w")
self._attach_copy_menu(self.ratio_label)
self.hint = ttk.Label(
status_frame,
text="Tipp: Rechtsklick + Ziehen auf dem linken Bild, um Bereiche auszuschließen. Esc beendet den Pick-Modus.",
)
self.hint.pack(anchor="w", pady=(2, 0))
self._attach_copy_menu(self.hint)
self.root.bind("<Escape>", self.disable_pick_mode)
def add_slider_with_value(self, parent, text, var, minimum, maximum, column=0):
cell = ttk.Frame(parent)
cell.grid(row=0, column=column, sticky="we", padx=6)
header = ttk.Frame(cell)
header.pack(fill="x")
name_lbl = ttk.Label(header, text=text)
name_lbl.pack(side="left")
self._attach_copy_menu(name_lbl)
val_lbl = ttk.Label(header, text=f"{float(var.get()):.0f}")
val_lbl.pack(side="right")
self._attach_copy_menu(val_lbl)
style_name = getattr(self, "scale_style", "Horizontal.TScale")
ttk.Scale(
cell,
from_=minimum,
to=maximum,
orient="horizontal",
variable=var,
style=style_name,
command=self.on_slider_change,
).pack(fill="x", pady=(2, 8))
def on_var_change(*_):
val_lbl.config(text=f"{float(var.get()):.0f}")
try:
var.trace_add("write", on_var_change)
except Exception:
var.trace("w", lambda *_: on_var_change()) # type: ignore[attr-defined]
def on_slider_change(self, *_):
if self._update_job is not None:
try:
self.root.after_cancel(self._update_job)
except Exception:
pass
self._update_job = self.root.after(self.update_delay_ms, self.update_preview)
def _init_copy_menu(self):
self._copy_target = None
self.copy_menu = tk.Menu(self.root, tearoff=0)
self.copy_menu.add_command(label="Kopieren", command=self._copy_current_label)
def _attach_copy_menu(self, widget):
widget.bind("<Button-3>", lambda event, w=widget: self._show_copy_menu(event, w))
widget.bind("<Control-c>", lambda event, w=widget: self._copy_widget_text(w))
def _show_copy_menu(self, event, widget):
self._copy_target = widget
try:
self.copy_menu.tk_popup(event.x_root, event.y_root)
finally:
self.copy_menu.grab_release()
def _copy_current_label(self):
if self._copy_target is not None:
self._copy_widget_text(self._copy_target)
def _copy_widget_text(self, widget):
try:
text = widget.cget("text")
except Exception:
text = ""
if not text:
return
try:
self.root.clipboard_clear()
self.root.clipboard_append(text)
except Exception:
pass
__all__ = ["UIBuilderMixin"]