From 4511ea1c12f26ec21c7b24d32f11dffe9517908f Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 23 Aug 2017 00:51:42 +1000 Subject: [PATCH] Add a file selector for file parameters in model algorithms Makes it more obvious to users that a fixed filename can be used here --- python/plugins/processing/gui/wrappers.py | 45 ++++++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/python/plugins/processing/gui/wrappers.py b/python/plugins/processing/gui/wrappers.py index 9dc99b55253..e918c0b13cc 100644 --- a/python/plugins/processing/gui/wrappers.py +++ b/python/plugins/processing/gui/wrappers.py @@ -483,24 +483,59 @@ class FileWidgetWrapper(WidgetWrapper): if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH): return FileSelectionPanel(self.param.behavior() == QgsProcessingParameterFile.Folder, self.param.extension()) else: - widget = QComboBox() - widget.setEditable(True) + self.combo = QComboBox() + self.combo.setEditable(True) files = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFile, OutputFile) for f in files: - widget.addItem(self.dialog.resolveValueDescription(f), f) + self.combo.addItem(self.dialog.resolveValueDescription(f), f) + if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional: + self.combo.setEditText("") + widget = QWidget() + layout = QHBoxLayout() + layout.setMargin(0) + layout.setContentsMargins(0, 0, 0, 0) + layout.setSpacing(2) + layout.addWidget(self.combo) + btn = QToolButton() + btn.setText('…') + btn.setToolTip(self.tr("Select file")) + btn.clicked.connect(self.selectFile) + layout.addWidget(btn) + widget.setLayout(layout) return widget + def selectFile(self): + settings = QgsSettings() + if os.path.isdir(os.path.dirname(self.combo.currentText())): + path = os.path.dirname(self.combo.currentText()) + if settings.contains('/Processing/LastInputPath'): + path = settings.value('/Processing/LastInputPath') + else: + path = '' + + if self.param.extension(): + filter = self.tr('{} files').format(self.param.extension().upper()) + ' (*.' + self.param.extension() + self.tr( + ');;All files (*.*)') + else: + filter = self.tr('All files (*.*)') + + filename, selected_filter = QFileDialog.getOpenFileName(self.widget, + self.tr('Select file'), path, + filter) + if filename: + self.combo.setEditText(filename) + def setValue(self, value): if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH): self.widget.setText(value) else: - self.setComboValue(value) + self.setComboValue(value, combobox=self.combo) def value(self): if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH): return self.widget.getValue() else: - return self.comboValue() + return self.comboValue(combobox=self.combo) class FixedTableWidgetWrapper(WidgetWrapper):