OpenModelFromFileAction.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. ***************************************************************************
  3. OpenModelFromFileAction.py
  4. ---------------------
  5. Date : February 2018
  6. Copyright : (C) 2018 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 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, QDir
  23. from qgis.core import QgsApplication, QgsSettings
  24. from qgis.utils import iface
  25. from processing.gui.ToolboxAction import ToolboxAction
  26. from processing.modeler.ModelerDialog import ModelerDialog
  27. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  28. class OpenModelFromFileAction(ToolboxAction):
  29. def __init__(self):
  30. self.name = QCoreApplication.translate('OpenModelFromFileAction', 'Open Existing Model…')
  31. self.group = self.tr('Tools')
  32. def getIcon(self):
  33. return QgsApplication.getThemeIcon("/processingModel.svg")
  34. def execute(self):
  35. settings = QgsSettings()
  36. lastDir = settings.value('Processing/lastModelsDir', QDir.homePath())
  37. filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox,
  38. self.tr('Open Model', 'AddModelFromFileAction'), lastDir,
  39. self.tr('Processing models (*.model3 *.MODEL3)', 'AddModelFromFileAction'))
  40. if filename:
  41. settings.setValue('Processing/lastModelsDir',
  42. QFileInfo(filename).absoluteDir().absolutePath())
  43. dlg = ModelerDialog.create()
  44. dlg.loadModel(filename)
  45. dlg.show()
  46. dlg.activate()