from __future__ import annotations from typing import Dict, List, Optional from PyQt6.QtCore import Qt from PyQt6.QtWidgets import ( QButtonGroup, QDialog, QDialogButtonBox, QFileDialog, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QRadioButton, QVBoxLayout, ) from photo_sorter.core.config_manager import AppConfig, TargetConfig, ActionType KEYS = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] class SettingsDialog(QDialog): """Dialog zur Konfiguration der Ziele und der Standard-Aktion.""" def __init__(self, config: AppConfig, parent=None) -> None: super().__init__(parent) self.setWindowTitle("Ziele konfigurieren") self._config = config self._target_rows: Dict[str, Dict[str, object]] = {} self._action: ActionType = config.default_action main_layout = QVBoxLayout(self) grid = QGridLayout() grid.addWidget(QLabel("Taste"), 0, 0) grid.addWidget(QLabel("Label"), 0, 1) grid.addWidget(QLabel("Verzeichnis"), 0, 2) grid.addWidget(QLabel("…"), 0, 3) existing_by_key: Dict[str, TargetConfig] = {t.key: t for t in config.targets} for row, key in enumerate(KEYS, start=1): key_label = QLabel(key) label_edit = QLineEdit() path_edit = QLineEdit() path_edit.setReadOnly(True) browse_btn = QPushButton("Wählen…") def make_browse(k: str, pe: QLineEdit): def browse() -> None: directory = QFileDialog.getExistingDirectory(self, "Zielverzeichnis wählen") if directory: pe.setText(directory) return browse browse_btn.clicked.connect(make_browse(key, path_edit)) # type: ignore[arg-type] grid.addWidget(key_label, row, 0) grid.addWidget(label_edit, row, 1) grid.addWidget(path_edit, row, 2) grid.addWidget(browse_btn, row, 3) existing = existing_by_key.get(key) if existing: label_edit.setText(existing.label) path_edit.setText(existing.path) self._target_rows[key] = { "label_edit": label_edit, "path_edit": path_edit, } main_layout.addLayout(grid) # Move/Copy Auswahl action_layout = QHBoxLayout() action_layout.addWidget(QLabel("Aktion:")) self._move_radio = QRadioButton("Verschieben") self._copy_radio = QRadioButton("Kopieren") bg = QButtonGroup(self) bg.addButton(self._move_radio) bg.addButton(self._copy_radio) if config.default_action == "copy": self._copy_radio.setChecked(True) else: self._move_radio.setChecked(True) action_layout.addWidget(self._move_radio) action_layout.addWidget(self._copy_radio) action_layout.addStretch(1) main_layout.addLayout(action_layout) # Buttons buttons = QDialogButtonBox( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel, parent=self, ) buttons.accepted.connect(self.accept) # type: ignore[arg-type] buttons.rejected.connect(self.reject) # type: ignore[arg-type] main_layout.addWidget(buttons) def get_config(self) -> AppConfig: targets: List[TargetConfig] = [] for key in KEYS: row = self._target_rows[key] label = row["label_edit"].text().strip() # type: ignore[assignment] path = row["path_edit"].text().strip() # type: ignore[assignment] if not path: continue targets.append(TargetConfig(key=key, path=path, label=label)) action: ActionType = "move" if self._copy_radio.isChecked(): action = "copy" return AppConfig(targets=targets, default_action=action)