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.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
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")
|
||||
@@ -9,6 +11,31 @@ 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()}
|
||||
|
||||
Reference in New Issue
Block a user