From bb7b6d41bc30b664fa05493131c1072f3dda2ae9 Mon Sep 17 00:00:00 2001 From: volaya Date: Wed, 7 Dec 2016 13:11:57 +0100 Subject: [PATCH] [processing] added predefined expressions to raster calculator --- .../algs/qgis/ui/ExpressionWidget.ui | 33 +++++++++-- .../algs/qgis/ui/RasterCalculatorWidgets.py | 59 ++++++++++++++++++- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/python/plugins/processing/algs/qgis/ui/ExpressionWidget.ui b/python/plugins/processing/algs/qgis/ui/ExpressionWidget.ui index e1b10659a5d..3c124ab85a7 100644 --- a/python/plugins/processing/algs/qgis/ui/ExpressionWidget.ui +++ b/python/plugins/processing/algs/qgis/ui/ExpressionWidget.ui @@ -7,7 +7,7 @@ 0 0 644 - 296 + 493 @@ -343,9 +343,6 @@ Expression - - 0 - @@ -362,6 +359,34 @@ + + + + Predefined expressions + + + + + + + + + + 0 + 0 + + + + Add... + + + + + buttonAddPredefined + buttonAddPredefined + comboPredefined + + diff --git a/python/plugins/processing/algs/qgis/ui/RasterCalculatorWidgets.py b/python/plugins/processing/algs/qgis/ui/RasterCalculatorWidgets.py index 97ea10376c9..195b13257dd 100644 --- a/python/plugins/processing/algs/qgis/ui/RasterCalculatorWidgets.py +++ b/python/plugins/processing/algs/qgis/ui/RasterCalculatorWidgets.py @@ -2,21 +2,62 @@ from processing.gui.wrappers import WidgetWrapper, DIALOG_STANDARD, DIALOG_BATCH from processing.tools import dataobjects from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel from qgis.PyQt.QtWidgets import QListWidget, QLineEdit, QPushButton -from qgis.PyQt.QtGui import QTextCursor +from qgis.PyQt.QtGui import QTextCursor, QLabel, QComboBox, QSizePolicy, QSpacerItem from processing.core.outputs import OutputRaster from processing.core.parameters import ParameterRaster from processing.gui.wrappers import InvalidParameterValue import os from qgis.PyQt import uic from functools import partial +import re pluginPath = os.path.dirname(__file__) +WIDGET_DLG, BASE_DLG = uic.loadUiType( + os.path.join(pluginPath, 'PredefinedExpressionDialog.ui')) + +class PredefinedExpressionDialog(BASE_DLG, WIDGET_DLG): + + def __init__(self, expression, options): + super(PredefinedExpressionDialog, self).__init__() + self.setupUi(self) + + self.filledExpression = None + self.options = options + self.expression = expression + self.variables = set(re.findall('\[.*?\]', expression)) + self.comboBoxes = {} + for variable in self.variables: + label = QLabel(variable[1:-1]) + combo = QComboBox() + for opt in self.options.keys(): + combo.addItem(opt) + self.comboBoxes[variable] = combo + self.groupBox.layout().addWidget(label) + self.groupBox.layout().addWidget(combo) + + verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) + self.groupBox.layout().addItem(verticalSpacer) + + self.buttonBox.rejected.connect(self.cancelPressed) + self.buttonBox.accepted.connect(self.okPressed) + + def cancelPressed(self): + self.close() + + def okPressed(self): + self.filledExpression = self.expression + for name, combo in self.comboBoxes.items(): + self.filledExpression = self.filledExpression.replace(name, + self.options[combo.currentText()]) + self.close() + WIDGET, BASE = uic.loadUiType( os.path.join(pluginPath, 'ExpressionWidget.ui')) - class ExpressionWidget(BASE, WIDGET): + expressions = {"NDVI": "([NIR] - [Red]) % ([NIR] + [Red])"} + def __init__(self, options): super(ExpressionWidget, self).__init__(None) self.setupUi(self) @@ -36,6 +77,20 @@ class ExpressionWidget(BASE, WIDGET): for button in buttons: button.clicked.connect(partial(addButtonText, button.text())) self.listWidget.itemDoubleClicked.connect(doubleClicked) + + self.fillPredefined() + self.buttonAddPredefined.clicked.connect(self.addPredefined) + + def addPredefined(self): + expression = self.expressions[self.comboPredefined.currentText()] + dlg = PredefinedExpressionDialog(expression, self.options) + dlg.exec_() + if dlg.filledExpression: + self.text.setPlainText(dlg.filledExpression) + + def fillPredefined(self): + for expression in self.expressions: + self.comboPredefined.addItem(expression) def setList(self, options): self.options = options