Skip to content

Commit

Permalink
Simplify TableFieldWidgetWrapper
Browse files Browse the repository at this point in the history
Conflicts:
	python/plugins/processing/gui/ParametersPanel.py
	python/plugins/processing/gui/wrappers.py
  • Loading branch information
arnaud-morvan authored and volaya committed Oct 5, 2016
1 parent c1bc364 commit 9e36582
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 62 deletions.
8 changes: 1 addition & 7 deletions python/plugins/processing/gui/ParametersPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,4 @@ def buttonToggled(self, value):
button.setChecked(False)

def getWidgetWrapperFromParameter(self, param):
wrapper = param.wrapper(self.parent)
wrapper.widgetValueHasChanged.connect(self.widgetValueHasChanged)
return wrapper

def widgetValueHasChanged(self, wrapper):
for wrapper in self.wrappers.values():
wrapper.anotherParameterWidgetHasChanged(wrapper)
return param.wrapper(self.parent)
102 changes: 47 additions & 55 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import locale
import os

from qgis.core import QgsCoordinateReferenceSystem
from qgis.core import QgsCoordinateReferenceSystem, QgsVectorLayer
from qgis.PyQt.QtWidgets import QCheckBox, QComboBox, QLineEdit, QPlainTextEdit
from qgis.PyQt.QtCore import pyqtSignal, QObject, QVariant

Expand All @@ -42,7 +42,7 @@
from processing.gui.PointSelectionPanel import PointSelectionPanel
from processing.core.parameters import (ParameterBoolean, ParameterPoint, ParameterFile,
ParameterRaster, ParameterVector, ParameterNumber, ParameterString, ParameterTable,
ParameterTableField, ParameterExtent, ParameterFixedTable, ParameterCrs)
ParameterTableField, ParameterExtent, ParameterFixedTable, ParameterCrs, _resolveLayers)
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.FileSelectionPanel import FileSelectionPanel
from processing.core.outputs import (OutputFile, OutputRaster, OutputVector, OutputNumber,
Expand Down Expand Up @@ -97,8 +97,7 @@ def comboValue(self, validator=None):
if validator is not None and not validator(v):
raise InvalidParameterValue()
return v
else:
return self.widget.itemData(self.widget.currentIndex())
return self.widget.itemData(self.widget.currentIndex())

def createWidget(self):
pass
Expand All @@ -125,9 +124,6 @@ def setComboValue(self, value):
def value(self):
pass

def anotherParameterWidgetHasChanged(self, wrapper):
pass

def postInitialize(self, wrappers):
pass

Expand Down Expand Up @@ -403,7 +399,9 @@ def createWidget(self):
opts = [getExtendedLayerName(opt) for opt in options]
return MultipleInputPanel(opts)
elif self.dialogType == DIALOG_BATCH:
return BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
widget = BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
widget.textChanged
return widget
else:
options = [self.dialog.resolveValueDescription(opt) for opt in self._getOptions()]
return MultipleInputPanel(options)
Expand Down Expand Up @@ -557,9 +555,12 @@ def createWidget(self):
if self.dialogType == DIALOG_STANDARD:
widget = QComboBox()
self._populate(widget)
widget.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
return widget
elif self.dialogType == DIALOG_BATCH:
return BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
widget = BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
widget.valueChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
return widget
else:
widget = QComboBox()
layers = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
Expand All @@ -578,7 +579,6 @@ def _populate(self, widget):
widget.addItem(self.NOT_SELECTED, None)
for layer in layers:
widget.addItem(getExtendedLayerName(layer), layer)
widget.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
widget.name = self.param.name

def refresh(self):
Expand All @@ -588,19 +588,18 @@ def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
pass # TODO
elif self.dialogType == DIALOG_BATCH:
self.widget.setText(value)
self.widget.setValue(value)
else:
self.setComboValue(value)


def value(self):
if self.dialogType == DIALOG_STANDARD:
try:
return self.widget.itemData(self.widget.currentIndex())
except:
return self.widget.getValue()
elif self.dialogType == DIALOG_BATCH:
return self.widget.getText()
return self.widget.value()
else:
def validator(v):
if not bool(v):
Expand Down Expand Up @@ -736,19 +735,18 @@ class TableFieldWidgetWrapper(WidgetWrapper):
NOT_SET = '[Not set]'

def createWidget(self):
self._layer = None

if self.param.multiple:
if self.dialogType == DIALOG_STANDARD:
return MultipleInputPanel(options=[])
return MultipleInputPanel(options=[])
else:
return QLineEdit()
else:
if self.dialogType == DIALOG_STANDARD:
if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH):
widget = QComboBox()
return widget
elif self.dialogType == DIALOG_BATCH:
widget = QLineEdit()
if self.param.default:
widget.setText(self.param.default)
if self.dialogType == DIALOG_BATCH:
widget.setEditable(True) # Should be removed at the end
return widget
else:
widget = QComboBox()
Expand All @@ -763,28 +761,42 @@ def createWidget(self):
def postInitialize(self, wrappers):
for wrapper in wrappers:
if wrapper.param.name == self.param.parent:
layer = wrapper.widget.itemData(wrapper.widget.currentIndex())
if layer is not None:
fields = self.getFields(layer, wrapper.param.datatype)
if self.param.multiple:
self.widget.updateForOptions(fields)
else:
self.widget.clear()
if self.param.optional:
self.widget.addItem(self.tr(self.NOT_SET))
self.widget.addItems(fields)
# self.refreshItems()
if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH):
self.setLayer(wrapper.value())
wrapper.widgetValueHasChanged.connect(self.parentValueChanged)
break

