Skip to content

Commit

Permalink
[processing] allow saving custom predefined expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Dec 8, 2016
1 parent b133b12 commit e41c2a7
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 3 deletions.
51 changes: 51 additions & 0 deletions python/plugins/processing/algs/qgis/ui/AddNewExpressionDialog.ui
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>596</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>New expression</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="txtName"/>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Expression</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="txtExpression"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
15 changes: 14 additions & 1 deletion python/plugins/processing/algs/qgis/ui/ExpressionWidget.ui
Expand Up @@ -381,10 +381,23 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonSavePredefined">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Save...</string>
</property>
</widget>
</item>
</layout>
<zorder>buttonAddPredefined</zorder>
<zorder>buttonAddPredefined</zorder>
<zorder>comboPredefined</zorder>
<zorder>buttonSavePredefined</zorder>
</widget>
</item>
</layout>
Expand Down
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Predefined formula</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Variables</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2"/>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
57 changes: 55 additions & 2 deletions python/plugins/processing/algs/qgis/ui/RasterCalculatorWidgets.py
@@ -1,5 +1,6 @@
from processing.gui.wrappers import WidgetWrapper, DIALOG_STANDARD, DIALOG_BATCH
from processing.tools import dataobjects
from processing.tools.system import userFolder, mkdir
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from qgis.PyQt.QtWidgets import (QListWidget, QLineEdit, QPushButton, QLabel,
QComboBox, QSpacerItem, QSizePolicy)
Expand All @@ -11,8 +12,32 @@
from qgis.PyQt import uic
from functools import partial
import re
import json

pluginPath = os.path.dirname(__file__)
WIDGET_ADD_NEW, BASE_ADD_NEW = uic.loadUiType(
os.path.join(pluginPath, 'AddNewExpressionDialog.ui'))

class AddNewExpressionDialog(BASE_ADD_NEW, WIDGET_ADD_NEW):

def __init__(self, expression):
super(AddNewExpressionDialog, self).__init__()
self.setupUi(self)

self.name = None
self.expression = None
self.txtExpression.setPlainText(expression)
self.buttonBox.rejected.connect(self.cancelPressed)
self.buttonBox.accepted.connect(self.okPressed)

def cancelPressed(self):
self.close()

def okPressed(self):
self.name = self.txtName.text()
self.expression = self.txtExpression.toPlainText()
self.close()

WIDGET_DLG, BASE_DLG = uic.loadUiType(
os.path.join(pluginPath, 'PredefinedExpressionDialog.ui'))

Expand Down Expand Up @@ -57,7 +82,7 @@ def okPressed(self):

class ExpressionWidget(BASE, WIDGET):

expressions = {"NDVI": "([NIR] - [Red]) % ([NIR] + [Red])"}
_expressions = {"NDVI": "([NIR] - [Red]) % ([NIR] + [Red])"}

def __init__(self, options):
super(ExpressionWidget, self).__init__(None)
Expand All @@ -79,17 +104,45 @@ def addButtonText(text):
button.clicked.connect(partial(addButtonText, button.text()))
self.listWidget.itemDoubleClicked.connect(doubleClicked)

self.expressions = {}
if os.path.exists(self.expsFile()):
with open(self.expsFile()) as f:
self.expressions.update(json.load(f))
self.expressions.update(self._expressions)

self.fillPredefined()
self.buttonAddPredefined.clicked.connect(self.addPredefined)

self.buttonSavePredefined.clicked.connect(self.savePredefined)


def expsFile(self):
return os.path.join(userFolder(), 'rastercalcexpressions.json')

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 savePredefined(self):
exp = self.text.toPlainText()
used = [v for v in self.options.values() if v in exp]

for i, v in enumerate(used):
exp = exp.replace(v, chr(97 + i))

dlg = AddNewExpressionDialog(exp)
dlg.exec_()
if dlg.name:
self.expressions[dlg.name] = dlg.expression

with open(self.expsFile(), "w") as f:
f.write(json.dumps(self.expressions))

def fillPredefined(self):
self.comboPredefined.clear()
for expression in self.expressions:
self.comboPredefined.addItem(expression)

Expand Down

0 comments on commit e41c2a7

Please sign in to comment.