QGIS/python/plugins/processing/gui/BatchProcessingDialog.py

365 lines
16 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
BatchProcessingDialog.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
2013-08-12 20:44:27 +02:00
from processing.gui.Postprocessing import Postprocessing
from processing.parameters.ParameterFile import ParameterFile
from processing.gui.FileSelectionPanel import FileSelectionPanel
from processing.parameters.ParameterRaster import ParameterRaster
from processing.parameters.ParameterTable import ParameterTable
from processing.parameters.ParameterVector import ParameterVector
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from processing.gui.AlgorithmExecutionDialog import AlgorithmExecutionDialog
from processing.gui.CrsSelectionPanel import CrsSelectionPanel
from processing.outputs.OutputNumber import OutputNumber
from processing.outputs.OutputString import OutputString
from processing.core.ProcessingUtils import ProcessingUtils
from processing.parameters.ParameterExtent import ParameterExtent
from processing.parameters.ParameterCrs import ParameterCrs
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
from processing.parameters.ParameterBoolean import ParameterBoolean
from processing.parameters.ParameterSelection import ParameterSelection
from processing.parameters.ParameterFixedTable import ParameterFixedTable
from processing.gui.FixedTablePanel import FixedTablePanel
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput
from processing.gui.BatchOutputSelectionPanel import BatchOutputSelectionPanel
from processing.gui.AlgorithmExecutor import AlgorithmExecutor
from processing.outputs.OutputHTML import OutputHTML
from processing.core.ProcessingResults import ProcessingResults
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.UnthreadedAlgorithmExecutor import UnthreadedAlgorithmExecutor
2012-09-15 18:25:25 +03:00
class BatchProcessingDialog(AlgorithmExecutionDialog):
def __init__(self, alg):
self.algs = None
self.showAdvanced = False
2012-09-15 18:25:25 +03:00
self.table = QtGui.QTableWidget(None)
2012-12-10 00:12:07 +01:00
AlgorithmExecutionDialog.__init__(self, alg, self.table)
self.setWindowModality(1)
2012-09-15 18:25:25 +03:00
self.algEx = None
self.resize(800, 500)
self.setWindowTitle("Batch Processing - " + self.alg.name)
for param in self.alg.parameters:
if param.isAdvanced:
self.advancedButton = QtGui.QPushButton()
self.advancedButton.setText("Show advanced parameters")
self.advancedButton.setMaximumWidth(150)
self.buttonBox.addButton(self.advancedButton, QtGui.QDialogButtonBox.ActionRole)
self.advancedButton.clicked.connect(self.showAdvancedParametersClicked)
break
2012-09-15 18:25:25 +03:00
self.addRowButton = QtGui.QPushButton()
self.addRowButton.setText("Add row")
self.buttonBox.addButton(self.addRowButton, QtGui.QDialogButtonBox.ActionRole)
self.deleteRowButton = QtGui.QPushButton()
self.deleteRowButton.setText("Delete row")
self.buttonBox.addButton(self.addRowButton, QtGui.QDialogButtonBox.ActionRole)
self.buttonBox.addButton(self.deleteRowButton, QtGui.QDialogButtonBox.ActionRole)
2012-12-10 00:12:07 +01:00
self.table.setColumnCount(self.alg.getVisibleParametersCount() + self.alg.getVisibleOutputsCount() + 1)
2012-09-15 18:25:25 +03:00
self.setTableContent()
self.table.horizontalHeader().setStretchLastSection(True)
self.table.verticalHeader().setVisible(False)
2012-12-10 00:12:07 +01:00
self.table.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
2012-09-15 18:25:25 +03:00
self.addRowButton.clicked.connect(self.addRow)
self.deleteRowButton.clicked.connect(self.deleteRow)
self.table.horizontalHeader().sectionDoubleClicked.connect(self.headerDoubleClicked)
2013-02-28 22:08:32 +01:00
def headerDoubleClicked(self, col):
widget = self.table.cellWidget(0, col)
if isinstance(widget, QtGui.QComboBox):
widgetValue = widget.currentIndex()
for row in range(1, self.table.rowCount()):
self.table.cellWidget(row, col).setCurrentIndex(widgetValue)
elif isinstance(widget, ExtentSelectionPanel):
widgetValue = widget.getValue()
for row in range(1, self.table.rowCount()):
if widgetValue != None:
self.table.cellWidget(row, col).text.setText(widgetValue)
else:
2013-02-28 22:08:32 +01:00
self.table.cellWidget(row, col).text.setText("")
elif isinstance(widget, CrsSelectionPanel):
widgetValue = widget.getValue()
for row in range(1, self.table.rowCount()):
self.table.cellWidget(row, col).setAuthid(widgetValue)
elif isinstance(widget, FileSelectionPanel):
widgetValue = widget.getValue()
for row in range(1, self.table.rowCount()):
2013-03-26 14:15:12 +01:00
self.table.cellWidget(row, col).setText(widgetValue)
elif isinstance(widget, QtGui.QLineEdit):
widgetValue = widget.text()
for row in range(1, self.table.rowCount()):
2013-03-26 14:15:12 +01:00
self.table.cellWidget(row, col).setText(widgetValue)
elif isinstance(widget, BatchInputSelectionPanel):
widgetValue = widget.getText()
for row in range(1, self.table.rowCount()):
self.table.cellWidget(row, col).setText(widgetValue)
2013-03-26 14:15:12 +01:00
else:
2013-02-28 22:08:32 +01:00
pass
2012-09-15 18:25:25 +03:00
def setTableContent(self):
i = 0
for param in self.alg.parameters:
self.table.setColumnWidth(i,250)
self.table.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem(param.description))
if param.isAdvanced:
self.table.setColumnHidden(i, not self.showAdvanced)
2012-09-15 18:25:25 +03:00
i+=1
for out in self.alg.outputs:
self.table.setColumnWidth(i,250)
self.table.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem(out.description))
i+=1
2013-02-28 22:08:32 +01:00
self.table.setColumnWidth(i, 200)
self.table.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem("Load in QGIS"))
2013-02-28 22:08:32 +01:00
2012-09-15 18:25:25 +03:00
for i in range(3):
self.addRow()
2013-02-28 22:08:32 +01:00
2012-09-15 18:25:25 +03:00
def accept(self):
self.algs = []
self.load = []
2012-09-15 18:25:25 +03:00
for row in range(self.table.rowCount()):
alg = self.alg.getCopy()#copy.deepcopy(self.alg)
col = 0
for param in alg.parameters:
if param.hidden:
continue
2012-09-15 18:25:25 +03:00
widget = self.table.cellWidget(row, col)
if not self.setParameterValueFromWidget(param, widget, alg):
self.progressLabel.setText("<b>Missing parameter value: " + param.description + " (row " + str(row + 1) + ")</b>")
2012-09-15 18:25:25 +03:00
self.algs = None
return
col+=1
for out in alg.outputs:
if out.hidden:
continue
2012-09-15 18:25:25 +03:00
widget = self.table.cellWidget(row, col)
text = widget.getValue()
if text.strip() != "":
out.value = text
col+=1
else:
self.progressLabel.setText("<b>Wrong or missing parameter value: " + out.description + " (row " + str(row + 1) + ")</b>")
2012-09-15 18:25:25 +03:00
self.algs = None
return
self.algs.append(alg)
2013-02-28 22:08:32 +01:00
widget = self.table.cellWidget(row, col)
self.load.append(widget.currentIndex() == 0)
2012-09-15 18:25:25 +03:00
2012-12-10 00:12:07 +01:00
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
2012-09-15 18:25:25 +03:00
self.table.setEnabled(False)
2013-08-12 20:44:27 +02:00
if ProcessingConfig.getSetting(ProcessingConfig.USE_THREADS):
self.tabWidget.setCurrentIndex(1)
2012-09-15 18:25:25 +03:00
self.nextAlg(0)
else:
i=0
2012-09-15 18:25:25 +03:00
self.progress.setMaximum(len(self.algs))
for alg in self.algs:
self.setBaseText("Processing algorithm " + str(i+1) + "/" + str(len(self.algs)) + "...")
if UnthreadedAlgorithmExecutor.runalg(alg, self):
if self.load[i]:
2013-08-12 20:44:27 +02:00
Postprocessing.handleAlgorithmResults(alg, self, False)
2012-09-15 18:25:25 +03:00
i+=1
else:
QApplication.restoreOverrideCursor()
return
self.finishAll()
2013-02-28 22:08:32 +01:00
2012-09-15 18:25:25 +03:00
def loadHTMLResults(self, alg, i):
for out in alg.outputs:
if out.hidden or not out.open:
continue
if isinstance(out, OutputHTML):
2013-08-12 20:44:27 +02:00
ProcessingResults.addResult(out.description + "[" + str(i) + "]", out.value)
2012-09-15 18:25:25 +03:00
def cancel(self):
self.algs = None
if self.algEx:
2013-03-26 14:15:12 +01:00
self.algEx.terminate()
self.table.setEnabled(True)
2012-09-15 18:25:25 +03:00
@pyqtSlot()
def finish(self, i):
2013-03-26 14:15:12 +01:00
if not self.stop:
if self.load[i]:
2013-08-12 20:44:27 +02:00
Postprocessing.handleAlgorithmResults(self.algs[i], self, False)
i += 1
if len(self.algs) == i:
self.finishAll()
self.algEx = None
else:
self.nextAlg(i)
2012-12-10 00:12:07 +01:00
@pyqtSlot(str)
2012-09-15 18:25:25 +03:00
def error(self, msg):
QApplication.restoreOverrideCursor()
QMessageBox.critical(self, "Error", msg)
2013-08-12 20:44:27 +02:00
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, msg)
if self.algEx:
2013-03-26 14:15:12 +01:00
self.algEx.terminate()
self.table.setEnabled(True)
2012-12-10 00:12:07 +01:00
2012-09-15 18:25:25 +03:00
def nextAlg(self, i):
self.stop = False
self.setBaseText("Processing algorithm " + str(i + 1) + "/" + str(len(self.algs)) + "...")
2012-09-15 18:25:25 +03:00
self.algEx = AlgorithmExecutor(self.algs[i]);
self.algEx.percentageChanged.connect(self.setPercentage)
self.algEx.textChanged.connect(self.setText)
2012-09-15 18:25:25 +03:00
self.algEx.error.connect(self.error)
self.algEx.finished.connect(lambda: self.finish(i))
self.algEx.infoSet.connect(self.setInfo)
2013-08-12 20:44:27 +02:00
if ProcessingConfig.getSetting(ProcessingConfig.SHOW_DEBUG_IN_DIALOG):
self.algEx.commandSet.connect(self.setCommand)
self.algEx.debugInfoSet.connect(self.setDebugInfo)
self.algEx.consoleInfoSet.connect(self.setConsoleInfo)
2012-09-15 18:25:25 +03:00
self.algEx.start()
2012-12-10 00:12:07 +01:00
def createSummaryTable(self):
createTable = False
for out in self.algs[0].outputs:
if isinstance(out, (OutputNumber,OutputString)):
createTable = True
break
if not createTable:
2012-12-10 00:12:07 +01:00
return
2013-08-12 20:44:27 +02:00
outputFile = ProcessingUtils.getTempFilename("html")
2012-12-10 00:12:07 +01:00
f = open(outputFile, "w")
for alg in self.algs:
2012-11-20 11:01:29 +01:00
f.write("<hr>\n")
2012-12-10 00:12:07 +01:00
for out in alg.outputs:
if isinstance(out, (OutputNumber,OutputString)):
f.write("<p>" + out.description + ": " + str(out.value) + "</p>\n")
2012-11-20 11:01:29 +01:00
f.write("<hr>\n")
f.close()
2013-08-12 20:44:27 +02:00
ProcessingResults.addResult(self.algs[0].name + "[summary]", outputFile)
2012-12-10 00:12:07 +01:00
2012-09-15 18:25:25 +03:00
def finishAll(self):
i = 0
for alg in self.algs:
self.loadHTMLResults(alg, i)
i = i + 1
self.createSummaryTable()
2012-09-15 18:25:25 +03:00
QApplication.restoreOverrideCursor()
self.table.setEnabled(True)
QMessageBox.information(self, "Batch processing", "Batch processing successfully completed!")
def setParameterValueFromWidget(self, param, widget, alg = None):
2012-09-15 18:25:25 +03:00
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable, ParameterMultipleInput)):
value = widget.getText()
if unicode(value).strip() == "":
value = None
return param.setValue(value)
2012-09-15 18:25:25 +03:00
elif isinstance(param, ParameterBoolean):
return param.setValue(widget.currentIndex() == 0)
elif isinstance(param, ParameterSelection):
return param.setValue(widget.currentIndex())
elif isinstance(param, ParameterFixedTable):
return param.setValue(widget.table)
elif isinstance(param, (ParameterExtent)):
if alg != None:
widget.useNewAlg(alg)
return param.setValue(widget.getValue())
elif isinstance(param, (ParameterCrs, ParameterFile)):
2012-12-10 00:12:07 +01:00
return param.setValue(widget.getValue())
2012-09-15 18:25:25 +03:00
else:
return param.setValue(widget.text())
def getWidgetFromParameter(self, param, row, col):
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable, ParameterMultipleInput)):
item = BatchInputSelectionPanel(param, row, col, self)
elif isinstance(param, ParameterBoolean):
item = QtGui.QComboBox()
item.addItem("Yes")
item.addItem("No")
if param.default:
item.setCurrentIndex(0)
else:
item.setCurrentIndex(1)
elif isinstance(param, ParameterSelection):
item = QtGui.QComboBox()
item.addItems(param.options)
elif isinstance(param, ParameterFixedTable):
item = FixedTablePanel(param)
elif isinstance(param, ParameterExtent):
item = ExtentSelectionPanel(self, self.alg, param.default)
elif isinstance(param, ParameterCrs):
2012-12-10 00:12:07 +01:00
item = CrsSelectionPanel(param.default)
elif isinstance(param, ParameterFile):
item = FileSelectionPanel(param.isFolder)
2012-09-15 18:25:25 +03:00
else:
item = QtGui.QLineEdit()
try:
item.setText(str(param.default))
except:
pass
return item
def deleteRow(self):
if self.table.rowCount() > 2:
self.table.setRowCount(self.table.rowCount()-1)
def addRow(self):
self.table.setRowCount(self.table.rowCount()+1)
self.table.setRowHeight(self.table.rowCount()-1, 22)
i=0
for param in self.alg.parameters:
self.table.setCellWidget(self.table.rowCount()-1,i, self.getWidgetFromParameter(param, self.table.rowCount()-1, i))
i+=1
for out in self.alg.outputs:
self.table.setCellWidget(self.table.rowCount()-1,i, BatchOutputSelectionPanel(out, self.alg, self.table.rowCount()-1, i, self))
i+=1
2013-02-28 22:08:32 +01:00
item = QtGui.QComboBox()
item.addItem("Yes")
2013-02-28 22:08:32 +01:00
item.addItem("No")
item.setCurrentIndex(0)
self.table.setCellWidget(self.table.rowCount()-1, i, item)
def showAdvancedParametersClicked(self):
self.showAdvanced = not self.showAdvanced
if self.showAdvanced:
self.advancedButton.setText("Hide advanced parameters")
else:
self.advancedButton.setText("Show advanced parameters")
i = 0
for param in self.alg.parameters:
if param.isAdvanced:
self.table.setColumnHidden(i, not self.showAdvanced)
i+=1
2012-12-10 00:12:07 +01:00
def setText(self, text):
self.progressLabel.setText(self.baseText + " --- [" + text + "]")
2012-12-10 00:12:07 +01:00
def setBaseText(self, text):
self.baseText = text