mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Remove qgsfunction from qgis.utils.
Import from qgis.core so we don't break API
This commit is contained in:
parent
e14b7e4868
commit
7942325fd7
@ -29,7 +29,7 @@ QGIS utilities module
|
||||
"""
|
||||
|
||||
from PyQt4.QtCore import QCoreApplication, QLocale
|
||||
from qgis.core import QGis, QgsExpression, QgsMessageLog
|
||||
from qgis.core import QGis, QgsExpression, QgsMessageLog, qgsfunction
|
||||
from string import Template
|
||||
import sys
|
||||
import traceback
|
||||
@ -412,62 +412,6 @@ def closeProjectMacro():
|
||||
mod.closeProject()
|
||||
|
||||
|
||||
def qgsfunction(args, group, **kwargs):
|
||||
"""
|
||||
Decorator function used to define a user expression function.
|
||||
|
||||
Custom functions should take (values, feature, parent) as args,
|
||||
they can also shortcut naming feature and parent args by using *args
|
||||
if they are not needed in the function.
|
||||
|
||||
Functions should return a value compatible with QVariant
|
||||
|
||||
Eval errors can be raised using parent.setEvalErrorString()
|
||||
|
||||
Functions must be unregistered when no longer needed using
|
||||
QgsExpression.unregisterFunction
|
||||
|
||||
Example:
|
||||
@qgsfunction(2, 'test'):
|
||||
def add(values, feature, parent):
|
||||
pass
|
||||
|
||||
Will create and register a function in QgsExpression called 'add' in the
|
||||
'test' group that takes two arguments.
|
||||
|
||||
or not using feature and parent:
|
||||
|
||||
@qgsfunction(2, 'test'):
|
||||
def add(values, *args):
|
||||
pass
|
||||
"""
|
||||
helptemplate = Template("""<h3>$name function</h3><br>$doc""")
|
||||
|
||||
class QgsExpressionFunction(QgsExpression.Function):
|
||||
def __init__(self, name, args, group, helptext='', usesgeometry=False):
|
||||
QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry)
|
||||
|
||||
def func(self, values, feature, parent):
|
||||
pass
|
||||
|
||||
def wrapper(func):
|
||||
name = kwargs.get('name', func.__name__)
|
||||
usesgeometry = kwargs.get('usesgeometry', False)
|
||||
help = func.__doc__ or ''
|
||||
help = help.strip()
|
||||
if args == 0 and not name[0] == '$':
|
||||
name = '${0}'.format(name)
|
||||
func.__name__ = name
|
||||
help = helptemplate.safe_substitute(name=name, doc=help)
|
||||
f = QgsExpressionFunction(name, args, group, help, usesgeometry)
|
||||
f.func = func
|
||||
register = kwargs.get('register', True)
|
||||
if register:
|
||||
QgsExpression.registerFunction(f)
|
||||
return f
|
||||
|
||||
return wrapper
|
||||
|
||||
#######################
|
||||
# SERVER PLUGINS
|
||||
#
|
||||
|
Loading…
x
Reference in New Issue
Block a user