From 06eac911828d55122913b87b860f606b2425f4ae Mon Sep 17 00:00:00 2001 From: alexhroom Date: Fri, 4 Oct 2024 09:33:09 +0100 Subject: [PATCH] changed getter, setter and signal to attributes --- rascal2/widgets/inputs.py | 76 +++++++++++++-------------------------- 1 file changed, 24 insertions(+), 52 deletions(-) diff --git a/rascal2/widgets/inputs.py b/rascal2/widgets/inputs.py index 87f2374..6cb15ea 100644 --- a/rascal2/widgets/inputs.py +++ b/rascal2/widgets/inputs.py @@ -50,13 +50,17 @@ class BaseInputWidget(QtWidgets.QWidget): """ + data_getter = "text" + data_setter = "setText" + edit_signal = "textChanged" + def __init__(self, field_info: FieldInfo, parent=None): super().__init__(parent=parent) - self.editor = self.create_editor(field_info) - self.get_data = self.data_getter() - self.set_data = self.data_setter() - self.edited_signal = self.edit_signal() + self.editor: QtWidgets.QWidget = self.create_editor(field_info) + self.get_data: Callable = getattr(self.editor, self.data_getter) + self.set_data: Callable = getattr(self.editor, self.data_setter) + self.edited_signal: QtCore.pyqtSignal = getattr(self.editor, self.edit_signal) layout = QtWidgets.QVBoxLayout() layout.addWidget(self.editor) @@ -81,22 +85,14 @@ def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: """ return QtWidgets.QLineEdit(self) - def data_getter(self) -> Callable: - """The data getter function for the editor.""" - return self.editor.text - - def data_setter(self) -> Callable: - """The data setter function for the editor.""" - return self.editor.setText - - def edit_signal(self) -> QtCore.pyqtSignal: - """The signal produced when the editor data changes.""" - return self.editor.textChanged - class IntInputWidget(BaseInputWidget): """Input widget for integer data with optional minimum and maximum values.""" + data_getter = "value" + data_setter = "setValue" + edit_signal = "editingFinished" + def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: editor = QtWidgets.QSpinBox(self) for item in field_info.metadata: @@ -111,19 +107,14 @@ def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: return editor - def data_getter(self) -> Callable: - return self.editor.value - - def data_setter(self) -> Callable: - return self.editor.setValue - - def edit_signal(self) -> QtCore.pyqtSignal: - return self.editor.editingFinished - class FloatInputWidget(BaseInputWidget): """Input widget for float data with optional minimum and maximum values.""" + data_getter = "value" + data_setter = "setValue" + edit_signal = "editingFinished" + def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: editor = AdaptiveDoubleSpinBox(self) for item in field_info.metadata: @@ -140,50 +131,31 @@ def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: return editor - def data_getter(self) -> Callable: - return self.editor.value - - def data_setter(self) -> Callable: - return self.editor.setValue - - def edit_signal(self) -> QtCore.pyqtSignal: - return self.editor.editingFinished - class BoolInputWidget(BaseInputWidget): """Input widget for boolean data.""" + data_getter = "isChecked" + data_setter = "setChecked" + edit_signal = "checkStateChanged" + def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: return QtWidgets.QCheckBox(self) - def data_getter(self) -> Callable: - return self.editor.isChecked - - def data_setter(self) -> Callable: - return self.editor.setChecked - - def edit_signal(self) -> QtCore.pyqtSignal: - return self.editor.checkStateChanged - class EnumInputWidget(BaseInputWidget): """Input widget for Enums.""" + data_getter = "currentText" + data_setter = "setCurrentText" + edit_signal = "currentTextChanged" + def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget: editor = QtWidgets.QComboBox(self) editor.addItems(str(e) for e in field_info.annotation) return editor - def data_getter(self) -> Callable: - return self.editor.currentText - - def data_setter(self) -> Callable: - return self.editor.setCurrentText - - def edit_signal(self) -> QtCore.pyqtSignal: - return self.editor.currentTextChanged - class AdaptiveDoubleSpinBox(QtWidgets.QDoubleSpinBox): """A double spinbox which adapts to given numbers of decimals."""