Skip to content

Commit

Permalink
[processing]do not rebuild thhe whole toolbox tree when filtering or …
Browse files Browse the repository at this point in the history
…updating
  • Loading branch information
volaya committed Mar 26, 2014
1 parent 8c4bfa3 commit 0d63635
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 113 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/ProcessingPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions python/plugins/processing/gui/ContextAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
5 changes: 2 additions & 3 deletions python/plugins/processing/gui/CreateNewScriptAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from processing.gui.ScriptEditorDialog import ScriptEditorDialog
import processing.resources_rc


class CreateNewScriptAction(ToolboxAction):

SCRIPT_PYTHON = 0
Expand All @@ -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')
201 changes: 102 additions & 99 deletions python/plugins/processing/gui/ProcessingToolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)



5 changes: 2 additions & 3 deletions python/plugins/processing/modeler/CreateNewModelAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from processing.gui.ToolboxAction import ToolboxAction
from processing.modeler.ModelerDialog import ModelerDialog


class CreateNewModelAction(ToolboxAction):

def __init__(self):
Expand All @@ -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')
2 changes: 1 addition & 1 deletion python/plugins/processing/modeler/DeleteModelAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 1 addition & 2 deletions python/plugins/processing/modeler/EditModelAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.ModelerDialog import ModelerDialog


class EditModelAction(ContextAction):

def __init__(self):
Expand All @@ -42,4 +41,4 @@ def execute(self):
dlg = ModelerDialog(self.alg.getCopy())
dlg.exec_()
if dlg.update:
self.toolbox.updateTree()
self.updateToolbox()
5 changes: 2 additions & 3 deletions python/plugins/processing/modeler/SaveAsPythonScriptAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from processing.script.ScriptUtils import ScriptUtils
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput


class SaveAsPythonScriptAction(ContextAction):

def __init__(self):
Expand All @@ -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')
Expand Down

0 comments on commit 0d63635

Please sign in to comment.