def getFields(self, layer, datatype):
def parentValueChanged(self, wrapper):
self.setLayer(wrapper.value())

def setLayer(self, layer):
if isinstance(layer, basestring):
layer = dataobjects.getObjectFromUri(_resolveLayers(layer))
self._layer = layer
self.refreshItems()

def refreshItems(self):
if self.param.multiple:
self.widget.updateForOptions(self.getFields())
else:
self.widget.clear()
if self.param.optional:
self.widget.addItem(self.tr(self.NOT_SET))
self.widget.addItems(self.getFields())

def getFields(self):
if self._layer is None:
return []
fieldTypes = []
if datatype == ParameterTableField.DATA_TYPE_STRING:
if self.param.datatype == ParameterTableField.DATA_TYPE_STRING:
fieldTypes = [QVariant.String]
elif datatype == ParameterTableField.DATA_TYPE_NUMBER:
elif self.param.datatype == ParameterTableField.DATA_TYPE_NUMBER:
fieldTypes = [QVariant.Int, QVariant.Double, QVariant.LongLong,
QVariant.UInt, QVariant.ULongLong]

fieldNames = set()
for field in layer.fields():
for field in self._layer.fields():
if not fieldTypes or field.type() in fieldTypes:
fieldNames.add(unicode(field.name()))
return sorted(list(fieldNames), cmp=locale.strcoll)
Expand All @@ -802,13 +814,8 @@ def setValue(self, value):
self.widget.setText(value)
else:
if self.dialogType == DIALOG_STANDARD:
pass # TODO
elif self.dialogType == DIALOG_BATCH:
return self.widget.setText(value)
else:
self.setComboValue(value)


def value(self):
if self.param.multiple:
if self.dialogType == DIALOG_STANDARD:
Expand All @@ -821,30 +828,15 @@ def value(self):
raise InvalidParameterValue()
return text
else:
if self.dialogType == DIALOG_STANDARD:
if self.dialogType in (DIALOG_STANDARD, DIALOG_BATCH):
if self.param.optional and self.widget.currentIndex() == 0:
return None
return self.widget.currentText()
elif self.dialogType == DIALOG_BATCH:
return self.widget.text()
else:
def validator(v):
return bool(v) or self.param.optional
return self.comboValue(validator)

def anotherParameterWidgetHasChanged(self, wrapper):
if wrapper.param.name == self.param.parent:
layer = wrapper.value()
if layer is not None:
fields = self.getFields(layer, wrapper.param.datatype)
if self.param.multiple:
self.widget.updateForOptions(fields)
else:
self.widget.clear()
if self.param.optional:
self.widget.addItem(self.tr(self.NOT_SET))
self.widget.addItems(fields)


def GeometryPredicateWidgetWrapper(WidgetWrapper):

Expand Down

0 comments on commit 9e36582

Please sign in to comment.