Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] added predefined expressions to raster calculator
  • Loading branch information
volaya committed Dec 7, 2016
1 parent 8a3c1ef commit bb7b6d4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
33 changes: 29 additions & 4 deletions python/plugins/processing/algs/qgis/ui/ExpressionWidget.ui
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>644</width>
<height>296</height>
<height>493</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -343,9 +343,6 @@
<string>Expression</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QPlainTextEdit" name="text">
<property name="sizePolicy">
Expand All @@ -362,6 +359,34 @@
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Predefined expressions</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QComboBox" name="comboPredefined"/>
</item>
<item>
<widget class="QPushButton" name="buttonAddPredefined">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Add...</string>
</property>
</widget>
</item>
</layout>
<zorder>buttonAddPredefined</zorder>
<zorder>buttonAddPredefined</zorder>
<zorder>comboPredefined</zorder>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
59 changes: 57 additions & 2 deletions python/plugins/processing/algs/qgis/ui/RasterCalculatorWidgets.py
Expand Up @@ -2,21 +2,62 @@
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)
Expand All @@ -36,6 +77,20 @@ def addButtonText(text):
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
Expand Down

1 comment on commit bb7b6d4

@nirvn
Copy link
Contributor

@nirvn nirvn commented on bb7b6d4 Dec 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volaya , seems there's a git add PredefinedExpressionDialog.ui missing in this commit :)

Please sign in to comment.