Skip to content
Permalink
Browse files

[processing] alg menus can now be configured from Processing options

  • Loading branch information
volaya authored and alexbruy committed Mar 1, 2016
1 parent 5df0ce0 commit af8a62d93ec0de089e666d6656b570e65be1219b
@@ -34,14 +34,14 @@
from PyQt4.QtGui import QMenu, QAction, QIcon

from processing.core.Processing import Processing
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.ProcessingToolbox import ProcessingToolbox
from processing.gui.HistoryDialog import HistoryDialog
from processing.gui.ConfigDialog import ConfigDialog
from processing.gui.ResultsDialog import ResultsDialog
from processing.gui.CommanderWindow import CommanderWindow
from processing.modeler.ModelerDialog import ModelerDialog
from processing.tools.system import tempFolder
from processing.gui.menus import removeMenus, initializeMenus, createMenus


cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
@@ -120,7 +120,8 @@ def initGui(self):
self.iface.registerMainWindowAction(self.commanderAction,
self.tr('Ctrl+Alt+M'))

Processing.updateMenus()
initializeMenus()
createMenus()

def unload(self):
self.toolbox.setVisible(False)
@@ -138,6 +139,8 @@ def unload(self):
self.iface.unregisterMainWindowAction(self.resultsAction)
self.iface.unregisterMainWindowAction(self.commanderAction)

removeMenus()

