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

127 lines
4.7 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/>.
"""Segmented button control for mutually exclusive options."""
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QWidget, QHBoxLayout, QPushButton, QSizePolicy
from PySide6.QtGui import QFont
from ..theme import COLOR_ACCENT, COLOR_BG_INPUT, COLOR_BORDER, COLOR_TEXT_PRIMARY, COLOR_TEXT_SECONDARY
class SegmentButton(QWidget):
"""Row of exclusive buttons styled as a segmented control."""
selection_changed = Signal(int)
selection_changed_text = Signal(str)
def __init__(self, options: list[str], parent=None):
super().__init__(parent)
self._options = options
self._buttons: list[QPushButton] = []
self._selected = 0
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.setStyleSheet(f"""
SegmentButton {{
background: {COLOR_BG_INPUT};
border: 1px solid {COLOR_BORDER};
border-radius: 6px;
}}
""")
for i, label in enumerate(options):
btn = QPushButton(label)
btn.setCheckable(True)
btn.setCursor(Qt.CursorShape.PointingHandCursor)
btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
btn.setFixedHeight(28)
btn.clicked.connect(lambda checked, idx=i: self._on_click(idx))
self._buttons.append(btn)
layout.addWidget(btn)
self._apply_styles()
def _apply_styles(self):
for i, btn in enumerate(self._buttons):
active = (i == self._selected)
radius_left = "6px" if i == 0 else "0px"
radius_right = "6px" if i == len(self._buttons) - 1 else "0px"
if active:
btn.setStyleSheet(f"""
QPushButton {{
background-color: {COLOR_ACCENT};
color: {COLOR_TEXT_PRIMARY};
border: none;
border-radius: 0px;
border-top-left-radius: {radius_left};
border-bottom-left-radius: {radius_left};
border-top-right-radius: {radius_right};
border-bottom-right-radius: {radius_right};
font-weight: bold;
font-size: 12px;
padding: 2px 8px;
}}
""")
else:
btn.setStyleSheet(f"""
QPushButton {{
background-color: transparent;
color: {COLOR_TEXT_SECONDARY};
border: none;
border-radius: 0px;
border-top-left-radius: {radius_left};
border-bottom-left-radius: {radius_left};
border-top-right-radius: {radius_right};
border-bottom-right-radius: {radius_right};
font-size: 12px;
padding: 2px 8px;
}}
QPushButton:hover {{
color: {COLOR_TEXT_PRIMARY};
background-color: rgba(255,255,255,0.05);
}}
""")
btn.setChecked(active)
def _on_click(self, index: int):
if index == self._selected:
return
self._selected = index
self._apply_styles()
self.selection_changed.emit(index)
self.selection_changed_text.emit(self._options[index])
def selected_index(self) -> int:
return self._selected
def selected_text(self) -> str:
return self._options[self._selected]
def set_selected(self, index: int, emit: bool = True):
if index < 0 or index >= len(self._options):
return
if index == self._selected:
return
self._selected = index
self._apply_styles()
if emit:
self.selection_changed.emit(index)
self.selection_changed_text.emit(self._options[index])