mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
[processing] some methods to add Processing algorithms in menus and buttons
This commit is contained in:
parent
935dca0860
commit
4d4ce9495c
61
python/plugins/processing/gui/utils.py
Normal file
61
python/plugins/processing/gui/utils.py
Normal file
@ -0,0 +1,61 @@
|
||||
from qgis.utils import iface
|
||||
from PyQt4 import QtGui
|
||||
from processing.core.Processing import Processing
|
||||
from processing.gui.MessageDialog import MessageDialog
|
||||
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
||||
|
||||
algorithmsToolbar = None
|
||||
|
||||
def addAlgorithmEntry(algname, menuName, submenuName, actionText = None, icon = None, addButton = False):
|
||||
alg = Processing.getAlgorithm(algname)
|
||||
if alg is None:
|
||||
return
|
||||
if menuName:
|
||||
menu = getMenu(menuName, iface.mainWindow().menuBar())
|
||||
submenu = getMenu(submenuName, menu)
|
||||
action = QtGui.QAction(icon or alg.getIcon(), actionText or alg.name, iface.mainWindow())
|
||||
action.triggered.connect(lambda: _executeAlgorithm(alg))
|
||||
submenu.addAction(action)
|
||||
if addButton:
|
||||
global algorithmsToolbar
|
||||
if algorithmsToolbar is None:
|
||||
algorithmsToolbar = iface.addToolBar("ProcessingAlgorithms")
|
||||
algorithmsToolbar.addAction(action)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def getMenu(name, parent):
|
||||
menus = [c for c in parent.children() if isinstance(c, QtGui.QMenu)]
|
||||
menusDict = {m.title():m for m in menus}
|
||||
if name in menusDict:
|
||||
return menusDict[name]
|
||||
else:
|
||||
menu = QtGui.QMenu(name, parent)
|
||||
parent.addMenu(menu)
|
||||
return menu
|
||||
|
Loading…
x
Reference in New Issue
Block a user