ExportModelAsPythonScriptAction.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. ***************************************************************************
  3. EditModelAction.py
  4. ---------------------
  5. Date : February 2019
  6. Copyright : (C) 2019 by Nyall Dawson
  7. Email : nyall dot dawson at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Nyall Dawson'
  18. __date__ = 'February 2019'
  19. __copyright__ = '(C) 2019, Nyall Dawson'
  20. from qgis.PyQt.QtCore import QCoreApplication
  21. from qgis.core import QgsProcessingAlgorithm, QgsProcessing, QgsApplication
  22. from qgis.utils import iface
  23. from processing.gui.ContextAction import ContextAction
  24. from processing.script.ScriptEditorDialog import ScriptEditorDialog
  25. class ExportModelAsPythonScriptAction(ContextAction):
  26. def __init__(self):
  27. super().__init__()
  28. self.name = QCoreApplication.translate('ExportModelAsPythonScriptAction', 'Export Model as Python Algorithm…')
  29. def isEnabled(self):
  30. return isinstance(self.itemData, QgsProcessingAlgorithm) and self.itemData.provider().id() in ("model", "project")
  31. def icon(self):
  32. return QgsApplication.getThemeIcon('/mActionSaveAsPython.svg')
  33. def execute(self):
  34. alg = self.itemData
  35. dlg = ScriptEditorDialog(parent=iface.mainWindow())
  36. dlg.editor.setText('\n'.join(alg.asPythonCode(QgsProcessing.PythonQgsProcessingAlgorithmSubclass, 4)))
  37. dlg.show()