Add QLineEdit support to Form

This commit is contained in:
CPD 2025-03-12 14:49:32 +01:00
parent 4843e17961
commit aa8e1f822a

@ -8,7 +8,7 @@ class FileSelection(QWidget):
"""
Widget allowing the user to select a file or directory.
"""
value_changed = pyqtSignal(str)
valueChanged = pyqtSignal(str)
def __init__(self, init_file="", filemode=QFileDialog.FileMode.AnyFile, parent=None):
super().__init__(parent)
self.setLayout(QHBoxLayout())
@ -21,7 +21,7 @@ class FileSelection(QWidget):
# open file dialog when button is clicked
self.w_btn.clicked.connect(self._open_file_dialog)
# emit value_changed on self when w_edit emits value_changed
self.w_edit.textChanged.connect(self.value_changed)
self.w_edit.textChanged.connect(self.valueChanged)
self.file_mode = filemode
# remove all spacing and padding from the layout
@ -30,6 +30,9 @@ class FileSelection(QWidget):
def setValue(self, value: str):
self.w_edit.setText(value)
def value(self) -> str:
return self.w_edit.text()
def _open_file_dialog(self):
dialog = QFileDialog(self)
# only directories
@ -79,7 +82,10 @@ class SettingsForm(QWidget):
widget.stateChanged.connect(lambda v: self.value_updated(key, v))
elif isinstance(widget, FileSelection):
widget.setValue(value)
widget.value_changed.connect(lambda v: self.value_updated(key, v))
widget.valueChanged.connect(lambda v: self.value_updated(key, v))
elif isinstance(widget, QLineEdit):
widget.setText(value)
widget.textChanged.connect(lambda v: self.value_updated(key, v))
else:
raise ValueError(f"Unknown widget type: {type(widget)}")
self.layout().addRow(QLabel(label), widget)
@ -116,6 +122,8 @@ class SettingsForm(QWidget):
self.ws_form[key].setChecked(value)
elif isinstance(self.ws_form[key], FileSelection):
self.ws_form[key].setValue(value)
elif isinstance(self.ws_form[key], QLineEdit):
self.ws_form[key].setText(value)
else:
raise ValueError(f"Unknown widget type: {type(self.ws_form[key])}")
else:
@ -134,10 +142,12 @@ class SettingsForm(QWidget):
"""
if key in self.ws_form:
# get depending on widget type
if isinstance(self.ws_form[key], QSpinBox) or isinstance(self.ws_form[key], QDoubleSpinBox):
if isinstance(self.ws_form[key], QSpinBox) or isinstance(self.ws_form[key], QDoubleSpinBox) or isinstance(self.ws_form, FileSelection):
return self.ws_form[key].value()
elif isinstance(self.ws_form[key], QCheckBox):
return self.ws_form[key].isChecked()
elif isinstance(self.ws_form[key], QLineEdit):
return self.ws_form[key].text()
else:
raise ValueError(f"Unknown widget type: {type(self.ws_form[key])}")
else: