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.QtGui import QIcon
|
|
|
|
from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
|
|
|
|
from qgis.PyQt.QtCore import QSettings, QFileInfo
|
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
|
2016-05-19 12:25:04 +02:00
|
|
|
from processing.core.alglist import algList
|
2014-04-20 16:07:04 +02:00
|
|
|
|
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):
|
2015-05-18 19:51:26 +03:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'model.png'))
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
def execute(self):
|
2015-04-10 16:20:04 +03:00
|
|
|
settings = QSettings()
|
|
|
|
lastDir = settings.value('Processing/lastModelsDir', '')
|
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
|
|
|
filename = QFileDialog.getOpenFileName(self.toolbox,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Open model', 'AddModelFromFileAction'), lastDir,
|
|
|
|
self.tr('Processing model files (*.model *.MODEL)', 'AddModelFromFileAction'))
|
2014-04-20 16:07:04 +02:00
|
|
|
if filename:
|
|
|
|
try:
|
2015-04-10 16:20:04 +03:00
|
|
|
settings.setValue('Processing/lastModelsDir',
|
2015-08-22 14:29:41 +02:00
|
|
|
QFileInfo(filename).absoluteDir().absolutePath())
|
2015-04-10 16:20:04 +03:00
|
|
|
|
2014-08-22 11:27:57 +02:00
|
|
|
ModelerAlgorithm.fromFile(filename)
|
2014-04-20 16:07:04 +02:00
|
|
|
except WrongModelException:
|
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
|
2014-06-08 00:21:12 +02:00
|
|
|
except:
|
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,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Error reading model', 'AddModelFromFileAction'),
|
|
|
|
self.tr('Cannot read file', 'AddModelFromFileAction'))
|
2015-06-22 10:12:02 +02:00
|
|
|
return
|
2016-07-25 14:17:46 +03:00
|
|
|
destFilename = os.path.join(ModelerUtils.defaultModelsFolder(), os.path.basename(filename))
|
2015-08-22 14:29:41 +02:00
|
|
|
shutil.copyfile(filename, destFilename)
|
2016-05-19 12:25:04 +02:00
|
|
|
algList.reloadProvider('model')
|