OpenScriptFromFileAction.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. ***************************************************************************
  3. OpenScriptFromFileAction.py
  4. ---------------------
  5. Date : May 2018
  6. Copyright : (C) 2018 by Mathieu Pellerin
  7. Email : nirvn dot asia 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 2018'
  19. __copyright__ = '(C) 2018, Nyall Dawson'
  20. import os
  21. from qgis.PyQt.QtWidgets import QFileDialog
  22. from qgis.PyQt.QtCore import QFileInfo, QCoreApplication
  23. from qgis.core import QgsApplication, QgsSettings
  24. from qgis.utils import iface
  25. from processing.gui.ToolboxAction import ToolboxAction
  26. from processing.script.ScriptEditorDialog import ScriptEditorDialog
  27. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  28. class OpenScriptFromFileAction(ToolboxAction):
  29. def __init__(self):
  30. self.name = QCoreApplication.translate('OpenScriptFromFileAction', 'Open Existing Script…')
  31. self.group = self.tr('Tools')
  32. def getIcon(self):
  33. return QgsApplication.getThemeIcon("/processingScript.svg")
  34. def execute(self):
  35. settings = QgsSettings()
  36. lastDir = settings.value('Processing/lastScriptsDir', '')
  37. filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox,
  38. self.tr('Open Script', 'AddScriptFromFileAction'), lastDir,
  39. self.tr('Processing scripts (*.py *.PY)', 'AddScriptFromFileAction'))
  40. if filename:
  41. settings.setValue('Processing/lastScriptsDir',
  42. QFileInfo(filename).absoluteDir().absolutePath())
  43. dlg = ScriptEditorDialog(filePath=filename, parent=iface.mainWindow())
  44. dlg.show()