2014-04-20 16:07:04 +02:00
|
|
|
# -*- 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'
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
|
2018-02-22 00:36:30 +01:00
|
|
|
from qgis.PyQt.QtCore import QFileInfo, QCoreApplication
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2017-07-03 21:25:04 +10:00
|
|
|
from qgis.core import QgsApplication, QgsSettings, QgsProcessingModelAlgorithm
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
from processing.gui.ToolboxAction import ToolboxAction
|
2018-03-03 12:41:05 -05:00
|
|
|
from processing.modeler.exceptions import WrongModelException
|
2014-04-20 16:07:04 +02:00
|
|
|
from processing.modeler.ModelerUtils import ModelerUtils
|
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
|
|
|
|
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
class AddModelFromFileAction(ToolboxAction):
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-05-21 10:51:22 +07:00
|
|
|
self.name = QCoreApplication.translate('AddModelFromFileAction', 'Add Model to Toolbox…')
|
2018-01-29 16:03:01 +02:00
|
|
|
self.group = self.tr('Tools')
|
2014-04-20 16:07:04 +02:00
|
|
|
|
|
|
|
def getIcon(self):
|
2017-01-09 12:34:46 +10:00
|
|
|
return QgsApplication.getThemeIcon("/processingModel.svg")
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
def execute(self):
|
2017-03-03 20:39:17 +01:00
|
|
|
settings = QgsSettings()
|
2015-04-10 16:20:04 +03:00
|
|
|
lastDir = settings.value('Processing/lastModelsDir', '')
|
2016-09-05 11:39:43 +10:00
|
|
|
filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox,
|
2018-02-21 18:29:42 +10:00
|
|
|
self.tr('Open Model', 'AddModelFromFileAction'), lastDir,
|
2018-08-03 08:34:05 +03:00
|
|
|
self.tr('Processing models (*.model3 *.MODEL3)', 'AddModelFromFileAction'))
|
2014-04-20 16:07:04 +02:00
|
|
|
if filename:
|
2017-06-20 17:26:57 +10:00
|
|
|
settings.setValue('Processing/lastModelsDir',
|
|
|
|
QFileInfo(filename).absoluteDir().absolutePath())
|
2015-04-10 16:20:04 +03:00
|
|
|
|
2017-07-03 21:25:04 +10:00
|
|
|
alg = QgsProcessingModelAlgorithm()
|
2017-06-20 17:26:57 +10:00
|
|
|
if not alg.fromFile(filename):
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
QMessageBox.warning(
|
|
|
|
self.toolbox,
|
2018-02-21 18:29:42 +10:00
|
|
|
self.tr('Open Model', 'AddModelFromFileAction'),
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
self.tr('The selected file does not contain a valid model', 'AddModelFromFileAction'))
|
2014-04-20 16:07:04 +02:00
|
|
|
return
|
2016-07-25 16:22:21 +03:00
|
|
|
destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename))
|
2015-08-22 14:29:41 +02:00
|
|
|
shutil.copyfile(filename, destFilename)
|
2017-04-04 11:28:28 +10:00
|
|
|
QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()
|