Skip to content

Commit

Permalink
Merge pull request #3038 from volaya/provider_add_remove
Browse files Browse the repository at this point in the history
[processing] different approach for provider add/remove
  • Loading branch information
alexbruy committed Apr 27, 2016
2 parents c96f66e + bee4068 commit 87575bd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 52 deletions.
2 changes: 0 additions & 2 deletions python/plugins/processing/ProcessingPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def initGui(self):
self.toolbox = ProcessingToolbox()
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.toolbox)
self.toolbox.hide()
Processing.addAlgListListener(self.toolbox)

self.menu = QMenu(self.iface.mainWindow().menuBar())
self.menu.setObjectName('processing')
Expand Down Expand Up @@ -147,7 +146,6 @@ def openCommander(self):
self.commander = CommanderWindow(
self.iface.mainWindow(),
self.iface.mapCanvas())
Processing.addAlgListListener(self.commander)
self.commander.prepareGui()
self.commander.show()

Expand Down
36 changes: 9 additions & 27 deletions python/plugins/processing/core/Processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import sys
import traceback

from PyQt.QtCore import Qt, QCoreApplication
from PyQt.QtCore import Qt, QCoreApplication, QObject, pyqtSignal
from PyQt.QtWidgets import QApplication
from PyQt.QtGui import QCursor

Expand Down Expand Up @@ -61,6 +61,12 @@

from processing.tools import dataobjects

class AlgListWatcher(QObject):

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

algListWatcher = AlgListWatcher()

class Processing:

Expand Down Expand Up @@ -93,6 +99,7 @@ def addProvider(provider, updateList=True):
ProcessingConfig.readSettings()
if updateList:
Processing.updateAlgsList()
algListWatcher.providerAdded.emit(provider.getName())
except:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Expand All @@ -111,7 +118,7 @@ def removeProvider(provider):
provider.unload()
Processing.providers.remove(provider)
del Processing.algs[provider.getName()]
Processing.fireAlgsListHasChanged()
algListWatcher.providerRemoved.emit(provider.getName())
except:
# This try catch block is here to avoid problems if the
# plugin with a provider is unloaded after the Processing
Expand Down Expand Up @@ -151,8 +158,6 @@ def initialize():
ProcessingConfig.readSettings()
RenderingStyles.loadStyles()
Processing.loadFromProviders()
# Inform registered listeners that all providers' algorithms have been loaded
Processing.fireAlgsListHasChanged()

