Initial commit
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
# 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/>.
|
||||
|
||||
"""Main application window."""
|
||||
|
||||
import logging
|
||||
|
||||
from PySide6.QtCore import Qt, QTimer
|
||||
from PySide6.QtGui import QKeySequence, QShortcut
|
||||
from PySide6.QtWidgets import (
|
||||
QMainWindow, QWidget, QHBoxLayout, QScrollArea, QStackedWidget, QSizePolicy,
|
||||
)
|
||||
|
||||
from .theme import COLOR_BG_PANEL, COLOR_BORDER
|
||||
from .sidebar import Sidebar
|
||||
from .video_widget import VideoWidget
|
||||
from .panels import ImageParamsPanel, ColorPanel, ConsolePanel, StreamPanel, SettingsPanel
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
"""YoloCam main window with sidebar, stacked panels, and video feed."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setWindowTitle("YoloCam")
|
||||
self.resize(1100, 570)
|
||||
|
||||
self._build_menu_bar()
|
||||
|
||||
self.statusBar().showMessage("Disconnected")
|
||||
|
||||
central = QWidget()
|
||||
self.setCentralWidget(central)
|
||||
root_layout = QHBoxLayout(central)
|
||||
root_layout.setContentsMargins(0, 0, 0, 0)
|
||||
root_layout.setSpacing(0)
|
||||
|
||||
self.sidebar = Sidebar()
|
||||
self.sidebar.page_changed.connect(self._on_page_changed)
|
||||
root_layout.addWidget(self.sidebar)
|
||||
|
||||
self.params_panel = ImageParamsPanel()
|
||||
self.color_panel = ColorPanel()
|
||||
self.console_panel = ConsolePanel()
|
||||
self.stream_panel = StreamPanel()
|
||||
self.settings_panel = SettingsPanel()
|
||||
|
||||
self._stacked = QStackedWidget()
|
||||
self._panels = [
|
||||
self.params_panel,
|
||||
self.color_panel,
|
||||
self.console_panel,
|
||||
self.stream_panel,
|
||||
self.settings_panel,
|
||||
]
|
||||
|
||||
for panel in self._panels:
|
||||
scroll = QScrollArea()
|
||||
scroll.setWidget(panel)
|
||||
scroll.setWidgetResizable(True)
|
||||
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||
scroll.setStyleSheet(f"""
|
||||
QScrollArea {{ border-right: 1px solid {COLOR_BORDER}; background-color: {COLOR_BG_PANEL}; }}
|
||||
""")
|
||||
self._stacked.addWidget(scroll)
|
||||
|
||||
self._stacked.setFixedWidth(400)
|
||||
root_layout.addWidget(self._stacked)
|
||||
|
||||
self.video_widget = VideoWidget(device_index=-1)
|
||||
self.video_widget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
|
||||
root_layout.addWidget(self.video_widget, 1)
|
||||
|
||||
self._controller = None
|
||||
|
||||
def set_controller(self, controller):
|
||||
"""Attach the CameraController after construction."""
|
||||
self._controller = controller
|
||||
controller.status_message.connect(self.statusBar().showMessage)
|
||||
controller.connection_changed.connect(self._on_connection_changed)
|
||||
|
||||
QTimer.singleShot(500, controller.connect_camera)
|
||||
|
||||
def _build_menu_bar(self):
|
||||
menu_bar = self.menuBar()
|
||||
|
||||
app_menu = menu_bar.addMenu("YoloCam")
|
||||
self._connect_action = app_menu.addAction("Connect to Camera")
|
||||
self._connect_action.setShortcut(QKeySequence("Ctrl+Shift+C"))
|
||||
self._connect_action.triggered.connect(self._toggle_connection)
|
||||
app_menu.addSeparator()
|
||||
|
||||
quit_action = app_menu.addAction("Quit")
|
||||
quit_action.setShortcut(QKeySequence.StandardKey.Quit)
|
||||
quit_action.triggered.connect(self.close)
|
||||
|
||||
view_menu = menu_bar.addMenu("View")
|
||||
|
||||
for i, (label, key) in enumerate([
|
||||
("Camera", "1"), ("Color", "2"), ("Console", "3"),
|
||||
("Stream", "4"), ("Settings", "5"),
|
||||
]):
|
||||
action = view_menu.addAction(label)
|
||||
action.setShortcut(QKeySequence(f"Ctrl+{key}"))
|
||||
action.triggered.connect(lambda checked, idx=i: self._switch_panel(idx))
|
||||
|
||||
view_menu.addSeparator()
|
||||
fullscreen_action = view_menu.addAction("Toggle Fullscreen")
|
||||
fullscreen_action.setShortcut(QKeySequence.StandardKey.FullScreen)
|
||||
fullscreen_action.triggered.connect(self._toggle_fullscreen)
|
||||
|
||||
help_menu = menu_bar.addMenu("Help")
|
||||
help_menu.addAction("Documentation")
|
||||
|
||||
def _switch_panel(self, index: int):
|
||||
if 0 <= index < self._stacked.count():
|
||||
self._stacked.setCurrentIndex(index)
|
||||
self.sidebar.set_active(index)
|
||||
|
||||
def _toggle_fullscreen(self):
|
||||
if self.isFullScreen():
|
||||
self.showNormal()
|
||||
else:
|
||||
self.showFullScreen()
|
||||
|
||||
def _on_page_changed(self, index: int):
|
||||
if 0 <= index < self._stacked.count():
|
||||
self._stacked.setCurrentIndex(index)
|
||||
|
||||
def _toggle_connection(self):
|
||||
if self._controller is None:
|
||||
return
|
||||
|
||||
if self._controller.client.connection_state == "connected":
|
||||
self._controller.disconnect_camera()
|
||||
else:
|
||||
self._controller.connect_camera()
|
||||
|
||||
def _on_connection_changed(self, state: str):
|
||||
if state == "connected":
|
||||
self._connect_action.setText("Disconnect")
|
||||
else:
|
||||
self._connect_action.setText("Connect to Camera")
|
||||
|
||||
def closeEvent(self, event):
|
||||
if self._controller:
|
||||
self._controller.disconnect_camera()
|
||||
|
||||
self.video_widget.stop()
|
||||
super().closeEvent(event)
|
||||
Reference in New Issue
Block a user