"""Small dialogs used by the main laser-control window.""" from __future__ import annotations from PyQt6.QtCore import QRegularExpression from PyQt6.QtGui import QRegularExpressionValidator from PyQt6.QtWidgets import ( QCheckBox, QDialog, QDialogButtonBox, QLabel, QLineEdit, QVBoxLayout, ) from laser_control.constants import PROFILE_NAME_ALLOWED_PATTERN, PROFILE_NAME_MAX_LENGTH class ProfileSaveDialog(QDialog): """Ask the user for a short profile name before saving it to the device SD card.""" def __init__(self, *, custom_waveform_available: bool, parent=None) -> None: super().__init__(parent) self.setWindowTitle("Сохранить профиль на SD") self.setModal(True) layout = QVBoxLayout(self) note = QLabel( "Введите короткое имя профиля для маленького LCD на устройстве. " f"Допустимо до {PROFILE_NAME_MAX_LENGTH} ASCII-символов: буквы, цифры, пробел, '-' и '_'." ) note.setWordWrap(True) self._name_edit = QLineEdit(self) self._name_edit.setPlaceholderText("Например: Factory Saw") self._name_edit.setMaxLength(PROFILE_NAME_MAX_LENGTH) self._name_edit.setValidator( QRegularExpressionValidator(QRegularExpression(PROFILE_NAME_ALLOWED_PATTERN), self) ) self._waveform_checkbox = QCheckBox( "Сохранить и пользовательскую форму из вкладки «Своя форма»", self, ) self._waveform_checkbox.setVisible(custom_waveform_available) self._buttons = QDialogButtonBox( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel, parent=self, ) self._buttons.accepted.connect(self.accept) self._buttons.rejected.connect(self.reject) self._buttons.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) self._name_edit.textChanged.connect(self._update_accept_state) layout.addWidget(note) layout.addWidget(self._name_edit) layout.addWidget(self._waveform_checkbox) layout.addWidget(self._buttons) def profile_name(self) -> str: """Return the trimmed display name entered by the user.""" return self._name_edit.text().strip() def include_custom_waveform(self) -> bool: """Return True when a valid custom waveform should be saved with the profile.""" return self._waveform_checkbox.isVisible() and self._waveform_checkbox.isChecked() def _update_accept_state(self) -> None: self._buttons.button(QDialogButtonBox.StandardButton.Ok).setEnabled( bool(self.profile_name()) )