From 5c19713291ecbe275538fcf68580a74a75b2bbba Mon Sep 17 00:00:00 2001 From: Victor Olaya Date: Mon, 14 Apr 2014 08:56:00 +0200 Subject: [PATCH] [processing] allow selection of open layers in batch interface --- .../gui/BatchInputSelectionPanel.py | 59 +++++++++++++++++-- .../processing/gui/BatchProcessingDialog.py | 26 +++++--- .../processing/gui/MultipleInputDialog.py | 2 +- .../plugins/processing/tools/dataobjects.py | 2 +- 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/python/plugins/processing/gui/BatchInputSelectionPanel.py b/python/plugins/processing/gui/BatchInputSelectionPanel.py index 7685fe07d2d8..7d24e3412490 100644 --- a/python/plugins/processing/gui/BatchInputSelectionPanel.py +++ b/python/plugins/processing/gui/BatchInputSelectionPanel.py @@ -28,6 +28,11 @@ import os from PyQt4 import QtGui, QtCore from processing.parameters.ParameterMultipleInput import ParameterMultipleInput +from processing.gui.MultipleInputDialog import MultipleInputDialog +from processing.tools import dataobjects +from processing.parameters.ParameterRaster import ParameterRaster +from processing.parameters.ParameterVector import ParameterVector +from processing.parameters.ParameterTable import ParameterTable class BatchInputSelectionPanel(QtGui.QWidget): @@ -49,11 +54,55 @@ def __init__(self, param, row, col, batchDialog, parent=None): self.horizontalLayout.addWidget(self.text) self.pushButton = QtGui.QPushButton() self.pushButton.setText('...') - self.pushButton.clicked.connect(self.showSelectionDialog) + self.pushButton.clicked.connect(self.showPopupMenu) self.horizontalLayout.addWidget(self.pushButton) self.setLayout(self.horizontalLayout) + + def showPopupMenu(self): + popupmenu = QtGui.QMenu() + if not (isinstance(self.param, ParameterMultipleInput) + and self.param.datatype == ParameterMultipleInput.TYPE_FILE): + selectLayerAction = QtGui.QAction('Select from open layers', + self.pushButton) + selectLayerAction.triggered.connect(self.showLayerSelectionDialog) + popupmenu.addAction(selectLayerAction) + selectFileAction = QtGui.QAction('Select from filesystem', + self.pushButton) + selectFileAction.triggered.connect(self.showFileSelectionDialog) + popupmenu.addAction(selectFileAction) + popupmenu.exec_(QtGui.QCursor.pos()) - def showSelectionDialog(self): + def showLayerSelectionDialog(self): + if (isinstance(self.param, ParameterRaster) + or (isinstance(self.param, ParameterMultipleInput) + and self.param.datatype == ParameterMultipleInput.TYPE_RASTER)): + layers = dataobjects.getRasterLayers() + elif isinstance(self.param, ParameterTable): + layers = dataobjects.getTables() + else: + if isinstance(self.param, ParameterVector): + datatype = self.param.shapetype + else: + datatype = [self.param.datatype] + layers = dataobjects.getVectorLayers(datatype) + dlg = MultipleInputDialog([layer.name() for layer in layers]) + dlg.exec_() + if dlg.selectedoptions is not None: + selected = dlg.selectedoptions + if len(selected) == 1: + self.text.setText(layers[selected[0]]) + else: + if isinstance(self.param, ParameterMultipleInput): + self.text.setText(';'.join(layers[idx].name() for idx in selected)) + else: + rowdif = len(layers) - (self.table.rowCount() - self.row) + for i in range(rowdif): + self.batchDialog.addRow() + for i, layeridx in enumerate(selected): + self.table.cellWidget(i + self.row, + self.col).setText(layers[layeridx].name()) + + def showFileSelectionDialog(self): settings = QtCore.QSettings() text = unicode(self.text.text()) if os.path.isdir(text): @@ -72,7 +121,7 @@ def showSelectionDialog(self): if len(files) == 1: settings.setValue('/Processing/LastInputPath', os.path.dirname(unicode(files[0]))) - self.text.setText(str(files[0])) + self.text.setText(files[0]) else: settings.setValue('/Processing/LastInputPath', os.path.dirname(unicode(files[0]))) @@ -82,9 +131,9 @@ def showSelectionDialog(self): rowdif = len(files) - (self.table.rowCount() - self.row) for i in range(rowdif): self.batchDialog.addRow() - for i in range(len(files)): + for i, f in enumerate(files): self.table.cellWidget(i + self.row, - self.col).setText(files[i]) + self.col).setText(f) def setText(self, text): return self.text.setText(text) diff --git a/python/plugins/processing/gui/BatchProcessingDialog.py b/python/plugins/processing/gui/BatchProcessingDialog.py index 405b7df6f908..16dc33ad8427 100644 --- a/python/plugins/processing/gui/BatchProcessingDialog.py +++ b/python/plugins/processing/gui/BatchProcessingDialog.py @@ -25,7 +25,7 @@ __revision__ = '$Format:%H$' -from PyQt4 import QtCore, QtGui +from PyQt4 import QtGui from PyQt4.QtCore import * from PyQt4.QtGui import * from processing.core.ProcessingResults import ProcessingResults @@ -86,8 +86,10 @@ def __init__(self, alg): self.buttonBox.addButton(self.deleteRowButton, QtGui.QDialogButtonBox.ActionRole) + nOutputs = self.alg.getVisibleOutputsCount() + 1 + if nOutputs == 1: nOutputs = 0 self.table.setColumnCount(self.alg.getVisibleParametersCount() - + self.alg.getVisibleOutputsCount() + 1) + + nOutputs) self.setTableContent() self.table.horizontalHeader().setStretchLastSection(True) self.table.verticalHeader().setVisible(False) @@ -146,8 +148,9 @@ def setTableContent(self): QtGui.QTableWidgetItem(out.description)) i += 1 - self.table.setColumnWidth(i, 200) - self.table.setHorizontalHeaderItem(i, + if self.alg.getVisibleOutputsCount(): + self.table.setColumnWidth(i, 200) + self.table.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem('Load in QGIS')) for i in range(3): @@ -314,21 +317,26 @@ def addRow(self): self.table.setRowHeight(self.table.rowCount() - 1, 22) i = 0 for param in self.alg.parameters: + if param.hidden: + continue self.table.setCellWidget(self.table.rowCount() - 1, i, self.getWidgetFromParameter(param, self.table.rowCount() - 1, i)) i += 1 for out in self.alg.outputs: + if out.hidden: + continue self.table.setCellWidget(self.table.rowCount() - 1, i, BatchOutputSelectionPanel(out, self.alg, self.table.rowCount() - 1, i, self)) i += 1 - item = QtGui.QComboBox() - item.addItem('Yes') - item.addItem('No') - item.setCurrentIndex(0) - self.table.setCellWidget(self.table.rowCount() - 1, i, item) + if self.alg.getVisibleOutputsCount(): + item = QtGui.QComboBox() + item.addItem('Yes') + item.addItem('No') + item.setCurrentIndex(0) + self.table.setCellWidget(self.table.rowCount() - 1, i, item) def showAdvancedParametersClicked(self): self.showAdvanced = not self.showAdvanced diff --git a/python/plugins/processing/gui/MultipleInputDialog.py b/python/plugins/processing/gui/MultipleInputDialog.py index 46721ae1905c..eefc54deff21 100644 --- a/python/plugins/processing/gui/MultipleInputDialog.py +++ b/python/plugins/processing/gui/MultipleInputDialog.py @@ -38,7 +38,7 @@ def __init__(self, options, selectedoptions=None): self.setupUi(self) self.options = options - self.selectedoptions = selectedoptions + self.selectedoptions = selectedoptions or [] # Additional buttons self.btnSelectAll = QPushButton(self.tr('Select all')) diff --git a/python/plugins/processing/tools/dataobjects.py b/python/plugins/processing/tools/dataobjects.py index bfc00860bb65..e073acbb02b4 100644 --- a/python/plugins/processing/tools/dataobjects.py +++ b/python/plugins/processing/tools/dataobjects.py @@ -66,7 +66,7 @@ def getSupportedOutputTableExtensions(): def getRasterLayers(): layers = QgsMapLayerRegistry.instance().mapLayers().values() - raster = list() + raster = [] for layer in layers: if layer.type() == layer.RasterLayer: