/
AlgorithmDialog.py
217 lines (182 loc) · 8.86 KB
/
AlgorithmDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
"""
***************************************************************************
AlgorithmDialog.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. *
* *
***************************************************************************
"""
from builtins import str
from builtins import range
__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$'
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QMessageBox, QApplication, QPushButton, QWidget, QVBoxLayout
from qgis.PyQt.QtGui import QCursor, QColor, QPalette
from qgis.core import QgsMapLayerRegistry
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
from processing.gui.ParametersPanel import ParametersPanel
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
from processing.gui.AlgorithmExecutor import runalg, runalgIterating
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterFixedTable
from processing.core.parameters import ParameterRange
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterPoint
from processing.core.parameters import ParameterGeometryPredicate
from processing.core.outputs import OutputRaster
from processing.core.outputs import OutputVector
from processing.core.outputs import OutputTable
class AlgorithmDialog(AlgorithmDialogBase):
def __init__(self, alg):
AlgorithmDialogBase.__init__(self, alg)
self.alg = alg
self.mainWidget = ParametersPanel(self, alg)
self.setMainWidget()
self.cornerWidget = QWidget()
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 5)
self.tabWidget.setStyleSheet("QTabBar::tab { height: 30px; }")
self.runAsBatchButton = QPushButton(self.tr("Run as batch process..."))
self.runAsBatchButton.clicked.connect(self.runAsBatch)
layout.addWidget(self.runAsBatchButton)
self.cornerWidget.setLayout(layout)
self.tabWidget.setCornerWidget(self.cornerWidget)
QgsMapLayerRegistry.instance().layerWasAdded.connect(self.mainWidget.layerRegistryChanged)
QgsMapLayerRegistry.instance().layersWillBeRemoved.connect(self.mainWidget.layerRegistryChanged)
def runAsBatch(self):
self.close()
dlg = BatchAlgorithmDialog(self.alg)
dlg.show()
dlg.exec_()
def setParamValues(self):
params = self.alg.parameters
outputs = self.alg.outputs
for param in params:
if param.hidden:
continue
wrapper = self.mainWidget.wrappers[param.name]
if not self.setParamValue(param, wrapper):
raise AlgorithmDialogBase.InvalidParameterValue(param, wrapper.widget)
for output in outputs:
if output.hidden:
continue
output.value = self.mainWidget.valueItems[output.name].getValue()
if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
output.open = self.mainWidget.checkBoxes[output.name].isChecked()
return True
def setParamValue(self, param, wrapper, alg=None):
return param.setValue(wrapper.value())
def accept(self):
self.settings.setValue("/Processing/dialogBase", self.saveGeometry())
checkCRS = ProcessingConfig.getSetting(ProcessingConfig.WARN_UNMATCHING_CRS)
try:
self.setParamValues()
if checkCRS and not self.alg.checkInputCRS():
reply = QMessageBox.question(self, self.tr("Unmatching CRS's"),
self.tr('Layers do not all use the same CRS. This can '
'cause unexpected results.\nDo you want to '
'continue?'),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.No:
return
msg = self.alg._checkParameterValuesBeforeExecuting()
if msg:
QMessageBox.warning(
self, self.tr('Unable to execute algorithm'), msg)
return
self.btnRun.setEnabled(False)
self.btnClose.setEnabled(False)
buttons = self.mainWidget.iterateButtons
self.iterateParam = None
for i in range(len(list(buttons.values()))):
button = list(buttons.values())[i]
if button.isChecked():
self.iterateParam = list(buttons.keys())[i]
break
self.progressBar.setMaximum(0)
self.lblProgress.setText(self.tr('Processing algorithm...'))
# Make sure the Log tab is visible before executing the algorithm
try:
self.tabWidget.setCurrentIndex(1)
self.repaint()
except:
pass
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
self.setInfo(
self.tr('<b>Algorithm %s starting...</b>') % self.alg.name)
if self.iterateParam:
if runalgIterating(self.alg, self.iterateParam, self):
self.finish()
else:
QApplication.restoreOverrideCursor()
self.resetGUI()
else:
command = self.alg.getAsCommand()
if command:
ProcessingLog.addToLog(
ProcessingLog.LOG_ALGORITHM, command)
if runalg(self.alg, self):
self.finish()
else:
QApplication.restoreOverrideCursor()
self.resetGUI()
except AlgorithmDialogBase.InvalidParameterValue as e:
try:
self.buttonBox.accepted.connect(lambda:
e.widget.setPalette(QPalette()))
palette = e.widget.palette()
palette.setColor(QPalette.Base, QColor(255, 255, 0))
e.widget.setPalette(palette)
self.lblProgress.setText(
self.tr('<b>Missing parameter value: %s</b>') % e.parameter.description)
return
except:
QMessageBox.critical(self,
self.tr('Unable to execute algorithm'),
self.tr('Wrong or missing parameter values'))
def finish(self):
keepOpen = ProcessingConfig.getSetting(ProcessingConfig.KEEP_DIALOG_OPEN)
if self.iterateParam is None:
if not handleAlgorithmResults(self.alg, self, not keepOpen):
self.resetGUI()
return
self.executed = True
self.setInfo('Algorithm %s finished' % self.alg.name)
QApplication.restoreOverrideCursor()
if not keepOpen:
self.close()
else:
self.resetGUI()
if self.alg.getHTMLOutputsCount() > 0:
self.setInfo(
self.tr('HTML output has been generated by this algorithm.'
'\nOpen the results dialog to check it.'))
def closeEvent(self, evt):
QgsMapLayerRegistry.instance().layerWasAdded.disconnect(self.mainWidget.layerRegistryChanged)
QgsMapLayerRegistry.instance().layersWillBeRemoved.disconnect(self.mainWidget.layerRegistryChanged)
super(AlgorithmDialog, self).closeEvent(evt)