def openCommander(self):
if self.commander is None:
self.commander = CommanderWindow(
@@ -60,9 +60,6 @@
from processing.tools import dataobjects


algorithmsToolbar = None


class Processing:

listeners = []
@@ -378,273 +375,6 @@ def runAlgorithm(algOrName, onFinish, *args, **kwargs):
progress.close()
return alg

@staticmethod
def createVectorMenu():
algs = ['qgis:distancematrix', 'qgis:sumlinelengths',
'qgis:pointsinpolygon', 'qgis:listuniquevalues',
'qgis:basicstatisticsfornumericfields',
'qgis:basicstatisticsfortextfields',
'qgis:nearestneighbouranalysis', 'qgis:meancoordinates',
'qgis:lineintersecions']
for alg in algs:
Processing._addVectorAlgorithm(alg, Processing.tr('&Analysis Tools'))

algs = ['qgis:randomselection', 'qgis:randomselectionwithinsubsets',
'qgis:randompointsinextent', 'qgis:randompointsinlayerbounds',
'qgis:randompointsinsidepolygonsfixed',
'qgis:randompointsinsidepolygonsvariable',
'qgis:regularpoints', 'qgis:vectorgrid',
'qgis:selectbylocation', 'qgis:polygonfromlayerextent']
for alg in algs:
Processing._addVectorAlgorithm(alg, Processing.tr('&Research Tools'))

algs = ['qgis:convexhull', 'qgis:fixeddistancebuffer',
'qgis:variabledistancebuffer', 'qgis:intersection',
'qgis:union', 'qgis:symmetricaldifference', 'qgis:clip',
'qgis:difference', 'qgis:dissolve',
'qgis:eliminatesliverpolygons']
for alg in algs:
Processing._addVectorAlgorithm(alg, Processing.tr('&Geoprocessing Tools'))

algs = ['qgis:checkvalidity', 'qgis:exportaddgeometrycolumns',
'qgis:polygoncentroids', 'qgis:delaunaytriangulation',
'qgis:voronoipolygons', 'qgis:simplifygeometries',
'qgis:densifygeometries', 'qgis:multiparttosingleparts',
'qgis:singlepartstomultipart', 'qgis:polygonstolines',
'qgis:linestopolygons', 'qgis:extractnodes']
for alg in algs:
Processing._addVectorAlgorithm(alg, Processing.tr('G&eometry Tools'))

algs = ['qgis:definecurrentprojection',
'qgis:joinattributesbylocation', 'qgis:splitvectorlayer',
'qgis:mergevectorlayers','qgis:createspatialindex']
for alg in algs:
Processing._addVectorAlgorithm(alg, Processing.tr('&Data Management Tools'))

@staticmethod
def createRasterMenu():
algs = ['gdalogr:warpreproject', 'gdalogr:assignprojection', 'gdalogr:extractprojection']
for alg in algs:
Processing._addRasterAlgorithm(alg, Processing.tr('Projections'))

algs = ['gdalogr:rasterize', 'gdalogr:rasterize_over',
'gdalogr:polygonize', 'gdalogr:translate', 'gdalogr:rgbtopct',
'gdalogr:pcttorgb']
for alg in algs:
Processing._addRasterAlgorithm(alg, Processing.tr('Conversion'))

algs = ['gdalogr:contour', 'gdalogr:cliprasterbyextent',
'gdalogr:cliprasterbymasklayer']
for alg in algs:
Processing._addRasterAlgorithm(alg, Processing.tr('Extraction'))

algs = ['gdalogr:sieve', 'gdalogr:nearblack', 'gdalogr:fillnodata',
'gdalogr:proximity', 'gdalogr:griddatametrics',
'gdalogr:gridaverage', 'gdalogr:gridinvdist',
'gdalogr:gridnearestneighbor', 'gdalogr:aspect',
'gdalogr:hillshade', 'gdalogr:roughness', 'gdalogr:slope',
'gdalogr:tpi', 'gdalogr:tri']
for alg in algs:
Processing._addRasterAlgorithm(alg, Processing.tr('Analysis'))

algs = ['gdalogr:buildvirtualraster', 'gdalogr:merge',
'gdalogr:rasterinfo', 'gdalogr:overviews', 'gdalogr:tileindex']
for alg in algs:
Processing._addRasterAlgorithm(alg, Processing.tr('Miscellaneous'))

@staticmethod
def removeVectorMenu():
algs = ['qgis:distancematrix', 'qgis:sumlinelengths',
'qgis:pointsinpolygon', 'qgis:listuniquevalues',
'qgis:basicstatisticsfornumericfields',
'qgis:basicstatisticsfortextfields',
'qgis:nearestneighbouranalysis', 'qgis:meancoordinates',
'qgis:lineintersecions']
for alg in algs:
Processing._removeVectorAlgorithm(alg, Processing.tr('&Analysis Tools'))

algs = ['qgis:randomselection', 'qgis:randomselectionwithinsubsets',
'qgis:randompointsinextent', 'qgis:randompointsinlayerbounds',
'qgis:randompointsinsidepolygonsfixed',
'qgis:randompointsinsidepolygonsvariable',
'qgis:regularpoints', 'qgis:vectorgrid',
'qgis:selectbylocation', 'qgis:polygonfromlayerextent']
for alg in algs:
Processing._removeVectorAlgorithm(alg, Processing.tr('&Research Tools'))

algs = ['qgis:convexhull', 'qgis:fixeddistancebuffer',
'qgis:variabledistancebuffer', 'qgis:intersection',
'qgis:union', 'qgis:symmetricaldifference', 'qgis:clip',
'qgis:difference', 'qgis:dissolve',
'qgis:eliminatesliverpolygons']
for alg in algs:
Processing._removeVectorAlgorithm(alg, Processing.tr('&Geoprocessing Tools'))

algs = ['qgis:checkvalidity', 'qgis:exportaddgeometrycolumns',
'qgis:polygoncentroids', 'qgis:delaunaytriangulation',
'qgis:voronoipolygons', 'qgis:simplifygeometries',
'qgis:densifygeometries', 'qgis:multiparttosingleparts',
'qgis:singlepartstomultipart', 'qgis:polygonstolines',
'qgis:linestopolygons', 'qgis:extractnodes']
for alg in algs:
Processing._removeVectorAlgorithm(alg, Processing.tr('G&eometry Tools'))

algs = ['qgis:definecurrentprojection',
'qgis:joinattributesbylocation', 'qgis:splitvectorlayer',
'qgis:mergevectorlayers','qgis:createspatialindex']
for alg in algs:
Processing._removeVectorAlgorithm(alg, Processing.tr('&Data Management Tools'))

@staticmethod
def removeRasterMenu():
algs = ['gdalogr:warpreproject', 'gdalogr:assignprojection', 'gdalogr:extractprojection']
for alg in algs:
Processing._removeRasterAlgorithm(alg, Processing.tr('Projections'))

algs = ['gdalogr:rasterize', 'gdalogr:rasterize_over',
'gdalogr:polygonize', 'gdalogr:translate', 'gdalogr:rgbtopct',
'gdalogr:pcttorgb']
for alg in algs:
Processing._removeRasterAlgorithm(alg, Processing.tr('Conversion'))

algs = ['gdalogr:contour', 'gdalogr:cliprasterbyextent',
'gdalogr:cliprasterbymasklayer']
for alg in algs:
Processing._removeRasterAlgorithm(alg, Processing.tr('Extraction'))

algs = ['gdalogr:sieve', 'gdalogr:nearblack', 'gdalogr:fillnodata',
'gdalogr:proximity', 'gdalogr:griddatametrics',
'gdalogr:gridaverage', 'gdalogr:gridinvdist',
'gdalogr:gridnearestneighbor', 'gdalogr:aspect',
'gdalogr:hillshade', 'gdalogr:roughness', 'gdalogr:slope',
'gdalogr:tpi', 'gdalogr:tri']
for alg in algs:
Processing._removeRasterAlgorithm(alg, Processing.tr('Analysis'))

algs = ['gdalogr:buildvirtualraster', 'gdalogr:merge',
'gdalogr:rasterinfo', 'gdalogr:overviews', 'gdalogr:tileindex']
for alg in algs:
Processing._removeRasterAlgorithm(alg, Processing.tr('Miscellaneous'))

@staticmethod
def _addRasterAlgorithm(name, group):
Processing.addAlgorithmEntry(name, Processing.tr('&Raster'), group)

@staticmethod
def _addVectorAlgorithm( name, group):
Processing.addAlgorithmEntry(name, Processing.tr('Vect&or'), group)

@staticmethod
def _removeRasterAlgorithm(name, group):
Processing.removeAlgorithmEntry(name, Processing.tr('&Raster'), group)

@staticmethod
def _removeVectorAlgorithm( name, group):
Processing.removeAlgorithmEntry(name, Processing.tr('Vect&or'), group)

@staticmethod
def updateMenus():
showVector = ProcessingConfig.getSetting(
ProcessingConfig.SHOW_VECTOR_MENUS)
if showVector:
Processing.createVectorMenu()
else:
Processing.removeVectorMenu()

showRaster = ProcessingConfig.getSetting(
ProcessingConfig.SHOW_RASTER_MENUS)
if showRaster:
Processing.createRasterMenu()
else:
Processing.removeRasterMenu()

@staticmethod
def addAlgorithmEntry(algname, menuName, submenuName, actionText=None, icon=None, addButton=False):
alg = Processing.getAlgorithm(algname)
if alg is None:
return

action = QAction(icon or alg.getIcon(), actionText or alg.name, iface.mainWindow())
action.triggered.connect(lambda: Processing._executeAlgorithm(alg))

if menuName:
menu = Processing.getMenu(menuName, iface.mainWindow().menuBar())
submenu = Processing.getMenu(submenuName, menu)
submenu.addAction(action)

if addButton:
global algorithmsToolbar
if algorithmsToolbar is None:
algorithmsToolbar = iface.addToolBar('ProcessingAlgorithms')
algorithmsToolbar.addAction(action)

@staticmethod
def removeAlgorithmEntry(algname, menuName, submenuName, actionText=None, delButton=True):
alg = Processing.getAlgorithm(algname)
if alg is None:
return

if menuName:
menu = Processing.getMenu(menuName, iface.mainWindow().menuBar())
subMenu = Processing.getMenu(submenuName, menu)
action = Processing.findAction(subMenu.actions(), alg, actionText)
if action is not None:
subMenu.removeAction(action)

if len(subMenu.actions()) == 0:
menu.removeAction(subMenu.menuAction())

if delButton:
global algorithmsToolbar
if algorithmsToolbar is not None:
action = Processing.findAction(algorithmsToolbar.actions(), alg, actionText)
if action is not None:
algorithmsToolbar.removeAction(action)

@staticmethod
def _executeAlgorithm(alg):
message = alg.checkBeforeOpeningParametersDialog()
if message:
dlg = MessageDialog()
dlg.setTitle(tr('Missing dependency'))
dlg.setMessage(
tr('<h3>Missing dependency. This algorithm cannot '
'be run :-( </h3>\n%s') % message)
dlg.exec_()
return
alg = alg.getCopy()
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = AlgorithmDialog(alg)
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)

