Skip to content

Commit

Permalink
[processing] different approach for provider add/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Apr 27, 2016
1 parent 8a9cb05 commit f33749a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 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
49 changes: 37 additions & 12 deletions python/plugins/processing/gui/ProcessingToolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
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.gui.MessageDialog import MessageDialog
Expand Down Expand Up @@ -83,6 +83,9 @@ def openSettings(url):

self.fillTree()

algListWatcher.providerRemoved.connect(self.removeProvider)
algListWatcher.providerAdded.connect(self.addProvider)

def showDisabled(self):
self.txtDisabled.setVisible(False)
for providerName in self.disabledWithMatchingAlgs:
Expand Down Expand Up @@ -154,27 +157,31 @@ 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, updateAlgsList=True):
if updateAlgsList:
self.updateAlgList = False
Processing.updateAlgsList()
self.updateAlgList = True
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 _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 +311,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 f33749a

Please sign in to comment.