26
26
27
27
__revision__ = '$Format:%H$'
28
28
29
+ from pprint import pformat
30
+ import time
31
+
29
32
from qgis .PyQt .QtWidgets import QApplication , QMessageBox , QSizePolicy
30
33
from qgis .PyQt .QtGui import QCursor
31
34
from qgis .PyQt .QtCore import Qt
32
35
33
- from qgis .core import QgsProcessingParameterDefinition
36
+ from qgis .core import (QgsProcessingParameterDefinition ,
37
+ QgsProcessingParameterRasterOutput ,
38
+ QgsProcessingParameterFeatureSink ,
39
+ QgsProcessingOutputLayerDefinition ,
40
+ QgsProcessingOutputHtml ,
41
+ QgsProcessingOutputNumber ,
42
+ QgsProject )
43
+
34
44
from qgis .gui import QgsMessageBar
35
45
36
46
from processing .gui .BatchPanel import BatchPanel
37
47
from processing .gui .AlgorithmDialogBase import AlgorithmDialogBase
38
48
from processing .gui .AlgorithmExecutor import execute
39
49
from processing .gui .Postprocessing import handleAlgorithmResults
40
50
41
- from processing .core .ProcessingResults import ProcessingResults
42
-
43
- from processing .core .outputs import OutputNumber
44
- from processing .core .outputs import OutputString
45
- from processing .core .outputs import OutputHTML
51
+ from processing .core .ProcessingResults import resultsList
46
52
47
53
from processing .tools .system import getTempFilename
48
54
from processing .tools import dataobjects
@@ -56,7 +62,6 @@ def __init__(self, alg):
56
62
AlgorithmDialogBase .__init__ (self , alg )
57
63
58
64
self .alg = alg
59
- self .alg_parameters = []
60
65
61
66
self .setWindowTitle (self .tr ('Batch Processing - {0}' ).format (self .alg .displayName ()))
62
67
@@ -69,11 +74,11 @@ def __init__(self, alg):
69
74
self .layout ().insertWidget (0 , self .bar )
70
75
71
76
def accept (self ):
72
- self .alg_parameters = []
73
- self .load = []
74
- self .canceled = False
77
+ alg_parameters = []
78
+ load = []
75
79
76
80
context = dataobjects .createContext ()
81
+ feedback = self .createFeedback ()
77
82
78
83
for row in range (self .mainWidget .tblParameters .rowCount ()):
79
84
col = 0
@@ -87,82 +92,99 @@ def accept(self):
87
92
self .bar .pushMessage ("" , self .tr ('Wrong or missing parameter value: {0} (row {1})' ).format (
88
93
param .description (), row + 1 ),
89
94
level = QgsMessageBar .WARNING , duration = 5 )
90
- self .algs = None
91
95
return
92
96
col += 1
93
- for out in alg .destinationParameterDefinitions ():
97
+ count_visible_outputs = 0
98
+ for out in self .alg .destinationParameterDefinitions ():
94
99
if out .flags () & QgsProcessingParameterDefinition .FlagHidden :
95
100
continue
96
101
102
+ count_visible_outputs += 1
97
103
widget = self .mainWidget .tblParameters .cellWidget (row , col )
98
104
text = widget .getValue ()
99
- if text .strip () != '' :
100
- out .value = text
105
+ if param .checkValueIsAcceptable (text , context ):
106
+ if isinstance (out , (QgsProcessingParameterRasterOutput ,
107
+ QgsProcessingParameterFeatureSink )):
108
+ # load rasters and sinks on completion
109
+ parameters [out .name ()] = QgsProcessingOutputLayerDefinition (text , context .project ())
110
+ else :
111
+ parameters [out .name ()] = text
101
112
col += 1
102
113
else :
103
114
self .bar .pushMessage ("" , self .tr ('Wrong or missing output value: {0} (row {1})' ).format (
104
115
out .description (), row + 1 ),
105
116
level = QgsMessageBar .WARNING , duration = 5 )
106
- self .algs = None
107
117
return
108
118
109
- self .alg_parameters .append (parameters )
110
- if self .alg .countVisibleOutputs ():
111
- widget = self .mainWidget .tblParameters .cellWidget (row , col )
112
- self .load .append (widget .currentIndex () == 0 )
113
- else :
114
- self .load .append (False )
119
+ alg_parameters .append (parameters )
115
120
116
121
QApplication .setOverrideCursor (QCursor (Qt .WaitCursor ))
117
122
self .mainWidget .setEnabled (False )
123
+ self .buttonCancel .setEnabled (True )
118
124
119
- self .progressBar .setMaximum (len (self .algs ))
120
125
# Make sure the Log tab is visible before executing the algorithm
121
126
try :
122
127
self .tabWidget .setCurrentIndex (1 )
123
128
self .repaint ()
124
129
except :
125
130
pass
126
131
127
- for count , parameters in enumerate (self .alg_parameters ):
128
- self .setText (self .tr ('\n Processing algorithm {0}/{1}...' ).format (count + 1 , len (self .alg_parameters )))
132
+ start_time = time .time ()
133
+
134
+ algorithm_results = []
135
+ for count , parameters in enumerate (alg_parameters ):
136
+ if feedback .isCanceled ():
137
+ break
138
+ self .setText (self .tr ('\n Processing algorithm {0}/{1}...' ).format (count + 1 , len (alg_parameters )))
129
139
self .setInfo (self .tr ('<b>Algorithm {0} starting...</b>' ).format (self .alg .displayName ()), escape_html = False )
130
- ret , results = execute (self .alg , parameters , context , self .feedback )
131
- if ret and not self .canceled :
132
- if self .load [count ]:
133
- handleAlgorithmResults (self .alg , context , self .feedback , False )
140
+
141
+ feedback .pushInfo (self .tr ('Input parameters:' ))
142
+ feedback .pushCommandInfo (pformat (parameters ))
143
+ feedback .pushInfo ('' )
144
+
145
+ alg_start_time = time .time ()
146
+ ret , results = execute (self .alg , parameters , context , feedback )
147
+ if ret :
134
148
self .setInfo (self .tr ('Algorithm {0} correctly executed...' ).format (self .alg .displayName ()), escape_html = False )
149
+ feedback .setProgress (100 )
150
+ feedback .pushInfo (
151
+ self .tr ('Execution completed in {0:0.2f} seconds' .format (time .time () - alg_start_time )))
152
+ feedback .pushInfo (self .tr ('Results:' ))
153
+ feedback .pushCommandInfo (pformat (results ))
154
+ feedback .pushInfo ('' )
155
+ algorithm_results .append (results )
135
156
else :
136
- QApplication .restoreOverrideCursor ()
137
- return
157
+ break
138
158
139
- self .finish ( )
159
+ feedback . pushInfo ( self .tr ( 'Batch execution completed in {0:0.2f} seconds' . format ( time . time () - start_time )) )
140
160
141
- def finish (self ):
142
- for count , parameters in enumerate (self .alg_parameters ):
143
- self .loadHTMLResults (self .alg , count )
161
+ handleAlgorithmResults (self .alg , context , feedback , False )
144
162
145
- self .createSummaryTable ()
163
+ self .finish (algorithm_results )
164
+ self .buttonCancel .setEnabled (False )
165
+
166
+ def finish (self , algorithm_results ):
167
+ for count , results in enumerate (algorithm_results ):
168
+ self .loadHTMLResults (results , count )
169
+
170
+ self .createSummaryTable (algorithm_results )
146
171
QApplication .restoreOverrideCursor ()
147
172
148
173
self .mainWidget .setEnabled (True )
149
174
QMessageBox .information (self , self .tr ('Batch processing' ),
150
175
self .tr ('Batch processing completed' ))
151
176
152
- def loadHTMLResults (self , alg , num ):
153
- for out in alg .outputs :
154
- if out .flags () & QgsProcessingParameterDefinition .FlagHidden or not out .open :
155
- continue
156
-
157
- if isinstance (out , OutputHTML ):
158
- ProcessingResults .addResult (
159
- '{} [{}]' .format (out .description (), num ), out .value )
177
+ def loadHTMLResults (self , results , num ):
178
+ for out in self .alg .outputDefinitions ():
179
+ if isinstance (out , QgsProcessingOutputHtml ) and out .name () in results and results [out .name ()]:
180
+ resultsList .addResult (icon = self .alg .icon (), name = '{} [{}]' .format (out .description (), num ),
181
+ result = results [out .name ()])
160
182
161
- def createSummaryTable (self ):
183
+ def createSummaryTable (self , algorithm_results ):
162
184
createTable = False
163
185
164
- for out in self .algs [ 0 ]. outputs :
165
- if isinstance (out , (OutputNumber , OutputString )):
186
+ for out in self .alg . outputDefinitions () :
187
+ if isinstance (out , (QgsProcessingOutputNumber , QgsProcessingOutputString )):
166
188
createTable = True
167
189
break
168
190
@@ -171,12 +193,12 @@ def createSummaryTable(self):
171
193
172
194
outputFile = getTempFilename ('html' )
173
195
with codecs .open (outputFile , 'w' , encoding = 'utf-8' ) as f :
174
- for alg in self . algs :
196
+ for res in algorithm_results :
175
197
f .write ('<hr>\n ' )
176
- for out in alg .outputs :
177
- if isinstance (out , (OutputNumber , OutputString )) :
178
- f .write ('<p>{}: {}</p>\n ' .format (out .description (), out .value ))
198
+ for out in self . alg .outputDefinitions () :
199
+ if isinstance (out , (QgsProcessingOutputNumber , QgsProcessingOutputString )) and out . name () in res :
200
+ f .write ('<p>{}: {}</p>\n ' .format (out .description (), res [ out .name ()] ))
179
201
f .write ('<hr>\n ' )
180
202
181
- ProcessingResults .addResult (
182
- '{} [summary]' .format (self .algs [ 0 ] .name ), outputFile )
203
+ resultsList .addResult (self . alg . icon (),
204
+ '{} [summary]' .format (self .alg .name () ), outputFile )
0 commit comments