Compare commits
18 Commits
6f89c6878a
...
dev-linux
| Author | SHA1 | Date | |
|---|---|---|---|
| 3841b44a0a | |||
| 2187536c7d | |||
| 6133c9fb18 | |||
| e006fea037 | |||
| 6acb690f16 | |||
| ab266c2767 | |||
| 2072cd8c93 | |||
| cfea46c653 | |||
| 46af4e8588 | |||
| 05024f075c | |||
| b9caf46104 | |||
| 9d389e6e5f | |||
| 9d60843ec5 | |||
| 63e6386239 | |||
| 1d627080ce | |||
| 1aa65743e9 | |||
| 69a31e153f | |||
| 1896d75c50 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -216,3 +216,7 @@ __marimo__/
|
|||||||
|
|
||||||
# Streamlit
|
# Streamlit
|
||||||
.streamlit/secrets.toml
|
.streamlit/secrets.toml
|
||||||
|
|
||||||
|
|
||||||
|
media/
|
||||||
|
*.db
|
||||||
31
controllers/camera_controller.py
Normal file
31
controllers/camera_controller.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
from PySide6.QtCore import QObject, QThread, Signal
|
||||||
|
from ..core.base import BaseImageSource, BaseControlSource
|
||||||
|
|
||||||
|
|
||||||
|
class CameraController(QObject):
|
||||||
|
new_frame = Signal(object)
|
||||||
|
errorOccurred = Signal(str)
|
||||||
|
|
||||||
|
def __init__(self, image_source: BaseImageSource, control_source: BaseControlSource, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.image_source = image_source
|
||||||
|
self.control_source = control_source
|
||||||
|
|
||||||
|
self.camera_thread = QThread()
|
||||||
|
self.moveToThread(self.camera_thread)
|
||||||
|
|
||||||
|
self.image_source.moveToThread(self.camera_thread)
|
||||||
|
self.control_source.moveToThread(self.camera_thread)
|
||||||
|
|
||||||
|
self.image_source.new_frame.connect(self.new_frame)
|
||||||
|
self.image_source.errorOccurred.connect(self.errorOccurred)
|
||||||
|
self.control_source.errorOccurred.connect(self.errorOccurred)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.camera_thread.start()
|
||||||
|
self.image_source.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.image_source.stop()
|
||||||
|
self.camera_thread.quit()
|
||||||
|
self.camera_thread.wait()
|
||||||
78
controllers/main_controller.py
Normal file
78
controllers/main_controller.py
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
from PySide6.QtWidgets import QPushButton
|
||||||
|
from pathlib import Path
|
||||||
|
from core.database import DatabaseManager
|
||||||
|
from core.media import MediaRepository
|
||||||
|
from ui.widgets.color_list_widget import ColorListWidget
|
||||||
|
from ui.widgets.thumbnail_list_widget import ThumbnailListWidget
|
||||||
|
from ui.widgets.split_view_widget import SplitView
|
||||||
|
from .camera_controller import CameraController
|
||||||
|
from ..core.gphoto_adapter import GPhotoImageSource, GPhotoControlSource
|
||||||
|
import gphoto2 as gp
|
||||||
|
|
||||||
|
class MainController:
|
||||||
|
def __init__(self, view):
|
||||||
|
self.db = DatabaseManager()
|
||||||
|
self.db.connect()
|
||||||
|
self.media_repo = MediaRepository(self.db)
|
||||||
|
self.media_repo.sync_media()
|
||||||
|
|
||||||
|
camera = gp.Camera()
|
||||||
|
camera.init()
|
||||||
|
stream = GPhotoImageSource(camera=camera, fps=15)
|
||||||
|
controll = GPhotoControlSource(camera=camera)
|
||||||
|
self.camera_controller = CameraController(stream, controll)
|
||||||
|
|
||||||
|
self.view = view
|
||||||
|
self.color_list: ColorListWidget = view.color_list_widget
|
||||||
|
self.thumbnail_list: ThumbnailListWidget = view.thumbnail_widget
|
||||||
|
self.split_view: SplitView = view.preview_widget
|
||||||
|
|
||||||
|
self.photo_button: QPushButton = view.photo_button
|
||||||
|
self.photo_button.clicked.connect(self.take_photo)
|
||||||
|
|
||||||
|
self.color_list.colorSelected.connect(self.on_color_selected)
|
||||||
|
self.color_list.editColor.connect(self.on_edit_color)
|
||||||
|
self.thumbnail_list.selectedThumbnail.connect(self.on_thumbnail_selected)
|
||||||
|
|
||||||
|
self.camera_controller.errorOccurred.connect(self.split_view.widget_start.set_info_text)
|
||||||
|
self.camera_controller.new_frame.connect(self.split_view.set_live_image)
|
||||||
|
self.split_view.widget_start.camera_start_btn.clicked.connect(self.camera_controller.start)
|
||||||
|
|
||||||
|
def start_camera(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def load_colors(self) -> None:
|
||||||
|
colors = self.db.get_all_colors()
|
||||||
|
print("Loaded colors:", colors)
|
||||||
|
self.color_list.set_colors(colors)
|
||||||
|
|
||||||
|
|
||||||
|
def on_color_selected(self, color_name: str):
|
||||||
|
print(f"Wybrano kolor: {color_name}")
|
||||||
|
color_id = self.db.get_color_id(color_name)
|
||||||
|
if color_id is not None:
|
||||||
|
media_items = self.db.get_media_for_color(color_id)
|
||||||
|
print(f"Media dla koloru {color_name} (ID: {color_id}):", media_items)
|
||||||
|
|
||||||
|
self.thumbnail_list.list_widget.clear()
|
||||||
|
for media in media_items:
|
||||||
|
if media['file_type'] == 'photo':
|
||||||
|
file_name = Path(media['media_path']).name
|
||||||
|
self.thumbnail_list.add_thumbnail(media['media_path'], file_name, media['id'])
|
||||||
|
else:
|
||||||
|
print(f"Nie znaleziono koloru o nazwie: {color_name}")
|
||||||
|
|
||||||
|
def on_edit_color(self, color_name: str):
|
||||||
|
print(f"Edycja koloru: {color_name}")
|
||||||
|
|
||||||
|
def on_thumbnail_selected(self, media_id: int):
|
||||||
|
media = self.db.get_media(media_id)
|
||||||
|
if media:
|
||||||
|
print(f"Wybrano miniaturę o ID: {media_id}, ścieżka: {media['media_path']}")
|
||||||
|
self.split_view.set_reference_image(media['media_path'])
|
||||||
|
else:
|
||||||
|
print(f"Nie znaleziono mediów o ID: {media_id}")
|
||||||
|
|
||||||
|
def take_photo(self):
|
||||||
|
print("Robienie zdjęcia...")
|
||||||
|
self.split_view.toglle_live_view()
|
||||||
64
controllers/mock_gphoto.py
Normal file
64
controllers/mock_gphoto.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
class GPhoto2Error(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class CameraFileMock:
|
||||||
|
"""Mock obiektu zwracanego przez gphoto2.Camera.capture_preview()"""
|
||||||
|
|
||||||
|
def __init__(self, frame: np.ndarray):
|
||||||
|
# Kodowanie do JPEG, żeby symulować prawdziwe dane z kamery
|
||||||
|
success, buf = cv2.imencode(".jpg", frame)
|
||||||
|
if not success:
|
||||||
|
raise GPhoto2Error("Nie udało się zakodować ramki testowej.")
|
||||||
|
self._data = buf.tobytes()
|
||||||
|
|
||||||
|
def get_data_and_size(self):
|
||||||
|
return self._data
|
||||||
|
return self._data, len(self._data)
|
||||||
|
|
||||||
|
|
||||||
|
class Camera:
|
||||||
|
def __init__(self):
|
||||||
|
self._frame_counter = 0
|
||||||
|
self._running = False
|
||||||
|
|
||||||
|
def init(self):
|
||||||
|
self._running = True
|
||||||
|
print("[my_gphoto] Kamera MOCK zainicjalizowana")
|
||||||
|
|
||||||
|
def exit(self):
|
||||||
|
self._running = False
|
||||||
|
print("[my_gphoto] Kamera MOCK wyłączona")
|
||||||
|
|
||||||
|
def capture_preview(self):
|
||||||
|
if not self._running:
|
||||||
|
raise GPhoto2Error("Kamera MOCK nie jest uruchomiona")
|
||||||
|
|
||||||
|
# przykład 1: wczytaj stały obrazek z pliku
|
||||||
|
# frame = cv2.imread("test_frame.jpg")
|
||||||
|
# if frame is None:
|
||||||
|
# raise GPhoto2Error("Nie znaleziono test_frame.jpg")
|
||||||
|
|
||||||
|
# przykład 2: wygeneruj kolorową planszę
|
||||||
|
h, w = 480, 640
|
||||||
|
color = (self._frame_counter % 255, 100, 200)
|
||||||
|
frame = np.full((h, w, 3), color, dtype=np.uint8)
|
||||||
|
|
||||||
|
# dodanie napisu
|
||||||
|
text = "OBRAZ TESTOWY"
|
||||||
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
|
scale = 1.5
|
||||||
|
thickness = 3
|
||||||
|
color_text = (255, 255, 255)
|
||||||
|
|
||||||
|
(text_w, text_h), _ = cv2.getTextSize(text, font, scale, thickness)
|
||||||
|
x = (w - text_w) // 2
|
||||||
|
y = (h + text_h) // 2
|
||||||
|
cv2.putText(frame, text, (x, y), font, scale, color_text, thickness, cv2.LINE_AA)
|
||||||
|
|
||||||
|
|
||||||
|
self._frame_counter += 1
|
||||||
|
return CameraFileMock(frame)
|
||||||
26
core/base.py
Normal file
26
core/base.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from PySide6.QtCore import QObject, Signal
|
||||||
|
from PySide6.QtGui import QPixmap
|
||||||
|
|
||||||
|
|
||||||
|
class BaseImageSource(QObject):
|
||||||
|
new_frame = Signal(QPixmap)
|
||||||
|
errorOccurred = Signal(str)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
class BaseControlSource(QObject):
|
||||||
|
errorOccurred = Signal(str)
|
||||||
|
parameterChanged = Signal(str, object)
|
||||||
|
|
||||||
|
def set_parameter(self, name: str, value):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def get_parameter(self, name: str):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def list_parameters(self) -> dict:
|
||||||
|
raise NotImplementedError
|
||||||
69
core/camera_manager.py
Normal file
69
core/camera_manager.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import cv2
|
||||||
|
import gphoto2 as gp
|
||||||
|
|
||||||
|
from controllers.camera_controller import CameraController
|
||||||
|
from .gphoto_adapter import GPhotoImageSource, GPhotoControlSource
|
||||||
|
from .opencv_adapter import OpenCVImageSource, OpenCVControlSource
|
||||||
|
|
||||||
|
|
||||||
|
class CameraManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.devices = [] # lista wykrytych kamer
|
||||||
|
|
||||||
|
def detect_devices(self):
|
||||||
|
self.devices.clear()
|
||||||
|
|
||||||
|
# --- Wykrywanie webcamów / grabberów HDMI
|
||||||
|
for index in range(5): # sprawdź kilka indeksów
|
||||||
|
cap = cv2.VideoCapture(index)
|
||||||
|
if cap.isOpened():
|
||||||
|
self.devices.append({
|
||||||
|
"id": f"opencv:{index}",
|
||||||
|
"name": f"Webcam / HDMI Grabber #{index}",
|
||||||
|
"type": "opencv",
|
||||||
|
"index": index
|
||||||
|
})
|
||||||
|
cap.release()
|
||||||
|
|
||||||
|
# --- Wykrywanie kamer gphoto2
|
||||||
|
cameras = gp.Camera.autodetect() # type: ignore
|
||||||
|
for i, (name, addr) in enumerate(cameras):
|
||||||
|
self.devices.append({
|
||||||
|
"id": f"gphoto:{i}",
|
||||||
|
"name": f"{name} ({addr})",
|
||||||
|
"type": "gphoto",
|
||||||
|
"addr": addr
|
||||||
|
})
|
||||||
|
|
||||||
|
return self.devices
|
||||||
|
|
||||||
|
def create_controller(self, device_id, hybrid_with=None):
|
||||||
|
"""
|
||||||
|
Tworzy CameraController na podstawie id urządzenia.
|
||||||
|
Można podać hybrid_with="opencv" albo "gphoto" żeby zbudować hybrydę.
|
||||||
|
"""
|
||||||
|
device = next((d for d in self.devices if d["id"] == device_id), None)
|
||||||
|
if not device:
|
||||||
|
raise ValueError(f"Nie znaleziono urządzenia {device_id}")
|
||||||
|
|
||||||
|
# Webcam / grabber
|
||||||
|
if device["type"] == "opencv":
|
||||||
|
cap = cv2.VideoCapture(device["index"])
|
||||||
|
img = OpenCVImageSource(device["index"])
|
||||||
|
ctrl = OpenCVControlSource(cap)
|
||||||
|
return CameraController(img, ctrl)
|
||||||
|
|
||||||
|
# GPhoto camera
|
||||||
|
elif device["type"] == "gphoto":
|
||||||
|
cam = gp.Camera() # type: ignore
|
||||||
|
cam.init()
|
||||||
|
img = GPhotoImageSource(cam)
|
||||||
|
ctrl = GPhotoControlSource(cam)
|
||||||
|
return CameraController(img, ctrl)
|
||||||
|
|
||||||
|
# Hybrydowy tryb
|
||||||
|
elif device["type"] == "hybrid":
|
||||||
|
raise NotImplementedError("Tu możesz połączyć OpenCV + GPhoto w hybrydę")
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Nieobsługiwany typ urządzenia: {device['type']}")
|
||||||
156
core/database.py
Normal file
156
core/database.py
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
# core/database.py
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
from pathlib import Path
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
DB_FILE = Path("app.db")
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseManager:
|
||||||
|
def __init__(self, db_path: Path = DB_FILE):
|
||||||
|
self.db_path = db_path
|
||||||
|
self.conn: sqlite3.Connection | None = None
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Połączenie z bazą
|
||||||
|
# -------------------------
|
||||||
|
def connect(self):
|
||||||
|
"""Nawiązuje połączenie z bazą i tworzy tabele, jeśli ich nie ma."""
|
||||||
|
self.conn = sqlite3.connect(self.db_path)
|
||||||
|
self.conn.row_factory = sqlite3.Row
|
||||||
|
self._create_tables()
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
if self.conn:
|
||||||
|
self.conn.close()
|
||||||
|
self.conn = None
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Tworzenie tabel
|
||||||
|
# -------------------------
|
||||||
|
def _create_tables(self):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS colors (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT UNIQUE NOT NULL,
|
||||||
|
icon_path TEXT NOT NULL
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS media (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
color_id INTEGER NOT NULL,
|
||||||
|
media_path TEXT NOT NULL,
|
||||||
|
file_type TEXT CHECK(file_type IN ('photo','video')),
|
||||||
|
timestamp TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY(color_id) REFERENCES colors(id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Operacje na kolorach
|
||||||
|
# -------------------------
|
||||||
|
def add_color(self, name: str, icon_path: str):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"INSERT OR IGNORE INTO colors (name, icon_path) VALUES (?, ?)",
|
||||||
|
(name, icon_path),
|
||||||
|
)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def get_color_id(self, name: str) -> int | None:
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("SELECT id FROM colors WHERE name = ?", (name,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
return row["id"] if row else None
|
||||||
|
|
||||||
|
def get_all_colors(self) -> list[dict]:
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("SELECT * FROM colors ORDER BY name")
|
||||||
|
rows = cur.fetchall()
|
||||||
|
return [dict(r) for r in rows]
|
||||||
|
|
||||||
|
def update_color_name(self, old_name: str, new_name: str):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("UPDATE colors SET name = ? WHERE name = ?", (new_name, old_name))
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def update_color_icon(self, name: str, icon_path: str):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("UPDATE colors SET icon_path = ? WHERE name = ?", (icon_path, name))
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def delete_color(self, name: str):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("DELETE FROM colors WHERE name = ?", (name,))
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Operacje na plikach
|
||||||
|
# -------------------------
|
||||||
|
def add_media(self, color_id: int, media_path: str, file_type: str, timestamp: str | None = None):
|
||||||
|
if timestamp is None:
|
||||||
|
timestamp = datetime.now().isoformat()
|
||||||
|
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO media (color_id, media_path, file_type, timestamp) VALUES (?, ?, ?, ?)",
|
||||||
|
(color_id, media_path, file_type, timestamp),
|
||||||
|
)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def get_media(self, media_id: int) -> dict | None:
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("SELECT * FROM media WHERE id = ?", (media_id,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
return dict(row) if row else None
|
||||||
|
|
||||||
|
def get_media_for_color(self, color_id: int) -> list[dict]:
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("SELECT * FROM media WHERE color_id = ? ORDER BY timestamp DESC", (color_id,))
|
||||||
|
rows = cur.fetchall()
|
||||||
|
return [dict(r) for r in rows]
|
||||||
|
|
||||||
|
def delete_media(self, color_id: int, media_path: str):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("DELETE FROM media WHERE color_id = ? AND media_path = ?", (color_id, media_path))
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def delete_all_media_for_color(self, color_id: int):
|
||||||
|
if self.conn is None:
|
||||||
|
raise RuntimeError("Database not connected")
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
cur.execute("DELETE FROM media WHERE color_id = ?", (color_id,))
|
||||||
|
self.conn.commit()
|
||||||
84
core/gphoto_adapter.py
Normal file
84
core/gphoto_adapter.py
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
from PySide6.QtCore import QObject, QThread, Signal, QTimer
|
||||||
|
from PySide6.QtGui import QImage, QPixmap
|
||||||
|
|
||||||
|
from .base import BaseImageSource, BaseControlSource
|
||||||
|
|
||||||
|
import gphoto2 as gp
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# import gphoto2 as gp
|
||||||
|
# except:
|
||||||
|
# from . import mock_gphoto as gp
|
||||||
|
|
||||||
|
class GPhotoImageSource(BaseImageSource):
|
||||||
|
|
||||||
|
def __init__(self, camera: gp.Camera, fps=10, parent=None): # type: ignore
|
||||||
|
super().__init__(parent)
|
||||||
|
self.camera = camera
|
||||||
|
self.fps = fps
|
||||||
|
self.timer = None
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.timer = QTimer()
|
||||||
|
self.timer.timeout.connect(self._grab_frame)
|
||||||
|
self.timer.start(int(1000 / self.fps))
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
if self.timer:
|
||||||
|
self.timer.stop()
|
||||||
|
|
||||||
|
def _grab_frame(self):
|
||||||
|
try:
|
||||||
|
file = self.camera.capture_preview()
|
||||||
|
data = file.get_data_and_size()
|
||||||
|
frame = np.frombuffer(data, dtype=np.uint8)
|
||||||
|
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
|
||||||
|
if frame is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||||
|
h, w, ch = rgb_image.shape
|
||||||
|
qimg = QImage(rgb_image.data, w, h, ch * w, QImage.Format.Format_RGB888)
|
||||||
|
pixmap = QPixmap.fromImage(qimg)
|
||||||
|
self.new_frame.emit(pixmap)
|
||||||
|
except gp.GPhoto2Error as e:
|
||||||
|
self.errorOccurred.emit(f"GPhoto2 error: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
class GPhotoControlSource(BaseControlSource):
|
||||||
|
|
||||||
|
def __init__(self, camera: gp.Camera, parent=None): # type: ignore
|
||||||
|
super().__init__(parent)
|
||||||
|
self.camera = camera
|
||||||
|
|
||||||
|
def set_parameter(self, name, value):
|
||||||
|
try:
|
||||||
|
config = self.camera.get_config()
|
||||||
|
child = config.get_child_by_name(name)
|
||||||
|
child.set_value(value)
|
||||||
|
self.camera.set_config(config)
|
||||||
|
self.parameterChanged.emit(name, value)
|
||||||
|
except gp.GPhoto2Error as e:
|
||||||
|
self.errorOccurred.emit(str(e))
|
||||||
|
|
||||||
|
def get_parameter(self, name):
|
||||||
|
try:
|
||||||
|
config = self.camera.get_config()
|
||||||
|
child = config.get_child_by_name(name)
|
||||||
|
return child.get_value()
|
||||||
|
except gp.GPhoto2Error as e:
|
||||||
|
self.errorOccurred.emit(str(e))
|
||||||
|
return None
|
||||||
|
|
||||||
|
def list_parameters(self):
|
||||||
|
params = {}
|
||||||
|
try:
|
||||||
|
config = self.camera.get_config()
|
||||||
|
for child in config.get_children():
|
||||||
|
params[child.get_name()] = child.get_value()
|
||||||
|
except gp.GPhoto2Error as e:
|
||||||
|
self.errorOccurred.emit(str(e))
|
||||||
|
return params
|
||||||
95
core/media.py
Normal file
95
core/media.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
from core.database import DatabaseManager
|
||||||
|
|
||||||
|
MEDIA_DIR = Path("media")
|
||||||
|
DEFAULT_ICON = Path("media/default_icon.jpg")
|
||||||
|
|
||||||
|
class MediaRepository:
|
||||||
|
def __init__(self, db: DatabaseManager):
|
||||||
|
self.db = db
|
||||||
|
|
||||||
|
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())
|
||||||
64
core/mock_gphoto.py
Normal file
64
core/mock_gphoto.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
class GPhoto2Error(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class CameraFileMock:
|
||||||
|
"""Mock obiektu zwracanego przez gphoto2.Camera.capture_preview()"""
|
||||||
|
|
||||||
|
def __init__(self, frame: np.ndarray):
|
||||||
|
# Kodowanie do JPEG, żeby symulować prawdziwe dane z kamery
|
||||||
|
success, buf = cv2.imencode(".jpg", frame)
|
||||||
|
if not success:
|
||||||
|
raise GPhoto2Error("Nie udało się zakodować ramki testowej.")
|
||||||
|
self._data = buf.tobytes()
|
||||||
|
|
||||||
|
def get_data_and_size(self):
|
||||||
|
return self._data
|
||||||
|
return self._data, len(self._data)
|
||||||
|
|
||||||
|
|
||||||
|
class Camera:
|
||||||
|
def __init__(self):
|
||||||
|
self._frame_counter = 0
|
||||||
|
self._running = False
|
||||||
|
|
||||||
|
def init(self):
|
||||||
|
self._running = True
|
||||||
|
print("[my_gphoto] Kamera MOCK zainicjalizowana")
|
||||||
|
|
||||||
|
def exit(self):
|
||||||
|
self._running = False
|
||||||
|
print("[my_gphoto] Kamera MOCK wyłączona")
|
||||||
|
|
||||||
|
def capture_preview(self):
|
||||||
|
if not self._running:
|
||||||
|
raise GPhoto2Error("Kamera MOCK nie jest uruchomiona")
|
||||||
|
|
||||||
|
# przykład 1: wczytaj stały obrazek z pliku
|
||||||
|
# frame = cv2.imread("test_frame.jpg")
|
||||||
|
# if frame is None:
|
||||||
|
# raise GPhoto2Error("Nie znaleziono test_frame.jpg")
|
||||||
|
|
||||||
|
# przykład 2: wygeneruj kolorową planszę
|
||||||
|
h, w = 480, 640
|
||||||
|
color = (self._frame_counter % 255, 100, 200)
|
||||||
|
frame = np.full((h, w, 3), color, dtype=np.uint8)
|
||||||
|
|
||||||
|
# dodanie napisu
|
||||||
|
text = "OBRAZ TESTOWY"
|
||||||
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
|
scale = 1.5
|
||||||
|
thickness = 3
|
||||||
|
color_text = (255, 255, 255)
|
||||||
|
|
||||||
|
(text_w, text_h), _ = cv2.getTextSize(text, font, scale, thickness)
|
||||||
|
x = (w - text_w) // 2
|
||||||
|
y = (h + text_h) // 2
|
||||||
|
cv2.putText(frame, text, (x, y), font, scale, color_text, thickness, cv2.LINE_AA)
|
||||||
|
|
||||||
|
|
||||||
|
self._frame_counter += 1
|
||||||
|
return CameraFileMock(frame)
|
||||||
83
core/opencv_adapter.py
Normal file
83
core/opencv_adapter.py
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
from PySide6.QtCore import QObject, Signal, QTimer
|
||||||
|
from PySide6.QtGui import QImage, QPixmap
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
from .base import BaseImageSource, BaseControlSource
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class OpenCVImageSource(BaseImageSource):
|
||||||
|
|
||||||
|
def __init__(self, device_index=0, fps=30, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.device_index = device_index
|
||||||
|
self.fps = fps
|
||||||
|
self.cap = None
|
||||||
|
self.timer = None
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.cap = cv2.VideoCapture(self.device_index)
|
||||||
|
if not self.cap.isOpened():
|
||||||
|
self.errorOccurred.emit(f"Nie mogę otworzyć kamery {self.device_index}")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.timer = QTimer()
|
||||||
|
self.timer.timeout.connect(self._grab_frame)
|
||||||
|
self.timer.start(int(1000 / self.fps))
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
if self.timer:
|
||||||
|
self.timer.stop()
|
||||||
|
if self.cap:
|
||||||
|
self.cap.release()
|
||||||
|
|
||||||
|
def _grab_frame(self):
|
||||||
|
if self.cap is None:
|
||||||
|
self.errorOccurred.emit(f"Kamera niezaincjalizowana!")
|
||||||
|
return
|
||||||
|
|
||||||
|
ret, frame = self.cap.read()
|
||||||
|
if not ret:
|
||||||
|
self.errorOccurred.emit("Brak obrazu z kamery OpenCV")
|
||||||
|
return
|
||||||
|
|
||||||
|
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||||
|
h, w, ch = rgb_image.shape
|
||||||
|
qimg = QImage(rgb_image.data, w, h, ch * w, QImage.Format.Format_RGB888)
|
||||||
|
pixmap = QPixmap.fromImage(qimg)
|
||||||
|
self.new_frame.emit(pixmap)
|
||||||
|
|
||||||
|
|
||||||
|
class OpenCVControlSource(BaseControlSource):
|
||||||
|
|
||||||
|
def __init__(self, cap: cv2.VideoCapture, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.cap = cap
|
||||||
|
|
||||||
|
def set_parameter(self, name, value):
|
||||||
|
prop_id = getattr(cv2, name, None)
|
||||||
|
if prop_id is None:
|
||||||
|
self.errorOccurred.emit(f"Nieznany parametr {name}")
|
||||||
|
return
|
||||||
|
self.cap.set(prop_id, value)
|
||||||
|
self.parameterChanged.emit(name, value)
|
||||||
|
|
||||||
|
def get_parameter(self, name):
|
||||||
|
prop_id = getattr(cv2, name, None)
|
||||||
|
if prop_id is None:
|
||||||
|
self.errorOccurred.emit(f"Nieznany parametr {name}")
|
||||||
|
return None
|
||||||
|
return self.cap.get(prop_id)
|
||||||
|
|
||||||
|
def list_parameters(self):
|
||||||
|
return {
|
||||||
|
"CAP_PROP_BRIGHTNESS": self.cap.get(cv2.CAP_PROP_BRIGHTNESS),
|
||||||
|
"CAP_PROP_CONTRAST": self.cap.get(cv2.CAP_PROP_CONTRAST),
|
||||||
|
"CAP_PROP_SATURATION": self.cap.get(cv2.CAP_PROP_SATURATION),
|
||||||
|
"CAP_PROP_GAIN": self.cap.get(cv2.CAP_PROP_GAIN),
|
||||||
|
"CAP_PROP_EXPOSURE": self.cap.get(cv2.CAP_PROP_EXPOSURE),
|
||||||
|
}
|
||||||
19
main.py
Normal file
19
main.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import sys
|
||||||
|
from PySide6.QtWidgets import QApplication
|
||||||
|
|
||||||
|
from ui.main_window import MainWindow
|
||||||
|
from controllers.main_controller import MainController
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
app.setStyle("Fusion")
|
||||||
|
window = MainWindow()
|
||||||
|
controller = MainController(window)
|
||||||
|
controller.load_colors()
|
||||||
|
window.show()
|
||||||
|
sys.exit(app.exec())
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
6
requirements.txt
Normal file
6
requirements.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
numpy==2.2.6
|
||||||
|
opencv-python==4.12.0.88
|
||||||
|
PySide6==6.9.2
|
||||||
|
PySide6_Addons==6.9.2
|
||||||
|
PySide6_Essentials==6.9.2
|
||||||
|
shiboken6==6.9.2
|
||||||
66
ui/main_window.py
Normal file
66
ui/main_window.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import sys
|
||||||
|
from PySide6.QtWidgets import (
|
||||||
|
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
|
||||||
|
QPushButton, QLabel, QSplitter, QStackedWidget, QLineEdit
|
||||||
|
)
|
||||||
|
from PySide6.QtCore import Qt, Slot
|
||||||
|
from PySide6.QtGui import QPalette, QColor
|
||||||
|
|
||||||
|
from ui.widgets.placeholder_widget import PlaceholderWidget
|
||||||
|
from ui.widgets.color_list_widget import ColorListWidget
|
||||||
|
from ui.widgets.thumbnail_list_widget import ThumbnailListWidget
|
||||||
|
from ui.widgets.split_view_widget import SplitView
|
||||||
|
|
||||||
|
class MainWindow(QMainWindow):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.setWindowTitle("Mayo Stain Helper")
|
||||||
|
self.resize(1920, 1080)
|
||||||
|
self.setup_ui()
|
||||||
|
|
||||||
|
def setup_ui(self):
|
||||||
|
# Central widget and main layout
|
||||||
|
central_widget = QWidget()
|
||||||
|
main_layout = QHBoxLayout(central_widget)
|
||||||
|
self.setCentralWidget(central_widget)
|
||||||
|
|
||||||
|
self.preview_widget = SplitView()
|
||||||
|
|
||||||
|
self.thumbnail_widget = ThumbnailListWidget()
|
||||||
|
self.thumbnail_widget.setFixedWidth(200)
|
||||||
|
|
||||||
|
self.control_widget = QWidget()
|
||||||
|
self.control_widget.setFixedWidth(300)
|
||||||
|
|
||||||
|
main_layout.addWidget(self.preview_widget)
|
||||||
|
main_layout.addWidget(self.thumbnail_widget)
|
||||||
|
main_layout.addWidget(self.control_widget)
|
||||||
|
|
||||||
|
control_layout = QVBoxLayout(self.control_widget)
|
||||||
|
control_layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
|
||||||
|
histogram_view = PlaceholderWidget("Histogram View", "#FF5733")
|
||||||
|
histogram_view.setFixedHeight(200)
|
||||||
|
|
||||||
|
self.color_list_widget = ColorListWidget(self.control_widget)
|
||||||
|
|
||||||
|
self.record_button = QPushButton("Nagraj Wideo")
|
||||||
|
self.record_button.setMinimumHeight(40)
|
||||||
|
self.record_button.setStyleSheet("font-size: 12pt;")
|
||||||
|
|
||||||
|
self.photo_button = QPushButton("Zrób zdjęcie")
|
||||||
|
self.photo_button.setMinimumHeight(40)
|
||||||
|
self.photo_button.setStyleSheet("font-size: 12pt;")
|
||||||
|
|
||||||
|
control_layout.addWidget(histogram_view)
|
||||||
|
control_layout.addWidget(self.color_list_widget)
|
||||||
|
control_layout.addWidget(self.record_button)
|
||||||
|
control_layout.addWidget(self.photo_button)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
136
ui/widgets/color_list_widget.py
Normal file
136
ui/widgets/color_list_widget.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
from PySide6.QtWidgets import (
|
||||||
|
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QListView,
|
||||||
|
QAbstractItemView, QComboBox, QLabel, QStyledItemDelegate, QStyleOptionButton, QStyle,
|
||||||
|
)
|
||||||
|
from PySide6.QtGui import QPixmap, QIcon, QColor, QStandardItemModel, QStandardItem, QPainter, QCursor
|
||||||
|
from PySide6.QtCore import Qt, QSortFilterProxyModel, QSize, QRect, Signal, QModelIndex, QEvent, QItemSelectionModel
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
class FilterProxy(QSortFilterProxyModel):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.search_text = ""
|
||||||
|
|
||||||
|
def setSearchText(self, text: str):
|
||||||
|
self.search_text = text
|
||||||
|
self.invalidateFilter()
|
||||||
|
|
||||||
|
def filterAcceptsRow(self, source_row, source_parent):
|
||||||
|
if not self.search_text:
|
||||||
|
return True
|
||||||
|
model = self.sourceModel()
|
||||||
|
index = model.index(source_row, 0, source_parent)
|
||||||
|
name = model.data(index, Qt.ItemDataRole.DisplayRole)
|
||||||
|
return self.search_text.lower() in name.lower()
|
||||||
|
|
||||||
|
|
||||||
|
class EditButtonDelegate(QStyledItemDelegate):
|
||||||
|
editClicked = Signal(QModelIndex)
|
||||||
|
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.button_clicked_row = None
|
||||||
|
self.margin = 8
|
||||||
|
|
||||||
|
def paint(self, painter, option, index):
|
||||||
|
super().paint(painter, option, index)
|
||||||
|
|
||||||
|
if option.state & QStyle.StateFlag.State_MouseOver:
|
||||||
|
button = QStyleOptionButton()
|
||||||
|
button.rect = QRect(option.rect.right() - 60, option.rect.top() + self.margin , 60, option.rect.height() - self.margin * 2) # type: ignore
|
||||||
|
button.text = "EDYTUJ" # type: ignore
|
||||||
|
QApplication.style().drawControl(QStyle.ControlElement.CE_PushButton, button, painter)
|
||||||
|
|
||||||
|
|
||||||
|
def editorEvent(self, event, model, option, index):
|
||||||
|
button_rect = QRect(option.rect.right() - 60, option.rect.top() + self.margin , 60, option.rect.height() - self.margin * 2)
|
||||||
|
|
||||||
|
if event.type() == QEvent.Type.MouseButtonRelease:
|
||||||
|
# button_rect = QRect(option.rect.right() - 60, option.rect.top(), 60, option.rect.height())
|
||||||
|
if button_rect.contains(event.pos()):
|
||||||
|
self.button_clicked_row = index.row()
|
||||||
|
self.editClicked.emit(index)
|
||||||
|
return True
|
||||||
|
return super().editorEvent(event, model, option, index)
|
||||||
|
|
||||||
|
|
||||||
|
class ColorListWidget(QWidget):
|
||||||
|
editColor = Signal(str)
|
||||||
|
colorSelected = Signal(str)
|
||||||
|
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.colors = None
|
||||||
|
|
||||||
|
layout = QVBoxLayout(self)
|
||||||
|
|
||||||
|
self.filter_edit = QLineEdit()
|
||||||
|
self.filter_edit.setPlaceholderText("Szukaj po nazwie koloru...")
|
||||||
|
self.filter_edit.setMinimumHeight(32)
|
||||||
|
self.filter_edit.setStyleSheet("font-size: 12pt;")
|
||||||
|
|
||||||
|
edit_clear = self.filter_edit.addAction(QIcon("media/EditClear.svg"), QLineEdit.ActionPosition.TrailingPosition)
|
||||||
|
edit_clear.triggered.connect(lambda: self.filter_edit.clear())
|
||||||
|
|
||||||
|
layout.addWidget(self.filter_edit)
|
||||||
|
|
||||||
|
self.model = QStandardItemModel(self)
|
||||||
|
|
||||||
|
self.proxy_model = FilterProxy(self)
|
||||||
|
self.proxy_model.setSourceModel(self.model)
|
||||||
|
|
||||||
|
self.list_view = QListView()
|
||||||
|
self.list_view.setModel(self.proxy_model)
|
||||||
|
|
||||||
|
self.list_view.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
|
||||||
|
self.list_view.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
|
||||||
|
self.list_view.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectItems)
|
||||||
|
self.list_view.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
|
||||||
|
self.list_view.setUniformItemSizes(True)
|
||||||
|
self.list_view.setSpacing(2)
|
||||||
|
self.list_view.setIconSize(QSize(36, 36))
|
||||||
|
self.list_view.setMouseTracking(True)
|
||||||
|
|
||||||
|
layout.addWidget(self.list_view)
|
||||||
|
|
||||||
|
self.delegate = EditButtonDelegate(self.list_view)
|
||||||
|
self.delegate.editClicked.connect(self.on_edit_clicked)
|
||||||
|
self.list_view.setItemDelegate(self.delegate)
|
||||||
|
|
||||||
|
self.filter_edit.textChanged.connect(self.on_filter_changed)
|
||||||
|
self.list_view.clicked.connect(self.on_item_clicked)
|
||||||
|
|
||||||
|
def on_filter_changed(self, text: str):
|
||||||
|
self.proxy_model.setSearchText(text)
|
||||||
|
self.list_view.clearSelection()
|
||||||
|
|
||||||
|
def set_colors(self, db_colors: list[dict]):
|
||||||
|
self.model.clear()
|
||||||
|
for color in db_colors:
|
||||||
|
item = QStandardItem( color['name'])
|
||||||
|
item.setIcon(QIcon(color['icon_path']))
|
||||||
|
self.model.appendRow(item)
|
||||||
|
|
||||||
|
def on_item_clicked(self, index):
|
||||||
|
if self.delegate.button_clicked_row == index.row():
|
||||||
|
self.delegate.button_clicked_row = None
|
||||||
|
return
|
||||||
|
|
||||||
|
self.list_view.selectionModel().select(index, QItemSelectionModel.SelectionFlag.ClearAndSelect)
|
||||||
|
source_index = self.proxy_model.mapToSource(index)
|
||||||
|
item = self.model.itemFromIndex(source_index)
|
||||||
|
if item:
|
||||||
|
line = item.text()
|
||||||
|
# print(f"Kliknięto kolor: {line}")
|
||||||
|
self.colorSelected.emit(line)
|
||||||
|
return line
|
||||||
|
|
||||||
|
def on_edit_clicked(self, index):
|
||||||
|
source_index = self.proxy_model.mapToSource(index)
|
||||||
|
# print("Edit:", self.model.itemFromIndex(source_index).text())
|
||||||
|
item = self.model.itemFromIndex(source_index)
|
||||||
|
if item:
|
||||||
|
line = item.text()
|
||||||
|
# print(f"Edycja koloru: {line}")
|
||||||
|
self.editColor.emit(line)
|
||||||
35
ui/widgets/placeholder_widget.py
Normal file
35
ui/widgets/placeholder_widget.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout
|
||||||
|
from PySide6.QtGui import QPalette, QColor
|
||||||
|
from PySide6.QtCore import Qt
|
||||||
|
|
||||||
|
class PlaceholderWidget(QWidget):
|
||||||
|
"""A custom widget to display a colored background with centered text."""
|
||||||
|
def __init__(self, text: str, color: str, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.setAutoFillBackground(True)
|
||||||
|
self.set_color(color)
|
||||||
|
|
||||||
|
self.label = QLabel(text)
|
||||||
|
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
|
||||||
|
font = self.label.font()
|
||||||
|
font.setPointSize(24)
|
||||||
|
self.label.setFont(font)
|
||||||
|
self.label.setWordWrap(True)
|
||||||
|
|
||||||
|
layout = QVBoxLayout(self)
|
||||||
|
layout.addWidget(self.label)
|
||||||
|
self.setLayout(layout)
|
||||||
|
|
||||||
|
def set_text(self, text: str):
|
||||||
|
self.label.setText(text)
|
||||||
|
|
||||||
|
def set_color(self, color: str):
|
||||||
|
palette = self.palette()
|
||||||
|
palette.setColor(QPalette.ColorRole.Window, QColor(color))
|
||||||
|
self.setPalette(palette)
|
||||||
|
|
||||||
|
def set_text_color(self, color: str):
|
||||||
|
palette = self.label.palette()
|
||||||
|
palette.setColor(QPalette.ColorRole.WindowText, QColor(color))
|
||||||
|
self.label.setPalette(palette)
|
||||||
164
ui/widgets/split_view_widget.py
Normal file
164
ui/widgets/split_view_widget.py
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
from PySide6.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsPixmapItem, QApplication, QMainWindow, QWidget, QVBoxLayout, QSplitter, QStackedWidget, QPushButton, QLabel
|
||||||
|
from PySide6.QtGui import QPixmap, QWheelEvent, QPainter, QBrush, QColor
|
||||||
|
from PySide6.QtCore import Qt
|
||||||
|
import sys
|
||||||
|
from ui.widgets.placeholder_widget import PlaceholderWidget
|
||||||
|
|
||||||
|
class ZoomableImageView(QGraphicsView):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
|
||||||
|
# Scena i element obrazu
|
||||||
|
self._scene = QGraphicsScene(self)
|
||||||
|
self.setScene(self._scene)
|
||||||
|
self._scene.setBackgroundBrush(QBrush(QColor(20, 20, 20))) # ciemne tło
|
||||||
|
|
||||||
|
self._pixmap_item = QGraphicsPixmapItem()
|
||||||
|
self._scene.addItem(self._pixmap_item)
|
||||||
|
|
||||||
|
# Ustawienia widoku
|
||||||
|
self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag) # przesuwanie myszą
|
||||||
|
self.setRenderHint(QPainter.RenderHint.Antialiasing)
|
||||||
|
self.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
|
||||||
|
|
||||||
|
# Wyłączenie suwaków
|
||||||
|
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||||
|
self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||||
|
# Parametry zoomu
|
||||||
|
self._zoom_factor = 1.25
|
||||||
|
self._current_scale = 1.0
|
||||||
|
|
||||||
|
def set_image(self, pixmap: QPixmap):
|
||||||
|
# pixmap = QPixmap(image_path)
|
||||||
|
self._pixmap_item.setPixmap(pixmap)
|
||||||
|
self._scene.setSceneRect(pixmap.rect())
|
||||||
|
# self.reset_transform()
|
||||||
|
|
||||||
|
def reset_transform(self):
|
||||||
|
"""Resetuje skalowanie i ustawia 1:1"""
|
||||||
|
self._current_scale = 1.0
|
||||||
|
self.setTransform(self.transform().fromScale(1, 1))
|
||||||
|
|
||||||
|
def wheelEvent(self, event: QWheelEvent):
|
||||||
|
"""Zoom kółkiem myszy"""
|
||||||
|
if event.modifiers() & Qt.KeyboardModifier.ControlModifier: # zoom tylko z CTRL
|
||||||
|
if event.angleDelta().y() > 0:
|
||||||
|
zoom = self._zoom_factor
|
||||||
|
else:
|
||||||
|
zoom = 1 / self._zoom_factor
|
||||||
|
|
||||||
|
self._current_scale *= zoom
|
||||||
|
self.scale(zoom, zoom)
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
super().wheelEvent(event) # normalne przewijanie
|
||||||
|
|
||||||
|
class CameraPlaceholder(QWidget):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
|
||||||
|
self.setAutoFillBackground(True)
|
||||||
|
self.setStyleSheet("background-color: #141414;")
|
||||||
|
|
||||||
|
layout = QVBoxLayout(self)
|
||||||
|
layout.setSpacing(20)
|
||||||
|
|
||||||
|
self.camera_start_btn = QPushButton("Start Camera")
|
||||||
|
self.camera_start_btn.setFixedSize(200, 50)
|
||||||
|
style_sheet = """
|
||||||
|
QPushButton {
|
||||||
|
/* --- Styl podstawowy --- */
|
||||||
|
background-color: transparent;
|
||||||
|
border: 2px solid #CECECE; /* Grubość, styl i kolor obramowania */
|
||||||
|
border-radius: 25px; /* Kluczowa właściwość do zaokrąglenia rogów! */
|
||||||
|
color: #CECECE;
|
||||||
|
padding: 10px 20px; /* Wewnętrzny margines */
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPushButton:hover {
|
||||||
|
/* --- Styl po najechaniu myszką --- */
|
||||||
|
color: #F0F0F0;
|
||||||
|
border: 2px solid #F0F0F0;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPushButton:pressed {
|
||||||
|
/* --- Styl po naciśnięciu --- */
|
||||||
|
background-color: #e0e0e0; /* Ciemniejsze tło w momencie kliknięcia */
|
||||||
|
border: 2px solid #e0e0e0; /* Zmiana koloru ramki dla sygnalizacji akcji */
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
self.camera_start_btn.setStyleSheet(style_sheet)
|
||||||
|
|
||||||
|
self.info_label = QLabel("Kliknij, aby uruchomić kamerę")
|
||||||
|
self.info_label.setStyleSheet("background-color: transparent; color: #CECECE; font-size: 18px;")
|
||||||
|
self.info_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
|
||||||
|
layout.addStretch()
|
||||||
|
layout.addWidget(self.camera_start_btn, alignment=Qt.AlignmentFlag.AlignCenter)
|
||||||
|
layout.addWidget(self.info_label)
|
||||||
|
layout.addStretch()
|
||||||
|
self.setLayout(layout)
|
||||||
|
|
||||||
|
def set_info_text(self, text: str):
|
||||||
|
self.info_label.setText(text)
|
||||||
|
|
||||||
|
class SplitView(QSplitter):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
print("Inicjalizacja SplitView2")
|
||||||
|
self.setOrientation(Qt.Orientation.Vertical)
|
||||||
|
|
||||||
|
self.widget_start = CameraPlaceholder()
|
||||||
|
self.widget_live = ZoomableImageView()
|
||||||
|
# self.widget_live = PlaceholderWidget("Camera View", "#750466")
|
||||||
|
self.widget_ref = ZoomableImageView()
|
||||||
|
# self.widget_ref = PlaceholderWidget("Image View", "#007981")
|
||||||
|
|
||||||
|
self.stack = QStackedWidget()
|
||||||
|
self.stack.addWidget(self.widget_start)
|
||||||
|
self.stack.addWidget(self.widget_live)
|
||||||
|
self.stack.setCurrentWidget(self.widget_start)
|
||||||
|
|
||||||
|
self.addWidget(self.stack)
|
||||||
|
self.addWidget(self.widget_ref)
|
||||||
|
|
||||||
|
self.setSizes([self.height(), 0])
|
||||||
|
|
||||||
|
pixmap = QPixmap("media/empty_guitar_h.jpg")
|
||||||
|
# pixmap.fill(Qt.GlobalColor.lightGray)
|
||||||
|
self.widget_live.set_image(pixmap)
|
||||||
|
|
||||||
|
def toggle_orientation(self):
|
||||||
|
if self.orientation() == Qt.Orientation.Vertical:
|
||||||
|
self.setOrientation(Qt.Orientation.Horizontal)
|
||||||
|
self.setSizes([self.width()//2, self.width()//2])
|
||||||
|
else:
|
||||||
|
self.setOrientation(Qt.Orientation.Vertical)
|
||||||
|
self.setSizes([self.height()//2, self.height()//2])
|
||||||
|
|
||||||
|
# def set_live_image(self, path_image: str):
|
||||||
|
# """Ustawienie obrazu na żywo"""
|
||||||
|
# pixmap = QPixmap(path_image)
|
||||||
|
# self.widget_live.set_image(pixmap)
|
||||||
|
|
||||||
|
def set_live_image(self, pixmap: QPixmap):
|
||||||
|
"""Ustawienie obrazu na żywo"""
|
||||||
|
self.widget_live.set_image(pixmap)
|
||||||
|
if self.stack.currentWidget() != self.widget_live:
|
||||||
|
self.stack.setCurrentWidget(self.widget_live)
|
||||||
|
|
||||||
|
def set_reference_image(self, path_image: str):
|
||||||
|
"""Ustawienie obrazu referencyjnego"""
|
||||||
|
pixmap = QPixmap(path_image)
|
||||||
|
self.widget_ref.set_image(pixmap)
|
||||||
|
|
||||||
|
def toglle_live_view(self):
|
||||||
|
"""Przełączanie widoku na żywo"""
|
||||||
|
if self.stack.currentWidget() == self.widget_start:
|
||||||
|
self.stack.setCurrentWidget(self.widget_live)
|
||||||
|
else:
|
||||||
|
self.stack.setCurrentWidget(self.widget_start)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
89
ui/widgets/thumbnail_list_widget.py
Normal file
89
ui/widgets/thumbnail_list_widget.py
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
from PySide6.QtWidgets import (
|
||||||
|
QApplication, QWidget, QVBoxLayout, QListWidget, QListWidgetItem,
|
||||||
|
QLabel, QHBoxLayout
|
||||||
|
)
|
||||||
|
from PySide6.QtGui import QPixmap, QIcon
|
||||||
|
from PySide6.QtCore import Qt, QSize, Signal
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def make_thumbnail(image_path: str, size: QSize) -> QPixmap:
|
||||||
|
pixmap = QPixmap(image_path)
|
||||||
|
|
||||||
|
if pixmap.isNull():
|
||||||
|
pixmap = QPixmap("media/empty_guitar_h.jpg") # pusta miniatura, gdy nie uda się wczytać
|
||||||
|
|
||||||
|
# dopasuj tak, aby całkowicie wypełnić prostokąt (cover)
|
||||||
|
scaled = pixmap.scaled(
|
||||||
|
size,
|
||||||
|
Qt.AspectRatioMode.KeepAspectRatioByExpanding,
|
||||||
|
Qt.TransformationMode.SmoothTransformation
|
||||||
|
)
|
||||||
|
|
||||||
|
# wytnij nadmiar, żeby miało dokładnie żądany rozmiar
|
||||||
|
x = (scaled.width() - size.width()) // 2
|
||||||
|
y = (scaled.height() - size.height()) // 2
|
||||||
|
cropped = scaled.copy(x, y, size.width(), size.height())
|
||||||
|
|
||||||
|
return cropped
|
||||||
|
|
||||||
|
|
||||||
|
class ThumbnailItemWidget(QWidget):
|
||||||
|
def __init__(self, image_path: str, text: str = "", parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
|
||||||
|
layout = QHBoxLayout(self)
|
||||||
|
layout.setContentsMargins(6, 6, 6, 6)
|
||||||
|
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
|
||||||
|
# miniatura
|
||||||
|
pixmap = make_thumbnail(image_path, QSize(180, 160))
|
||||||
|
self.icon_label = QLabel()
|
||||||
|
self.icon_label.setPixmap(pixmap)
|
||||||
|
|
||||||
|
# podpis
|
||||||
|
self.text_label = QLabel(text)
|
||||||
|
self.text_label.setStyleSheet("font-size: 12pt;")
|
||||||
|
self.text_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
|
||||||
|
# ustawiamy pionowy layout dla ikony i tekstu
|
||||||
|
vbox = QVBoxLayout()
|
||||||
|
vbox.setContentsMargins(0, 0, 0, 0)
|
||||||
|
vbox.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
vbox.addWidget(self.icon_label)
|
||||||
|
vbox.addWidget(self.text_label)
|
||||||
|
|
||||||
|
layout.addLayout(vbox)
|
||||||
|
|
||||||
|
|
||||||
|
class ThumbnailListWidget(QWidget):
|
||||||
|
selectedThumbnail = Signal(int) # sygnał z ID wybranego elementu
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
layout = QVBoxLayout(self)
|
||||||
|
layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
|
||||||
|
self.list_widget = QListWidget()
|
||||||
|
# self.list_widget.setIconSize(QSize(100, 100))
|
||||||
|
self.list_widget.setSpacing(2)
|
||||||
|
|
||||||
|
layout.addWidget(self.list_widget)
|
||||||
|
|
||||||
|
self.list_widget.itemPressed.connect(self.on_item_pressed)
|
||||||
|
|
||||||
|
def add_thumbnail(self, image_path: str, text: str, id: int):
|
||||||
|
item = QListWidgetItem()
|
||||||
|
item.setData(Qt.ItemDataRole.UserRole, id)
|
||||||
|
item.setSizeHint(QSize(192, 192)) # rozmiar „wiersza”
|
||||||
|
|
||||||
|
print(f"Adding thumbnail: {image_path} with text: {text}")
|
||||||
|
widget = ThumbnailItemWidget(image_path, text)
|
||||||
|
self.list_widget.addItem(item)
|
||||||
|
self.list_widget.setItemWidget(item, widget)
|
||||||
|
|
||||||
|
def on_item_pressed(self, item: QListWidgetItem):
|
||||||
|
row = self.list_widget.row(item)
|
||||||
|
id = item.data(Qt.ItemDataRole.UserRole)
|
||||||
|
print(f"Kliknięto miniaturę w wierszu: {row}, obiekt ID: {id}")
|
||||||
|
self.selectedThumbnail.emit(id)
|
||||||
|
|
||||||
Reference in New Issue
Block a user