Skip to content

Commit

Permalink
[processing] New c++ QgsProcessingRegistry class
Browse files Browse the repository at this point in the history
Intended as a registry for the various processing components,
including providers, algorithms and various parameters and outputs.

Currently handles only registration of providers, as a step toward
removing processing's algList (this requires first porting
the algorithm class to c++)

A QgsProcessingRegistry instance is attached to QgsApplication,
and is retrieved by QgsApplication::processingRegistry()
  • Loading branch information
nyalldawson committed Jan 11, 2017
1 parent bb24dfe commit dca697b
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 65 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -260,6 +260,7 @@
%Include layertree/qgslayertreeutils.sip

%Include processing/qgsprocessingprovider.sip
%Include processing/qgsprocessingregistry.sip

%Include raster/qgsbilinearrasterresampler.sip
%Include raster/qgsbrightnesscontrastfilter.sip
Expand Down
72 changes: 72 additions & 0 deletions python/core/processing/qgsprocessingregistry.sip
@@ -0,0 +1,72 @@
/**
* \class QgsProcessingRegistry
* \ingroup core
* Registry for various processing components, including providers, algorithms
* and various parameters and outputs.
*
* QgsProcessingRegistry is not usually directly created, but rather accessed through
* QgsApplication::processingRegistry().
* \note added in QGIS 3.0
*/
class QgsProcessingRegistry : QObject
{
%TypeHeaderCode
#include <qgsprocessingregistry.h>
%End

public:

/**
* Constructor for QgsProcessingRegistry.
*/
QgsProcessingRegistry( QObject* parent /TransferThis/ = nullptr );

~QgsProcessingRegistry();

/**
* Get list of available providers.
*/
QList<QgsProcessingProvider*> providers() const;

/**
* Add a processing provider to the registry. Ownership of the provider is transferred to the registry.
* Returns false if the provider could not be added (eg if a provider with a duplicate ID already exists
* in the registry).
* @see removeProvider()
*/
bool addProvider( QgsProcessingProvider* provider /Transfer/ );

/**
* Removes a provider implementation from the registry (the provider object is deleted).
* Returns false if the provider could not be removed (eg provider does not exist in the registry).
* @see addProvider()
*/
bool removeProvider( QgsProcessingProvider* provider );

/**
* Removes a provider implementation from the registry (the provider object is deleted).
* Returns false if the provider could not be removed (eg provider does not exist in the registry).
* @see addProvider()
*/
bool removeProvider( const QString& providerId );

/**
* Returns a matching provider by provider ID.
*/
QgsProcessingProvider* providerById( const QString& id );

signals:

//! Emitted when a provider has been added to the registry.
void providerAdded( const QString& id );

//! Emitted when a provider is removed from the registry
void providerRemoved( const QString& id );

private:

//! Registry cannot be copied
QgsProcessingRegistry( const QgsProcessingRegistry& other );
//! Registry cannot be copied
//QgsProcessingRegistry& operator=( const QgsProcessingRegistry& other );
};
7 changes: 7 additions & 0 deletions python/core/qgsapplication.sip
Expand Up @@ -442,6 +442,13 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
*/
static QgsMessageLog* messageLog();

/**
* Returns the application's processing registry, used for managing processing providers,
* algorithms, and various parameters and outputs.
* @note added in QGIS 3.0
*/
static QgsProcessingRegistry* processingRegistry();

%If(ANDROID)
//dummy method to workaround sip generation issue issue
bool x11EventFilter ( XEvent * event );
Expand Down
14 changes: 5 additions & 9 deletions python/plugins/processing/core/Processing.py
Expand Up @@ -38,7 +38,8 @@
from qgis.PyQt.QtGui import QCursor

from qgis.utils import iface
from qgis.core import QgsMessageLog
from qgis.core import (QgsMessageLog,
QgsApplication)

import processing
from processing.core.AlgorithmProvider import AlgorithmProvider
Expand Down Expand Up @@ -87,7 +88,7 @@ def addProvider(provider, updateList=True):
"""Use this method to add algorithms from external providers.
"""

if provider.id() in [p.id() for p in algList.providers]:
if provider.id() in [p.id() for p in QgsApplication.processingRegistry().providers()]:
return
try:
provider.initializeSettings()
Expand Down Expand Up @@ -128,11 +129,6 @@ def removeProvider(provider):
# before I find out how to properly avoid that.
pass

