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

183 lines
6.6 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/>.
"""Exposure settings section with mode-dependent controls."""
from PySide6.QtCore import Signal
from PySide6.QtWidgets import QWidget, QHBoxLayout, QLabel, QComboBox
from ..widgets import CollapsibleSection, SegmentButton, ParamSlider, ToggleSwitch
S3_ISO_VALUES = [
100, 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250,
1600, 2000, 2500, 3200, 4000, 5000, 6400, 8000, 9600, 12800,
]
SHUTTER_LABELS = [
("1/25", 40000), ("1/30", 33333), ("1/50", 20000), ("1/60", 16666),
("1/80", 12500), ("1/100", 10000), ("1/125", 8000), ("1/160", 6250),
("1/200", 5000), ("1/250", 4000), ("1/320", 3125), ("1/400", 2500),
("1/500", 2000), ("1/640", 1562), ("1/800", 1250), ("1/1000", 1000),
("1/1250", 800), ("1/1600", 625), ("1/2000", 500), ("1/2500", 400),
("1/3200", 312), ("1/4000", 250), ("1/5000", 200), ("1/6400", 156),
("1/8000", 125),
]
EV_LABELS = [
"-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
"0", "+1", "+2", "+3", "+4", "+5", "+6", "+7", "+8",
]
class ExposureSection(CollapsibleSection):
"""Collapsible panel for exposure with auto/manual mode switching."""
param_changed = Signal(str, object)
def __init__(self, parent=None):
super().__init__("Exposure", parent)
self.exposure_mode = SegmentButton(["Auto", "Manual"])
self.exposure_mode.selection_changed.connect(self._mode_changed)
self.add_widget(self._labeled("Mode", self.exposure_mode))
self.hdr_toggle = ToggleSwitch()
self.hdr_toggle.toggled.connect(lambda v: self.param_changed.emit("hdr", v))
self._hdr_row = self._toggle_row("HDR", self.hdr_toggle)
self.add_widget(self._hdr_row)
self.anti_flicker = SegmentButton(["Close", "50Hz", "60Hz"])
self.anti_flicker.selection_changed.connect(
lambda idx: self.param_changed.emit("anti-flicker", idx)
)
self._anti_flicker_row = self._labeled("Anti-Flicker", self.anti_flicker)
self.add_widget(self._anti_flicker_row)
self.ev_slider = ParamSlider("EV Comp", min_val=0, max_val=16, default=8, step=1)
self.ev_slider.value_changed.connect(lambda v: self.param_changed.emit("exposure-ev", v))
self.add_widget(self.ev_slider)
self.iso_combo = QComboBox()
for iso in S3_ISO_VALUES:
self.iso_combo.addItem(str(iso), iso)
self.iso_combo.currentIndexChanged.connect(self._iso_changed)
self._iso_row = self._labeled("ISO", self.iso_combo)
self.add_widget(self._iso_row)
self.shutter_combo = QComboBox()
for label, us in SHUTTER_LABELS:
self.shutter_combo.addItem(label, us)
self.shutter_combo.setCurrentIndex(3)
self.shutter_combo.currentIndexChanged.connect(self._shutter_changed)
self._shutter_row = self._labeled("Shutter", self.shutter_combo)
self.add_widget(self._shutter_row)
self.exposure_lock_toggle = ToggleSwitch()
self.exposure_lock_toggle.toggled.connect(
lambda v: self.param_changed.emit("exposure-lock", v)
)
self._exp_lock_row = self._toggle_row("Exposure Lock", self.exposure_lock_toggle)
self.add_widget(self._exp_lock_row)
self._apply_mode_visibility(auto=True)
@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
@staticmethod
def _toggle_row(text: str, toggle: ToggleSwitch) -> QWidget:
row = QWidget()
layout = QHBoxLayout(row)
layout.setContentsMargins(0, 2, 0, 2)
layout.setSpacing(8)
lbl = QLabel(text)
lbl.setFixedWidth(170)
layout.addWidget(lbl)
layout.addStretch()
layout.addWidget(toggle)
return row
def _mode_changed(self, index: int):
auto = (index == 0)
self.param_changed.emit("exposure-mode", index)
self._apply_mode_visibility(auto)
def _apply_mode_visibility(self, auto: bool):
self._anti_flicker_row.setVisible(auto)
self.ev_slider.setVisible(auto)
self._exp_lock_row.setVisible(auto)
self._iso_row.setVisible(not auto)
self._shutter_row.setVisible(not auto)
def _iso_changed(self, index: int):
if index >= 0:
iso_val = self.iso_combo.itemData(index)
self.param_changed.emit("exposure-iso", iso_val)
def _shutter_changed(self, index: int):
if index >= 0:
us_val = self.shutter_combo.itemData(index)
self.param_changed.emit("exposure-time", us_val)
def set_exposure_mode(self, value: int):
self.exposure_mode.set_selected(value, emit=False)
self._apply_mode_visibility(value == 0)
def set_hdr(self, enabled: bool):
self.hdr_toggle.set_checked(enabled, emit=False)
def set_anti_flicker(self, value: int):
self.anti_flicker.set_selected(value, emit=False)
def set_ev(self, raw: int):
self.ev_slider.set_value(float(raw), emit=False)
def set_iso(self, value: int):
"""Set ISO by raw protocol value."""
self.iso_combo.blockSignals(True)
for i in range(self.iso_combo.count()):
if self.iso_combo.itemData(i) == value:
self.iso_combo.setCurrentIndex(i)
break
self.iso_combo.blockSignals(False)
def set_shutter(self, us: int):
"""Set shutter by microsecond value."""
self.shutter_combo.blockSignals(True)
for i in range(self.shutter_combo.count()):
if self.shutter_combo.itemData(i) == us:
self.shutter_combo.setCurrentIndex(i)
break
self.shutter_combo.blockSignals(False)
def set_exposure_lock(self, enabled: bool):
self.exposure_lock_toggle.set_checked(enabled, emit=False)