2014-11-12 19:36:12 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
BatchAlgorithmDialog.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import range
|
2014-11-12 19:36:12 +02:00
|
|
|
|
|
|
|
__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$'
|
|
|
|
|
2016-09-27 19:51:06 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QApplication, QMessageBox, QSizePolicy
|
|
|
|
from qgis.PyQt.QtGui import QCursor
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import Qt
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2016-09-16 14:32:00 +02:00
|
|
|
from qgis.gui import QgsMessageBar
|
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
from processing.gui.BatchPanel import BatchPanel
|
|
|
|
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
|
2017-03-22 17:17:14 +02:00
|
|
|
from processing.gui.AlgorithmExecutor import execute
|
2014-11-12 19:36:12 +02:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
|
|
|
|
|
|
|
from processing.core.ProcessingResults import ProcessingResults
|
|
|
|
|
|
|
|
from processing.core.outputs import OutputNumber
|
|
|
|
from processing.core.outputs import OutputString
|
|
|
|
from processing.core.outputs import OutputHTML
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tools.system import getTempFilename
|
2017-04-26 14:33:53 +10:00
|
|
|
from processing.tools import dataobjects
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
|
|
|
import codecs
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
class BatchAlgorithmDialog(AlgorithmDialogBase):
|
|
|
|
|
|
|
|
def __init__(self, alg):
|
|
|
|
AlgorithmDialogBase.__init__(self, alg)
|
|
|
|
|
|
|
|
self.alg = alg
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
self.setWindowTitle(self.tr('Batch Processing - {0}').format(self.alg.displayName()))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2016-09-14 09:51:17 +02:00
|
|
|
self.setMainWidget(BatchPanel(self, self.alg))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2016-01-08 08:27:22 +01:00
|
|
|
self.textShortHelp.setVisible(False)
|
|
|
|
|
2016-09-16 14:32:00 +02:00
|
|
|
self.bar = QgsMessageBar()
|
|
|
|
self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
|
|
|
|
self.layout().insertWidget(0, self.bar)
|
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
def accept(self):
|
|
|
|
self.algs = []
|
|
|
|
self.load = []
|
|
|
|
self.canceled = False
|
|
|
|
|
|
|
|
for row in range(self.mainWidget.tblParameters.rowCount()):
|
2017-05-19 10:24:59 +10:00
|
|
|
alg = self.alg
|
2014-11-12 19:36:12 +02:00
|
|
|
col = 0
|
2017-05-15 12:29:44 +10:00
|
|
|
for param in alg.parameterDefinitions():
|
|
|
|
if param.hidden or param.isDestination():
|
2014-11-12 19:36:12 +02:00
|
|
|
continue
|
2016-09-16 14:32:00 +02:00
|
|
|
wrapper = self.mainWidget.wrappers[row][col]
|
|
|
|
if not self.mainWidget.setParamValue(param, wrapper, alg):
|
2017-03-04 16:23:36 +01:00
|
|
|
self.bar.pushMessage("", self.tr('Wrong or missing parameter value: {0} (row {1})').format(
|
|
|
|
param.description, row + 1),
|
2016-09-19 11:52:31 +03:00
|
|
|
level=QgsMessageBar.WARNING, duration=5)
|
2014-11-12 19:36:12 +02:00
|
|
|
self.algs = None
|
|
|
|
return
|
|
|
|
col += 1
|
2017-05-15 12:29:44 +10:00
|
|
|
for out in alg.destinationParameterDefinitions():
|
2014-11-12 19:36:12 +02:00
|
|
|
if out.hidden:
|
|
|
|
continue
|
|
|
|
|
|
|
|
widget = self.mainWidget.tblParameters.cellWidget(row, col)
|
|
|
|
text = widget.getValue()
|
|
|
|
if text.strip() != '':
|
|
|
|
out.value = text
|
|
|
|
col += 1
|
|
|
|
else:
|
2017-03-04 16:23:36 +01:00
|
|
|
self.bar.pushMessage("", self.tr('Wrong or missing output value: {0} (row {1})').format(
|
|
|
|
out.description, row + 1),
|
2016-09-16 14:32:00 +02:00
|
|
|
level=QgsMessageBar.WARNING, duration=5)
|
2014-11-12 19:36:12 +02:00
|
|
|
self.algs = None
|
|
|
|
return
|
|
|
|
|
|
|
|
self.algs.append(alg)
|
2017-05-15 12:29:44 +10:00
|
|
|
if len(self.alg.destinationParameterDefinitions()) > 0:
|
2014-11-12 19:36:12 +02:00
|
|
|
widget = self.mainWidget.tblParameters.cellWidget(row, col)
|
|
|
|
self.load.append(widget.currentIndex() == 0)
|
|
|
|
else:
|
|
|
|
self.load.append(False)
|
|
|
|
|
|
|
|
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
|
|
|
|
self.mainWidget.setEnabled(False)
|
|
|
|
|
|
|
|
self.progressBar.setMaximum(len(self.algs))
|
|
|
|
# Make sure the Log tab is visible before executing the algorithm
|
|
|
|
try:
|
|
|
|
self.tabWidget.setCurrentIndex(1)
|
|
|
|
self.repaint()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
context = dataobjects.createContext()
|
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
for count, alg in enumerate(self.algs):
|
2017-03-04 16:23:36 +01:00
|
|
|
self.setText(self.tr('\nProcessing algorithm {0}/{1}...').format(count + 1, len(self.algs)))
|
2017-03-29 12:51:59 +10:00
|
|
|
self.setInfo(self.tr('<b>Algorithm {0} starting...</b>').format(alg.displayName()))
|
2017-04-26 14:33:53 +10:00
|
|
|
if execute(alg, context, self.feedback) and not self.canceled:
|
2014-11-12 19:36:12 +02:00
|
|
|
if self.load[count]:
|
2017-04-26 14:33:53 +10:00
|
|
|
handleAlgorithmResults(alg, context, self.feedback, False)
|
2017-03-29 12:51:59 +10:00
|
|
|
self.setInfo(self.tr('Algorithm {0} correctly executed...').format(alg.displayName()))
|
2014-11-12 19:36:12 +02:00
|
|
|
else:
|
|
|
|
QApplication.restoreOverrideCursor()
|
|
|
|
return
|
|
|
|
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
for count, alg in enumerate(self.algs):
|
|
|
|
self.loadHTMLResults(alg, count)
|
|
|
|
|
|
|
|
self.createSummaryTable()
|
|
|
|
QApplication.restoreOverrideCursor()
|
|
|
|
|
|
|
|
self.mainWidget.setEnabled(True)
|
|
|
|
QMessageBox.information(self, self.tr('Batch processing'),
|
2015-09-29 17:51:30 +02:00
|
|
|
self.tr('Batch processing completed'))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
|
|
|
def loadHTMLResults(self, alg, num):
|
|
|
|
for out in alg.outputs:
|
|
|
|
if out.hidden or not out.open:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(out, OutputHTML):
|
|
|
|
ProcessingResults.addResult(
|
|
|
|
'{} [{}]'.format(out.description, num), out.value)
|
|
|
|
|
|
|
|
def createSummaryTable(self):
|
|
|
|
createTable = False
|
|
|
|
|
|
|
|
for out in self.algs[0].outputs:
|
|
|
|
if isinstance(out, (OutputNumber, OutputString)):
|
|
|
|
createTable = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not createTable:
|
|
|
|
return
|
|
|
|
|
|
|
|
outputFile = getTempFilename('html')
|
|
|
|
with codecs.open(outputFile, 'w', encoding='utf-8') as f:
|
|
|
|
for alg in self.algs:
|
|
|
|
f.write('<hr>\n')
|
|
|
|
for out in alg.outputs:
|
|
|
|
if isinstance(out, (OutputNumber, OutputString)):
|
|
|
|
f.write('<p>{}: {}</p>\n'.format(out.description, out.value))
|
|
|
|
f.write('<hr>\n')
|
|
|
|
|
|
|
|
ProcessingResults.addResult(
|
|
|
|
'{} [summary]'.format(self.algs[0].name), outputFile)
|