Skip to content

Commit

Permalink
[processing] allow selection parameters with option populated from da…
Browse files Browse the repository at this point in the history
…ta source
  • Loading branch information
volaya committed May 21, 2015
1 parent 8c8a123 commit a58085b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 14 additions & 2 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -17,6 +17,7 @@
* *
***************************************************************************
"""
from processing.tools.vector import resolveFieldIndex, features

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -536,10 +537,21 @@ def getFileFilter(self):

class ParameterSelection(Parameter):

def __init__(self, name='', description='', options=[], default=0):
def __init__(self, name='', description='', options=[], default=0, isSource = False):
Parameter.__init__(self, name, description)
self.options = options
if isinstance(self.options, basestring):
if isSource:
self.options = []
layer = QgsVectorLayer(options[0], "layer", "ogr")
if layer.isValid():
try:
index = resolveFieldIndex(layer, options[1])
feats = features(layer)
for feature in feats:
self.options.append(unicode(feature.attributes()[index]))
except ValueError:
pass
elif isinstance(self.options, basestring):
self.options = self.options.split(";")
self.value = None
self.default = int(default)
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/script/ScriptAlgorithm.py
Expand Up @@ -169,6 +169,9 @@ def processParameterLine(self, line):
param = ParameterMultipleInput(tokens[0], desc,
ParameterMultipleInput.TYPE_VECTOR_ANY)
param.optional = False
elif tokens[1].lower().strip().startswith('selectionfromfile'):
options = tokens[1].strip()[len('selectionfromfile '):].split(';')
param = ParameterSelection(tokens[0], desc, options, isSource=True)
elif tokens[1].lower().strip().startswith('selection'):
options = tokens[1].strip()[len('selection '):].split(';')
param = ParameterSelection(tokens[0], desc, options)
Expand Down

0 comments on commit a58085b

Please sign in to comment.