@staticmethod
def updateAlgsList():
Expand All @@ -162,7 +167,6 @@ def updateAlgsList():
"""
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
Processing.loadFromProviders()
Processing.fireAlgsListHasChanged()
QApplication.restoreOverrideCursor()

@staticmethod
Expand All @@ -177,28 +181,6 @@ def updateProviders():
for provider in providers:
provider.loadAlgorithms()

@staticmethod
def addAlgListListener(listener):
"""
Listener should implement a algsListHasChanged() method.
Whenever the list of algorithms changes, that method will be
called for all registered listeners.
"""
Processing.listeners.append(listener)

@staticmethod
def removeAlgListListener(listener):
try:
Processing.listeners.remove(listener)
except:
pass

@staticmethod
def fireAlgsListHasChanged():
for listener in Processing.listeners:
listener.algsListHasChanged()

@staticmethod
def loadAlgorithms():
Processing.algs = {}
Expand Down
8 changes: 7 additions & 1 deletion python/plugins/processing/core/ProcessingConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@

import os

from PyQt.QtCore import QPyNullVariant, QCoreApplication, QSettings
from PyQt.QtCore import QPyNullVariant, QCoreApplication, QSettings, QObject, pyqtSignal
from PyQt.QtGui import QIcon
from processing.tools.system import defaultOutputFolder
import processing.tools.dataobjects

class SettingsWatcher(QObject):

settingsChanged = pyqtSignal()

settingsWatcher = SettingsWatcher()


class ProcessingConfig:

Expand Down
6 changes: 4 additions & 2 deletions python/plugins/processing/gui/ConfigDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
from qgis.gui import QgsDoubleSpinBox
from qgis.gui import QgsSpinBox

from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingConfig import Setting
from processing.core.ProcessingConfig import (ProcessingConfig,
settingsWatcher,
Setting)
from processing.core.Processing import Processing
from processing.gui.menus import updateMenus
from processing.gui.menus import menusSettingsGroup
Expand Down Expand Up @@ -249,6 +250,7 @@ def accept(self):
return
setting.save()
Processing.updateAlgsList()
settingsWatcher.settingsChanged.emit()
updateMenus()

QDialog.accept(self)
Expand Down
59 changes: 39 additions & 20 deletions python/plugins/processing/gui/ProcessingToolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
from qgis.utils import iface

from processing.gui.Postprocessing import handleAlgorithmResults
from processing.core.Processing import Processing
from processing.core.Processing import Processing, algListWatcher
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingConfig import ProcessingConfig, settingsWatcher
from processing.gui.MessageDialog import MessageDialog
from processing.gui import AlgorithmClassification
from processing.gui.AlgorithmDialog import AlgorithmDialog
Expand All @@ -53,8 +53,6 @@

class ProcessingToolbox(BASE, WIDGET):

updateAlgList = True

def __init__(self):
super(ProcessingToolbox, self).__init__(None)
self.tipWasClosed = False
Expand Down Expand Up @@ -83,6 +81,10 @@ def openSettings(url):

self.fillTree()

algListWatcher.providerRemoved.connect(self.removeProvider)
algListWatcher.providerAdded.connect(self.addProvider)
settingsWatcher.settingsChanged.connect(self.fillTree)

def showDisabled(self):
self.txtDisabled.setVisible(False)
for providerName in self.disabledWithMatchingAlgs:
Expand Down Expand Up @@ -154,27 +156,26 @@ def activateProvider(self, providerName):
QMessageBox.warning(self, "Activate provider",
"The provider has been activated, but it might need additional configuration.")

def algsListHasChanged(self):
if self.updateAlgList:
self.fillTree()
self.textChanged()
def updateProvider(self, providerName):
item = self._providerItem(providerName)
if item is not None:
item.refresh()
item.sortChildren(0, Qt.AscendingOrder)
for i in xrange(item.childCount()):
item.child(i).sortChildren(0, Qt.AscendingOrder)
self.addRecentAlgorithms(True)

def removeProvider(self, providerName):
item = self._providerItem(providerName)
if item is not None:
self.algorithmTree.invisibleRootItem().removeChild(item)

def updateProvider(self, providerName, updateAlgsList=True):
if updateAlgsList:
self.updateAlgList = False
Processing.updateAlgsList()
self.updateAlgList = True
def _providerItem(self, providerName):
for i in xrange(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.providerName == providerName:
child.refresh()
# sort categories and items in categories
child.sortChildren(0, Qt.AscendingOrder)
for i in xrange(child.childCount()):
child.child(i).sortChildren(0, Qt.AscendingOrder)
break
self.addRecentAlgorithms(True)
return child

def showPopupMenu(self, point):
item = self.algorithmTree.itemAt(point)
Expand Down Expand Up @@ -304,6 +305,24 @@ def addRecentAlgorithms(self, updating):

self.algorithmTree.setWordWrap(True)

def addProvider(self, providerName):
name = 'ACTIVATE_' + providerName.upper().replace(' ', '_')
providerItem = TreeProviderItem(providerName, None, self)
if ProcessingConfig.getSetting(name):
providerItem.setHidden(providerItem.childCount() == 0)
else:
providerItem = TreeProviderItem(providerName, None, self)
providerItem.setHidden(True)
self.disabledProviderItems[providerName] = providerItem

for i in xrange(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.text(0) > providerItem.text(0):
break
self.algorithmTree.insertTopLevelItem(i, providerItem)


def fillTreeUsingProviders(self):
self.algorithmTree.clear()
self.disabledProviderItems = {}
Expand Down

0 comments on commit 87575bd

Please sign in to comment.