# 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 . """Focus settings section.""" from PySide6.QtCore import Signal from PySide6.QtWidgets import QWidget, QHBoxLayout, QLabel, QComboBox from ..widgets import CollapsibleSection, SegmentButton, ToggleSwitch, ParamSlider class FocusSection(CollapsibleSection): """Collapsible panel for focus settings with mode-dependent controls.""" param_changed = Signal(str, object) MODES = ["AF-S", "Manual", "AF-C", "Face"] MODE_VALUES = {"AF-S": 1, "Manual": 2, "AF-C": 3, "Face": 4} def __init__(self, parent=None): super().__init__("Focus", parent) self.focus_mode = SegmentButton(self.MODES) self.focus_mode.set_selected(2, emit=False) self.focus_mode.selection_changed_text.connect(self._mode_changed) self.add_widget(self._labeled("Mode", self.focus_mode)) self.focus_area_combo = QComboBox() self.focus_area_combo.addItems(["Large", "Middle", "Small"]) self.focus_area_combo.currentIndexChanged.connect( lambda idx: self.param_changed.emit("focus-level", idx) ) self._focus_area_row = self._labeled("Focus Area", self.focus_area_combo) self.add_widget(self._focus_area_row) self.manual_focus = ParamSlider("Manual Focus", min_val=0, max_val=100, default=50, step=1) self.manual_focus.value_changed.connect( lambda v: self.param_changed.emit("focus-manual", v) ) self._manual_focus_row = self.manual_focus self.add_widget(self._manual_focus_row) self.focus_power_toggle = ToggleSwitch(checked=True) self.focus_power_toggle.toggled.connect( lambda v: self.param_changed.emit("focus-caf", v) ) self._focus_power_row = self._toggle_row("Focus Power", self.focus_power_toggle) self.add_widget(self._focus_power_row) self.focus_speed = SegmentButton(["Basic", "Quick"]) self.focus_speed.selection_changed.connect( lambda idx: self.param_changed.emit("focus-trigger", idx) ) self._focus_speed_row = self._labeled("Speed", self.focus_speed) self.add_widget(self._focus_speed_row) self._apply_mode_visibility("AF-C") @staticmethod def _labeled(text: str, widget: QWidget, stretch: bool = False) -> QWidget: row = QWidget() layout = QHBoxLayout(row) layout.setContentsMargins(0, 2, 0, 2) layout.setSpacing(8) lbl = QLabel(text) lbl.setFixedWidth(110) layout.addWidget(lbl) if stretch: layout.addStretch() layout.addWidget(widget) if not stretch: 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, mode_text: str): value = self.MODE_VALUES.get(mode_text, 3) self.param_changed.emit("focus-mode", value) self._apply_mode_visibility(mode_text) def _apply_mode_visibility(self, mode_text: str): self._focus_area_row.setVisible(mode_text in ("AF-S", "AF-C")) self._manual_focus_row.setVisible(mode_text == "Manual") self._focus_power_row.setVisible(mode_text in ("AF-C", "Face")) self._focus_speed_row.setVisible(mode_text == "AF-C") def set_focus_mode(self, value: int): """Set focus mode by raw protocol value (1=AFS, 2=Manual, 3=AFC, 4=Face).""" value_to_index = {1: 0, 2: 1, 3: 2, 4: 3} idx = value_to_index.get(value, 2) self.focus_mode.set_selected(idx, emit=False) self._apply_mode_visibility(self.MODES[idx]) def set_focus_area(self, value: int): self.focus_area_combo.blockSignals(True) self.focus_area_combo.setCurrentIndex(value) self.focus_area_combo.blockSignals(False) def set_focus_power(self, enabled: bool): self.focus_power_toggle.set_checked(enabled, emit=False) def set_focus_speed(self, value: int): self.focus_speed.set_selected(value, emit=False) def set_manual_focus(self, value: int): self.manual_focus.set_value(float(value), emit=False)