@staticmethod
def providerById(id):
"""Returns the provider with the given id."""
return algList.providerById(id)

@staticmethod
def activateProvider(providerOrName, activate=True):
provider_id = providerOrName.id() if isinstance(providerOrName, AlgorithmProvider) else providerOrName
Expand All @@ -155,7 +151,7 @@ def initialize():
@staticmethod
def addScripts(folder):
Processing.initialize()
provider = Processing.providerById("qgis")
provider = QgsApplication.processingRegistry().providerById("qgis")
scripts = ScriptUtils.loadFromFolder(folder)
# fix_print_with_import
print(scripts)
Expand All @@ -168,7 +164,7 @@ def addScripts(folder):

@staticmethod
def removeScripts(folder):
provider = Processing.providerById("qgis")
provider = QgsApplication.processingRegistry().providerById("qgis")
for alg in provider.externalAlgs[::-1]:
path = os.path.dirname(alg.descriptionFile)
if path == folder:
Expand Down
21 changes: 6 additions & 15 deletions python/plugins/processing/core/alglist.py
Expand Up @@ -25,13 +25,13 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsProcessingRegistry)
from qgis.PyQt.QtCore import QObject, pyqtSignal


class AlgorithmList(QObject):

providerAdded = pyqtSignal(str)
providerRemoved = pyqtSignal(str)
providerUpdated = pyqtSignal(str)

# A dictionary of algorithms. Keys are names of providers
Expand All @@ -41,13 +41,10 @@ class AlgorithmList(QObject):
providers = []

def removeProvider(self, provider_id):
for p in self.providers:
if p.id() == provider_id:
self.providers.remove(p)
break
if provider_id in self.algs:
del self.algs[provider_id]
self.providerRemoved.emit(provider_id)

QgsApplication.processingRegistry().removeProvider(provider_id)

def reloadProvider(self, provider_id):
for p in self.providers:
Expand All @@ -58,14 +55,8 @@ def reloadProvider(self, provider_id):
break

def addProvider(self, provider):
self.providers.append(provider)
self.algs[provider.id()] = {a.commandLineName(): a for a in provider.algs}
self.providerAdded.emit(provider.id())

def providerById(self, id):
for provider in self.providers:
if provider.id() == id:
return provider
if QgsApplication.processingRegistry().addProvider(provider):
self.algs[provider.id()] = {a.commandLineName(): a for a in provider.algs}

def getAlgorithm(self, name):
for provider in list(self.algs.values()):
Expand Down
10 changes: 6 additions & 4 deletions python/plugins/processing/gui/ProcessingToolbox.py
Expand Up @@ -33,6 +33,8 @@
from qgis.PyQt.QtCore import Qt, QCoreApplication
from qgis.PyQt.QtWidgets import QMenu, QAction, QTreeWidgetItem, QLabel, QMessageBox
from qgis.utils import iface
from qgis.core import (QgsApplication,
QgsProcessingRegistry)

from processing.gui.Postprocessing import handleAlgorithmResults
from processing.core.Processing import Processing
Expand Down Expand Up @@ -82,8 +84,8 @@ def openSettings(url):

self.fillTree()

algList.providerRemoved.connect(self.removeProvider)
algList.providerAdded.connect(self.addProvider)
QgsApplication.processingRegistry().providerRemoved.connect(self.removeProvider)
QgsApplication.processingRegistry().providerAdded.connect(self.addProvider)
algList.providerUpdated.connect(self.updateProvider)
settingsWatcher.settingsChanged.connect(self.fillTree)

Expand Down Expand Up @@ -159,7 +161,7 @@ def activateProvider(self, id):
self.fillTree()
self.textChanged()
self.showDisabled()
provider = Processing.providerById(id)
provider = QgsApplication.processingRegistry().providerById(id)
if not provider.canBeActivated():
QMessageBox.warning(self, "Activate provider",
"The provider has been activated, but it might need additional configuration.")
Expand Down Expand Up @@ -376,7 +378,7 @@ def __init__(self, provider_id, tree, toolbox):
self.tree = tree
self.toolbox = toolbox
self.provider_id = provider_id
self.provider = Processing.providerById(provider_id)
self.provider = QgsApplication.processingRegistry().providerById(provider_id)
self.setIcon(0, self.provider.icon())
self.populate()

Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/gui/ScriptEditorDialog.py
Expand Up @@ -43,7 +43,6 @@
from qgis.core import QgsApplication
from qgis.utils import iface

