AddModelFromFileAction.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. ***************************************************************************
  3. EditScriptAction.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__ = 'April 2014'
  19. __copyright__ = '(C) 201, Victor Olaya'
  20. import os
  21. import shutil
  22. from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
  23. from qgis.PyQt.QtCore import QFileInfo, QCoreApplication, QDir
  24. from qgis.core import QgsApplication, QgsSettings, QgsProcessingModelAlgorithm
  25. from processing.gui.ToolboxAction import ToolboxAction
  26. from processing.modeler.ModelerUtils import ModelerUtils
  27. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  28. class AddModelFromFileAction(ToolboxAction):
  29. def __init__(self):
  30. self.name = QCoreApplication.translate('AddModelFromFileAction', 'Add Model to Toolbox…')
  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. alg = QgsProcessingModelAlgorithm()
  44. if not alg.fromFile(filename):
  45. QMessageBox.warning(
  46. self.toolbox,
  47. self.tr('Open Model', 'AddModelFromFileAction'),
  48. self.tr('The selected file does not contain a valid model', 'AddModelFromFileAction'))
  49. return
  50. if QgsApplication.instance().processingRegistry().algorithmById(f'model:{alg.id()}'):
  51. QMessageBox.warning(
  52. self.toolbox,
  53. self.tr('Open Model', 'AddModelFromFileAction'),
  54. self.tr('Model with the same name already exists', 'AddModelFromFileAction'))
  55. return
  56. destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename))
  57. if os.path.exists(destFilename):
  58. reply = QMessageBox.question(
  59. self.toolbox,
  60. self.tr('Open Model', 'AddModelFromFileAction'),
  61. self.tr('There is already a model file with the same name. Overwrite?', 'AddModelFromFileAction'),
  62. QMessageBox.Yes | QMessageBox.No,
  63. QMessageBox.No)
  64. if reply == QMessageBox.No:
  65. return
  66. shutil.copyfile(filename, destFilename)
  67. QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()