AddScriptFromTemplateAction.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. ***************************************************************************
  3. AddScriptFromTemplateAction.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2018 by Matteo Ghetta
  7. Email : matteo dot ghetta 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__ = 'Matteo Ghetta'
  18. __date__ = 'March 2018'
  19. __copyright__ = '(C) 2018, Matteo Ghetta'
  20. import os
  21. import codecs
  22. from qgis.PyQt.QtCore import QCoreApplication
  23. from qgis.utils import iface
  24. from processing.gui.ToolboxAction import ToolboxAction
  25. from processing.script.ScriptEditorDialog import ScriptEditorDialog
  26. class AddScriptFromTemplateAction(ToolboxAction):
  27. def __init__(self):
  28. self.name = QCoreApplication.translate("AddScriptFromTemplate", "Create New Script from Template…")
  29. self.group = self.tr("Tools")
  30. def execute(self):
  31. dlg = ScriptEditorDialog(parent=iface.mainWindow())
  32. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  33. templatePath = os.path.join(
  34. pluginPath, 'script', 'ScriptTemplate.py')
  35. with codecs.open(templatePath, 'r', encoding='utf-8') as f:
  36. templateTxt = f.read()
  37. dlg.editor.setText(templateTxt)
  38. dlg.show()