from processing.core.alglist import algList
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.gui.HelpEditionDialog import HelpEditionDialog
from processing.algs.r.RAlgorithm import RAlgorithm
Expand Down Expand Up @@ -279,10 +278,10 @@ def setHasChanged(self, hasChanged):
def runAlgorithm(self):
if self.algType == self.SCRIPT_PYTHON:
alg = ScriptAlgorithm(None, self.editor.text())
alg.provider = algList.providerById('script')
alg.provider = QgsApplication.processingRegistry().providerById('script')
if self.algType == self.SCRIPT_R:
alg = RAlgorithm(None, self.editor.text())
alg.provider = algList.providerById('r')
alg.provider = QgsApplication.processingRegistry().providerById('r')

dlg = alg.getCustomParametersDialog()
if not dlg:
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/gui/menus.py
Expand Up @@ -8,6 +8,7 @@
from processing.gui.MessageDialog import MessageDialog
from processing.gui.AlgorithmDialog import AlgorithmDialog
from qgis.utils import iface
from qgis.core import QgsApplication
from processing.gui.MessageBarProgress import MessageBarProgress
from processing.gui.AlgorithmExecutor import runalg
from processing.gui.Postprocessing import handleAlgorithmResults
Expand Down Expand Up @@ -111,7 +112,7 @@


def initializeMenus():
for provider in algList.providers:
for provider in QgsApplication.processingRegistry().providers():
for alg in provider.algs:
d = defaultMenuEntries.get(alg.commandLineName(), "")
setting = Setting(menusSettingsGroup, "MENU_" + alg.commandLineName(),
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -272,7 +272,7 @@ def closeEvent(self, evt):
def editHelp(self):
if self.alg.provider is None:
# Might happen if model is opened from modeler dialog
self.alg.provider = algList.providerById('model')
self.alg.provider = QgsApplication.processingRegistry().providerById('model')
alg = self.alg.getCopy()
dlg = HelpEditionDialog(alg)
dlg.exec_()
Expand All @@ -287,7 +287,7 @@ def runModel(self):

if self.alg.provider is None:
# Might happen if model is opened from modeler dialog
self.alg.provider = algList.providerById('model')
self.alg.provider = QgsApplication.processingRegistry().providerById('model')
alg = self.alg.getCopy()
dlg = AlgorithmDialog(alg)
dlg.exec_()
Expand Down Expand Up @@ -640,7 +640,7 @@ def fillAlgorithmTreeUsingProviders(self):

if len(groups) > 0:
providerItem = QTreeWidgetItem()
provider = algList.providerById(provider_id)
provider = QgsApplication.processingRegistry().providerById(provider_id)
providerItem.setText(0, provider.name())
providerItem.setToolTip(0, provider.name())
providerItem.setIcon(0, provider.icon())
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/script/ScriptSelector.py
Expand Up @@ -31,7 +31,7 @@
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QTreeWidgetItem, QFileDialog

from processing.core.alglist import algList
from qgis.core import QgsApplication

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
Expand All @@ -47,7 +47,7 @@ def __init__(self):
self.scripts = None

allScripts = defaultdict(list)
alglist = algList.providerById("script").algs
alglist = QgsApplication.processingRegistry().providerById("script").algs
for script in alglist:
allScripts[script.group].append(script)

Expand Down
3 changes: 3 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -79,6 +79,7 @@ SET(QGIS_CORE_SRCS
auth/qgsauthmethodregistry.cpp

processing/qgsprocessingprovider.cpp
processing/qgsprocessingregistry.cpp

qgis.cpp
qgsapplication.cpp
Expand Down Expand Up @@ -562,6 +563,8 @@ SET(QGIS_CORE_MOC_HDRS
composer/qgsgroupungroupitemscommand.h
composer/qgspaperitem.h

processing/qgsprocessingregistry.h

raster/qgsrasterlayer.h
raster/qgsrasterdataprovider.h

Expand Down

0 comments on commit dca697b

Please sign in to comment.