QGIS/python/plugins/processing/gui/NumberInputPanel.py

136 lines
6.2 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
NumberInputPanel.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
2016-09-27 19:51:06 +02:00
from builtins import str
2016-09-21 18:24:26 +02:00
from builtins import range
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
2015-05-18 21:04:20 +03:00
import os
2016-04-29 11:39:26 +02:00
from qgis.PyQt import uic
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import pyqtSignal
2016-05-13 13:17:48 +03:00
from qgis.PyQt.QtWidgets import QDialog
2015-05-18 21:04:20 +03:00
from qgis.core import (QgsDataSourceUri,
2016-05-13 09:32:13 +03:00
QgsCredentials,
QgsExpression,
QgsRasterLayer,
QgsExpressionContextScope)
from qgis.gui import QgsEncodingFileDialog, QgsExpressionBuilderDialog
from qgis.utils import iface
from processing.core.parameters import ParameterNumber, ParameterVector, ParameterRaster
from processing.core.outputs import OutputNumber, OutputVector, OutputRaster
from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput, CompoundValue
2012-09-15 18:25:25 +03:00
2015-05-18 21:04:20 +03:00
pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
2015-05-18 21:04:20 +03:00
2015-05-18 21:04:20 +03:00
class NumberInputPanel(BASE, WIDGET):
2012-09-15 18:25:25 +03:00
hasChanged = pyqtSignal()
def __init__(self, param, modelParametersDialog=None):
2015-05-18 21:04:20 +03:00
super(NumberInputPanel, self).__init__(None)
self.setupUi(self)
self.param = param
self.modelParametersDialog = modelParametersDialog
if param.default:
self.setValue(param.default)
self.btnSelect.clicked.connect(self.showExpressionsBuilder)
self.leText.textChanged.connect(lambda: self.hasChanged.emit())
def showExpressionsBuilder(self):
context = self.param.expressionContext()
if self.modelParametersDialog is not None:
context.popScope()
values = self.modelParametersDialog.getAvailableValuesOfType(ParameterNumber, OutputNumber)
variables = {}
for value in values:
if isinstance(value, ValueFromInput):
name = value.name
element = self.modelParametersDialog.model.inputs[name].param
desc = element.description
else:
name = "%s_%s" % (value.alg, value.output)
alg = self.modelParametersDialog.model.algs[value.alg]
out = alg.algorithm.getOutputFromName(value.output)
desc = "Output '%s' from algorithm '%s" % (out.description, alg.description)
variables[name] = desc
values = self.modelParametersDialog.getAvailableValuesOfType(ParameterVector, OutputVector)
values.extend(self.modelParametersDialog.getAvailableValuesOfType(ParameterRaster, OutputRaster))
for value in values:
if isinstance(value, ValueFromInput):
name = value.name
element = self.modelParametersDialog.model.inputs[name].param
desc = element.description
else:
name = "%s_%s" % (value.alg, value.output)
alg = self.modelParametersDialog.model.algs[value.alg]
element = alg.algorithm.getOutputFromName(value.output)
desc = "Output '%s' from algorithm '%s" % (element.description, alg.description)
variables['%s_minx' % name] = "Minimum X of %s" % desc
variables['%s_miny' % name] = "Maximum X of %s" % desc
variables['%s_maxx' % name] = "Minimum Y of %s" % desc
variables['%s_maxy' % name] = "Maximum Y of %s" % desc
if isinstance(element, (ParameterRaster, OutputRaster)):
variables['%s_min' % name] = "Minimum value of %s" % desc
variables['%s_max' % name] = "Maximum value of %s" % desc
variables['%s_avg' % name] = "Mean value of %s" % desc
variables['%s_stddev' % name] = "Standard deviation of %s" % desc
dlg = QgsExpressionBuilderDialog(None, self.leText.text(), self, 'generic', context)
2016-09-27 19:51:06 +02:00
for variable, desc in list(variables.items()):
dlg.expressionBuilder().registerItem("Modeler", variable, "@" + variable, desc, highlightedItem=True)
2016-05-13 09:32:13 +03:00
dlg.setWindowTitle(self.tr('Expression based input'))
if dlg.exec_() == QDialog.Accepted:
exp = QgsExpression(dlg.expressionText())
if not exp.hasParserError():
self.setValue(dlg.expressionText())
2012-09-15 18:25:25 +03:00
def getValue(self):
if self.modelParametersDialog:
value = self.leText.text()
values = []
for param in self.modelParametersDialog.model.parameters:
if isinstance(param, ParameterNumber):
if "@" + param.name in value:
values.append(ValueFromInput(param.name))
2016-09-27 19:51:06 +02:00
for alg in list(self.modelParametersDialog.model.algs.values()):
for out in alg.algorithm.outputs:
if isinstance(out, OutputNumber) and "@%s_%s" % (alg.name, out.name) in value:
values.append(ValueFromOutput(alg.name, out.name))
if values:
return CompoundValue(values, value)
else:
return value
else:
return self.leText.text()
def setValue(self, value):
2016-09-27 19:51:06 +02:00
self.leText.setText(str(value))