|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + AlgorithmLocatorFilter.py |
| 6 | + ------------------------- |
| 7 | + Date : May 2017 |
| 8 | + Copyright : (C) 2017 by Nyall Dawson |
| 9 | + Email : nyall dot dawson at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Nyall Dawson' |
| 21 | +__date__ = 'May 2017' |
| 22 | +__copyright__ = '(C) 2017, Nyall Dawson' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | + |
| 29 | +from qgis.core import (QgsApplication, |
| 30 | + QgsProcessingAlgorithm) |
| 31 | +from qgis.gui import (QgsLocatorFilter, |
| 32 | + QgsLocatorResult) |
| 33 | +from processing.gui.MessageDialog import MessageDialog |
| 34 | +from processing.gui.AlgorithmDialog import AlgorithmDialog |
| 35 | +from qgis.utils import iface |
| 36 | + |
| 37 | +class AlgorithmLocatorFilter(QgsLocatorFilter): |
| 38 | + |
| 39 | + def __init__(self, parent=None): |
| 40 | + super(AlgorithmLocatorFilter, self).__init__(parent) |
| 41 | + |
| 42 | + def fetchResults(self,string,context,feedback): |
| 43 | + for a in QgsApplication.processingRegistry().algorithms(): |
| 44 | + if feedback.isCanceled(): |
| 45 | + return |
| 46 | + if a.flags() & QgsProcessingAlgorithm.FlagHideFromToolbox: |
| 47 | + continue |
| 48 | + |
| 49 | + if string.lower() in a.displayName().lower() or [t for t in a.tags() if string.lower() in t.lower()]: |
| 50 | + result = QgsLocatorResult() |
| 51 | + result.filter = self |
| 52 | + result.displayString = a.displayName() |
| 53 | + result.icon = a.icon() |
| 54 | + result.userData = a.id() |
| 55 | + self.resultFetched.emit(result) |
| 56 | + |
| 57 | + def triggerResult(self, result): |
| 58 | + a = QgsApplication.processingRegistry().algorithmById(result.userData) |
| 59 | + if a: |
| 60 | + alg = a.getCopy() |
| 61 | + message = alg.checkBeforeOpeningParametersDialog() |
| 62 | + if message: |
| 63 | + dlg = MessageDialog() |
| 64 | + dlg.setTitle(self.tr('Missing dependency')) |
| 65 | + dlg.setMessage(message) |
| 66 | + dlg.exec_() |
| 67 | + return |
| 68 | + dlg = alg.getCustomParametersDialog() |
| 69 | + if not dlg: |
| 70 | + dlg = AlgorithmDialog(alg) |
| 71 | + canvas = iface.mapCanvas() |
| 72 | + prevMapTool = canvas.mapTool() |
| 73 | + dlg.show() |
| 74 | + dlg.exec_() |
| 75 | + if canvas.mapTool() != prevMapTool: |
| 76 | + try: |
| 77 | + canvas.mapTool().reset() |
| 78 | + except: |
| 79 | + pass |
| 80 | + canvas.setMapTool(prevMapTool) |
0 commit comments