Skip to content
Permalink
Browse files

add value changed signal for custom interpolation parameter wrapper

move duplicated code into common file
  • Loading branch information
alexbruy committed Dec 29, 2018
1 parent 37f14f8 commit 357cf270ff6623df94ab36c12f7964fb61e72297
@@ -31,7 +31,6 @@

from qgis.core import (QgsRectangle,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterNumber,
QgsProcessingParameterExtent,
QgsProcessingParameterRasterDestination,
@@ -41,48 +40,11 @@
QgsGridFileWriter)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.algs.qgis.ui.InterpolationWidgets import ParameterInterpolationData

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class ParameterInterpolationData(QgsProcessingParameterDefinition):

def __init__(self, name='', description=''):
super().__init__(name, description)
self.setMetadata({
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
})

def type(self):
return 'idw_interpolation_data'

def clone(self):
return ParameterInterpolationData(self.name(), self.description())

@staticmethod
def parseValue(value):
if value is None:
return None

if value == '':
return None

if isinstance(value, str):
return value if value != '' else None
else:
return ParameterInterpolationData.dataToString(value)

@staticmethod
def dataToString(data):
s = ''
for c in data:
s += '{}::~::{}::~::{:d}::~::{:d};'.format(c[0],
c[1],
c[2],
c[3])
return s[:-1]


class IdwInterpolation(QgisAlgorithm):

INTERPOLATION_DATA = 'INTERPOLATION_DATA'
@@ -31,7 +31,6 @@

from qgis.core import (QgsProcessingUtils,
QgsProcessing,
QgsProcessingParameterDefinition,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingParameterExtent,
@@ -45,48 +44,11 @@
QgsGridFileWriter)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.algs.qgis.ui.InterpolationWidgets import ParameterInterpolationData

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class ParameterInterpolationData(QgsProcessingParameterDefinition):

def __init__(self, name='', description=''):
super().__init__(name, description)
self.setMetadata({
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
})

def type(self):
return 'tin_interpolation_data'

def clone(self):
return ParameterInterpolationData(self.name(), self.description())

@staticmethod
def parseValue(value):
if value is None:
return None

if value == '':
return None

if isinstance(value, str):
return value if value != '' else None
else:
return ParameterInterpolationData.dataToString(value)

@staticmethod
def dataToString(data):
s = ''
for c in data:
s += '{}::~:: {}::~:: {:d}::~:: {:d};'.format(c[0],
c[1],
c[2],
c[3])
return s[:-1]


class TinInterpolation(QgisAlgorithm):
INTERPOLATION_DATA = 'INTERPOLATION_DATA'
METHOD = 'METHOD'
@@ -36,7 +36,8 @@
QgsMapLayer,
QgsMapLayerProxyModel,
QgsWkbTypes,
QgsProcessingUtils
QgsProcessingUtils,
QgsProcessingParameterDefinition
)
from qgis.core import QgsFieldProxyModel
from qgis.analysis import QgsInterpolator
@@ -45,25 +46,69 @@
from processing.tools import dataobjects

pluginPath = os.path.dirname(__file__)


class ParameterInterpolationData(QgsProcessingParameterDefinition):

def __init__(self, name='', description=''):
super().__init__(name, description)
self.setMetadata({
'widget_wrapper': 'processing.algs.qgis.ui.InterpolationDataWidget.InterpolationDataWidgetWrapper'
})

def type(self):
return 'idw_interpolation_data'

def clone(self):
return ParameterInterpolationData(self.name(), self.description())

@staticmethod
def parseValue(value):
if value is None:
return None

if value == '':
return None

if isinstance(value, str):
return value if value != '' else None
else:
return ParameterInterpolationData.dataToString(value)

@staticmethod
def dataToString(data):
s = ''
for c in data:
s += '{}::~::{}::~::{:d}::~::{:d};'.format(c[0],
c[1],
c[2],
c[3])
return s[:-1]


WIDGET, BASE = uic.loadUiType(os.path.join(pluginPath, 'interpolationdatawidgetbase.ui'))


class InterpolationDataWidget(BASE, WIDGET):

hasChanged = pyqtSlot()

def __init__(self):
super(InterpolationDataWidget, self).__init__(None)
self.setupUi(self)

self.btnAdd.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg'))
self.btnRemove.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg'))

self.btnAdd.clicked.connect(self.addLayer)
self.btnRemove.clicked.connect(self.removeLayer)

self.cmbLayers.layerChanged.connect(self.layerChanged)
self.cmbLayers.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.cmbFields.setFilters(QgsFieldProxyModel.Numeric)
self.cmbFields.setLayer(self.cmbLayers.currentLayer())

@pyqtSlot()
def on_btnAdd_clicked(self):
def addLayer(self):
layer = self.cmbLayers.currentLayer()

attribute = ''
@@ -72,14 +117,15 @@ def on_btnAdd_clicked(self):
else:
attribute = self.cmbFields.currentField()

self.addLayerData(layer.name(), attribute)
self._addLayerData(layer.name(), attribute)
self.hasChanged.emit()

@pyqtSlot()
def on_btnRemove_clicked(self):
def removeLayer(self):
item = self.layersTree.currentItem()
if not item:
return
self.layersTree.invisibleRootItem().removeChild(item)
self.hasChanged.emit()

def layerChanged(self, layer):
self.chkUseZCoordinate.setEnabled(False)
@@ -97,7 +143,7 @@ def layerChanged(self, layer):

self.cmbFields.setLayer(layer)

def addLayerData(self, layerName, attribute):
def _addLayerData(self, layerName, attribute):
item = QTreeWidgetItem()
item.setText(0, layerName)
item.setText(1, attribute)
@@ -119,6 +165,7 @@ def setValue(self, value):

comboBox = self.layersTree.itemWidget(self.layersTree.topLevelItem(i), 2)
comboBox.setCurrentIndex(comboBox.findText(v[3]))
self.hasChanged.emit()

def value(self):
layers = ''
@@ -162,7 +209,9 @@ def value(self):
class InterpolationDataWidgetWrapper(WidgetWrapper):

def createWidget(self):
return InterpolationDataWidget()
widget = InterpolationDataWidget()
widget.hasChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
return widget

def setValue(self, value):
self.widget.setValue(value)

0 comments on commit 357cf27

Please sign in to comment.
You can’t perform that action at this time.