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

110 lines
4.5 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/>.
"""Preset bar for quick scene switching."""
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import (
QWidget, QHBoxLayout, QPushButton, QSizePolicy, QMenu, QInputDialog,
)
from ..theme import COLOR_ACCENT, COLOR_BG_INPUT, COLOR_BORDER, COLOR_TEXT_PRIMARY, COLOR_TEXT_SECONDARY
class PresetBar(QWidget):
"""Horizontal bar with preset buttons and an add button."""
preset_selected = Signal(str)
save_requested = Signal()
preset_deleted = Signal(str)
preset_renamed = Signal(str, str)
def __init__(self, parent=None):
super().__init__(parent)
self._presets: list[str] = []
self._active: str | None = None
self._buttons: dict[str, QPushButton] = {}
self._layout = QHBoxLayout(self)
self._layout.setContentsMargins(0, 4, 0, 4)
self._layout.setSpacing(4)
self._add_btn = QPushButton("+")
self._add_btn.setFixedSize(28, 28)
self._add_btn.setCursor(Qt.CursorShape.PointingHandCursor)
self._add_btn.setToolTip("Save new preset")
self._add_btn.setStyleSheet(f"""
QPushButton {{ background-color: {COLOR_BG_INPUT}; color: {COLOR_TEXT_SECONDARY};
border: 1px solid {COLOR_BORDER}; border-radius: 4px; font-size: 16px; font-weight: bold; }}
QPushButton:hover {{ color: {COLOR_TEXT_PRIMARY}; border-color: {COLOR_ACCENT}; }}
""")
self._add_btn.clicked.connect(self.save_requested.emit)
self._layout.addStretch()
self._layout.addWidget(self._add_btn)
def set_presets(self, names: list[str], active: str | None = None):
for btn in self._buttons.values():
self._layout.removeWidget(btn)
btn.deleteLater()
self._buttons.clear()
self._presets = list(names)
self._active = active
insert_pos = 0
for name in names:
btn = self._make_button(name)
self._layout.insertWidget(insert_pos, btn)
self._buttons[name] = btn
insert_pos += 1
self._apply_styles()
def _make_button(self, name: str) -> QPushButton:
btn = QPushButton(name)
btn.setFixedHeight(28)
btn.setCursor(Qt.CursorShape.PointingHandCursor)
btn.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
btn.clicked.connect(lambda: self._select(name))
btn.customContextMenuRequested.connect(lambda pos, n=name: self._context_menu(n, pos))
return btn
def _select(self, name: str):
self._active = name
self._apply_styles()
self.preset_selected.emit(name)
def _context_menu(self, name: str, pos):
menu = QMenu(self)
rename_action = menu.addAction("Rename")
delete_action = menu.addAction("Delete")
action = menu.exec(self._buttons[name].mapToGlobal(pos))
if action == rename_action:
new_name, ok = QInputDialog.getText(self, "Rename Preset", "New name:", text=name)
if ok and new_name and new_name != name:
self.preset_renamed.emit(name, new_name)
elif action == delete_action:
self.preset_deleted.emit(name)
def _apply_styles(self):
for name, btn in self._buttons.items():
active = (name == self._active)
if active:
btn.setStyleSheet(f"""
QPushButton {{ background-color: {COLOR_ACCENT}; color: {COLOR_TEXT_PRIMARY};
border: none; border-radius: 4px; font-weight: bold; padding: 2px 10px; }}
""")
else:
btn.setStyleSheet(f"""
QPushButton {{ background-color: {COLOR_BG_INPUT}; color: {COLOR_TEXT_SECONDARY};
border: 1px solid {COLOR_BORDER}; border-radius: 4px; padding: 2px 10px; }}
QPushButton:hover {{ color: {COLOR_TEXT_PRIMARY}; border-color: {COLOR_ACCENT}; }}
""")