Skip to content

Commit

Permalink
Add a file selector for file parameters in model algorithms
Browse files Browse the repository at this point in the history
Makes it more obvious to users that a fixed filename can be
used here
  • Loading branch information
nyalldawson committed Aug 22, 2017
1 parent 451a3fa commit 4511ea1
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,24 +483,59 @@ def createWidget(self):
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):
Expand Down

0 comments on commit 4511ea1

Please sign in to comment.