QGIS/python/plugins/processing/modeler/AddModelFromFileAction.py

71 lines
3.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
***************************************************************************
EditScriptAction.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'April 2014'
__copyright__ = '(C) 201, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import shutil
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
from qgis.PyQt.QtCore import QFileInfo
from qgis.core import QgsApplication, QgsSettings, QgsProcessingModelAlgorithm
from processing.gui.ToolboxAction import ToolboxAction
from processing.modeler.WrongModelException import WrongModelException
from processing.modeler.ModelerUtils import ModelerUtils
pluginPath = os.path.split(os.path.dirname(__file__))[0]
class AddModelFromFileAction(ToolboxAction):
def __init__(self):
self.name, self.i18n_name = self.trAction('Add model from file')
self.group, self.i18n_group = self.trAction('Tools')
def getIcon(self):
return QgsApplication.getThemeIcon("/processingModel.svg")
2014-05-02 10:32:37 +02:00
def execute(self):
settings = QgsSettings()
lastDir = settings.value('Processing/lastModelsDir', '')
filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox,
self.tr('Open model', 'AddModelFromFileAction'), lastDir,
2017-06-24 13:34:14 +10:00
self.tr('Processing model files (*.model3 *.MODEL3)', 'AddModelFromFileAction'))
if filename:
settings.setValue('Processing/lastModelsDir',
QFileInfo(filename).absoluteDir().absolutePath())
alg = QgsProcessingModelAlgorithm()
if not alg.fromFile(filename):
QMessageBox.warning(
self.toolbox,
self.tr('Error reading model', 'AddModelFromFileAction'),
self.tr('The selected file does not contain a valid model', 'AddModelFromFileAction'))
return
destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename))
shutil.copyfile(filename, destFilename)
QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()