Skip to content

Commit 5c19713

Browse files
committed
[processing] allow selection of open layers in batch interface
1 parent 06fe244 commit 5c19713

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

python/plugins/processing/gui/BatchInputSelectionPanel.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
import os
2929
from PyQt4 import QtGui, QtCore
3030
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput
31+
from processing.gui.MultipleInputDialog import MultipleInputDialog
32+
from processing.tools import dataobjects
33+
from processing.parameters.ParameterRaster import ParameterRaster
34+
from processing.parameters.ParameterVector import ParameterVector
35+
from processing.parameters.ParameterTable import ParameterTable
3136

3237

3338
class BatchInputSelectionPanel(QtGui.QWidget):
@@ -49,11 +54,55 @@ def __init__(self, param, row, col, batchDialog, parent=None):
4954
self.horizontalLayout.addWidget(self.text)
5055
self.pushButton = QtGui.QPushButton()
5156
self.pushButton.setText('...')
52-
self.pushButton.clicked.connect(self.showSelectionDialog)
57+
self.pushButton.clicked.connect(self.showPopupMenu)
5358
self.horizontalLayout.addWidget(self.pushButton)
5459
self.setLayout(self.horizontalLayout)
60+
61+
def showPopupMenu(self):
62+
popupmenu = QtGui.QMenu()
63+
if not (isinstance(self.param, ParameterMultipleInput)
64+
and self.param.datatype == ParameterMultipleInput.TYPE_FILE):
65+
selectLayerAction = QtGui.QAction('Select from open layers',
66+
self.pushButton)
67+
selectLayerAction.triggered.connect(self.showLayerSelectionDialog)
68+
popupmenu.addAction(selectLayerAction)
69+
selectFileAction = QtGui.QAction('Select from filesystem',
70+
self.pushButton)
71+
selectFileAction.triggered.connect(self.showFileSelectionDialog)
72+
popupmenu.addAction(selectFileAction)
73+
popupmenu.exec_(QtGui.QCursor.pos())
5574

56-
def showSelectionDialog(self):
75+
def showLayerSelectionDialog(self):
76+
if (isinstance(self.param, ParameterRaster)
77+
or (isinstance(self.param, ParameterMultipleInput)
78+
and self.param.datatype == ParameterMultipleInput.TYPE_RASTER)):
79+
layers = dataobjects.getRasterLayers()
80+
elif isinstance(self.param, ParameterTable):
81+
layers = dataobjects.getTables()
82+
else:
83+
if isinstance(self.param, ParameterVector):
84+
datatype = self.param.shapetype
85+
else:
86+
datatype = [self.param.datatype]
87+
layers = dataobjects.getVectorLayers(datatype)
88+
dlg = MultipleInputDialog([layer.name() for layer in layers])
89+
dlg.exec_()
90+
if dlg.selectedoptions is not None:
91+
selected = dlg.selectedoptions
92+
if len(selected) == 1:
93+
self.text.setText(layers[selected[0]])
94+
else:
95+
if isinstance(self.param, ParameterMultipleInput):
96+
self.text.setText(';'.join(layers[idx].name() for idx in selected))
97+
else:
98+
rowdif = len(layers) - (self.table.rowCount() - self.row)
99+
for i in range(rowdif):
100+
self.batchDialog.addRow()
101+
for i, layeridx in enumerate(selected):
102+
self.table.cellWidget(i + self.row,
103+
self.col).setText(layers[layeridx].name())
104+
105+
def showFileSelectionDialog(self):
57106
settings = QtCore.QSettings()
58107
text = unicode(self.text.text())
59108
if os.path.isdir(text):
@@ -72,7 +121,7 @@ def showSelectionDialog(self):
72121
if len(files) == 1:
73122
settings.setValue('/Processing/LastInputPath',
74123
os.path.dirname(unicode(files[0])))
75-
self.text.setText(str(files[0]))
124+
self.text.setText(files[0])
76125
else:
77126
settings.setValue('/Processing/LastInputPath',
78127
os.path.dirname(unicode(files[0])))
@@ -82,9 +131,9 @@ def showSelectionDialog(self):
82131
rowdif = len(files) - (self.table.rowCount() - self.row)
83132
for i in range(rowdif):
84133
self.batchDialog.addRow()
85-
for i in range(len(files)):
134+
for i, f in enumerate(files):
86135
self.table.cellWidget(i + self.row,
87-
self.col).setText(files[i])
136+
self.col).setText(f)
88137

