diff --git a/.gitignore b/.gitignore index 087c8ca..3d59451 100644 Binary files a/.gitignore and b/.gitignore differ diff --git a/README.md b/README.md index 99cf33f..0c4e604 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ICRA
Interactive Color Range Analyzer is being reimagined with a PySide6 user interface.
- This branch focuses on building a native desktop shell with modern window controls before porting the colour-analysis features. + This branch focuses on building a native desktop shell with modern window controls before porting the color-analysis features.
diff --git a/app/__init__.py b/app/__init__.py index f3c94c3..24a9a05 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,14 +2,8 @@ from __future__ import annotations -try: # Legacy Tk support remains optional - from .app import ICRAApp, start_app as start_tk_app # type: ignore[attr-defined] -except Exception: # pragma: no cover - ICRAApp = None # type: ignore[assignment] - start_tk_app = None # type: ignore[assignment] - from .qt import create_application as create_qt_app, run as run_qt_app start_app = run_qt_app -__all__ = ["ICRAApp", "start_tk_app", "create_qt_app", "run_qt_app", "start_app"] +__all__ = ["create_qt_app", "run_qt_app", "start_app"] diff --git a/app/app.py b/app/app.py deleted file mode 100644 index bbfe85c..0000000 --- a/app/app.py +++ /dev/null @@ -1,176 +0,0 @@ -"""Application composition root.""" - -from __future__ import annotations - -import ctypes -import platform -import tkinter as tk -from importlib import resources - -from .gui import ColorPickerMixin, ExclusionMixin, ThemeMixin, UIBuilderMixin -from .i18n import I18nMixin -from .logic import DEFAULTS, LANGUAGE, RESET_EXCLUSIONS_ON_IMAGE_CHANGE, ImageProcessingMixin, ResetMixin - - -class ICRAApp( - I18nMixin, - ThemeMixin, - UIBuilderMixin, - ImageProcessingMixin, - ExclusionMixin, - ColorPickerMixin, - ResetMixin, -): - """Tkinter based application for isolating configurable colour ranges.""" - - def __init__(self, root: tk.Tk): - self.root = root - self.init_i18n(LANGUAGE) - self.root.title(self._t("app.title")) - self._setup_window() - - # Theme and styling - self.init_theme() - - # Tkinter state variables - self.DEFAULTS = DEFAULTS.copy() - self.hue_min = tk.DoubleVar(value=self.DEFAULTS["hue_min"]) - self.hue_max = tk.DoubleVar(value=self.DEFAULTS["hue_max"]) - self.sat_min = tk.DoubleVar(value=self.DEFAULTS["sat_min"]) - self.val_min = tk.DoubleVar(value=self.DEFAULTS["val_min"]) - self.val_max = tk.DoubleVar(value=self.DEFAULTS["val_max"]) - self.alpha = tk.IntVar(value=self.DEFAULTS["alpha"]) - self.ref_hue = None - - # Debounce for heavy preview updates - self.update_delay_ms = 400 - self._update_job = None - - # Exclusion rectangles (preview coordinates) - self.exclude_shapes: list[dict[str, object]] = [] - self._rubber_start = None - self._rubber_id = None - self._stroke_preview_id = None - self.exclude_mode = "rect" - self.reset_exclusions_on_switch = RESET_EXCLUSIONS_ON_IMAGE_CHANGE - self._exclude_mask = None - self._exclude_mask_dirty = True - self._exclude_mask_px = None - self._exclude_canvas_ids: list[int] = [] - self._current_stroke: list[tuple[int, int]] | None = None - self.free_draw_width = 14 - self.pick_mode = False - - # Image references - self.image_path = None - self.orig_img = None - self.preview_img = None - self.preview_tk = None - self.overlay_tk = None - self.image_paths = [] - self.current_image_index = -1 - - # Build UI - self.setup_ui() - self._init_copy_menu() - self.bring_to_front() - - def _setup_window(self) -> None: - screen_width = self.root.winfo_screenwidth() - screen_height = self.root.winfo_screenheight() - default_width = int(screen_width * 0.8) - default_height = int(screen_height * 0.8) - default_x = (screen_width - default_width) // 2 - default_y = (screen_height - default_height) // 4 - self._window_geometry = f"{default_width}x{default_height}+{default_x}+{default_y}" - self._is_maximized = True - self._use_overrideredirect = True - self.root.geometry(f"{screen_width}x{screen_height}+0+0") - self.root.configure(bg="#f2f2f7") - try: - self.root.overrideredirect(True) - except Exception: - try: - self.root.attributes("-type", "splash") - except Exception: - pass - self._window_icon_ref = None - self._apply_window_icon() - self._init_window_chrome() - - def _ensure_taskbar_entry(self) -> None: - """Force the borderless window to show up in the Windows taskbar.""" - try: - if platform.system() != "Windows": - return - hwnd = self.root.winfo_id() - if not hwnd: - self.root.after(50, self._ensure_taskbar_entry) - return - - GWL_EXSTYLE = -20 - WS_EX_TOOLWINDOW = 0x00000080 - WS_EX_APPWINDOW = 0x00040000 - SWP_NOSIZE = 0x0001 - SWP_NOMOVE = 0x0002 - SWP_NOZORDER = 0x0004 - SWP_FRAMECHANGED = 0x0020 - - user32 = ctypes.windll.user32 # type: ignore[attr-defined] - shell32 = ctypes.windll.shell32 # type: ignore[attr-defined] - - style = user32.GetWindowLongW(hwnd, GWL_EXSTYLE) - new_style = (style & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW - if new_style != style: - user32.SetWindowLongW(hwnd, GWL_EXSTYLE, new_style) - user32.SetWindowPos( - hwnd, - 0, - 0, - 0, - 0, - 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED, - ) - - app_id = ctypes.c_wchar_p("ICRA.App") - shell32.SetCurrentProcessExplicitAppUserModelID(app_id) - except Exception: - pass - - def _apply_window_icon(self) -> None: - try: - icon_resource = resources.files("app.assets").joinpath("logo.png") - with resources.as_file(icon_resource) as icon_path: - icon = tk.PhotoImage(file=str(icon_path)) - self.root.iconphoto(False, icon) - self._window_icon_ref = icon - except Exception: - self._window_icon_ref = None - - def _init_window_chrome(self) -> None: - """Configure a borderless window while retaining a taskbar entry.""" - try: - self.root.bind("", self._restore_borderless) - self.root.after(0, self._restore_borderless) - self.root.after(0, self._ensure_taskbar_entry) - except Exception: - pass - - def _restore_borderless(self, _event=None) -> None: - try: - if self._use_overrideredirect: - self.root.overrideredirect(True) - self._ensure_taskbar_entry() - except Exception: - pass - - -def start_app() -> None: - """Entry point used by the CLI script.""" - root = tk.Tk() - app = ICRAApp(root) - root.mainloop() - - -__all__ = ["ICRAApp", "start_app"] diff --git a/app/gui/__init__.py b/app/gui/__init__.py deleted file mode 100644 index f6e1550..0000000 --- a/app/gui/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -"""GUI-related mixins and helpers for the application.""" - -from .color_picker import ColorPickerMixin -from .exclusions import ExclusionMixin -from .theme import ThemeMixin -from .ui import UIBuilderMixin - -__all__ = [ - "ColorPickerMixin", - "ExclusionMixin", - "ThemeMixin", - "UIBuilderMixin", -] diff --git a/app/gui/color_picker.py b/app/gui/color_picker.py deleted file mode 100644 index f7f945e..0000000 --- a/app/gui/color_picker.py +++ /dev/null @@ -1,158 +0,0 @@ -"""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 - hue_span: float = 45.0 # degrees around the picked hue - selected_colour: tuple[int, int, int] | None = None - - def choose_color(self): - title = self._t("dialog.choose_colour_title") if hasattr(self, "_t") else "Choose colour" - rgb, hex_colour = colorchooser.askcolor(title=title) - if rgb is None: - return - r, g, b = (int(round(channel)) for channel in rgb) - hue_deg, sat_pct, val_pct = self._apply_rgb_selection(r, g, b) - label = hex_colour or f"RGB({r}, {g}, {b})" - message = self._t( - "status.color_selected", - label=label, - hue=hue_deg, - saturation=sat_pct, - value=val_pct, - ) - self.status.config(text=message) - self._update_selected_colour(r, g, b) - - def apply_sample_colour(self, hex_colour: str, name: str | None = None) -> None: - """Apply a predefined colour preset.""" - rgb = self._parse_hex_colour(hex_colour) - if rgb is None: - return - hue_deg, sat_pct, val_pct = self._apply_rgb_selection(*rgb) - if self.pick_mode: - self.pick_mode = False - label = name or hex_colour.upper() - message = self._t( - "status.sample_colour", - label=label, - hex_code=hex_colour, - hue=hue_deg, - saturation=sat_pct, - value=val_pct, - ) - self.status.config(text=message) - self._update_selected_colour(*rgb) - - def enable_pick_mode(self): - if self.preview_img is None: - messagebox.showinfo( - self._t("dialog.info_title"), - self._t("dialog.load_image_first"), - ) - return - self.pick_mode = True - self.status.config(text=self._t("status.pick_mode_ready")) - - def disable_pick_mode(self, event=None): - if self.pick_mode: - self.pick_mode = False - self.status.config(text=self._t("status.pick_mode_ended")) - - 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 - hue_deg, sat_pct, val_pct = self._apply_rgb_selection(r, g, b) - self.disable_pick_mode() - self.status.config( - text=self._t( - "status.pick_mode_from_image", - hue=hue_deg, - saturation=sat_pct, - value=val_pct, - ) - ) - self._update_selected_colour(r, g, b) - - def _apply_rgb_selection(self, r: int, g: int, b: int) -> tuple[float, float, float]: - """Update slider ranges based on an RGB colour and return HSV summary.""" - h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0) - hue_deg = (h * 360.0) % 360.0 - self.ref_hue = hue_deg - self._set_slider_targets(hue_deg, s, v) - self.update_preview() - return hue_deg, s * 100.0, v * 100.0 - - def _update_selected_colour(self, r: int, g: int, b: int) -> None: - self.selected_colour = (r, g, b) - hex_colour = f"#{r:02x}{g:02x}{b:02x}" - if hasattr(self, "current_colour_sw"): - try: - self.current_colour_sw.configure(background=hex_colour) - except Exception: - pass - if hasattr(self, "current_colour_label"): - try: - self.current_colour_label.configure(text=f"({hex_colour})") - except Exception: - pass - - def _set_slider_targets(self, hue_deg: float, saturation: float, value: float) -> None: - span = getattr(self, "hue_span", 45.0) - self.hue_min.set((hue_deg - span) % 360) - self.hue_max.set((hue_deg + span) % 360) - - sat_pct = saturation * 100.0 - sat_margin = 35.0 - sat_min = max(0.0, min(100.0, sat_pct - sat_margin)) - if saturation <= 0.05: - sat_min = 0.0 - self.sat_min.set(sat_min) - - v_pct = value * 100.0 - val_margin = 35.0 - val_min = max(0.0, v_pct - val_margin) - val_max = min(100.0, v_pct + val_margin) - if value <= 0.15: - val_max = min(45.0, max(val_max, 25.0)) - if value >= 0.85: - val_min = max(55.0, min(val_min, 80.0)) - if val_max <= val_min: - val_max = min(100.0, val_min + 10.0) - self.val_min.set(val_min) - self.val_max.set(val_max) - - @staticmethod - def _parse_hex_colour(hex_colour: str | None) -> tuple[int, int, int] | None: - if not hex_colour: - return None - value = hex_colour.strip().lstrip("#") - if len(value) == 3: - value = "".join(ch * 2 for ch in value) - if len(value) != 6: - return None - try: - r = int(value[0:2], 16) - g = int(value[2:4], 16) - b = int(value[4:6], 16) - except ValueError: - return None - return r, g, b - - -__all__ = ["ColorPickerMixin"] diff --git a/app/gui/exclusions.py b/app/gui/exclusions.py deleted file mode 100644 index 639eddb..0000000 --- a/app/gui/exclusions.py +++ /dev/null @@ -1,207 +0,0 @@ -"""Mouse handlers for exclusion shapes.""" - -from __future__ import annotations - - -class ExclusionMixin: - """Manage exclusion shapes (rectangles and freehand strokes) on the preview canvas.""" - - def _exclude_start(self, event): - if self.preview_img is None: - return - mode = getattr(self, "exclude_mode", "rect") - x = max(0, min(self.preview_img.width - 1, int(event.x))) - y = max(0, min(self.preview_img.height - 1, int(event.y))) - if mode == "free": - self._current_stroke = [(x, y)] - preview_id = getattr(self, "_stroke_preview_id", None) - if preview_id: - try: - self.canvas_orig.delete(preview_id) - except Exception: - pass - accent = self._exclusion_preview_colour() - self._stroke_preview_id = self.canvas_orig.create_line( - x, - y, - x, - y, - fill=accent, - width=2, - smooth=True, - capstyle="round", - joinstyle="round", - ) - self._rubber_start = None - return - self._rubber_start = (x, y) - if self._rubber_id: - try: - self.canvas_orig.delete(self._rubber_id) - except Exception: - pass - accent = self._exclusion_preview_colour() - self._rubber_id = self.canvas_orig.create_rectangle(x, y, x, y, outline=accent, width=2) - - def _exclude_drag(self, event): - mode = getattr(self, "exclude_mode", "rect") - if mode == "free": - stroke = getattr(self, "_current_stroke", None) - if not stroke: - 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))) - if stroke[-1] != (x, y): - stroke.append((x, y)) - preview_id = getattr(self, "_stroke_preview_id", None) - if preview_id: - coords = [coord for point in stroke for coord in point] - self.canvas_orig.coords(preview_id, *coords) - return - 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): - mode = getattr(self, "exclude_mode", "rect") - if mode == "free": - stroke = getattr(self, "_current_stroke", None) - if stroke and len(stroke) > 2: - polygon = self._close_polygon(self._compress_stroke(stroke)) - if len(polygon) >= 3: - shape = { - "kind": "polygon", - "points": polygon, - } - self.exclude_shapes.append(shape) - stamper = getattr(self, "_stamp_shape_on_mask", None) - if callable(stamper): - stamper(shape) - else: - self._exclude_mask_dirty = True - self._current_stroke = None - preview_id = getattr(self, "_stroke_preview_id", None) - if preview_id: - try: - self.canvas_orig.delete(preview_id) - except Exception: - pass - self._stroke_preview_id = None - self.update_preview() - return - 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: - shape = {"kind": "rect", "coords": (rx0, ry0, rx1, ry1)} - self.exclude_shapes.append(shape) - stamper = getattr(self, "_stamp_shape_on_mask", None) - if callable(stamper): - stamper(shape) - else: - self._exclude_mask_dirty = True - if self._rubber_id: - try: - self.canvas_orig.delete(self._rubber_id) - except Exception: - pass - self._rubber_start = None - self._rubber_id = None - self.update_preview() - - def clear_excludes(self): - self.exclude_shapes = [] - self._rubber_start = None - self._current_stroke = None - if self._rubber_id: - try: - self.canvas_orig.delete(self._rubber_id) - except Exception: - pass - self._rubber_id = None - if self._stroke_preview_id: - try: - self.canvas_orig.delete(self._stroke_preview_id) - except Exception: - pass - self._stroke_preview_id = None - for item in getattr(self, "_exclude_canvas_ids", []): - try: - self.canvas_orig.delete(item) - except Exception: - pass - self._exclude_canvas_ids = [] - self._exclude_mask = None - self._exclude_mask_px = None - self._exclude_mask_dirty = True - self.update_preview() - - def undo_exclude(self): - if not getattr(self, "exclude_shapes", None): - return - self.exclude_shapes.pop() - self._exclude_mask_dirty = True - self.update_preview() - - def toggle_exclusion_mode(self): - current = getattr(self, "exclude_mode", "rect") - next_mode = "free" if current == "rect" else "rect" - self.exclude_mode = next_mode - self._current_stroke = None - if next_mode == "free": - if self._rubber_id: - try: - self.canvas_orig.delete(self._rubber_id) - except Exception: - pass - self._rubber_id = None - self._rubber_start = None - else: - if self._stroke_preview_id: - try: - self.canvas_orig.delete(self._stroke_preview_id) - except Exception: - pass - self._stroke_preview_id = None - self._rubber_id = None - message_key = "status.free_draw_enabled" if next_mode == "free" else "status.free_draw_disabled" - if hasattr(self, "status"): - try: - self.status.config(text=self._t(message_key)) - except Exception: - pass - - @staticmethod - def _compress_stroke(points: list[tuple[int, int]]) -> list[tuple[int, int]]: - """Reduce duplicate points without altering the drawn path too much.""" - if not points: - return [] - compressed: list[tuple[int, int]] = [points[0]] - for point in points[1:]: - if point != compressed[-1]: - compressed.append(point) - return compressed - - def _exclusion_preview_colour(self) -> str: - is_dark = getattr(self, "theme", "light") == "dark" - return "#ffd700" if is_dark else "#c56217" - - @staticmethod - def _close_polygon(points: list[tuple[int, int]]) -> list[tuple[int, int]]: - """Ensure the polygon is closed by repeating the start if necessary.""" - if len(points) < 3: - return points - closed = list(points) - if closed[0] != closed[-1]: - closed.append(closed[0]) - return closed - - -__all__ = ["ExclusionMixin"] diff --git a/app/gui/theme.py b/app/gui/theme.py deleted file mode 100644 index 24d23b0..0000000 --- a/app/gui/theme.py +++ /dev/null @@ -1,106 +0,0 @@ -"""Theme and window helpers.""" - -from __future__ import annotations - -import platform -from tkinter import ttk - -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 - scale_style: str - - def init_theme(self) -> None: - """Initialise ttk style handling and apply the detected theme.""" - self.style = ttk.Style() - self.style.theme_use("clam") - - 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" - - self.scale_style = "Horizontal.TScale" - - if self.theme == "dark": - bg, fg = "#0f0f10", "#f1f1f1" - status_fg = "#f5f5f5" - highlight_fg = "#f2c744" - else: - bg, fg = "#ffffff", "#202020" - status_fg = "#1c1c1c" - highlight_fg = "#c56217" - 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)) - s.configure( - "TButton", padding=8, relief="flat", background="#e0e0e0", foreground=fg, font=("Segoe UI", 10) - ) - s.map("TButton", background=[("active", "#d0d0d0")]) - - button_refresher = getattr(self, "_refresh_toolbar_buttons_theme", None) - if callable(button_refresher): - button_refresher() - - nav_refresher = getattr(self, "_refresh_navigation_buttons_theme", None) - if callable(nav_refresher): - nav_refresher() - - status_refresher = getattr(self, "_refresh_status_palette", None) - if callable(status_refresher) and hasattr(self, "status"): - status_refresher(status_fg) - - accent_refresher = getattr(self, "_refresh_accent_labels", None) - if callable(accent_refresher) and hasattr(self, "filename_label"): - accent_refresher(highlight_fg) - - canvas_refresher = getattr(self, "_refresh_canvas_backgrounds", None) - if callable(canvas_refresher): - canvas_refresher() - - 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"] diff --git a/app/gui/ui.py b/app/gui/ui.py deleted file mode 100644 index af36ad5..0000000 --- a/app/gui/ui.py +++ /dev/null @@ -1,731 +0,0 @@ -"""UI helpers and reusable Tk callbacks.""" - -from __future__ import annotations - -import colorsys -import tkinter as tk -import tkinter.font as tkfont -from tkinter import ttk - - -class UIBuilderMixin: - """Constructs the Tkinter UI and common widgets.""" - - def setup_ui(self) -> None: - self._create_titlebar() - - toolbar = ttk.Frame(self.root) - toolbar.pack(fill=tk.X, padx=12, pady=(4, 2)) - buttons = [ - ("🖼", self._t("toolbar.open_image"), self.load_image), - ("📂", self._t("toolbar.open_folder"), self.load_folder), - ("🎨", self._t("toolbar.choose_color"), self.choose_color), - ("🖱", self._t("toolbar.pick_from_image"), self.enable_pick_mode), - ("💾", self._t("toolbar.save_overlay"), self.save_overlay), - ("△", self._t("toolbar.toggle_free_draw"), self.toggle_exclusion_mode), - ("🧹", self._t("toolbar.clear_excludes"), self.clear_excludes), - ("↩", self._t("toolbar.undo_exclude"), self.undo_exclude), - ("🔄", self._t("toolbar.reset_sliders"), self.reset_sliders), - ("🌓", self._t("toolbar.toggle_theme"), self.toggle_theme), - ] - self._toolbar_buttons: list[dict[str, object]] = [] - self._nav_buttons: list[tk.Button] = [] - - buttons_frame = ttk.Frame(toolbar) - buttons_frame.pack(side=tk.LEFT) - for icon, label, command in buttons: - self._add_toolbar_button(buttons_frame, icon, label, command) - - status_container = ttk.Frame(toolbar) - status_container.pack(side=tk.RIGHT, expand=True, fill=tk.X) - self.status = ttk.Label( - status_container, - text=self._t("status.no_file"), - anchor="e", - foreground="#efefef", - ) - self.status.pack(fill=tk.X) - self._attach_copy_menu(self.status) - self.status_default_text = self.status.cget("text") - self._status_palette = {"fg": self.status.cget("foreground")} - - palette_frame = ttk.Frame(self.root) - palette_frame.pack(fill=tk.X, padx=12, pady=(6, 8)) - default_colour = self._default_colour_hex() - - current_frame = ttk.Frame(palette_frame) - current_frame.pack(side=tk.LEFT, padx=(0, 16)) - ttk.Label(current_frame, text=self._t("palette.current")).pack(side=tk.LEFT, padx=(0, 6)) - self.current_colour_sw = tk.Canvas( - current_frame, - width=24, - height=24, - highlightthickness=0, - background=default_colour, - bd=0, - ) - self.current_colour_sw.pack(side=tk.LEFT, pady=2) - self.current_colour_label = ttk.Label(current_frame, text=f"({default_colour})") - self.current_colour_label.pack(side=tk.LEFT, padx=(6, 0)) - - ttk.Label(palette_frame, text=self._t("palette.more")).pack(side=tk.LEFT, padx=(0, 8)) - swatch_container = ttk.Frame(palette_frame) - swatch_container.pack(side=tk.LEFT) - for name, hex_code in self._preset_colours(): - self._add_palette_swatch(swatch_container, name, hex_code) - - sliders_frame = ttk.Frame(self.root) - sliders_frame.pack(fill=tk.X, padx=12, pady=4) - sliders = [ - (self._t("sliders.hue_min"), self.hue_min, 0, 360), - (self._t("sliders.hue_max"), self.hue_max, 0, 360), - (self._t("sliders.sat_min"), self.sat_min, 0, 100), - (self._t("sliders.val_min"), self.val_min, 0, 100), - (self._t("sliders.val_max"), self.val_max, 0, 100), - (self._t("sliders.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) - - left_column = ttk.Frame(main) - left_column.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 6)) - left_column.grid_columnconfigure(1, weight=1) - left_column.grid_rowconfigure(0, weight=1) - - self._create_navigation_button(left_column, "◀", self.show_previous_image, column=0) - - self.canvas_orig = tk.Canvas( - left_column, - bg=self._canvas_background_colour(), - highlightthickness=0, - relief="flat", - ) - self.canvas_orig.grid(row=0, column=1, sticky="nsew") - self.canvas_orig.bind("", self.on_canvas_click) - self.canvas_orig.bind("", self._exclude_start) - self.canvas_orig.bind("", self._exclude_drag) - self.canvas_orig.bind("", self._exclude_end) - - right_column = ttk.Frame(main) - right_column.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=(6, 0)) - right_column.grid_columnconfigure(0, weight=1) - right_column.grid_rowconfigure(0, weight=1) - - self.canvas_overlay = tk.Canvas( - right_column, - bg=self._canvas_background_colour(), - highlightthickness=0, - relief="flat", - ) - self.canvas_overlay.grid(row=0, column=0, sticky="nsew") - self._create_navigation_button(right_column, "▶", self.show_next_image, column=1) - - - info_frame = ttk.Frame(self.root) - info_frame.pack(fill=tk.X, padx=12, pady=(0, 12)) - self.filename_label = ttk.Label( - info_frame, - text="—", - font=("Segoe UI", 10, "bold"), - anchor="center", - justify="center", - ) - self.filename_label.pack(anchor="center") - self._attach_copy_menu(self.filename_label) - - self.ratio_label = ttk.Label( - info_frame, - text=self._t("stats.placeholder"), - font=("Segoe UI", 10, "bold"), - anchor="center", - justify="center", - ) - self.ratio_label.pack(anchor="center", pady=(4, 0)) - self._attach_copy_menu(self.ratio_label) - - self.root.bind("", self.disable_pick_mode) - self.root.bind("", self._maybe_focus_window) - - 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 _preset_colours(self): - return [ - (self._t("palette.swatch.red"), "#ff3b30"), - (self._t("palette.swatch.orange"), "#ff9500"), - (self._t("palette.swatch.yellow"), "#ffd60a"), - (self._t("palette.swatch.green"), "#34c759"), - (self._t("palette.swatch.teal"), "#5ac8fa"), - (self._t("palette.swatch.blue"), "#0a84ff"), - (self._t("palette.swatch.violet"), "#af52de"), - (self._t("palette.swatch.magenta"), "#ff2d55"), - (self._t("palette.swatch.white"), "#ffffff"), - (self._t("palette.swatch.grey"), "#8e8e93"), - (self._t("palette.swatch.black"), "#000000"), - ] - - def _add_palette_swatch(self, parent, name: str, hex_code: str) -> None: - swatch = tk.Canvas( - parent, - width=24, - height=24, - highlightthickness=0, - background=hex_code, - bd=0, - relief="flat", - takefocus=1, - cursor="hand2", - ) - swatch.pack(side=tk.LEFT, padx=4, pady=2) - - def trigger(_event=None, colour=hex_code, label=name): - self.apply_sample_colour(colour, label) - - swatch.bind("", trigger) - swatch.bind("", trigger) - swatch.bind("", trigger) - swatch.bind("", lambda _e: swatch.configure(cursor="hand2")) - swatch.bind("", lambda _e: swatch.configure(cursor="arrow")) - - def _add_toolbar_button(self, parent, icon: str, label: str, command) -> None: - font = tkfont.Font(root=self.root, family="Segoe UI", size=9) - padding_x = 12 - gap = font.measure(" ") - icon_width = font.measure(icon) or font.measure(" ") - label_width = font.measure(label) - width = padding_x * 2 + icon_width + gap + label_width - height = 28 - radius = 9 - bg = self.root.cget("bg") if hasattr(self.root, "cget") else "#f2f2f7" - canvas = tk.Canvas( - parent, - width=width, - height=height, - bd=0, - highlightthickness=0, - bg=bg, - relief="flat", - cursor="hand2", - takefocus=1, - ) - canvas.pack(side=tk.LEFT, padx=4, pady=1) - - palette = self._toolbar_palette() - rect_id = self._create_round_rect( - canvas, - 1, - 1, - width - 1, - height - 1, - radius, - fill=palette["normal"], - outline=palette["outline"], - width=1, - ) - icon_id = canvas.create_text( - padding_x, - height / 2, - text=icon, - font=font, - fill=palette["text"], - anchor="w", - ) - label_id = canvas.create_text( - padding_x + icon_width + gap, - height / 2, - text=label, - font=font, - fill=palette["text"], - anchor="w", - ) - - button_data = { - "canvas": canvas, - "rect": rect_id, - "text_ids": (icon_id, label_id), - "command": command, - "palette": palette.copy(), - "dimensions": (width, height, radius), - } - self._toolbar_buttons.append(button_data) - - def set_fill(state: str) -> None: - pal: dict[str, str] = button_data["palette"] # type: ignore[index] - canvas.itemconfigure(rect_id, fill=pal[state]) # type: ignore[index] - - def execute(): - command() - - def on_press(_event=None): - set_fill("active") - - def on_release(event=None): - if event is not None and ( - event.x < 0 or event.y < 0 or event.x > width or event.y > height - ): - set_fill("normal") - return - set_fill("hover") - self.root.after_idle(execute) - - def on_enter(_event): - set_fill("hover") - - def on_leave(_event): - set_fill("normal") - - def on_focus_in(_event): - pal: dict[str, str] = button_data["palette"] # type: ignore[index] - canvas.itemconfigure(rect_id, outline=pal["outline_focus"]) # type: ignore[index] - - def on_focus_out(_event): - pal: dict[str, str] = button_data["palette"] # type: ignore[index] - canvas.itemconfigure(rect_id, outline=pal["outline"]) # type: ignore[index] - - def invoke_keyboard(_event=None): - set_fill("active") - canvas.after(120, lambda: set_fill("hover")) - self.root.after_idle(execute) - - canvas.bind("", on_press) - canvas.bind("", on_release) - canvas.bind("", on_enter) - canvas.bind("", on_leave) - canvas.bind("", on_focus_in) - canvas.bind("", on_focus_out) - canvas.bind("", invoke_keyboard) - canvas.bind("", invoke_keyboard) - - @staticmethod - def _create_round_rect(canvas: tk.Canvas, x1, y1, x2, y2, radius, **kwargs): - points = [ - x1 + radius, - y1, - x2 - radius, - y1, - x2, - y1, - x2, - y1 + radius, - x2, - y2 - radius, - x2, - y2, - x2 - radius, - y2, - x1 + radius, - y2, - x1, - y2, - x1, - y2 - radius, - x1, - y1 + radius, - x1, - y1, - ] - return canvas.create_polygon(points, smooth=True, splinesteps=24, **kwargs) - - def _create_navigation_button(self, container, symbol: str, command, *, column: int) -> None: - palette = self._navigation_palette() - bg = palette["bg"] - fg = palette["fg"] - container.grid_rowconfigure(0, weight=1) - btn = tk.Button( - container, - text=symbol, - command=command, - font=("Segoe UI", 26, "bold"), - relief="flat", - borderwidth=0, - background=bg, - activebackground=bg, - highlightthickness=0, - fg=fg, - activeforeground=fg, - cursor="hand2", - width=2, - ) - btn.grid(row=0, column=column, sticky="ns", padx=6) - self._nav_buttons.append(btn) - - def _create_titlebar(self) -> None: - bar_bg = "#1f1f1f" - title_bar = tk.Frame(self.root, bg=bar_bg, relief="flat", height=34) - title_bar.pack(fill=tk.X, side=tk.TOP) - title_bar.pack_propagate(False) - - logo = None - try: - from PIL import Image, ImageTk # type: ignore - from importlib import resources - - logo_resource = resources.files("app.assets").joinpath("logo.png") - with resources.as_file(logo_resource) as logo_path: - image = Image.open(logo_path).convert("RGBA") - image.thumbnail((26, 26)) - logo = ImageTk.PhotoImage(image) - except Exception: - logo = None - - if logo is not None: - logo_label = tk.Label(title_bar, image=logo, bg=bar_bg) - logo_label.image = logo # keep reference - logo_label.pack(side=tk.LEFT, padx=(10, 6), pady=4) - - title_label = tk.Label( - title_bar, - text=self._t("app.title"), - bg=bar_bg, - fg="#f5f5f5", - font=("Segoe UI", 11, "bold"), - anchor="w", - ) - title_label.pack(side=tk.LEFT, padx=6) - - btn_kwargs = { - "bg": bar_bg, - "fg": "#f5f5f5", - "activebackground": "#3a3a40", - "activeforeground": "#ffffff", - "borderwidth": 0, - "highlightthickness": 0, - "relief": "flat", - "font": ("Segoe UI", 10, "bold"), - "cursor": "hand2", - "width": 3, - } - - close_btn = tk.Button(title_bar, text="✕", command=self._close_app, **btn_kwargs) - close_btn.pack(side=tk.RIGHT, padx=6, pady=4) - close_btn.bind("", lambda _e: close_btn.configure(bg="#cf212f")) - close_btn.bind("", lambda _e: close_btn.configure(bg=bar_bg)) - - max_btn = tk.Button(title_bar, text="❐", command=self._toggle_maximize_window, **btn_kwargs) - max_btn.pack(side=tk.RIGHT, padx=0, pady=4) - max_btn.bind("", lambda _e: max_btn.configure(bg="#2c2c32")) - max_btn.bind("", lambda _e: max_btn.configure(bg=bar_bg)) - self._max_button = max_btn - - min_btn = tk.Button(title_bar, text="—", command=self._minimize_window, **btn_kwargs) - min_btn.pack(side=tk.RIGHT, padx=0, pady=4) - min_btn.bind("", lambda _e: min_btn.configure(bg="#2c2c32")) - min_btn.bind("", lambda _e: min_btn.configure(bg=bar_bg)) - - for widget in (title_bar, title_label): - widget.bind("", self._start_window_drag) - widget.bind("", self._perform_window_drag) - widget.bind("", lambda _e: self._toggle_maximize_window()) - - self._update_maximize_button() - - def _close_app(self) -> None: - try: - self.root.destroy() - except Exception: - pass - - def _start_window_drag(self, event) -> None: - if getattr(self, "_is_maximized", False): - cursor_x, cursor_y = event.x_root, event.y_root - self._toggle_maximize_window(force_state=False) - self.root.update_idletasks() - new_x = self.root.winfo_rootx() - new_y = self.root.winfo_rooty() - self._drag_offset = (cursor_x - new_x, cursor_y - new_y) - return - self._drag_offset = (event.x_root - self.root.winfo_rootx(), event.y_root - self.root.winfo_rooty()) - - def _perform_window_drag(self, event) -> None: - offset = getattr(self, "_drag_offset", None) - if offset is None: - return - x = event.x_root - offset[0] - y = event.y_root - offset[1] - self.root.geometry(f"+{x}+{y}") - if not getattr(self, "_is_maximized", False): - self._remember_window_geometry() - - def _remember_window_geometry(self) -> None: - try: - self._window_geometry = self.root.geometry() - except Exception: - pass - - def _monitor_work_area(self) -> tuple[int, int, int, int] | None: - try: - import ctypes - from ctypes import wintypes - - user32 = ctypes.windll.user32 # type: ignore[attr-defined] - root_x = self.root.winfo_rootx() - root_y = self.root.winfo_rooty() - width = max(self.root.winfo_width(), 1) - height = max(self.root.winfo_height(), 1) - center_x = root_x + width // 2 - center_y = root_y + height // 2 - - class MONITORINFO(ctypes.Structure): - _fields_ = [ - ("cbSize", wintypes.DWORD), - ("rcMonitor", wintypes.RECT), - ("rcWork", wintypes.RECT), - ("dwFlags", wintypes.DWORD), - ] - - monitor = user32.MonitorFromPoint( - wintypes.POINT(center_x, center_y), 2 # MONITOR_DEFAULTTONEAREST - ) - info = MONITORINFO() - info.cbSize = ctypes.sizeof(MONITORINFO) - if not user32.GetMonitorInfoW(monitor, ctypes.byref(info)): - return None - work = info.rcWork - return work.left, work.top, work.right, work.bottom - except Exception: - return None - - def _maximize_window(self) -> None: - self._remember_window_geometry() - work_area = self._monitor_work_area() - if work_area is None: - screen_width = self.root.winfo_screenwidth() - screen_height = self.root.winfo_screenheight() - left = 0 - top = 0 - width = screen_width - height = screen_height - else: - left, top, right, bottom = work_area - width = max(1, right - left) - height = max(1, bottom - top) - self.root.geometry(f"{width}x{height}+{left}+{top}") - self._is_maximized = True - self._update_maximize_button() - - def _restore_window(self) -> None: - geometry = getattr(self, "_window_geometry", None) - if not geometry: - screen_width = self.root.winfo_screenwidth() - screen_height = self.root.winfo_screenheight() - width = int(screen_width * 0.8) - height = int(screen_height * 0.8) - x = (screen_width - width) // 2 - y = (screen_height - height) // 4 - geometry = f"{width}x{height}+{x}+{y}" - self.root.geometry(geometry) - self._is_maximized = False - self._update_maximize_button() - - def _toggle_maximize_window(self, force_state: bool | None = None) -> None: - desired = force_state if force_state is not None else not getattr(self, "_is_maximized", False) - if desired: - self._maximize_window() - else: - self._restore_window() - - def _minimize_window(self) -> None: - try: - self._remember_window_geometry() - use_or = getattr(self, "_use_overrideredirect", False) - if use_or and hasattr(self.root, "overrideredirect"): - try: - self.root.overrideredirect(False) - except Exception: - pass - self.root.iconify() - if use_or: - restorer = getattr(self, "_restore_borderless", None) - if callable(restorer): - self.root.after(120, restorer) - elif hasattr(self.root, "overrideredirect"): - self.root.after(120, lambda: self.root.overrideredirect(True)) # type: ignore[arg-type] - except Exception: - pass - - def _update_maximize_button(self) -> None: - button = getattr(self, "_max_button", None) - if button is None: - return - symbol = "❐" if getattr(self, "_is_maximized", False) else "□" - button.configure(text=symbol) - - def _maybe_focus_window(self, _event) -> None: - try: - self.root.focus_set() - except Exception: - pass - - def _toolbar_palette(self) -> dict[str, str]: - is_dark = getattr(self, "theme", "light") == "dark" - if is_dark: - return { - "normal": "#2f2f35", - "hover": "#3a3a40", - "active": "#1f1f25", - "outline": "#4d4d50", - "outline_focus": "#7c7c88", - "text": "#f1f1f5", - } - return { - "normal": "#ffffff", - "hover": "#ededf4", - "active": "#dcdce6", - "outline": "#d0d0d8", - "outline_focus": "#a9a9b2", - "text": "#1f1f1f", - } - - def _navigation_palette(self) -> dict[str, str]: - is_dark = getattr(self, "theme", "light") == "dark" - default_bg = "#0f0f10" if is_dark else "#ededf2" - bg = self.root.cget("bg") if hasattr(self.root, "cget") else default_bg - fg = "#f5f5f5" if is_dark else "#1f1f1f" - return {"bg": bg, "fg": fg} - - def _refresh_toolbar_buttons_theme(self) -> None: - if not getattr(self, "_toolbar_buttons", None): - return - bg = self.root.cget("bg") if hasattr(self.root, "cget") else "#f2f2f7" - palette = self._toolbar_palette() - for data in self._toolbar_buttons: - canvas = data["canvas"] # type: ignore[index] - rect_id = data["rect"] # type: ignore[index] - text_ids = data["text_ids"] # type: ignore[index] - data["palette"] = palette.copy() - canvas.configure(bg=bg) - canvas.itemconfigure(rect_id, fill=palette["normal"], outline=palette["outline"]) - for text_id in text_ids: - canvas.itemconfigure(text_id, fill=palette["text"]) - - def _refresh_navigation_buttons_theme(self) -> None: - if not getattr(self, "_nav_buttons", None): - return - palette = self._navigation_palette() - for btn in self._nav_buttons: - btn.configure( - background=palette["bg"], - activebackground=palette["bg"], - fg=palette["fg"], - activeforeground=palette["fg"], - ) - - def _canvas_background_colour(self) -> str: - return "#0f0f10" if getattr(self, "theme", "light") == "dark" else "#ffffff" - - def _refresh_canvas_backgrounds(self) -> None: - bg = self._canvas_background_colour() - for attr in ("canvas_orig", "canvas_overlay"): - canvas = getattr(self, attr, None) - if canvas is not None: - try: - canvas.configure(bg=bg) - except Exception: - pass - - def _refresh_status_palette(self, fg: str) -> None: - self.status.configure(foreground=fg) - self._status_palette["fg"] = fg - - def _refresh_accent_labels(self, colour: str) -> None: - try: - self.filename_label.configure(foreground=colour) - self.ratio_label.configure(foreground=colour) - except Exception: - pass - - def _default_colour_hex(self) -> str: - defaults = getattr(self, "DEFAULTS", {}) - hue_min = float(defaults.get("hue_min", 0.0)) - hue_max = float(defaults.get("hue_max", hue_min)) - if hue_min <= hue_max: - hue = (hue_min + hue_max) / 2.0 - else: - span = ((hue_max + 360.0) - hue_min) / 2.0 - hue = (hue_min + span) % 360.0 - - sat_min = float(defaults.get("sat_min", 0.0)) - saturation = (sat_min + 100.0) / 2.0 - - val_min = float(defaults.get("val_min", 0.0)) - val_max = float(defaults.get("val_max", 100.0)) - value = (val_min + val_max) / 2.0 - - r, g, b = colorsys.hsv_to_rgb(hue / 360.0, saturation / 100.0, value / 100.0) - return f"#{int(r * 255):02x}{int(g * 255):02x}{int(b * 255):02x}" - - def _init_copy_menu(self): - self._copy_target = None - self.copy_menu = tk.Menu(self.root, tearoff=0) - label = self._t("menu.copy") if hasattr(self, "_t") else "Copy" - self.copy_menu.add_command(label=label, command=self._copy_current_label) - - def _attach_copy_menu(self, widget): - widget.bind("", lambda event, w=widget: self._show_copy_menu(event, w)) - widget.bind("", 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"] diff --git a/app/lang/de.toml b/app/lang/de.toml index 54179e4..1d0e38a 100644 --- a/app/lang/de.toml +++ b/app/lang/de.toml @@ -17,7 +17,7 @@ "status.loaded" = "Geladen: {name} — {dimensions}{position}" "status.filename_label" = "{name} — {dimensions}{position}" "status.color_selected" = "Farbe gewählt: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" -"status.sample_colour" = "Beispielfarbe gewählt: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" +"status.sample_color" = "Beispielfarbe gewählt: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.pick_mode_ready" = "Pick-Modus: Klicke links ins Bild, um Farbe zu wählen (Esc beendet)" "status.pick_mode_ended" = "Pick-Modus beendet." "status.pick_mode_from_image" = "Farbe vom Bild gewählt: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" @@ -49,7 +49,7 @@ "dialog.open_image_title" = "Bild wählen" "dialog.open_folder_title" = "Ordner mit Bildern wählen" "dialog.save_overlay_title" = "Overlay speichern als" -"dialog.choose_colour_title" = "Farbe wählen" +"dialog.choose_color_title" = "Farbe wählen" "dialog.images_filter" = "Bilder" "dialog.folder_not_found" = "Der Ordner wurde nicht gefunden." "dialog.folder_empty" = "Keine unterstützten Bilder im Ordner gefunden." @@ -59,4 +59,13 @@ "dialog.no_image_loaded" = "Kein Bild geladen." "dialog.no_preview_available" = "Keine Preview vorhanden." "dialog.overlay_saved" = "Overlay gespeichert: {path}" +"dialog.export_stats_title" = "Ordner-Statistiken exportieren (CSV)" +"dialog.csv_filter" = "CSV-Dateien (*.csv)" "status.drag_drop" = "Bild oder Ordner hier ablegen." +"status.exporting" = "Statistiken werden exportiert... ({current}/{total})" +"status.export_done" = "Export abgeschlossen: {path}" +"toolbar.export_folder" = "Ordner-Statistik" +"menu.file" = "Datei" +"menu.edit" = "Bearbeiten" +"menu.view" = "Ansicht" +"menu.tools" = "Werkzeuge" diff --git a/app/lang/en.toml b/app/lang/en.toml index 1857249..a95fbc0 100644 --- a/app/lang/en.toml +++ b/app/lang/en.toml @@ -2,7 +2,7 @@ "app.title" = "Interactive Color Range Analyzer" "toolbar.open_image" = "Open image" "toolbar.open_folder" = "Open folder" -"toolbar.choose_color" = "Choose colour" +"toolbar.choose_color" = "Choose color" "toolbar.pick_from_image" = "Pick from image" "toolbar.save_overlay" = "Save overlay" "toolbar.clear_excludes" = "Clear exclusions" @@ -16,13 +16,13 @@ "status.free_draw_disabled" = "Rectangle exclusion mode enabled." "status.loaded" = "Loaded: {name} — {dimensions}{position}" "status.filename_label" = "{name} — {dimensions}{position}" -"status.color_selected" = "Colour chosen: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" -"status.sample_colour" = "Sample colour applied: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" -"status.pick_mode_ready" = "Pick mode: Click the left image to choose a colour (Esc exits)" +"status.color_selected" = "Color chosen: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" +"status.sample_color" = "Sample color applied: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" +"status.pick_mode_ready" = "Pick mode: Click the left image to choose a color (Esc exits)" "status.pick_mode_ended" = "Pick mode ended." -"status.pick_mode_from_image" = "Colour picked from image: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" -"palette.current" = "Colour:" -"palette.more" = "More colours:" +"status.pick_mode_from_image" = "Color picked from image: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" +"palette.current" = "Color:" +"palette.more" = "More colors:" "palette.swatch.red" = "Red" "palette.swatch.orange" = "Orange" "palette.swatch.yellow" = "Yellow" @@ -49,7 +49,7 @@ "dialog.open_image_title" = "Select image" "dialog.open_folder_title" = "Select folder" "dialog.save_overlay_title" = "Save overlay as" -"dialog.choose_colour_title" = "Choose colour" +"dialog.choose_color_title" = "Choose color" "dialog.images_filter" = "Images" "dialog.folder_not_found" = "The folder could not be found." "dialog.folder_empty" = "No supported images were found in the folder." @@ -59,4 +59,13 @@ "dialog.no_image_loaded" = "No image loaded." "dialog.no_preview_available" = "No preview available." "dialog.overlay_saved" = "Overlay saved: {path}" +"dialog.export_stats_title" = "Export Folder Statistics (CSV)" +"dialog.csv_filter" = "CSV Files (*.csv)" "status.drag_drop" = "Drop an image or folder here to open it." +"status.exporting" = "Exporting statistics... ({current}/{total})" +"status.export_done" = "Export complete: {path}" +"toolbar.export_folder" = "Export Folder Stats" +"menu.file" = "File" +"menu.edit" = "Edit" +"menu.view" = "View" +"menu.tools" = "Tools" diff --git a/app/logic/__init__.py b/app/logic/__init__.py index c8e3624..f6b1fe2 100644 --- a/app/logic/__init__.py +++ b/app/logic/__init__.py @@ -1,25 +1,23 @@ -"""Logic utilities and mixins for processing and configuration.""" +"""Logic utilities and configuration constants.""" from .constants import ( BASE_DIR, DEFAULTS, IMAGES_DIR, LANGUAGE, + OVERLAY_COLOR, PREVIEW_MAX_SIZE, RESET_EXCLUSIONS_ON_IMAGE_CHANGE, SUPPORTED_IMAGE_EXTENSIONS, ) -from .image_processing import ImageProcessingMixin -from .reset import ResetMixin __all__ = [ "BASE_DIR", "DEFAULTS", "IMAGES_DIR", "LANGUAGE", + "OVERLAY_COLOR", "PREVIEW_MAX_SIZE", "RESET_EXCLUSIONS_ON_IMAGE_CHANGE", "SUPPORTED_IMAGE_EXTENSIONS", - "ImageProcessingMixin", - "ResetMixin", ] diff --git a/app/logic/constants.py b/app/logic/constants.py index 4ef98fd..6e403a4 100644 --- a/app/logic/constants.py +++ b/app/logic/constants.py @@ -94,7 +94,7 @@ def _extract_language(data: dict[str, Any]) -> str: _CONFIG_DATA = _load_config_data() -_OPTION_DEFAULTS = {"reset_exclusions_on_image_change": False} +_OPTION_DEFAULTS = {"reset_exclusions_on_image_change": False, "overlay_color": "#ff0000"} def _extract_options(data: dict[str, Any]) -> dict[str, Any]: @@ -105,6 +105,9 @@ def _extract_options(data: dict[str, Any]) -> dict[str, Any]: value = section.get("reset_exclusions_on_image_change") if isinstance(value, bool): result["reset_exclusions_on_image_change"] = value + color = section.get("overlay_color") + if isinstance(color, str) and color.startswith("#") and len(color) in (7, 9): + result["overlay_color"] = color return result @@ -112,3 +115,4 @@ DEFAULTS = {**_DEFAULTS_BASE, **_extract_default_overrides(_CONFIG_DATA)} LANGUAGE = _extract_language(_CONFIG_DATA) OPTIONS = {**_OPTION_DEFAULTS, **_extract_options(_CONFIG_DATA)} RESET_EXCLUSIONS_ON_IMAGE_CHANGE = OPTIONS["reset_exclusions_on_image_change"] +OVERLAY_COLOR = OPTIONS["overlay_color"] diff --git a/app/logic/image_processing.py b/app/logic/image_processing.py deleted file mode 100644 index f0c2819..0000000 --- a/app/logic/image_processing.py +++ /dev/null @@ -1,485 +0,0 @@ -"""Image loading, processing, and statistics logic.""" - -from __future__ import annotations - -import colorsys -from pathlib import Path -from typing import Iterable, Sequence, Tuple - -from tkinter import filedialog, messagebox - -from PIL import Image, ImageDraw, ImageTk - -from .constants import IMAGES_DIR, PREVIEW_MAX_SIZE, SUPPORTED_IMAGE_EXTENSIONS - - -class ImageProcessingMixin: - """Handles all image related operations.""" - - image_path: Path | None - orig_img: Image.Image | None - preview_img: Image.Image | None - preview_tk: ImageTk.PhotoImage | None - overlay_tk: ImageTk.PhotoImage | None - - image_paths: list[Path] - current_image_index: int - - def load_image(self) -> None: - default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd() - path = filedialog.askopenfilename( - title=self._t("dialog.open_image_title"), - filetypes=[(self._t("dialog.images_filter"), "*.webp *.png *.jpg *.jpeg *.bmp")], - initialdir=str(default_dir), - ) - if not path: - return - self._set_image_collection([Path(path)], 0) - - def load_folder(self) -> None: - default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd() - directory = filedialog.askdirectory( - title=self._t("dialog.open_folder_title"), - initialdir=str(default_dir), - ) - if not directory: - return - folder = Path(directory) - if not folder.exists(): - messagebox.showerror( - self._t("dialog.error_title"), - self._t("dialog.folder_not_found"), - ) - return - image_files = sorted( - ( - path - for path in folder.iterdir() - if path.suffix.lower() in SUPPORTED_IMAGE_EXTENSIONS and path.is_file() - ), - key=lambda item: item.name.lower(), - ) - if not image_files: - messagebox.showinfo( - self._t("dialog.info_title"), - self._t("dialog.folder_empty"), - ) - return - self._set_image_collection(image_files, 0) - - def show_next_image(self, event=None) -> None: - if not getattr(self, "image_paths", None): - return - if not self.image_paths: - return - current = getattr(self, "current_image_index", -1) - next_index = (current + 1) % len(self.image_paths) - self._display_image_by_index(next_index) - - def show_previous_image(self, event=None) -> None: - if not getattr(self, "image_paths", None): - return - if not self.image_paths: - return - current = getattr(self, "current_image_index", -1) - prev_index = (current - 1) % len(self.image_paths) - self._display_image_by_index(prev_index) - - def _set_image_collection(self, paths: Sequence[Path], start_index: int) -> None: - self.image_paths = list(paths) - if not self.image_paths: - return - self.exclude_shapes = [] - self._rubber_start = None - self._rubber_id = None - self._stroke_preview_id = None - self._exclude_canvas_ids = [] - self._exclude_mask = None - self._exclude_mask_px = None - self._exclude_mask_dirty = True - self.current_image_index = -1 - self._display_image_by_index(max(0, start_index)) - - def _display_image_by_index(self, index: int) -> None: - if not self.image_paths: - return - if index < 0 or index >= len(self.image_paths): - return - path = self.image_paths[index] - if not path.exists(): - messagebox.showerror( - self._t("dialog.error_title"), - self._t("dialog.file_missing", path=path), - ) - return - try: - image = Image.open(path).convert("RGBA") - except Exception as exc: - messagebox.showerror( - self._t("dialog.error_title"), - self._t("dialog.image_open_failed", error=exc), - ) - return - - self.image_path = path - self.orig_img = image - if getattr(self, "reset_exclusions_on_switch", False): - self.exclude_shapes = [] - self._rubber_start = None - self._rubber_id = None - self._stroke_preview_id = None - self._exclude_canvas_ids = [] - self._exclude_mask = None - self._exclude_mask_px = None - self._exclude_mask_dirty = True - self.pick_mode = False - - self.prepare_preview() - self.update_preview() - - dimensions = f"{self.orig_img.width}x{self.orig_img.height}" - suffix = f" [{index + 1}/{len(self.image_paths)}]" if len(self.image_paths) > 1 else "" - status_text = self._t("status.loaded", name=path.name, dimensions=dimensions, position=suffix) - self.status.config(text=status_text) - self.status_default_text = status_text - if hasattr(self, "filename_label"): - filename_text = self._t( - "status.filename_label", - name=path.name, - dimensions=dimensions, - position=suffix, - ) - self.filename_label.config(text=filename_text) - - self.current_image_index = index - - def save_overlay(self) -> None: - if self.orig_img is None: - messagebox.showinfo( - self._t("dialog.info_title"), - self._t("dialog.no_image_loaded"), - ) - return - if self.preview_img is None: - messagebox.showerror( - self._t("dialog.error_title"), - self._t("dialog.no_preview_available"), - ) - return - - overlay = self._build_overlay_image( - self.orig_img, - tuple(self.exclude_shapes), - alpha=int(self.alpha.get()), - scale_from_preview=self.preview_img.size, - is_match_fn=self.matches_target_color, - ) - merged = Image.alpha_composite(self.orig_img.convert("RGBA"), overlay) - - out_path = filedialog.asksaveasfilename( - defaultextension=".png", - filetypes=[("PNG", "*.png")], - title=self._t("dialog.save_overlay_title"), - ) - if not out_path: - return - merged.save(out_path) - messagebox.showinfo( - self._t("dialog.saved_title"), - self._t("dialog.overlay_saved", path=out_path), - ) - - def prepare_preview(self) -> None: - if self.orig_img is None: - return - width, height = self.orig_img.size - max_w, max_h = PREVIEW_MAX_SIZE - scale = min(max_w / width, max_h / height) - if scale <= 0: - scale = 1.0 - size = (max(1, int(width * scale)), max(1, int(height * scale))) - self.preview_img = self.orig_img.resize(size, Image.LANCZOS) - self.preview_tk = ImageTk.PhotoImage(self.preview_img) - self.canvas_orig.delete("all") - self.canvas_orig.config(width=size[0], height=size[1]) - self.canvas_overlay.config(width=size[0], height=size[1]) - self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk) - self._exclude_mask = None - self._exclude_mask_px = None - self._exclude_mask_dirty = True - if getattr(self, "exclude_shapes", None): - self._ensure_exclude_mask() - - def update_preview(self) -> None: - if self.preview_img is None: - return - self._ensure_exclude_mask() - merged = self.create_overlay_preview() - if merged is None: - return - self.overlay_tk = ImageTk.PhotoImage(merged) - self.canvas_overlay.delete("all") - self.canvas_overlay.create_image(0, 0, anchor="nw", image=self.overlay_tk) - - self.canvas_orig.delete("all") - self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk) - self._render_exclusion_overlays() - - stats = self.compute_stats_preview() - if stats: - matches_all, total_all = stats["all"] - matches_keep, total_keep = stats["keep"] - matches_ex, total_ex = stats["excl"] - r_with = (matches_keep / total_keep * 100) if total_keep else 0.0 - r_no = (matches_all / total_all * 100) if total_all else 0.0 - excl_share = (total_ex / total_all * 100) if total_all else 0.0 - excl_match = (matches_ex / total_ex * 100) if total_ex else 0.0 - self.ratio_label.config( - text=self._t( - "stats.summary", - with_pct=r_with, - without_pct=r_no, - excluded_pct=excl_share, - excluded_match_pct=excl_match, - ) - ) - - refresher = getattr(self, "_refresh_canvas_backgrounds", None) - if callable(refresher): - refresher() - else: - bg = "#0f0f10" if self.theme == "dark" else "#ffffff" - self.canvas_orig.configure(bg=bg) - self.canvas_overlay.configure(bg=bg) - - def create_overlay_preview(self) -> Image.Image | None: - if self.preview_img is None: - return None - self._ensure_exclude_mask() - base = self.preview_img.convert("RGBA") - overlay = Image.new("RGBA", base.size, (0, 0, 0, 0)) - draw = ImageDraw.Draw(overlay) - pixels = base.load() - mask_px = self._exclude_mask_px - width, height = base.size - alpha = int(self.alpha.get()) - for y in range(height): - for x in range(width): - if mask_px is not None and mask_px[x, y]: - continue - r, g, b, a = pixels[x, y] - if a == 0: - continue - if self.matches_target_color(r, g, b): - draw.point((x, y), fill=(255, 0, 0, alpha)) - merged = Image.alpha_composite(base, overlay) - outline = ImageDraw.Draw(merged) - accent_dark = (255, 215, 0, 200) - accent_light = (197, 98, 23, 200) - accent = accent_dark if getattr(self, "theme", "light") == "dark" else accent_light - for shape in getattr(self, "exclude_shapes", []): - if shape.get("kind") == "rect": - x0, y0, x1, y1 = shape["coords"] # type: ignore[index] - outline.rectangle([x0, y0, x1, y1], outline=accent, width=3) - elif shape.get("kind") == "polygon": - points = shape.get("points", []) - if len(points) < 2: - continue - path = points if points[0] == points[-1] else points + [points[0]] - outline.line(path, fill=accent, width=2, joint="round") - return merged - - def compute_stats_preview(self): - if self.preview_img is None: - return None - self._ensure_exclude_mask() - px = self.preview_img.convert("RGBA").load() - mask_px = self._exclude_mask_px - width, height = self.preview_img.size - matches_all = total_all = 0 - matches_keep = total_keep = 0 - matches_excl = total_excl = 0 - for y in range(height): - for x in range(width): - r, g, b, a = px[x, y] - if a == 0: - continue - excluded = bool(mask_px and mask_px[x, y]) - total_all += 1 - if self.matches_target_color(r, g, b): - matches_all += 1 - if not excluded: - total_keep += 1 - if self.matches_target_color(r, g, b): - matches_keep += 1 - else: - total_excl += 1 - if self.matches_target_color(r, g, b): - matches_excl += 1 - return { - "all": (matches_all, total_all), - "keep": (matches_keep, total_keep), - "excl": (matches_excl, total_excl), - } - - def matches_target_color(self, r, g, b) -> bool: - h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0) - hue = h * 360.0 - hmin = float(self.hue_min.get()) - hmax = float(self.hue_max.get()) - smin = float(self.sat_min.get()) / 100.0 - vmin = float(self.val_min.get()) / 100.0 - vmax = float(self.val_max.get()) / 100.0 - if hmin <= hmax: - hue_ok = hmin <= hue <= hmax - else: - hue_ok = (hue >= hmin) or (hue <= hmax) - return hue_ok and (s >= smin) and (v >= vmin) and (v <= vmax) - - def _is_excluded(self, x: int, y: int) -> bool: - self._ensure_exclude_mask() - if self._exclude_mask_px is None: - return False - try: - return bool(self._exclude_mask_px[x, y]) - except Exception: - return False - - @classmethod - def _build_overlay_image( - cls, - image: Image.Image, - shapes: Iterable[dict[str, object]], - *, - alpha: int, - scale_from_preview: Tuple[int, int], - is_match_fn, - ) -> Image.Image: - overlay = Image.new("RGBA", image.size, (0, 0, 0, 0)) - draw = ImageDraw.Draw(overlay) - pixels = image.load() - width, height = image.size - mask = cls._build_exclude_mask_for_size(tuple(shapes), scale_from_preview, image.size) - mask_px = mask.load() if mask else None - for y in range(height): - for x in range(width): - if mask_px is not None and mask_px[x, y]: - continue - r, g, b, a = pixels[x, y] - if a == 0: - continue - if is_match_fn(r, g, b): - draw.point((x, y), fill=(255, 0, 0, alpha)) - return overlay - - @classmethod - def _build_exclude_mask_for_size( - cls, - shapes: Iterable[dict[str, object]], - preview_size: Tuple[int, int], - target_size: Tuple[int, int], - ) -> Image.Image | None: - if not preview_size or not target_size or preview_size[0] == 0 or preview_size[1] == 0: - return None - mask = Image.new("L", target_size, 0) - draw = ImageDraw.Draw(mask) - scale_x = target_size[0] / preview_size[0] - scale_y = target_size[1] / preview_size[1] - for shape in shapes: - kind = shape.get("kind") - cls._draw_shape_on_mask(draw, shape, scale_x=scale_x, scale_y=scale_y) - return mask - - def _ensure_exclude_mask(self) -> None: - if self.preview_img is None: - return - size = self.preview_img.size - if ( - self._exclude_mask is None - or self._exclude_mask.size != size - or getattr(self, "_exclude_mask_dirty", False) - ): - self._exclude_mask = Image.new("L", size, 0) - draw = ImageDraw.Draw(self._exclude_mask) - for shape in getattr(self, "exclude_shapes", []): - self._draw_shape_on_mask(draw, shape, scale_x=1.0, scale_y=1.0) - self._exclude_mask_px = self._exclude_mask.load() - self._exclude_mask_dirty = False - elif self._exclude_mask_px is None: - self._exclude_mask_px = self._exclude_mask.load() - - def _stamp_shape_on_mask(self, shape: dict[str, object]) -> None: - if self.preview_img is None: - return - if self._exclude_mask is None or self._exclude_mask.size != self.preview_img.size: - self._exclude_mask_dirty = True - return - draw = ImageDraw.Draw(self._exclude_mask) - self._draw_shape_on_mask(draw, shape, scale_x=1.0, scale_y=1.0) - self._exclude_mask_px = self._exclude_mask.load() - - @staticmethod - def _draw_shape_on_mask( - draw: ImageDraw.ImageDraw, - shape: dict[str, object], - *, - scale_x: float, - scale_y: float, - ) -> None: - kind = shape.get("kind") - if kind == "rect": - x0, y0, x1, y1 = shape["coords"] # type: ignore[index] - draw.rectangle( - [ - x0 * scale_x, - y0 * scale_y, - x1 * scale_x, - y1 * scale_y, - ], - fill=255, - ) - elif kind == "polygon": - points = shape.get("points") - if not points or len(points) < 2: - return - scaled = [(px * scale_x, py * scale_y) for px, py in points] # type: ignore[misc] - draw.polygon(scaled, fill=255) - - def _render_exclusion_overlays(self) -> None: - if not hasattr(self, "canvas_orig"): - return - for item in getattr(self, "_exclude_canvas_ids", []): - try: - self.canvas_orig.delete(item) - except Exception: - pass - self._exclude_canvas_ids = [] - accent_dark = "#ffd700" - accent_light = "#c56217" - accent = accent_dark if getattr(self, "theme", "light") == "dark" else accent_light - for shape in getattr(self, "exclude_shapes", []): - kind = shape.get("kind") - if kind == "rect": - x0, y0, x1, y1 = shape["coords"] # type: ignore[index] - item = self.canvas_orig.create_rectangle( - x0, y0, x1, y1, outline=accent, width=3 - ) - self._exclude_canvas_ids.append(item) - elif kind == "polygon": - points = shape.get("points") - if not points or len(points) < 2: - continue - closed = points if points[0] == points[-1] else points + [points[0]] # type: ignore[operator] - coords = [coord for point in closed for coord in point] # type: ignore[misc] - item = self.canvas_orig.create_line( - *coords, - fill=accent, - width=2, - smooth=True, - capstyle="round", - joinstyle="round", - ) - self._exclude_canvas_ids.append(item) - - -__all__ = ["ImageProcessingMixin"] diff --git a/app/logic/reset.py b/app/logic/reset.py deleted file mode 100644 index e95aae4..0000000 --- a/app/logic/reset.py +++ /dev/null @@ -1,36 +0,0 @@ -"""Utility mixin for restoring default slider values.""" - -from __future__ import annotations - - -class ResetMixin: - def reset_sliders(self): - self.hue_min.set(self.DEFAULTS["hue_min"]) - self.hue_max.set(self.DEFAULTS["hue_max"]) - self.sat_min.set(self.DEFAULTS["sat_min"]) - self.val_min.set(self.DEFAULTS["val_min"]) - self.val_max.set(self.DEFAULTS["val_max"]) - self.alpha.set(self.DEFAULTS["alpha"]) - self.update_preview() - try: - default_hex = self._default_colour_hex() # type: ignore[attr-defined] - except Exception: - default_hex = None - if default_hex and hasattr(self, "_parse_hex_colour") and hasattr(self, "_update_selected_colour"): - try: - rgb = self._parse_hex_colour(default_hex) # type: ignore[attr-defined] - except Exception: - rgb = None - if rgb: - try: - self._update_selected_colour(*rgb) # type: ignore[arg-type,attr-defined] - except Exception: - pass - default_text = getattr(self, "status_default_text", None) - if default_text is None: - default_text = self._t("status.defaults_restored") if hasattr(self, "_t") else "Defaults restored." - if hasattr(self, "status"): - self.status.config(text=default_text) - - -__all__ = ["ResetMixin"] diff --git a/app/qt/app.py b/app/qt/app.py index 84bf2a5..a857eee 100644 --- a/app/qt/app.py +++ b/app/qt/app.py @@ -5,7 +5,7 @@ from __future__ import annotations import sys from pathlib import Path -from PySide6 import QtGui, QtWidgets +from PySide6 import QtCore, QtGui, QtWidgets from app.logic import DEFAULTS, LANGUAGE, RESET_EXCLUSIONS_ON_IMAGE_CHANGE from .main_window import MainWindow @@ -46,16 +46,21 @@ def create_application() -> QtWidgets.QApplication: def run() -> int: """Run the PySide6 GUI.""" app = create_application() + from app.logic import OVERLAY_COLOR window = MainWindow( language=LANGUAGE, defaults=DEFAULTS.copy(), reset_exclusions=RESET_EXCLUSIONS_ON_IMAGE_CHANGE, + overlay_color=OVERLAY_COLOR, ) - primary_screen = app.primaryScreen() - if primary_screen is not None: - geometry = primary_screen.availableGeometry() - window.setGeometry(geometry) - window.showMaximized() + + # Respect saved geometry from QSettings; fall back to maximised on first launch + settings = QtCore.QSettings("ICRA", "MainWindow") + if settings.value("geometry"): + window.show() else: + primary_screen = app.primaryScreen() + if primary_screen is not None: + window.setGeometry(primary_screen.availableGeometry()) window.showMaximized() return app.exec() diff --git a/app/qt/image_processor.py b/app/qt/image_processor.py index d89219a..fe20dfb 100644 --- a/app/qt/image_processor.py +++ b/app/qt/image_processor.py @@ -55,7 +55,8 @@ def _rgb_to_hsv_numpy(arr: np.ndarray) -> np.ndarray: v = cmax # Saturation - s = np.where(cmax > 0, delta / cmax, 0.0) + s = np.zeros_like(r) + np.divide(delta, cmax, out=s, where=cmax > 0) # Hue h = np.zeros_like(r) @@ -80,6 +81,11 @@ class QtImageProcessor: self.current_index: int = -1 self.stats = Stats() + # Overlay tint color + self.overlay_r = 255 + self.overlay_g = 0 + self.overlay_b = 0 + self.defaults: Dict[str, int] = { "hue_min": 0, "hue_max": 360, @@ -163,7 +169,7 @@ class QtImageProcessor: self.preview_img = self.orig_img.resize(size, Image.LANCZOS) def _rebuild_overlay(self) -> None: - """Build colour-match overlay using vectorized NumPy operations.""" + """Build color-match overlay using vectorized NumPy operations.""" if self.preview_img is None: self.overlay_img = None self.stats = Stats() @@ -212,7 +218,9 @@ class QtImageProcessor: # Build overlay image overlay_arr = np.zeros((base.height, base.width, 4), dtype=np.uint8) - overlay_arr[keep_match, 0] = 255 + overlay_arr[keep_match, 0] = self.overlay_r + overlay_arr[keep_match, 1] = self.overlay_g + overlay_arr[keep_match, 2] = self.overlay_b overlay_arr[keep_match, 3] = int(self.alpha) self.overlay_img = Image.fromarray(overlay_arr, "RGBA") @@ -239,7 +247,7 @@ class QtImageProcessor: val_ok = self.val_min <= v * 100.0 <= self.val_max return hue_ok and sat_ok and val_ok - def pick_colour(self, x: int, y: int) -> Tuple[float, float, float] | None: + def pick_color(self, x: int, y: int) -> Tuple[float, float, float] | None: """Return (hue°, sat%, val%) of the preview pixel at (x, y), or None.""" if self.preview_img is None: return None @@ -303,6 +311,17 @@ class QtImageProcessor: draw.polygon(points, fill=255) return mask + def set_overlay_color(self, hex_code: str) -> None: + """Set the RGB channels for the match overlay from a hex string.""" + if not hex_code.startswith("#") or len(hex_code) not in (7, 9): + return + try: + self.overlay_r = int(hex_code[1:3], 16) + self.overlay_g = int(hex_code[3:5], 16) + self.overlay_b = int(hex_code[5:7], 16) + except ValueError: + pass + def _build_exclusion_mask_numpy(self, size: Tuple[int, int]) -> np.ndarray: """Return a boolean (H, W) mask — True where pixels are excluded.""" w, h = size diff --git a/app/qt/main_window.py b/app/qt/main_window.py index 0c6129f..ddf609b 100644 --- a/app/qt/main_window.py +++ b/app/qt/main_window.py @@ -2,18 +2,20 @@ from __future__ import annotations +import csv from pathlib import Path from typing import Callable, Dict, List, Tuple +from PIL import Image from PySide6 import QtCore, QtGui, QtWidgets from app.i18n import I18nMixin from app.logic import SUPPORTED_IMAGE_EXTENSIONS from .image_processor import QtImageProcessor -DEFAULT_COLOUR = "#763e92" +DEFAULT_COLOR = "#763e92" -PRESET_COLOURS: List[Tuple[str, str]] = [ +PRESET_COLORS: List[Tuple[str, str]] = [ ("palette.swatch.red", "#ff3b30"), ("palette.swatch.orange", "#ff9500"), ("palette.swatch.yellow", "#ffd60a"), @@ -64,41 +66,8 @@ THEMES: Dict[str, Dict[str, str]] = { } -class ToolbarButton(QtWidgets.QPushButton): - """Rounded toolbar button inspired by the legacy design.""" - def __init__(self, icon_text: str, label: str, callback: Callable[[], None], parent: QtWidgets.QWidget | None = None): - text = f"{icon_text} {label}" - super().__init__(text, parent) - self.setCursor(QtCore.Qt.PointingHandCursor) - self.setFixedHeight(32) - metrics = QtGui.QFontMetrics(self.font()) - width = metrics.horizontalAdvance(text) + 28 - self.setMinimumWidth(width) - self.clicked.connect(callback) - - def apply_theme(self, colours: Dict[str, str]) -> None: - self.setStyleSheet( - f""" - QPushButton {{ - padding: 8px 16px; - border-radius: 10px; - border: 1px solid {colours['border']}; - background-color: rgba(255, 255, 255, 0.04); - color: {colours['text']}; - font-weight: 600; - }} - QPushButton:hover {{ - background-color: rgba(255, 255, 255, 0.12); - }} - QPushButton:pressed {{ - background-color: rgba(255, 255, 255, 0.18); - }} - """ - ) - - -class ColourSwatch(QtWidgets.QPushButton): +class ColorSwatch(QtWidgets.QPushButton): """Clickable palette swatch.""" def __init__(self, name: str, hex_code: str, callback: Callable[[str, str], None], parent: QtWidgets.QWidget | None = None): @@ -108,10 +77,10 @@ class ColourSwatch(QtWidgets.QPushButton): self.callback = callback self.setCursor(QtCore.Qt.PointingHandCursor) self.setFixedSize(28, 28) - self._apply_colour(hex_code) + self._apply_color(hex_code) self.clicked.connect(lambda: callback(hex_code, self.name_key)) - def _apply_colour(self, hex_code: str) -> None: + def _apply_color(self, hex_code: str) -> None: self.setStyleSheet( f""" QPushButton {{ @@ -125,16 +94,16 @@ class ColourSwatch(QtWidgets.QPushButton): """ ) - def apply_theme(self, colours: Dict[str, str]) -> None: + def apply_theme(self, colors: Dict[str, str]) -> None: self.setStyleSheet( f""" QPushButton {{ background-color: {self.hex_code}; - border: 2px solid {colours['border']}; + border: 2px solid {colors['border']}; border-radius: 6px; }} QPushButton:hover {{ - border-color: {colours['accent']}; + border-color: {colors['accent']}; }} """ ) @@ -194,22 +163,22 @@ class SliderControl(QtWidgets.QWidget): self.slider.blockSignals(False) self.value_edit.setText(str(value)) - def apply_theme(self, colours: Dict[str, str]) -> None: - self.title_label.setStyleSheet(f"color: {colours['text_muted']}; font-weight: 500;") + def apply_theme(self, colors: Dict[str, str]) -> None: + self.title_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;") self.value_edit.setStyleSheet( - f"color: {colours['text_dim']}; background: transparent; " - f"border: 1px solid {colours['border']}; border-radius: 4px; padding: 0 2px;" + f"color: {colors['text_dim']}; background: transparent; " + f"border: 1px solid {colors['border']}; border-radius: 4px; padding: 0 2px;" ) self.slider.setStyleSheet( f""" QSlider::groove:horizontal {{ - border: 1px solid {colours['border']}; + border: 1px solid {colors['border']}; height: 6px; background: rgba(255,255,255,0.14); border-radius: 4px; }} QSlider::handle:horizontal {{ - background: {colours['accent_secondary']}; + background: {colors['accent_secondary']}; border: 1px solid rgba(255,255,255,0.2); width: 14px; margin: -5px 0; @@ -271,8 +240,8 @@ class CanvasView(QtWidgets.QGraphicsView): def set_mode(self, mode: str) -> None: self.mode = mode - def set_accent(self, colour: str) -> None: - self._accent = QtGui.QColor(colour) + def set_accent(self, color: str) -> None: + self._accent = QtGui.QColor(color) self._redraw_shapes() def undo_last(self) -> None: @@ -399,7 +368,7 @@ class CanvasView(QtWidgets.QGraphicsView): class OverlayCanvas(QtWidgets.QGraphicsView): - """Read-only QGraphicsView for displaying the colour-match overlay.""" + """Read-only QGraphicsView for displaying the color-match overlay.""" def __init__(self, parent: QtWidgets.QWidget | None = None) -> None: super().__init__(parent) @@ -488,17 +457,17 @@ class TitleBar(QtWidgets.QWidget): ) return btn - def apply_theme(self, colours: Dict[str, str]) -> None: + def apply_theme(self, colors: Dict[str, str]) -> None: palette = self.palette() - palette.setColor(QtGui.QPalette.Window, QtGui.QColor(colours["titlebar_bg"])) + palette.setColor(QtGui.QPalette.Window, QtGui.QColor(colors["titlebar_bg"])) self.setPalette(palette) - self.title_label.setStyleSheet(f"color: {colours['text']}; font-weight: 600;") - hover_bg = "#d0342c" if colours["titlebar_bg"] != "#e9ebf5" else "#e6675a" + self.title_label.setStyleSheet(f"color: {colors['text']}; font-weight: 600;") + hover_bg = "#d0342c" if colors["titlebar_bg"] != "#e9ebf5" else "#e6675a" self.close_btn.setStyleSheet( f""" QPushButton {{ background-color: transparent; - color: {colours['text']}; + color: {colors['text']}; border: none; padding: 4px 10px; }} @@ -513,7 +482,7 @@ class TitleBar(QtWidgets.QWidget): f""" QPushButton {{ background-color: transparent; - color: {colours['text']}; + color: {colors['text']}; border: none; padding: 4px 10px; }} @@ -538,7 +507,7 @@ class TitleBar(QtWidgets.QWidget): class MainWindow(QtWidgets.QMainWindow, I18nMixin): """Main application window containing all controls.""" - def __init__(self, language: str, defaults: dict, reset_exclusions: bool) -> None: + def __init__(self, language: str, defaults: dict, reset_exclusions: bool, overlay_color: str | None = None) -> None: super().__init__() self.init_i18n(language) self.setWindowTitle(self._t("app.title")) @@ -560,12 +529,15 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self.processor = QtImageProcessor() self.processor.set_defaults(defaults) self.processor.reset_exclusions_on_switch = reset_exclusions + if overlay_color: + self.processor.set_overlay_color(overlay_color) self.content_layout = QtWidgets.QVBoxLayout(self.content) - self.content_layout.setContentsMargins(24, 24, 24, 24) + self.content_layout.setContentsMargins(24, 0, 24, 24) self.content_layout.setSpacing(18) - self.content_layout.addLayout(self._build_toolbar()) + self.content_layout.addWidget(self._build_menu_bar()) + self.content_layout.addLayout(self._build_palette()) self.content_layout.addLayout(self._build_sliders()) self.content_layout.addWidget(self._build_previews(), 1) @@ -576,7 +548,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self._is_maximised = False self._current_image_path: Path | None = None - self._current_colour = DEFAULT_COLOUR + self._current_color = DEFAULT_COLOR self._toolbar_actions: Dict[str, Callable[[], None]] = {} self._register_default_actions() @@ -587,7 +559,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self.image_view.pixel_clicked.connect(self._on_pixel_picked) self._sync_sliders_from_processor() - self._update_colour_display(DEFAULT_COLOUR, self._t("palette.current")) + self._update_color_display(DEFAULT_COLOR, self._t("palette.current")) self.current_theme = "dark" self._apply_theme(self.current_theme) @@ -598,6 +570,12 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): # Keyboard shortcuts self._setup_shortcuts() + # Slider debounce timer + self._slider_timer = QtCore.QTimer(self) + self._slider_timer.setSingleShot(True) + self._slider_timer.setInterval(80) + self._slider_timer.timeout.connect(self._refresh_overlay_only) + # Restore window geometry self._settings = QtCore.QSettings("ICRA", "MainWindow") geometry = self._settings.value("geometry") @@ -626,33 +604,38 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): # UI builders ------------------------------------------------------------ - def _build_toolbar(self) -> QtWidgets.QHBoxLayout: - layout = QtWidgets.QHBoxLayout() - layout.setSpacing(12) + def _build_menu_bar(self) -> QtWidgets.QMenuBar: + self.menu_bar = QtWidgets.QMenuBar(self) - buttons = [ - ("open_image", "🖼", "toolbar.open_image"), - ("open_folder", "📂", "toolbar.open_folder"), - ("choose_color", "🎨", "toolbar.choose_color"), - ("pick_from_image", "🖱", "toolbar.pick_from_image"), - ("save_overlay", "💾", "toolbar.save_overlay"), - ("toggle_free_draw", "△", "toolbar.toggle_free_draw"), - ("clear_excludes", "🧹", "toolbar.clear_excludes"), - ("undo_exclude", "↩", "toolbar.undo_exclude"), - ("reset_sliders", "🔄", "toolbar.reset_sliders"), - ("toggle_theme", "🌓", "toolbar.toggle_theme"), - ] - self._toolbar_buttons: Dict[str, ToolbarButton] = {} - for key, icon_txt, text_key in buttons: - label = self._t(text_key) - button = ToolbarButton(icon_txt, label, lambda _checked=False, k=key: self._invoke_action(k)) - layout.addWidget(button) - self._toolbar_buttons[key] = button + # File Menu + file_menu = self.menu_bar.addMenu(self._t("menu.file")) + file_menu.addAction("🖼 " + self._t("toolbar.open_image"), lambda: self._invoke_action("open_image"), "Ctrl+O") + file_menu.addAction("📂 " + self._t("toolbar.open_folder"), lambda: self._invoke_action("open_folder"), "Ctrl+Shift+O") + file_menu.addAction("📊 " + self._t("toolbar.export_folder"), lambda: self._invoke_action("export_folder")) + file_menu.addSeparator() + file_menu.addAction("💾 " + self._t("toolbar.save_overlay"), lambda: self._invoke_action("save_overlay"), "Ctrl+S") - layout.addStretch(1) + # Edit Menu + edit_menu = self.menu_bar.addMenu(self._t("menu.edit")) + edit_menu.addAction("↩ " + self._t("toolbar.undo_exclude"), lambda: self._invoke_action("undo_exclude"), "Ctrl+Z") + edit_menu.addAction("🧹 " + self._t("toolbar.clear_excludes"), lambda: self._invoke_action("clear_excludes")) + edit_menu.addSeparator() + edit_menu.addAction("🔄 " + self._t("toolbar.reset_sliders"), lambda: self._invoke_action("reset_sliders"), "Ctrl+R") + + # Tools Menu + tools_menu = self.menu_bar.addMenu(self._t("menu.tools")) + tools_menu.addAction("🎨 " + self._t("toolbar.choose_color"), lambda: self._invoke_action("choose_color")) + tools_menu.addAction("🖱 " + self._t("toolbar.pick_from_image"), lambda: self._invoke_action("pick_from_image")) + tools_menu.addAction("△ " + self._t("toolbar.toggle_free_draw"), lambda: self._invoke_action("toggle_free_draw")) + + # View Menu + view_menu = self.menu_bar.addMenu(self._t("menu.view")) + view_menu.addAction("🌓 " + self._t("toolbar.toggle_theme"), lambda: self._invoke_action("toggle_theme")) + + # Status label logic remains but moved to palette layout or kept minimal + # We will add it to the palette layout so that it stays on top self.status_label = QtWidgets.QLabel(self._t("status.no_file")) - layout.addWidget(self.status_label, 0, QtCore.Qt.AlignRight) - return layout + return self.menu_bar def _build_palette(self) -> QtWidgets.QHBoxLayout: layout = QtWidgets.QHBoxLayout() @@ -664,13 +647,13 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self.current_label = QtWidgets.QLabel(self._t("palette.current")) current_group.addWidget(self.current_label) - self.current_colour_swatch = QtWidgets.QLabel() - self.current_colour_swatch.setFixedSize(28, 28) - self.current_colour_swatch.setStyleSheet(f"background-color: {DEFAULT_COLOUR}; border-radius: 6px;") - current_group.addWidget(self.current_colour_swatch) + self.current_color_swatch = QtWidgets.QLabel() + self.current_color_swatch.setFixedSize(28, 28) + self.current_color_swatch.setStyleSheet(f"background-color: {DEFAULT_COLOR}; border-radius: 6px;") + current_group.addWidget(self.current_color_swatch) - self.current_colour_label = QtWidgets.QLabel(f"({DEFAULT_COLOUR})") - current_group.addWidget(self.current_colour_label) + self.current_color_label = QtWidgets.QLabel(f"({DEFAULT_COLOR})") + current_group.addWidget(self.current_color_label) layout.addLayout(current_group) self.more_label = QtWidgets.QLabel(self._t("palette.more")) @@ -678,13 +661,15 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): swatch_container = QtWidgets.QHBoxLayout() swatch_container.setSpacing(8) - self.swatch_buttons: List[ColourSwatch] = [] - for name_key, hex_code in PRESET_COLOURS: - swatch = ColourSwatch(self._t(name_key), hex_code, self._update_colour_display) + self.swatch_buttons: List[ColorSwatch] = [] + for name_key, hex_code in PRESET_COLORS: + swatch = ColorSwatch(self._t(name_key), hex_code, self._update_color_display) swatch_container.addWidget(swatch) self.swatch_buttons.append(swatch) layout.addLayout(swatch_container) + layout.addStretch(1) + layout.addWidget(self.status_label, 0, QtCore.Qt.AlignRight) return layout def _build_sliders(self) -> QtWidgets.QHBoxLayout: @@ -754,7 +739,8 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self._toolbar_actions = { "open_image": self.open_image, "open_folder": self.open_folder, - "choose_color": self.choose_colour, + "export_folder": self.export_folder, + "choose_color": self.choose_color, "pick_from_image": self.pick_from_image, "save_overlay": self.save_overlay, "toggle_free_draw": self.toggle_free_draw, @@ -810,6 +796,83 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self._current_image_path = loaded_path self._refresh_views() + def export_folder(self) -> None: + if not self.processor.preview_paths: + QtWidgets.QMessageBox.information(self, self._t("dialog.info_title"), self._t("dialog.no_image_loaded")) + return + + csv_path, _ = QtWidgets.QFileDialog.getSaveFileName( + self, + self._t("dialog.export_stats_title"), + str(self.processor.preview_paths[0].parent / "icra_stats.csv"), + self._t("dialog.csv_filter") + ) + if not csv_path: + return + + total = len(self.processor.preview_paths) + + is_eu = self.language == "de" + delimiter = ";" if is_eu else "," + decimal = "," if is_eu else "." + + headers = [ + "Filename", + "Color", + "Matching Pixels", + "Matching Pixels w/ Exclusions", + "Excluded Pixels" + ] + rows = [headers] + + for i, img_path in enumerate(self.processor.preview_paths): + self.status_label.setText(self._t("status.exporting", current=str(i+1), total=str(total))) + QtWidgets.QApplication.processEvents() # Keep UI vaguely responsive + + # Process without modifying the UI current_index + img = Image.open(img_path) + old_orig = self.processor.orig_img + old_preview = self.processor.preview_img + + self.processor.orig_img = img + self.processor._build_preview() + self.processor._rebuild_overlay() + s = self.processor.stats + + pct_all = (s.matches_all / s.total_all * 100) if s.total_all else 0.0 + pct_keep = (s.matches_keep / s.total_keep * 100) if s.total_keep else 0.0 + pct_excl = (s.total_excl / s.total_all * 100) if s.total_all else 0.0 + + pct_all_str = f"{pct_all:.2f}".replace(".", decimal) + pct_keep_str = f"{pct_keep:.2f}".replace(".", decimal) + pct_excl_str = f"{pct_excl:.2f}".replace(".", decimal) + + rows.append([ + img_path.name, + self._current_color, + pct_all_str, + pct_keep_str, + pct_excl_str + ]) + img.close() + + # Restore previous state + self.processor.orig_img = old_orig + self.processor.preview_img = old_preview + + # Compute max width per column for alignment, plus extra space so it's not cramped + col_widths = [max(len(str(item)) for item in col) + 4 for col in zip(*rows)] + + with open(csv_path, mode="w", newline="", encoding="utf-8") as f: + writer = csv.writer(f, delimiter=delimiter) + for row in rows: + padded_row = [f"{str(item):>{width}}" for item, width in zip(row, col_widths)] + writer.writerow(padded_row) + + # Restore overlay state for currently viewed image + self.processor._rebuild_overlay() + self.status_label.setText(self._t("status.export_done", path=csv_path)) + def show_previous_image(self) -> None: if not self.processor.preview_paths: QtWidgets.QMessageBox.information(self, self._t("dialog.info_title"), self._t("dialog.no_image_loaded")) @@ -838,17 +901,17 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): # Helpers ---------------------------------------------------------------- - def _update_colour_display(self, hex_code: str, label: str) -> None: - self._current_colour = hex_code - self.current_colour_swatch.setStyleSheet(f"background-color: {hex_code}; border-radius: 6px;") - self.current_colour_label.setText(f"({hex_code})") + def _update_color_display(self, hex_code: str, label: str) -> None: + self._current_color = hex_code + self.current_color_swatch.setStyleSheet(f"background-color: {hex_code}; border-radius: 6px;") + self.current_color_label.setText(f"({hex_code})") self.status_label.setText(f"{label}: {hex_code}") def _on_slider_change(self, key: str, value: int) -> None: self.processor.set_threshold(key, value) label = self._slider_title(key) self.status_label.setText(f"{label}: {value}") - self._refresh_overlay_only() + self._slider_timer.start() def _reset_sliders(self) -> None: for _, attr, _, _ in SLIDER_SPECS: @@ -904,7 +967,7 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self.status_label.setText(self._t("status.pick_mode_ended")) def _on_pixel_picked(self, x: int, y: int) -> None: - result = self.processor.pick_colour(x, y) + result = self.processor.pick_color(x, y) if result is None: self._exit_pick_mode() return @@ -927,14 +990,14 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): ctrl.set_value(value) self.processor.set_threshold(attr, value) - # Update colour swatch to the picked pixel colour + # Update color swatch to the picked pixel color h_norm = hue / 360.0 s_norm = sat / 100.0 v_norm = val / 100.0 import colorsys r, g, b = colorsys.hsv_to_rgb(h_norm, s_norm, v_norm) hex_code = "#{:02x}{:02x}{:02x}".format(int(r * 255), int(g * 255), int(b * 255)) - self._update_colour_display(hex_code, "") + self._update_color_display(hex_code, "") self.status_label.setText( self._t("status.pick_mode_from_image", hue=hue, saturation=sat, value=val) @@ -988,12 +1051,12 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self._settings.setValue("geometry", self.saveGeometry()) super().closeEvent(event) - def choose_colour(self) -> None: - colour = QtWidgets.QColorDialog.getColor(parent=self, title=self._t("dialog.choose_colour_title")) - if not colour.isValid(): + def choose_color(self) -> None: + color = QtWidgets.QColorDialog.getColor(parent=self, title=self._t("dialog.choose_color_title")) + if not color.isValid(): return - hex_code = colour.name() - self._update_colour_display(hex_code, self._t("dialog.choose_colour_title")) + hex_code = color.name() + self._update_color_display(hex_code, self._t("dialog.choose_color_title")) def save_overlay(self) -> None: pixmap = self.processor.overlay_pixmap() @@ -1034,34 +1097,66 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): self._apply_theme(self.current_theme) def _apply_theme(self, mode: str) -> None: - colours = THEMES[mode] - self.content.setStyleSheet(f"background-color: {colours['window_bg']};") + colors = THEMES[mode] + self.content.setStyleSheet(f"background-color: {colors['window_bg']};") self.image_view.setStyleSheet( - f"background-color: {colours['panel_bg']}; border: 1px solid {colours['border']}; border-radius: 12px;" + f"background-color: {colors['panel_bg']}; border: 1px solid {colors['border']}; border-radius: 12px;" ) - self.image_view.set_accent(colours["highlight"]) + self.image_view.set_accent(colors["highlight"]) self.overlay_view.setStyleSheet( - f"background-color: {colours['panel_bg']}; border: 1px solid {colours['border']}; border-radius: 12px;" + f"background-color: {colors['panel_bg']}; border: 1px solid {colors['border']}; border-radius: 12px;" ) - self.status_label.setStyleSheet(f"color: {colours['text_muted']}; font-weight: 500;") - self.current_label.setStyleSheet(f"color: {colours['text_muted']}; font-weight: 500;") - self.current_colour_label.setStyleSheet(f"color: {colours['text_dim']};") - self.more_label.setStyleSheet(f"color: {colours['text_muted']}; font-weight: 500;") - self.filename_label.setStyleSheet(f"color: {colours['text']}; font-weight: 600;") - self.ratio_label.setStyleSheet(f"color: {colours['highlight']}; font-weight: 600;") + self.status_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;") + self.current_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;") + self.current_color_label.setStyleSheet(f"color: {colors['text_dim']};") + self.more_label.setStyleSheet(f"color: {colors['text_muted']}; font-weight: 500;") + self.filename_label.setStyleSheet(f"color: {colors['text']}; font-weight: 600;") + self.ratio_label.setStyleSheet(f"color: {colors['highlight']}; font-weight: 600;") + + # Style MenuBar + self.menu_bar.setStyleSheet( + f""" + QMenuBar {{ + background-color: {colors['window_bg']}; + color: {colors['text']}; + font-weight: 500; + font-size: 13px; + border-bottom: 1px solid {colors['border']}; + }} + QMenuBar::item {{ + spacing: 8px; + padding: 6px 12px; + background: transparent; + border-radius: 4px; + }} + QMenuBar::item:selected {{ + background: rgba(128, 128, 128, 0.2); + }} + QMenu {{ + background-color: {colors['panel_bg']}; + color: {colors['text']}; + border: 1px solid {colors['border']}; + }} + QMenu::item {{ + padding: 6px 24px; + }} + QMenu::item:selected {{ + background-color: {colors['highlight']}; + color: #ffffff; + }} + """ + ) - for button in self._toolbar_buttons.values(): - button.apply_theme(colours) for swatch in self.swatch_buttons: - swatch.apply_theme(colours) + swatch.apply_theme(colors) for control in self._slider_controls.values(): - control.apply_theme(colours) + control.apply_theme(colors) self._style_nav_button(self.prev_button) self._style_nav_button(self.next_button) - self.title_bar.apply_theme(colours) + self.title_bar.apply_theme(colors) def _sync_sliders_from_processor(self) -> None: for _, attr, _, _ in SLIDER_SPECS: @@ -1076,11 +1171,11 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): return key def _style_nav_button(self, button: QtWidgets.QToolButton) -> None: - colours = THEMES[self.current_theme] + colors = THEMES[self.current_theme] button.setStyleSheet( - f"QToolButton {{ border-radius: 19px; background-color: {colours['panel_bg']}; " - f"border: 1px solid {colours['border']}; color: {colours['text']}; }}" - f"QToolButton:hover {{ background-color: {colours['accent_secondary']}; color: white; }}" + f"QToolButton {{ border-radius: 19px; background-color: {colors['panel_bg']}; " + f"border: 1px solid {colors['border']}; color: {colors['text']}; }}" + f"QToolButton:hover {{ background-color: {colors['accent_secondary']}; color: white; }}" ) button.setIconSize(QtCore.QSize(20, 20)) if button is getattr(self, "prev_button", None): @@ -1133,8 +1228,8 @@ class MainWindow(QtWidgets.QMainWindow, I18nMixin): return pixmap result = QtGui.QPixmap(pixmap) painter = QtGui.QPainter(result) - colour = QtGui.QColor(THEMES[self.current_theme]["highlight"]) - pen = QtGui.QPen(colour) + color = QtGui.QColor(THEMES[self.current_theme]["highlight"]) + pen = QtGui.QPen(color) pen.setWidth(3) pen.setCosmetic(True) pen.setCapStyle(QtCore.Qt.RoundCap) diff --git a/config.toml b/config.toml index fb26ca5..57155b1 100644 --- a/config.toml +++ b/config.toml @@ -5,6 +5,8 @@ language = "en" [options] # Set to true to clear exclusion shapes whenever the image changes. reset_exclusions_on_image_change = false +# Hex color code for the match overlay (e.g. "#ff0000" for Red, "#00ff00" for Green) +overlay_color = "#ff0000" [defaults] # Override any of the following keys to tweak the initial slider values: diff --git a/pyproject.toml b/pyproject.toml index 4e96982..0d36943 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,3 +23,8 @@ include = ["app"] [tool.setuptools.package-data] "app" = ["assets/logo.png", "lang/*.toml"] + +[dependency-groups] +dev = [ + "pytest>=9.0.2", +] diff --git a/tests/test_image_processor.py b/tests/test_image_processor.py new file mode 100644 index 0000000..e8037d2 --- /dev/null +++ b/tests/test_image_processor.py @@ -0,0 +1,89 @@ +import numpy as np +import pytest +from PIL import Image + +from app.qt.image_processor import Stats, _rgb_to_hsv_numpy, QtImageProcessor + + +def test_stats_summary(): + s = Stats( + matches_all=50, total_all=100, + matches_keep=40, total_keep=80, + matches_excl=10, total_excl=20 + ) + + # Mock translator + def mock_t(key, **kwargs): + if key == "stats.placeholder": + return "Placeholder" + return f"{kwargs['with_pct']:.1f} {kwargs['without_pct']:.1f} {kwargs['excluded_pct']:.1f} {kwargs['excluded_match_pct']:.1f}" + + res = s.summary(mock_t) + # with_pct: 40/80 = 50.0 + # without_pct: 50/100 = 50.0 + # excluded_pct: 20/100 = 20.0 + # excluded_match_pct: 10/20 = 50.0 + assert res == "50.0 50.0 20.0 50.0" + +def test_stats_empty(): + s = Stats() + assert s.summary(lambda k, **kw: "Empty") == "Empty" + + +def test_rgb_to_hsv_numpy(): + # Test red + arr = np.array([[[1.0, 0.0, 0.0]]], dtype=np.float32) + hsv = _rgb_to_hsv_numpy(arr) + assert np.allclose(hsv[0, 0], [0.0, 100.0, 100.0]) + + # Test green + arr = np.array([[[0.0, 1.0, 0.0]]], dtype=np.float32) + hsv = _rgb_to_hsv_numpy(arr) + assert np.allclose(hsv[0, 0], [120.0, 100.0, 100.0]) + + # Test blue + arr = np.array([[[0.0, 0.0, 1.0]]], dtype=np.float32) + hsv = _rgb_to_hsv_numpy(arr) + assert np.allclose(hsv[0, 0], [240.0, 100.0, 100.0]) + + # Test white + arr = np.array([[[1.0, 1.0, 1.0]]], dtype=np.float32) + hsv = _rgb_to_hsv_numpy(arr) + assert np.allclose(hsv[0, 0], [0.0, 0.0, 100.0]) + + # Test black + arr = np.array([[[0.0, 0.0, 0.0]]], dtype=np.float32) + hsv = _rgb_to_hsv_numpy(arr) + assert np.allclose(hsv[0, 0], [0.0, 0.0, 0.0]) + + +def test_qt_processor_matches_legacy(): + proc = QtImageProcessor() + proc.hue_min = 350 + proc.hue_max = 10 + proc.sat_min = 50 + proc.val_min = 50 + proc.val_max = 100 + + # Red wraps around 360, so H=0 -> ok + assert proc._matches(255, 0, 0) is True + # Green H=120 -> fail + assert proc._matches(0, 255, 0) is False + # Dark red S=100, V=25 -> fail because val_min=50 + assert proc._matches(64, 0, 0) is False + +def test_set_overlay_color(): + proc = QtImageProcessor() + # default red + assert proc.overlay_r == 255 + assert proc.overlay_g == 0 + assert proc.overlay_b == 0 + + proc.set_overlay_color("#00ff00") + assert proc.overlay_r == 0 + assert proc.overlay_g == 255 + assert proc.overlay_b == 0 + + # invalid hex does nothing + proc.set_overlay_color("blue") + assert proc.overlay_r == 0