Skip to content

Commit 4d4ce94

Browse files
committedSep 24, 2015
[processing] some methods to add Processing algorithms in menus and buttons
1 parent 935dca0 commit 4d4ce94

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
 
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from qgis.utils import iface
2+
from PyQt4 import QtGui
3+
from processing.core.Processing import Processing
4+
from processing.gui.MessageDialog import MessageDialog
5+
from processing.gui.AlgorithmDialog import AlgorithmDialog
6+
7+
algorithmsToolbar = None
8+
9+
def addAlgorithmEntry(algname, menuName, submenuName, actionText = None, icon = None, addButton = False):
10+
alg = Processing.getAlgorithm(algname)
11+
if alg is None:
12+
return
13+
if menuName:
14+
menu = getMenu(menuName, iface.mainWindow().menuBar())
15+
submenu = getMenu(submenuName, menu)
16+
action = QtGui.QAction(icon or alg.getIcon(), actionText or alg.name, iface.mainWindow())
17+
action.triggered.connect(lambda: _executeAlgorithm(alg))
18+
submenu.addAction(action)
19+
if addButton:
20+
global algorithmsToolbar
21+
if algorithmsToolbar is None:
22+
algorithmsToolbar = iface.addToolBar("ProcessingAlgorithms")
23+
algorithmsToolbar.addAction(action)
24+
25+
26+
def _executeAlgorithm(alg):
27+
message = alg.checkBeforeOpeningParametersDialog()
28+
if message:
29+
dlg = MessageDialog()
30+
dlg.setTitle(tr('Missing dependency'))
31+
dlg.setMessage(
32+
tr('<h3>Missing dependency. This algorithm cannot '
33+
'be run :-( </h3>\n%s') % message)
34+
dlg.exec_()
35+
return
36+
alg = alg.getCopy()
37+
dlg = alg.getCustomParametersDialog()
38+
if not dlg:
39+
dlg = AlgorithmDialog(alg)
40+
canvas = iface.mapCanvas()
41+
prevMapTool = canvas.mapTool()
42+
dlg.show()
43+
dlg.exec_()
44+
if canvas.mapTool() != prevMapTool:
45+
try:
46+
canvas.mapTool().reset()
47+
except:
48+
pass
49+
canvas.setMapTool(prevMapTool)
50+
51+
52+
def getMenu(name, parent):
53+
menus = [c for c in parent.children() if isinstance(c, QtGui.QMenu)]
54+
menusDict = {m.title():m for m in menus}
55+
if name in menusDict:
56+
return menusDict[name]
57+
else:
58+
menu = QtGui.QMenu(name, parent)
59+
parent.addMenu(menu)
60+
return menu
61+

0 commit comments

Comments
 (0)
Please sign in to comment.