Skip to content
Permalink
Browse files

[processing]do not rebuild thhe whole toolbox tree when filtering or …

…updating
  • Loading branch information
volaya committed Mar 26, 2014
1 parent 8c4bfa3 commit 0d63635133ad40accbcc5da4221f06464c2669b3
@@ -59,7 +59,7 @@ def initGui(self):
self.toolbox = ProcessingToolbox()
interface.iface.addDockWidget(Qt.RightDockWidgetArea, self.toolbox)
self.toolbox.hide()
Processing.addAlgListListener(self.toolbox)
#Processing.addAlgListListener(self.toolbox)

self.menu = QMenu(interface.iface.mainWindow())
self.menu.setTitle(QCoreApplication.translate('Processing',
@@ -142,7 +142,8 @@ def openModeler(self):
dlg.show()
dlg.exec_()
if dlg.update:
self.toolbox.updateTree()
Processing.updateAlgsList()
self.toolbox.updateProvider('model')

def openResults(self):
dlg = ResultsDialog()
@@ -31,3 +31,11 @@ class ContextAction:
def setData(self, alg, toolbox):
self.alg = alg
self.toolbox = toolbox

def updateToolbox(self):
'''
Updates the list of algorithms and then the toolbox.
It only update the item corresponding to the provider of the algorithm.
To be called after the action is executed, if needed
'''
self.toolbox.updateProvider(self.alg.provider.getName())
@@ -30,7 +30,6 @@
from processing.gui.ScriptEditorDialog import ScriptEditorDialog
import processing.resources_rc


class CreateNewScriptAction(ToolboxAction):

SCRIPT_PYTHON = 0
@@ -55,5 +54,5 @@ def execute(self):
dlg = ScriptEditorDialog(ScriptEditorDialog.SCRIPT_R, None)
dlg.show()
dlg.exec_()
if dlg.update:
self.toolbox.updateTree()
if dlg.update:
self.toolbox.updateProvider('model')
@@ -64,7 +64,7 @@ def __init__(self):
self.modeComboBox.setCurrentIndex(1)
self.modeComboBox.currentIndexChanged.connect(self.modeHasChanged)

self.searchBox.textChanged.connect(self.fillTree)
self.searchBox.textChanged.connect(self.textChanged)
self.algorithmTree.customContextMenuRequested.connect(
self.showPopupMenu)
self.algorithmTree.doubleClicked.connect(self.executeAlgorithm)
@@ -73,6 +73,33 @@ def __init__(self):
self.searchBox.setPlaceholderText(self.tr('Search...'))

self.fillTree()

def textChanged(self):
text = self.searchBox.text().strip(' ')
self._filterItem(self.algorithmTree.invisibleRootItem(), text)
if text:
self.algorithmTree.expandAll()
else:
self.algorithmTree.collapseAll()
self.algorithmTree.invisibleRootItem().child(0).setExpanded(True)

def _filterItem(self, item, text):
if (item.childCount() > 0):
show = False
for i in xrange(item.childCount()):
child = item.child(i)
showChild = self._filterItem(child, text)
show = showChild or show
item.setHidden(not show)
return show
elif isinstance(item, TreeAlgorithmItem):
hide = bool(text) and (text not in item.text(0))
item.setHidden(hide)
return not hide
else:
item.setHidden(True)
return False


def modeHasChanged(self):
idx = self.modeComboBox.currentIndex()
@@ -85,11 +112,20 @@ def modeHasChanged(self):

self.fillTree()

def algsListHasChanged(self):
self.fillTree()

def updateTree(self):
Processing.updateAlgsList()
self.fillTree()

def updateProvider(self, providerName, updateAlgsList = True):
if updateAlgsList:
Processing.updateAlgsList()
for i in xrange(self.algorithmTree.invisibleRootItem().childCount()):
child = self.algorithmTree.invisibleRootItem().child(i)
if isinstance(child, TreeProviderItem):
if child.providerName == providerName:
child.refresh()
break

def showPopupMenu(self, point):
item = self.algorithmTree.itemAt(point)
@@ -267,111 +303,24 @@ def fillTreeUsingCategories(self):
self.algorithmTree.addTopLevelItem(mainItem)

for providerName in Processing.algs.keys():
groups = {}
provider = Processing.algs[providerName]
if providerName not in providersToExclude:
continue
name = 'ACTIVATE_' + providerName.upper().replace(' ', '_')
if not ProcessingConfig.getSetting(name):
continue
if providerName not in providersToExclude:
continue
algs = provider.values()

# add algorithms

for alg in algs:
if not alg.showInToolbox:
continue
if text == '' or text.lower() in alg.name.lower():
if alg.group in groups:
groupItem = groups[alg.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, alg.group)
groupItem.setToolTip(0, alg.group)
groups[alg.group] = groupItem
algItem = TreeAlgorithmItem(alg)
groupItem.addChild(algItem)

actions = Processing.actions[providerName]
for action in actions:
if text == '' or text.lower() in action.name.lower():
if action.group in groups:
groupItem = groups[action.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, action.group)
groups[action.group] = groupItem
algItem = TreeActionItem(action)
groupItem.addChild(algItem)

if len(groups) > 0:
providerItem = QTreeWidgetItem()
providerItem.setText(0, Processing.getProviderFromName(
providerName).getDescription())
providerItem.setIcon(0, Processing.getProviderFromName(
providerName).getIcon())
providerItem.setToolTip(0, providerItem.text(0))
for groupItem in groups.values():
providerItem.addChild(groupItem)
self.algorithmTree.addTopLevelItem(providerItem)

if text != '':
self.algorithmTree.expandAll()

providerItem = TreeProviderItem(providerName)
self.algorithmTree.addTopLevelItem(providerItem)

def fillTreeUsingProviders(self):
self.algorithmTree.clear()
text = unicode(self.searchBox.text())
for providerName in Processing.algs.keys():
groups = {}
count = 0
provider = Processing.algs[providerName]
name = 'ACTIVATE_' + providerName.upper().replace(' ', '_')
if not ProcessingConfig.getSetting(name):
continue
algs = provider.values()

# Add algorithms
for alg in algs:
if not alg.showInToolbox:
continue
if text == '' or text.lower() in alg.name.lower():
if alg.group in groups:
groupItem = groups[alg.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, alg.group)
groupItem.setToolTip(0, alg.group)
groups[alg.group] = groupItem
algItem = TreeAlgorithmItem(alg)
groupItem.addChild(algItem)
count += 1

actions = Processing.actions[providerName]
for action in actions:
if text == '' or text.lower() in action.name.lower():
if action.group in groups:
groupItem = groups[action.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, action.group)
groups[action.group] = groupItem
algItem = TreeActionItem(action)
groupItem.addChild(algItem)

if len(groups) > 0:
providerItem = QTreeWidgetItem()
providerItem.setText(0, Processing.getProviderFromName(
providerName).getDescription()
+ ' [' + str(count) + ' geoalgorithms]')
providerItem.setIcon(0, Processing.getProviderFromName(
providerName).getIcon())
providerItem.setToolTip(0, providerItem.text(0))
for groupItem in groups.values():
providerItem.addChild(groupItem)
self.algorithmTree.addTopLevelItem(providerItem)
providerItem.setExpanded(text != '')
for groupItem in groups.values():
groupItem.setExpanded(text != '')
providerItem = TreeProviderItem(providerName)
self.algorithmTree.addTopLevelItem(providerItem)
providerItem.setHidden(providerItem.childCount() == 0)



class TreeAlgorithmItem(QTreeWidgetItem):
@@ -399,3 +348,57 @@ def __init__(self, action):
self.action = action
self.setText(0, action.name)
self.setIcon(0, action.getIcon())

class TreeProviderItem(QTreeWidgetItem):

def __init__(self, providerName):
QTreeWidgetItem.__init__(self)
self.providerName = providerName
self.provider = Processing.getProviderFromName(providerName)
self.setIcon(0, self.provider.getIcon())
self.populate()

def refresh(self):
self.takeChildren()
self.populate()

def populate(self):
groups = {}
count = 0
provider = Processing.algs[self.providerName]
algs = provider.values()

# Add algorithms
for alg in algs:
if not alg.showInToolbox:
continue
if alg.group in groups:
groupItem = groups[alg.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, alg.group)
groupItem.setToolTip(0, alg.group)
groups[alg.group] = groupItem
algItem = TreeAlgorithmItem(alg)
groupItem.addChild(algItem)
count += 1

actions = Processing.actions[self.providerName]
for action in actions:
if action.group in groups:
groupItem = groups[action.group]
else:
groupItem = QTreeWidgetItem()
groupItem.setText(0, action.group)
groups[action.group] = groupItem
algItem = TreeActionItem(action)
groupItem.addChild(algItem)

self.setText(0, self.provider.getDescription()
+ ' [' + str(count) + ' geoalgorithms]')
self.setToolTip(0, self.text(0))
for groupItem in groups.values():
self.addChild(groupItem)



@@ -30,7 +30,6 @@
from processing.gui.ToolboxAction import ToolboxAction
from processing.modeler.ModelerDialog import ModelerDialog


class CreateNewModelAction(ToolboxAction):

def __init__(self):
@@ -44,5 +43,5 @@ def execute(self):
dlg = ModelerDialog()
dlg.show()
dlg.exec_()
if dlg.update:
self.toolbox.updateTree()
if dlg.update:
self.toolbox.updateProvider('model')
@@ -46,4 +46,4 @@ def execute(self):
QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
os.remove(self.alg.descriptionFile)
self.toolbox.updateTree()
self.updateToolbox()
@@ -29,7 +29,6 @@
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.ModelerDialog import ModelerDialog


class EditModelAction(ContextAction):

def __init__(self):
@@ -42,4 +41,4 @@ def execute(self):
dlg = ModelerDialog(self.alg.getCopy())
dlg.exec_()
if dlg.update:
self.toolbox.updateTree()
self.updateToolbox()
@@ -34,7 +34,6 @@
from processing.script.ScriptUtils import ScriptUtils
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput


class SaveAsPythonScriptAction(ContextAction):

def __init__(self):
@@ -56,8 +55,8 @@ def execute(self):
fout.write(text)
fout.close()
if filename.replace('\\', '/').startswith(
ScriptUtils.scriptsFolder().replace('\\', '/')):
self.toolbox.updateTree()
ScriptUtils.scriptsFolder().replace('\\', '/')):
self.toolbox.updateProvider('script')
except:
QMessageBox.warning(self, self.tr('I/O error'),
self.tr('Unable to save edits. Reason:\n %s')

0 comments on commit 0d63635

Please sign in to comment.
You can’t perform that action at this time.