89138
def setText(self, text):
90139
return self.text.setText(text)

python/plugins/processing/gui/BatchProcessingDialog.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
__revision__ = '$Format:%H$'
2727

28-
from PyQt4 import QtCore, QtGui
28+
from PyQt4 import QtGui
2929
from PyQt4.QtCore import *
3030
from PyQt4.QtGui import *
3131
from processing.core.ProcessingResults import ProcessingResults
@@ -86,8 +86,10 @@ def __init__(self, alg):
8686
self.buttonBox.addButton(self.deleteRowButton,
8787
QtGui.QDialogButtonBox.ActionRole)
8888

89+
nOutputs = self.alg.getVisibleOutputsCount() + 1
90+
if nOutputs == 1: nOutputs = 0
8991
self.table.setColumnCount(self.alg.getVisibleParametersCount()
90-
+ self.alg.getVisibleOutputsCount() + 1)
92+
+ nOutputs)
9193
self.setTableContent()
9294
self.table.horizontalHeader().setStretchLastSection(True)
9395
self.table.verticalHeader().setVisible(False)
@@ -146,8 +148,9 @@ def setTableContent(self):
146148
QtGui.QTableWidgetItem(out.description))
147149
i += 1
148150

149-
self.table.setColumnWidth(i, 200)
150-
self.table.setHorizontalHeaderItem(i,
151+
if self.alg.getVisibleOutputsCount():
152+
self.table.setColumnWidth(i, 200)
153+
self.table.setHorizontalHeaderItem(i,
151154
QtGui.QTableWidgetItem('Load in QGIS'))
152155

153156
for i in range(3):
@@ -314,21 +317,26 @@ def addRow(self):
314317
self.table.setRowHeight(self.table.rowCount() - 1, 22)
315318
i = 0
316319
for param in self.alg.parameters:
320+
if param.hidden:
321+
continue
317322
self.table.setCellWidget(self.table.rowCount() - 1, i,
318323
self.getWidgetFromParameter(param,
319324
self.table.rowCount() - 1, i))
320325
i += 1
321326
for out in self.alg.outputs:
327+
if out.hidden:
328+
continue
322329
self.table.setCellWidget(self.table.rowCount() - 1, i,
323330
BatchOutputSelectionPanel(out, self.alg,
324331
self.table.rowCount() - 1, i, self))
325332
i += 1
326333

327-
item = QtGui.QComboBox()
328-
item.addItem('Yes')
329-
item.addItem('No')
330-
item.setCurrentIndex(0)
331-
self.table.setCellWidget(self.table.rowCount() - 1, i, item)
334+
if self.alg.getVisibleOutputsCount():
335+
item = QtGui.QComboBox()
336+
item.addItem('Yes')
337+
item.addItem('No')
338+
item.setCurrentIndex(0)
339+
self.table.setCellWidget(self.table.rowCount() - 1, i, item)
332340

333341
def showAdvancedParametersClicked(self):
334342
self.showAdvanced = not self.showAdvanced

python/plugins/processing/gui/MultipleInputDialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, options, selectedoptions=None):
3838
self.setupUi(self)
3939

4040
self.options = options
41-
self.selectedoptions = selectedoptions
41+
self.selectedoptions = selectedoptions or []
4242

4343
# Additional buttons
4444
self.btnSelectAll = QPushButton(self.tr('Select all'))

python/plugins/processing/tools/dataobjects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def getSupportedOutputTableExtensions():
6666

6767
def getRasterLayers():
6868
layers = QgsMapLayerRegistry.instance().mapLayers().values()
69-
raster = list()
69+
raster = []
7070

7171
for layer in layers:
7272
if layer.type() == layer.RasterLayer:

0 commit comments

Comments
 (0)