@staticmethod
def getMenu(name, parent):
menus = [c for c in parent.children() if isinstance(c, QMenu)]
menusDict = {m.title(): m for m in menus}
if name in menusDict:
return menusDict[name]
else:
menu = QMenu(name, parent)
parent.addMenu(menu)
return menu

@staticmethod
def findAction(actions, alg, actionText=None):
for action in actions:
if action.text() in [actionText, alg.name]:
return action
return None


@staticmethod
def tr(string, context=''):
@@ -41,8 +41,6 @@ class ProcessingConfig:
VECTOR_LINE_STYLE = 'VECTOR_LINE_STYLE'
VECTOR_POLYGON_STYLE = 'VECTOR_POLYGON_STYLE'
SHOW_RECENT_ALGORITHMS = 'SHOW_RECENT_ALGORITHMS'
SHOW_VECTOR_MENUS = 'SHOW_VECTOR_MENUS'
SHOW_RASTER_MENUS = 'SHOW_RASTER_MENUS'
USE_SELECTED = 'USE_SELECTED'
USE_FILENAME_AS_LAYER_NAME = 'USE_FILENAME_AS_LAYER_NAME'
KEEP_DIALOG_OPEN = 'KEEP_DIALOG_OPEN'
@@ -83,14 +81,6 @@ def initialize():
ProcessingConfig.tr('General'),
ProcessingConfig.SHOW_RECENT_ALGORITHMS,
ProcessingConfig.tr('Show recently executed algorithms'), True))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.SHOW_VECTOR_MENUS,
ProcessingConfig.tr('Add entries to the Vector menu'), True))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.SHOW_RASTER_MENUS,
ProcessingConfig.tr('Add entries to the Raster menu'), True))
ProcessingConfig.addSetting(Setting(
ProcessingConfig.tr('General'),
ProcessingConfig.SHOW_PROVIDERS_TOOLTIP,

0 comments on commit af8a62d

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