Skip to content

Commit

Permalink
[processing] Allow restricting fields to date/time types
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 30, 2016
1 parent b0662f9 commit 08505b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -1339,6 +1339,7 @@ class ParameterTableField(Parameter):

DATA_TYPE_NUMBER = 0
DATA_TYPE_STRING = 1
DATA_TYPE_DATETIME = 2
DATA_TYPE_ANY = -1

def __init__(self, name='', description='', parent=None, datatype=-1,
Expand Down Expand Up @@ -1376,6 +1377,8 @@ def dataType(self):
return 'numeric'
elif self.datatype == self.DATA_TYPE_STRING:
return 'string'
elif self.datatype == self.DATA_TYPE_DATETIME:
return 'datetime'
else:
return 'any'

Expand All @@ -1397,6 +1400,9 @@ def fromScriptCode(self, line):
elif definition.lower().strip().startswith('field string'):
parent = definition.strip()[len('field string') + 1:]
datatype = ParameterTableField.DATA_TYPE_STRING
elif definition.lower().strip().startswith('field datetime'):
parent = definition.strip()[len('field datetime') + 1:]
datatype = ParameterTableField.DATA_TYPE_DATETIME
else:
parent = definition.strip()[len('field') + 1:]
datatype = ParameterTableField.DATA_TYPE_ANY
Expand Down
5 changes: 4 additions & 1 deletion python/plugins/processing/gui/wrappers.py
Expand Up @@ -1022,7 +1022,8 @@ def createWidget(self):
widget.setFilters(QgsFieldProxyModel.Numeric)
elif self.param.datatype == ParameterTableField.DATA_TYPE_STRING:
widget.setFilters(QgsFieldProxyModel.String)

elif self.param.datatype == ParameterTableField.DATA_TYPE_DATETIME:
widget.setFilters(QgsFieldProxyModel.Date | QgsFieldProxyModel.Time)
return widget
else:
widget = QComboBox()
Expand Down Expand Up @@ -1067,6 +1068,8 @@ def getFields(self):
elif self.param.datatype == ParameterTableField.DATA_TYPE_NUMBER:
fieldTypes = [QVariant.Int, QVariant.Double, QVariant.LongLong,
QVariant.UInt, QVariant.ULongLong]
elif self.param.datatype == ParameterTableField.DATA_TYPE_DATETIME:
fieldTypes = [QVariant.Date, QVariant.Time, QVariant.DateTime]

fieldNames = set()
for field in self._layer.fields():
Expand Down
Expand Up @@ -140,6 +140,7 @@ def setupUi(self):
self.datatypeCombo.addItem(self.tr('Any'), -1)
self.datatypeCombo.addItem(self.tr('Number'), 0)
self.datatypeCombo.addItem(self.tr('String'), 1)
self.datatypeCombo.addItem(self.tr('Date/time'), 2)
self.verticalLayout.addWidget(self.datatypeCombo)

if self.param is not None and self.param.datatype is not None:
Expand Down

0 comments on commit 08505b3

Please sign in to comment.