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'
|
|
|
|
|
|
|
|
# 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
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.PyQt.QtCore import QFileInfo
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.core import QgsApplication, QgsSettings
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
from processing.gui.ToolboxAction import ToolboxAction
|
|
|
|
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
|
|
|
|
from processing.modeler.WrongModelException import WrongModelException
|
|
|
|
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):
|
2016-04-11 15:55:49 +03:00
|
|
|
self.name, self.i18n_name = self.trAction('Add model from file')
|
|
|
|
self.group, self.i18n_group = self.trAction('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,
|
|
|
|
self.tr('Open model', 'AddModelFromFileAction'), lastDir,
|
2017-06-24 13:34:14 +10:00
|
|
|
self.tr('Processing model files (*.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-06-20 17:26:57 +10:00
|
|
|
alg = ModelerAlgorithm()
|
|
|
|
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,
|
|
|
|
self.tr('Error reading model', 'AddModelFromFileAction'),
|
|
|
|
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()
|