from PySide6.QtWidgets import ( QDialog, QDialogButtonBox, QFormLayout, QLineEdit, QMessageBox, QVBoxLayout, ) class ItemOptionsDialog(QDialog): """Small, extensible options dialog shared by ports and connections.""" def __init__(self, title: str, name: str, parent=None, *, name_required: bool = True) -> None: super().__init__(parent) self.name_required = name_required self.setWindowTitle(title) self.resize(380, 120) layout = QVBoxLayout(self) self.form = QFormLayout() self.name_edit = QLineEdit(name, self) self.form.addRow("Name:", self.name_edit) layout.addLayout(self.form) buttons = QDialogButtonBox( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel, parent=self, ) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) @property def name(self) -> str: return self.name_edit.text().strip() def accept(self) -> None: if self.name_required and not self.name: QMessageBox.warning(self, "Invalid name", "The name cannot be empty.") return super().accept()