2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ScriptAlgorithmProvider.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__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
import os
|
|
|
|
|
2017-04-04 12:58:25 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsProcessingProvider)
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
2012-12-08 19:42:43 +02:00
|
|
|
from processing.gui.EditScriptAction import EditScriptAction
|
|
|
|
from processing.gui.DeleteScriptAction import DeleteScriptAction
|
|
|
|
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
|
|
|
|
from processing.script.ScriptUtils import ScriptUtils
|
2014-04-20 16:07:04 +02:00
|
|
|
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
|
2014-06-02 22:43:47 +02:00
|
|
|
from processing.gui.GetScriptsAndModels import GetScriptsAction
|
2017-04-04 12:52:21 +10:00
|
|
|
from processing.gui.ProviderActions import (ProviderActions,
|
|
|
|
ProviderContextMenuActions)
|
2016-05-27 21:07:44 +02:00
|
|
|
from processing.script.CreateScriptCollectionPluginAction import CreateScriptCollectionPluginAction
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-04-04 12:58:25 +10:00
|
|
|
class ScriptAlgorithmProvider(QgsProcessingProvider):
|
2014-07-13 17:16:24 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def __init__(self):
|
2017-01-09 09:18:25 +10:00
|
|
|
super().__init__()
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2017-04-04 09:46:46 +10:00
|
|
|
self.folder_algorithms = []
|
2017-04-04 12:42:00 +10:00
|
|
|
self.actions = [CreateNewScriptAction('Create new script',
|
|
|
|
CreateNewScriptAction.SCRIPT_PYTHON),
|
|
|
|
AddScriptFromFileAction(),
|
|
|
|
GetScriptsAction(),
|
|
|
|
CreateScriptCollectionPluginAction()]
|
2013-10-01 20:52:22 +03:00
|
|
|
self.contextMenuActions = \
|
|
|
|
[EditScriptAction(EditScriptAction.SCRIPT_PYTHON),
|
|
|
|
DeleteScriptAction(DeleteScriptAction.SCRIPT_PYTHON)]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
def load(self):
|
2017-04-04 12:22:29 +10:00
|
|
|
ProcessingConfig.settingIcons[self.name()] = self.icon()
|
2017-01-09 09:18:25 +10:00
|
|
|
ProcessingConfig.addSetting(Setting(self.name(),
|
2016-01-30 09:33:24 +11:00
|
|
|
ScriptUtils.SCRIPTS_FOLDER,
|
|
|
|
self.tr('Scripts folder', 'ScriptAlgorithmProvider'),
|
2016-05-24 21:56:17 +03:00
|
|
|
ScriptUtils.defaultScriptsFolder(), valuetype=Setting.MULTIPLE_FOLDERS))
|
2017-04-04 12:42:00 +10:00
|
|
|
ProviderActions.registerProviderActions(self, self.actions)
|
2017-04-04 12:52:21 +10:00
|
|
|
ProviderContextMenuActions.registerProviderContextMenuActions(self.contextMenuActions)
|
2017-04-04 13:28:44 +10:00
|
|
|
ProcessingConfig.readSettings()
|
|
|
|
self.refreshAlgorithms()
|
2017-04-04 09:46:46 +10:00
|
|
|
return True
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def unload(self):
|
2017-04-04 14:34:00 +10:00
|
|
|
ProcessingConfig.removeSetting(ScriptUtils.SCRIPTS_FOLDER)
|
2017-04-04 12:42:00 +10:00
|
|
|
ProviderActions.deregisterProviderActions(self)
|
2017-04-04 12:52:21 +10:00
|
|
|
ProviderContextMenuActions.deregisterProviderContextMenuActions(self.contextMenuActions)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def icon(self):
|
2017-01-09 12:34:46 +10:00
|
|
|
return QgsApplication.getThemeIcon("/processingScript.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("processingScript.svg")
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def id(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return 'script'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def name(self):
|
2014-10-03 21:56:24 +03:00
|
|
|
return self.tr('Scripts', 'ScriptAlgorithmProvider')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
def loadAlgorithms(self):
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2016-05-24 21:56:17 +03:00
|
|
|
folders = ScriptUtils.scriptsFolders()
|
|
|
|
for f in folders:
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs.extend(ScriptUtils.loadFromFolder(f))
|
|
|
|
self.algs.extend(self.folder_algorithms)
|
|
|
|
for a in self.algs:
|
2017-04-04 09:46:46 +10:00
|
|
|
self.addAlgorithm(a)
|
2015-11-27 08:50:29 +01:00
|
|
|
|
2015-11-27 09:12:43 +01:00
|
|
|
def addAlgorithmsFromFolder(self, folder):
|
2017-04-04 09:46:46 +10:00
|
|
|
self.folder_algorithms.extend(ScriptUtils.loadFromFolder(folder))
|