Skip to content

Commit

Permalink
Refactor provider actions and remove from AlgorithmProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 7, 2017
1 parent 117260d commit 651355d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
4 changes: 4 additions & 0 deletions python/plugins/processing/algs/r/RAlgorithmProvider.py
Expand Up @@ -38,6 +38,7 @@
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
from processing.script.WrongScriptException import WrongScriptException
from processing.gui.GetScriptsAndModels import GetRScriptsAction
from processing.gui.ProviderActions import ProviderActions
from processing.tools.system import isWindows

from .RUtils import RUtils
Expand All @@ -52,6 +53,7 @@ class RAlgorithmProvider(AlgorithmProvider):
def __init__(self):
super().__init__()
self.algs = []
self.actions = []
self.actions.append(CreateNewScriptAction(
'Create new R script', CreateNewScriptAction.SCRIPT_R))
self.actions.append(GetRScriptsAction())
Expand Down Expand Up @@ -79,6 +81,7 @@ def load(self):
ProcessingConfig.addSetting(Setting(
self.name(),
RUtils.R_USE64, self.tr('Use 64 bit version'), False))
ProviderActions.registerProviderActions(self, self.actions)
return True

def unload(self):
Expand All @@ -88,6 +91,7 @@ def unload(self):
ProcessingConfig.removeSetting(RUtils.R_FOLDER)
ProcessingConfig.removeSetting(RUtils.R_LIBS_USER)
ProcessingConfig.removeSetting(RUtils.R_USE64)
ProviderActions.deregisterProviderActions(self)

def isActive(self):
return ProcessingConfig.getSetting('ACTIVATE_R')
Expand Down
1 change: 0 additions & 1 deletion python/plugins/processing/core/AlgorithmProvider.py
Expand Up @@ -39,5 +39,4 @@ class AlgorithmProvider(QgsProcessingProvider):

def __init__(self):
super().__init__()
self.actions = []
self.contextMenuActions = []
5 changes: 0 additions & 5 deletions python/plugins/processing/core/Processing.py
Expand Up @@ -66,8 +66,6 @@

class Processing(object):

actions = {}

# All the registered context menu actions for the toolbox
contextMenuActions = []

Expand All @@ -81,7 +79,6 @@ def addProvider(provider):
if provider.load():
ProcessingConfig.readSettings()
provider.refreshAlgorithms()
Processing.actions[provider.id()] = provider.actions
Processing.contextMenuActions.extend(provider.contextMenuActions)
QgsApplication.processingRegistry().addProvider(provider)
PROVIDERS.append(provider)
Expand All @@ -102,8 +99,6 @@ def removeProvider(provider):
contributes a provider.
"""
provider.unload()
if provider.id() in Processing.actions:
del Processing.actions[provider.id()]
for act in provider.contextMenuActions:
Processing.contextMenuActions.remove(act)
QgsApplication.processingRegistry().removeProvider(provider.id())
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/gui/ProcessingToolbox.py
Expand Up @@ -47,6 +47,7 @@
from processing.gui.ConfigDialog import ConfigDialog
from processing.gui.MessageBarProgress import MessageBarProgress
from processing.gui.AlgorithmExecutor import execute
from processing.gui.ProviderActions import ProviderActions

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
Expand Down Expand Up @@ -416,8 +417,8 @@ def populate(self):
groupItem.addChild(algItem)
count += 1

if self.provider.id() in Processing.actions:
actions = Processing.actions[self.provider.id()]
if self.provider.id() in ProviderActions.actions:
actions = ProviderActions.actions[self.provider.id()]
for action in actions:
if action.group in groups:
groupItem = groups[action.group]
Expand Down
37 changes: 37 additions & 0 deletions python/plugins/processing/gui/ProviderActions.py
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
ProviderActions.py
-------------------
Date : April 2017
Copyright : (C) 2017 by Nyall Dawson
Email : nyall dot dawson 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__ = 'Nyall Dawson'
__date__ = 'April 2017'
__copyright__ = '(C) 2017, Nyall Dason'


class ProviderActions(object):
actions = {}

@staticmethod
def registerProviderActions(provider, actions):
""" Adds menu actions for a provider """
ProviderActions.actions[provider.id()] = actions

@staticmethod
def deregisterProviderActions(provider):
""" Removes menu actions for a provider """
if provider.id() in ProviderActions.actions:
del ProviderActions.actions[provider.id()]
5 changes: 5 additions & 0 deletions python/plugins/processing/modeler/ModelerAlgorithmProvider.py
Expand Up @@ -40,6 +40,7 @@
from processing.modeler.DeleteModelAction import DeleteModelAction
from processing.modeler.AddModelFromFileAction import AddModelFromFileAction
from processing.gui.GetScriptsAndModels import GetModelsAction
from processing.gui.ProviderActions import ProviderActions

pluginPath = os.path.split(os.path.dirname(__file__))[0]

Expand All @@ -57,8 +58,12 @@ def load(self):
ProcessingConfig.addSetting(Setting(self.name(),
ModelerUtils.MODELS_FOLDER, self.tr('Models folder', 'ModelerAlgorithmProvider'),
ModelerUtils.defaultModelsFolder(), valuetype=Setting.MULTIPLE_FOLDERS))
ProviderActions.registerProviderActions(self, self.actions)
return True

def unload(self):
ProviderActions.deregisterProviderActions(self)

def modelsFolder(self):
return ModelerUtils.modelsFolders()[0]

Expand Down
13 changes: 8 additions & 5 deletions python/plugins/processing/script/ScriptAlgorithmProvider.py
Expand Up @@ -37,6 +37,7 @@
from processing.script.ScriptUtils import ScriptUtils
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
from processing.gui.GetScriptsAndModels import GetScriptsAction
from processing.gui.ProviderActions import ProviderActions
from processing.script.CreateScriptCollectionPluginAction import CreateScriptCollectionPluginAction

pluginPath = os.path.split(os.path.dirname(__file__))[0]
Expand All @@ -48,11 +49,11 @@ def __init__(self):
super().__init__()
self.algs = []
self.folder_algorithms = []
self.actions.extend([CreateNewScriptAction('Create new script',
CreateNewScriptAction.SCRIPT_PYTHON),
AddScriptFromFileAction(),
GetScriptsAction(),
CreateScriptCollectionPluginAction()])
self.actions = [CreateNewScriptAction('Create new script',
CreateNewScriptAction.SCRIPT_PYTHON),
AddScriptFromFileAction(),
GetScriptsAction(),
CreateScriptCollectionPluginAction()]
self.contextMenuActions = \
[EditScriptAction(EditScriptAction.SCRIPT_PYTHON),
DeleteScriptAction(DeleteScriptAction.SCRIPT_PYTHON)]
Expand All @@ -63,10 +64,12 @@ def load(self):
ScriptUtils.SCRIPTS_FOLDER,
self.tr('Scripts folder', 'ScriptAlgorithmProvider'),
ScriptUtils.defaultScriptsFolder(), valuetype=Setting.MULTIPLE_FOLDERS))
ProviderActions.registerProviderActions(self, self.actions)
return True

def unload(self):
ProcessingConfig.addSetting(ScriptUtils.SCRIPTS_FOLDER)
ProviderActions.deregisterProviderActions(self)

def icon(self):
return QgsApplication.getThemeIcon("/processingScript.svg")
Expand Down

0 comments on commit 651355d

Please sign in to comment.