Files
yolocam-linux/yolocam-gui/yolocam_gui/ui/panels/console_panel.py
T
2026-02-15 12:50:49 -08:00

307 lines
10 KiB
Python

# Copyright (C) 2026 Seth Hillbrand
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Console panel with recording, AI tracking, and zoom controls."""
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
)
from PySide6.QtGui import QFont, QPainter, QColor
from ..theme import (
COLOR_ACCENT, COLOR_BG_PANEL, COLOR_BG_INPUT, COLOR_BORDER,
COLOR_TEXT_PRIMARY, COLOR_TEXT_SECONDARY,
)
from ..widgets import CollapsibleSection, SegmentButton
AI_TRACKING_MAP = {0: 2, 1: 0, 2: 1}
class _RecordButton(QPushButton):
"""Circular record button that toggles between ready and recording states."""
def __init__(self, parent=None):
super().__init__(parent)
self._recording = False
self.setFixedSize(60, 60)
self.setCursor(Qt.CursorShape.PointingHandCursor)
self.setStyleSheet("QPushButton { background: transparent; border: none; }")
@property
def recording(self) -> bool:
return self._recording
@recording.setter
def recording(self, value: bool):
self._recording = value
self.update()
def paintEvent(self, event):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(COLOR_ACCENT))
p.drawEllipse(2, 2, 56, 56)
p.setBrush(QColor(COLOR_TEXT_PRIMARY))
if self._recording:
# White square (stop icon)
p.drawRoundedRect(20, 20, 20, 20, 3, 3)
else:
# White circle (record icon)
p.drawEllipse(17, 17, 26, 26)
p.end()
class _RecordingSection(CollapsibleSection):
"""Recording controls with record button, duration, and status."""
param_changed = Signal(str, object)
def __init__(self, parent=None):
super().__init__("Recording", parent)
self._recording = False
btn_container = QWidget()
btn_layout = QVBoxLayout(btn_container)
btn_layout.setContentsMargins(0, 4, 0, 4)
btn_layout.setSpacing(6)
btn_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.record_btn = _RecordButton()
self.record_btn.clicked.connect(self._toggle_recording)
btn_layout.addWidget(self.record_btn, 0, Qt.AlignmentFlag.AlignCenter)
self.duration_label = QLabel("00:00:00")
duration_font = QFont("monospace", 14)
self.duration_label.setFont(duration_font)
self.duration_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
btn_layout.addWidget(self.duration_label)
self.status_label = QLabel("Ready")
self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.status_label.setStyleSheet(f"color: {COLOR_TEXT_SECONDARY};")
btn_layout.addWidget(self.status_label)
self.add_widget(btn_container)
def _toggle_recording(self):
self._recording = not self._recording
self.record_btn.recording = self._recording
if self._recording:
self.status_label.setText("Recording")
self.status_label.setStyleSheet(f"color: {COLOR_ACCENT};")
self.param_changed.emit("recording-start", True)
else:
self.status_label.setText("Ready")
self.status_label.setStyleSheet(f"color: {COLOR_TEXT_SECONDARY};")
self.param_changed.emit("recording-stop", True)
def set_recording_state(self, recording: bool):
self._recording = recording
self.record_btn.recording = recording
if recording:
self.status_label.setText("Recording")
self.status_label.setStyleSheet(f"color: {COLOR_ACCENT};")
else:
self.status_label.setText("Ready")
self.status_label.setStyleSheet(f"color: {COLOR_TEXT_SECONDARY};")
self.duration_label.setText("00:00:00")
def set_recording_duration(self, seconds: int):
h = seconds // 3600
m = (seconds % 3600) // 60
s = seconds % 60
self.duration_label.setText(f"{h:02d}:{m:02d}:{s:02d}")
class _AITrackingSection(CollapsibleSection):
"""AI tracking mode selection."""
param_changed = Signal(str, object)
def __init__(self, parent=None):
super().__init__("AI Tracking", parent)
self.mode_btn = SegmentButton(["Off", "Portrait", "Thing"])
self.mode_btn.selection_changed.connect(self._mode_changed)
self.add_widget(self._labeled("Mode", self.mode_btn))
def _mode_changed(self, index: int):
self.param_changed.emit("ai-tracking", AI_TRACKING_MAP[index])
def set_ai_tracking_mode(self, wire_value: int):
"""Set mode by wire protocol value (0=Portrait, 1=Thing, 2=Close)."""
reverse_map = {2: 0, 0: 1, 1: 2}
ui_index = reverse_map.get(wire_value, 0)
self.mode_btn.set_selected(ui_index, emit=False)
@staticmethod
def _labeled(text: str, widget: QWidget) -> QWidget:
row = QWidget()
layout = QHBoxLayout(row)
layout.setContentsMargins(0, 2, 0, 2)
layout.setSpacing(8)
lbl = QLabel(text)
lbl.setFixedWidth(110)
layout.addWidget(lbl)
layout.addWidget(widget)
layout.addStretch()
return row
class _ZoomSection(CollapsibleSection):
"""Zoom controls with +/- buttons and quick presets."""
ZOOM_MIN = 1.0
ZOOM_MAX = 4.0
ZOOM_STEP = 0.5
param_changed = Signal(str, object)
def __init__(self, parent=None):
super().__init__("Zoom Controls", parent)
self._zoom = 1.0
self.zoom_display = QLabel("1.0x")
display_font = QFont()
display_font.setPointSize(18)
display_font.setBold(True)
self.zoom_display.setFont(display_font)
self.zoom_display.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.add_widget(self.zoom_display)
step_row = QWidget()
step_layout = QHBoxLayout(step_row)
step_layout.setContentsMargins(0, 4, 0, 4)
step_layout.setSpacing(8)
step_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.zoom_minus = QPushButton("\u2212")
self.zoom_minus.setFixedSize(40, 40)
self.zoom_minus.setCursor(Qt.CursorShape.PointingHandCursor)
self.zoom_minus.setStyleSheet(f"""
QPushButton {{ font-size: 20px; font-weight: bold; background-color: {COLOR_BG_INPUT};
border: 1px solid {COLOR_BORDER}; border-radius: 20px; }}
QPushButton:hover {{ background-color: {COLOR_BORDER}; }}
""")
self.zoom_minus.clicked.connect(self._zoom_out)
step_layout.addWidget(self.zoom_minus)
self.zoom_plus = QPushButton("+")
self.zoom_plus.setFixedSize(40, 40)
self.zoom_plus.setCursor(Qt.CursorShape.PointingHandCursor)
self.zoom_plus.setStyleSheet(f"""
QPushButton {{ font-size: 20px; font-weight: bold; background-color: {COLOR_BG_INPUT};
border: 1px solid {COLOR_BORDER}; border-radius: 20px; }}
QPushButton:hover {{ background-color: {COLOR_BORDER}; }}
""")
self.zoom_plus.clicked.connect(self._zoom_in)
step_layout.addWidget(self.zoom_plus)
self.add_widget(step_row)
preset_row = QWidget()
preset_layout = QHBoxLayout(preset_row)
preset_layout.setContentsMargins(0, 2, 0, 2)
preset_layout.setSpacing(6)
for zoom_val in [1.0, 2.0, 3.0, 4.0]:
btn = QPushButton(f"{int(zoom_val)}x")
btn.setFixedSize(48, 28)
btn.setCursor(Qt.CursorShape.PointingHandCursor)
btn.clicked.connect(lambda checked, v=zoom_val: self._preset_clicked(v))
preset_layout.addWidget(btn)
preset_layout.addStretch()
self.add_widget(preset_row)
def _apply_zoom(self, value: float):
self._zoom = max(self.ZOOM_MIN, min(self.ZOOM_MAX, round(value, 1)))
self.zoom_display.setText(f"{self._zoom:.1f}x")
self.param_changed.emit("zoom", self._zoom)
def _zoom_in(self):
self._apply_zoom(self._zoom + self.ZOOM_STEP)
def _zoom_out(self):
self._apply_zoom(self._zoom - self.ZOOM_STEP)
def _preset_clicked(self, value: float):
self._apply_zoom(value)
def set_zoom(self, value: float):
self._zoom = max(self.ZOOM_MIN, min(self.ZOOM_MAX, round(value, 1)))
self.zoom_display.setText(f"{self._zoom:.1f}x")
class ConsolePanel(QWidget):
"""Panel with recording, AI tracking, and zoom controls."""
param_changed = Signal(str, object)
def __init__(self, parent=None):
super().__init__(parent)
self.setObjectName("consolePanel")
self.setStyleSheet(f"#consolePanel {{ background-color: {COLOR_BG_PANEL}; }}")
outer = QVBoxLayout(self)
outer.setContentsMargins(0, 0, 0, 0)
outer.setSpacing(0)
header = QWidget()
header.setFixedHeight(36)
header_layout = QHBoxLayout(header)
header_layout.setContentsMargins(12, 4, 12, 4)
header_layout.setSpacing(8)
title = QLabel("Console")
title_font = QFont()
title_font.setBold(True)
title_font.setPointSize(11)
title.setFont(title_font)
header_layout.addWidget(title)
header_layout.addStretch()
outer.addWidget(header)
self.recording = _RecordingSection()
self.ai_tracking = _AITrackingSection()
self.zoom = _ZoomSection()
for section in [self.recording, self.ai_tracking, self.zoom]:
section.param_changed.connect(self.param_changed.emit)
outer.addWidget(section)
outer.addStretch()
def set_recording_state(self, recording: bool):
self.recording.set_recording_state(recording)
def set_recording_duration(self, seconds: int):
self.recording.set_recording_duration(seconds)
def set_zoom(self, value: float):
self.zoom.set_zoom(value)
def set_ai_tracking_mode(self, wire_value: int):
self.ai_tracking.set_ai_tracking_mode(wire_value)