Files
BondGraph/BEdit/src/bedit/gui/dialogs/item_options.py
2026-07-20 14:02:22 +02:00

61 lines
1.7 KiB
Python

from PySide6.QtWidgets import (
QDialog,
QDialogButtonBox,
QCheckBox,
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,
show_name: bool | None = None,
) -> 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)
self.show_name_check = None
if show_name is not None:
self.show_name_check = QCheckBox("Show name below connection", self)
self.show_name_check.setChecked(show_name)
self.form.addRow("", self.show_name_check)
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()
@property
def show_name(self) -> bool:
return bool(self.show_name_check and self.show_name_check.isChecked())
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()