Files
MayoStainHelper/core/media.py
bartool 02edb186bb feat: Implement photo capture functionality
Implement the logic for capturing and saving photos from the live preview stream.

- Add `save_photo` method to `MediaRepository` to handle file saving and database updates.
- `MainController` now tracks the selected color and the latest camera frame.
- The "Take Photo" button is enabled only when a color is selected.
- Pressing the button saves the current preview frame to the correct media folder and refreshes the thumbnail list.
- Fixes indentation issues in `main_controller.py` caused by previous faulty replacements.
2025-10-14 10:04:01 +02:00

123 lines
4.5 KiB
Python

from pathlib import Path
import shutil
from datetime import datetime
from PySide6.QtGui import QPixmap
from core.database import db_manager
MEDIA_DIR = Path("media")
DEFAULT_ICON = Path("media/default_icon.jpg")
class MediaRepository:
def __init__(self):
self.db = db_manager
def save_photo(self, pixmap: QPixmap, color_name: str):
"""Saves a pixmap to the media directory for the given color."""
color_id = self.db.get_color_id(color_name)
if color_id is None:
print(f"Error: Could not find color ID for {color_name}")
return
# Create directory if it doesn't exist
color_dir = MEDIA_DIR / color_name
color_dir.mkdir(parents=True, exist_ok=True)
# Generate unique filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
filename = f"photo_{timestamp}.jpg"
file_path = color_dir / filename
# Save the pixmap
if not pixmap.save(str(file_path), "JPG", 95):
print(f"Error: Failed to save pixmap to {file_path}")
return
# Add to database
self.db.add_media(color_id, str(file_path), 'photo')
print(f"Saved photo to {file_path}")
def sync_media(self):
disk_colors = {d.name for d in MEDIA_DIR.iterdir() if d.is_dir()}
db_colors = {c["name"] for c in self.db.get_all_colors()}
# usuwanie brakujących kolorów
for missing in db_colors - disk_colors:
self.db.delete_color(missing)
# dodawanie nowych kolorów
for color in disk_colors - db_colors:
# has_icon = (MEDIA_DIR / color / "icon.jpg").exists()
# self.db.add_color(color, has_icon=has_icon)
# icon = MEDIA_DIR / color / "icon.jpg"
self.add_color(color)
# sprawdzanie plików dla każdego koloru
for color in disk_colors:
color_id = self.db.get_color_id(color)
if color_id is None:
continue
color_dir = MEDIA_DIR / color
icon_file = color_dir / "icon.jpg"
self.db.update_color_icon(color, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
disk_files = {f.as_posix() for f in color_dir.iterdir() if f.is_file() and f.name != "icon.jpg"}
db_files = {m["media_path"] for m in self.db.get_media_for_color(color_id)}
# usuń brakujące
for missing in db_files - disk_files:
self.db.delete_media(color_id, missing)
# dodaj nowe
for new in disk_files - db_files:
ftype = "photo" if Path(new).suffix.lower() in [".jpg", ".png"] else "video"
self.db.add_media(color_id, new, ftype)
def add_color(self, name: str, icon_path: Path | None = None):
color_dir = MEDIA_DIR / name
color_dir.mkdir(parents=True, exist_ok=True)
icon_file = color_dir / "icon.jpg"
if icon_path and icon_path.exists():
shutil.copy(icon_path, icon_file)
self.db.add_color(name, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
def remove_color(self, name: str):
if (MEDIA_DIR / name).exists():
shutil.rmtree(MEDIA_DIR / name)
self.db.delete_color(name)
def update_color(self, old_name: str, new_name: str, icon_path: Path | None = None):
old_dir = MEDIA_DIR / old_name
new_dir = MEDIA_DIR / new_name
icon_file = new_dir / "icon.jpg"
if old_dir.exists():
old_dir.rename(new_dir)
if icon_path and icon_path.exists():
shutil.copy(icon_path, icon_file)
self.db.update_color_name(old_name, new_name)
self.db.update_color_icon(new_name, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
def add_media(self, color: str, file_path: Path):
target_dir = MEDIA_DIR / color
target_dir.mkdir(exist_ok=True)
target_file = target_dir / file_path.name
shutil.copy(file_path, target_file)
ftype = "photo" if file_path.suffix.lower() in [".jpg", ".png"] else "video"
color_id = self.db.get_color_id(color)
if color_id is not None:
self.db.add_media(color_id, target_file.as_posix(), ftype)
def remove_media(self, color: str, file_path: Path):
# file_path = MEDIA_DIR / color / filename
if file_path.exists():
file_path.unlink()
color_id = self.db.get_color_id(color)
if color_id is not None:
self.db.delete_media(color_id, file_path.as_posix())