Skip to content

Commit

Permalink
fix wrong lambda, use a handler factory instead for more clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
mwinkens committed Jun 6, 2024
1 parent 7edda99 commit 10c5c53
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/dialogs/dialog_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def __init__(self, *args):
q_label = QLabel(label)
self.layout.addWidget(q_label, index, 0)
self.layout.addWidget(config_button, index, 1)
config_button.clicked.connect(lambda: self.browseSetting(config, q_label, config_button, filemode))
handler = self.createBrowseSettingHandler(config, q_label.text(), config_button, filemode)
config_button.clicked.connect(handler)

q_btn = QDialogButtonBox.StandardButton.Ok

Expand All @@ -38,17 +39,26 @@ def __init__(self, *args):

self.setModal(True)

def browseSetting(self, setting: str, q_label: QLabel, config_button: QPushButton, filemode: QFileDialog.FileMode):
def createBrowseSettingHandler(self, setting: str, text: str, config_button: QPushButton,
filemode: QFileDialog.FileMode):
def handler():
self.browseSetting(setting, text, config_button, filemode)
return handler

def browseSetting(self, setting: str, text: str, config_button: QPushButton, filemode: QFileDialog.FileMode):
value = ConfigManager.config()[setting]
if not value:
directory = os.getcwd()
else:
directory = Path(value).parent if Path(value).is_file() else (
value if Path(value).is_dir() else os.getcwd())

dialog = QFileDialog(self)
dialog = QFileDialog(self, caption=text)
dialog.setFileMode(filemode)
path, _ = dialog.getOpenFileName(self, q_label.text(), str(directory))
if path:
ConfigManager.setConfig(setting, path)
config_button.setText(path)
dialog.setDirectory(str(directory))
if dialog.exec():
paths = dialog.selectedFiles()
if paths:
path = paths[0]
ConfigManager.setConfig(setting, path)
config_button.setText(path)

0 comments on commit 10c5c53

Please sign in to comment.