ModelerAlgorithmProvider.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """
  2. ***************************************************************************
  3. ModelerAlgorithmProvider.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf 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__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. from qgis.core import (Qgis,
  22. QgsApplication,
  23. QgsProcessingProvider,
  24. QgsMessageLog,
  25. QgsProcessingModelAlgorithm,
  26. QgsRuntimeProfiler)
  27. from processing.core.ProcessingConfig import ProcessingConfig, Setting
  28. from processing.gui.ContextAction import ContextAction
  29. from processing.gui.ProviderActions import (ProviderActions,
  30. ProviderContextMenuActions)
  31. from processing.modeler.AddModelFromFileAction import AddModelFromFileAction
  32. from processing.modeler.CreateNewModelAction import CreateNewModelAction
  33. from processing.modeler.DeleteModelAction import DeleteModelAction
  34. from processing.modeler.EditModelAction import EditModelAction
  35. from processing.modeler.ExportModelAsPythonScriptAction import ExportModelAsPythonScriptAction
  36. from processing.modeler.OpenModelFromFileAction import OpenModelFromFileAction
  37. from processing.modeler.ModelerUtils import ModelerUtils
  38. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  39. class ModelerAlgorithmProvider(QgsProcessingProvider):
  40. def __init__(self):
  41. super().__init__()
  42. self.actions = [CreateNewModelAction(), OpenModelFromFileAction(), AddModelFromFileAction()]
  43. sep_action = ContextAction()
  44. sep_action.is_separator = True
  45. self.contextMenuActions = [EditModelAction(), DeleteModelAction(), sep_action, ExportModelAsPythonScriptAction()]
  46. self.algs = []
  47. self.isLoading = False
  48. # must reload models if providers list is changed - previously unavailable algorithms
  49. # which models depend on may now be available
  50. QgsApplication.processingRegistry().providerAdded.connect(self.onProviderAdded)
  51. def onProviderAdded(self, provider_id):
  52. self.refreshAlgorithms()
  53. def load(self):
  54. with QgsRuntimeProfiler.profile('Model Provider'):
  55. ProcessingConfig.settingIcons[self.name()] = self.icon()
  56. ProcessingConfig.addSetting(Setting(self.name(),
  57. ModelerUtils.MODELS_FOLDER, self.tr('Models folder', 'ModelerAlgorithmProvider'),
  58. ModelerUtils.defaultModelsFolder(), valuetype=Setting.MULTIPLE_FOLDERS))
  59. ProviderActions.registerProviderActions(self, self.actions)
  60. ProviderContextMenuActions.registerProviderContextMenuActions(self.contextMenuActions)
  61. ProcessingConfig.readSettings()
  62. self.refreshAlgorithms()
  63. return True
  64. def unload(self):
  65. ProviderActions.deregisterProviderActions(self)
  66. ProviderContextMenuActions.deregisterProviderContextMenuActions(self.contextMenuActions)
  67. def modelsFolder(self):
  68. return ModelerUtils.modelsFolders()[0]
  69. def name(self):
  70. return self.tr('Models', 'ModelerAlgorithmProvider')
  71. def id(self):
  72. return 'model'
  73. def icon(self):
  74. return QgsApplication.getThemeIcon("/processingModel.svg")
  75. def svgIconPath(self):
  76. return QgsApplication.iconPath("processingModel.svg")
  77. def supportsNonFileBasedOutput(self):
  78. return True
  79. def loadAlgorithms(self):
  80. with QgsRuntimeProfiler.profile('Load model algorithms'):
  81. if self.isLoading:
  82. return
  83. self.isLoading = True
  84. self.algs = []
  85. folders = ModelerUtils.modelsFolders()
  86. for f in folders:
  87. self.loadFromFolder(f)
  88. for a in self.algs:
  89. self.addAlgorithm(a)
  90. self.isLoading = False
  91. def loadFromFolder(self, folder):
  92. if not os.path.exists(folder):
  93. return
  94. for path, subdirs, files in os.walk(folder):
  95. for descriptionFile in files:
  96. if descriptionFile.endswith('model3'):
  97. fullpath = os.path.join(path, descriptionFile)
  98. alg = QgsProcessingModelAlgorithm()
  99. if alg.fromFile(fullpath):
  100. if alg.name():
  101. alg.setSourceFilePath(fullpath)
  102. self.algs.append(alg)
  103. else:
  104. QgsMessageLog.logMessage(self.tr('Could not load model {0}', 'ModelerAlgorithmProvider').format(descriptionFile),
  105. self.tr('Processing'), Qgis.Critical)