Skip to content

Commit 56e465c

Browse files
committed
[processing][needs-docs] Resurrect ability to convert models to scripts
Brings back QGIS 2.18's ability to directly convert a Processing model to an equivalent Processing Python script algorithm, correctly updated and working in the 3.x API. Available from the model dialog, and from the right-click context menu on an existing model. Sponsored by Solspec
1 parent bc76678 commit 56e465c

4 files changed

Lines changed: 77 additions & 7 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
EditModelAction.py
6+
---------------------
7+
Date : February 2019
8+
Copyright : (C) 2019 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'February 2019'
22+
__copyright__ = '(C) 2019, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from qgis.PyQt.QtCore import QCoreApplication
29+
from qgis.core import QgsProcessingModelAlgorithm, QgsProcessing, QgsApplication
30+
from processing.gui.ContextAction import ContextAction
31+
from processing.script.ScriptEditorDialog import ScriptEditorDialog
32+
33+
34+
class ExportModelAsPythonScriptAction(ContextAction):
35+
36+
def __init__(self):
37+
super().__init__()
38+
self.name = QCoreApplication.translate('ExportModelAsPythonScriptAction', 'Export Model as Python Algorithm…')
39+
40+
def isEnabled(self):
41+
return isinstance(self.itemData, QgsProcessingModelAlgorithm)
42+
43+
def icon(self):
44+
return QgsApplication.getThemeIcon('/mActionSaveAsPython.svg')
45+
46+
def execute(self):
47+
alg = self.itemData
48+
dlg = ScriptEditorDialog(None)
49+
50+
dlg.editor.setText('\n'.join(alg.asPythonCode(QgsProcessing.PythonQgsProcessingAlgorithmSubclass, 4)))
51+
dlg.show()

python/plugins/processing/modeler/ModelerAlgorithmProvider.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from processing.modeler.CreateNewModelAction import CreateNewModelAction
4848
from processing.modeler.DeleteModelAction import DeleteModelAction
4949
from processing.modeler.EditModelAction import EditModelAction
50+
from processing.modeler.ExportModelAsPythonScriptAction import ExportModelAsPythonScriptAction
5051
from processing.modeler.OpenModelFromFileAction import OpenModelFromFileAction
5152
from processing.modeler.exceptions import WrongModelException
5253
from processing.modeler.ModelerUtils import ModelerUtils
@@ -59,7 +60,9 @@ class ModelerAlgorithmProvider(QgsProcessingProvider):
5960
def __init__(self):
6061
super().__init__()
6162
self.actions = [CreateNewModelAction(), OpenModelFromFileAction(), AddModelFromFileAction()]
62-
self.contextMenuActions = [EditModelAction(), DeleteModelAction()]
63+
sep_action = ContextAction()
64+
sep_action.is_separator = True
65+
self.contextMenuActions = [EditModelAction(), DeleteModelAction(), sep_action, ExportModelAsPythonScriptAction()]
6366
self.algs = []
6467
self.isLoading = False
6568

python/plugins/processing/modeler/ModelerDialog.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
QVBoxLayout,
6262
QGridLayout,
6363
QFrame,
64-
QLineEdit)
64+
QLineEdit,
65+
QToolButton,
66+
QAction)
6567
from qgis.PyQt.QtGui import (QIcon,
6668
QImage,
6769
QPainter,
@@ -70,7 +72,7 @@
7072
from qgis.PyQt.QtPrintSupport import QPrinter
7173
from qgis.core import (Qgis,
7274
QgsApplication,
73-
QgsProcessingAlgorithm,
75+
QgsProcessing,
7476
QgsProject,
7577
QgsSettings,
7678
QgsMessageLog,
@@ -92,6 +94,7 @@
9294
from processing.modeler.ModelerUtils import ModelerUtils
9395
from processing.modeler.ModelerScene import ModelerScene
9496
from processing.modeler.ProjectProvider import PROJECT_PROVIDER_ID
97+
from processing.script.ScriptEditorDialog import ScriptEditorDialog
9598
from qgis.utils import iface
9699

97100

@@ -253,6 +256,13 @@ def __init__(self, model=None):
253256
self.mToolbar.setIconSize(iface.iconSize())
254257
self.setStyleSheet(iface.mainWindow().styleSheet())
255258

259+
self.toolbutton_export_to_script = QToolButton()
260+
self.toolbutton_export_to_script.setPopupMode(QToolButton.InstantPopup)
261+
self.export_to_script_algorithm_action = QAction(self.tr('Export as Script Algorithm…'))
262+
self.toolbutton_export_to_script.addActions([self.export_to_script_algorithm_action])
263+
self.mToolbar.insertWidget(self.mActionExportImage, self.toolbutton_export_to_script)
264+
self.export_to_script_algorithm_action.triggered.connect(self.export_as_script_algorithm)
265+
256266
self.mActionOpen.setIcon(
257267
QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
258268
self.mActionSave.setIcon(
@@ -275,8 +285,8 @@ def __init__(self, model=None):
275285
QgsApplication.getThemeIcon('/mActionSaveAsPDF.svg'))
276286
self.mActionExportSvg.setIcon(
277287
QgsApplication.getThemeIcon('/mActionSaveAsSVG.svg'))
278-
#self.mActionExportPython.setIcon(
279-
# QgsApplication.getThemeIcon('/mActionSaveAsPython.svg'))
288+
self.toolbutton_export_to_script.setIcon(
289+
QgsApplication.getThemeIcon('/mActionSaveAsPython.svg'))
280290
self.mActionEditHelp.setIcon(
281291
QgsApplication.getThemeIcon('/mActionEditHelpContent.svg'))
282292
self.mActionRun.setIcon(
@@ -814,3 +824,9 @@ def getPositionForAlgorithmItem(self):
814824
newX = MARGIN + BOX_WIDTH / 2
815825
newY = MARGIN * 2 + BOX_HEIGHT + BOX_HEIGHT / 2
816826
return QPointF(newX, newY)
827+
828+
def export_as_script_algorithm(self):
829+
dlg = ScriptEditorDialog(None)
830+
831+
dlg.editor.setText('\n'.join(self.model.asPythonCode(QgsProcessing.PythonQgsProcessingAlgorithmSubclass, 4)))
832+
dlg.show()

python/plugins/processing/ui/DlgModeler.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@
166166
</action>
167167
<action name="mActionExportPython">
168168
<property name="text">
169-
<string>Export as Python script…</string>
169+
<string>Export as Python Script…</string>
170170
</property>
171171
<property name="toolTip">
172-
<string>Export as Python script</string>
172+
<string>Export as Python Script</string>
173173
</property>
174174
</action>
175175
<action name="mActionEditHelp">

0 commit comments

Comments
 (0)