DeleteModelAction.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. ***************************************************************************
  3. DeleteModelAction.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 (QgsApplication,
  22. QgsProcessingAlgorithm,
  23. QgsProject)
  24. from qgis.PyQt.QtWidgets import QMessageBox
  25. from qgis.PyQt.QtCore import QCoreApplication
  26. from processing.gui.ContextAction import ContextAction
  27. from processing.modeler.ProjectProvider import PROJECT_PROVIDER_ID
  28. class DeleteModelAction(ContextAction):
  29. def __init__(self):
  30. super().__init__()
  31. self.name = QCoreApplication.translate('DeleteModelAction', 'Delete Model…')
  32. def isEnabled(self):
  33. return isinstance(self.itemData, QgsProcessingAlgorithm) and self.itemData.provider().id() in ("model", "project")
  34. def execute(self):
  35. model = self.itemData
  36. if model is None:
  37. return # shouldn't happen, but let's be safe
  38. project_provider = model.provider().id() == PROJECT_PROVIDER_ID
  39. if project_provider:
  40. msg = self.tr('Are you sure you want to delete this model from the current project?', 'DeleteModelAction')
  41. else:
  42. msg = self.tr('Are you sure you want to delete this model?', 'DeleteModelAction')
  43. reply = QMessageBox.question(
  44. None,
  45. self.tr('Delete Model', 'DeleteModelAction'),
  46. msg,
  47. QMessageBox.Yes | QMessageBox.No,
  48. QMessageBox.No)
  49. if reply == QMessageBox.Yes:
  50. if project_provider:
  51. provider = QgsApplication.processingRegistry().providerById(PROJECT_PROVIDER_ID)
  52. provider.remove_model(model)
  53. QgsProject.instance().setDirty(True)
  54. else:
  55. os.remove(model.sourceFilePath())
  56. QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()