Skip to content

Commit 2fc5c01

Browse files
committedJan 6, 2014
[processing] port QGIS native field calculator
1 parent f1551df commit 2fc5c01

File tree

7 files changed

+737
-41
lines changed

7 files changed

+737
-41
lines changed
 

‎python/plugins/processing/algs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ FILE(GLOB PY_FILES *.py)
22

33
ADD_SUBDIRECTORY(mmqgisx)
44
ADD_SUBDIRECTORY(ftools)
5+
ADD_SUBDIRECTORY(ui)
56

67
PLUGIN_INSTALL(processing ./algs ${PY_FILES})

‎python/plugins/processing/algs/FieldsCalculator.py

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,33 @@
2727

2828
from PyQt4.QtCore import *
2929
from qgis.core import *
30+
from processing import interface
3031
from processing.core.GeoAlgorithm import GeoAlgorithm
32+
from processing.core.GeoAlgorithmExecutionException import \
33+
GeoAlgorithmExecutionException
3134
from processing.parameters.ParameterVector import ParameterVector
3235
from processing.parameters.ParameterString import ParameterString
3336
from processing.parameters.ParameterNumber import ParameterNumber
37+
from processing.parameters.ParameterBoolean import ParameterBoolean
3438
from processing.parameters.ParameterSelection import ParameterSelection
3539
from processing.outputs.OutputVector import OutputVector
3640
from processing.tools import dataobjects, vector
3741

42+
from processing.algs.ui.FieldsCalculatorDialog import FieldsCalculatorDialog
3843

3944
class FieldsCalculator(GeoAlgorithm):
4045

4146
INPUT_LAYER = 'INPUT_LAYER'
47+
NEW_FIELD = 'NEW_FIELD'
4248
FIELD_NAME = 'FIELD_NAME'
4349
FIELD_TYPE = 'FIELD_TYPE'
4450
FIELD_LENGTH = 'FIELD_LENGTH'
4551
FIELD_PRECISION = 'FIELD_PRECISION'
4652
FORMULA = 'FORMULA'
4753
OUTPUT_LAYER = 'OUTPUT_LAYER'
4854

49-
TYPE_NAMES = ['Float', 'Integer', 'String']
50-
TYPES = [QVariant.Double, QVariant.Int, QVariant.String]
55+
TYPE_NAMES = ['Float', 'Integer', 'String', 'Date']
56+
TYPES = [QVariant.Double, QVariant.Int, QVariant.String, QVariant.Date]
5157

5258
def defineCharacteristics(self):
5359
self.name = 'Field calculator'
@@ -61,58 +67,95 @@ def defineCharacteristics(self):
6167
self.addParameter(ParameterNumber(self.FIELD_LENGTH, 'Field length',
6268
1, 255, 10))
6369
self.addParameter(ParameterNumber(self.FIELD_PRECISION,
64-
'Field precision', 0, 10, 5))
70+
'Field precision', 0, 15, 3))
71+
self.addParameter(ParameterBoolean(self.NEW_FIELD,
72+
'Create new field', True))
6573
self.addParameter(ParameterString(self.FORMULA, 'Formula'))
6674
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
6775

6876
def processAlgorithm(self, progress):
77+
layer = dataobjects.getObjectFromUri(
78+
self.getParameterValue(self.INPUT_LAYER))
6979
fieldName = self.getParameterValue(self.FIELD_NAME)
70-
fieldType = self.getParameterValue(self.FIELD_TYPE)
71-
fieldLength = self.getParameterValue(self.FIELD_LENGTH)
72-
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
80+
fieldType = self.TYPES[self.getParameterValue(self.FIELD_TYPE)]
81+
width = self.getParameterValue(self.FIELD_LENGTH)
82+
precision = self.getParameterValue(self.FIELD_PRECISION)
83+
newField = self.getParameterValue(self.NEW_FIELD)
7384
formula = self.getParameterValue(self.FORMULA)
85+
7486
output = self.getOutputFromName(self.OUTPUT_LAYER)
7587

76-
layer = dataobjects.getObjectFromUri(
77-
self.getParameterValue(self.INPUT_LAYER))
7888
provider = layer.dataProvider()
79-
fields = provider.fields()
80-
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',
81-
fieldLength, fieldPrecision))
89+
fields = layer.pendingFields()
90+
if newField:
91+
fields.append(QgsField(fieldName, fieldType, '', width, precision))
92+
8293
writer = output.getVectorWriter(fields, provider.geometryType(),
83-
layer.crs())
94+
layer.crs())
95+
96+
exp = QgsExpression(formula)
97+
98+
da = QgsDistanceArea()
99+
da.setSourceCrs(layer.crs().srsid())
100+
canvas = interface.iface.mapCanvas()
101+
da.setEllipsoidalMode(canvas.mapRenderer().hasCrsTransformEnabled())
102+
da.setEllipsoid(QgsProject.instance().readEntry('Measure',
103+
'/Ellipsoid',
104+
GEO_NONE)[0])
105+
exp.setGeomCalculator(da)
106+
107+
if not exp.prepare(layer.pendingFields()):
108+
raise GeoAlgorithmExecutionException(
109+
'Evaluation error: ' + exp.evalErrorString())
84110

85-
outFeat = QgsFeature()
86-
inGeom = QgsGeometry()
87-
nFeat = provider.featureCount()
88-
nElement = 0
111+
outFeature = QgsFeature()
112+
outFeature.initAttributes(len(fields))
113+
outFeature.setFields(fields)
114+
115+
error = ''
116+
calculationSuccess = True
117+
118+
current = 0
89119
features = vector.features(layer)
120+
total = 100.0 / len(features)
121+
122+
rownum = 1
123+
for f in features:
124+
exp.setCurrentRowNumber(rownum)
125+
value = exp.evaluate(f)
126+
if exp.hasEvalError():
127+
calculationSuccess = False
128+
error = exp.evalErrorString()
129+
break
130+
else:
131+
outFeature.setGeometry(f.geometry())
132+
for fld in f.fields():
133+
outFeature[fld.name()] = f[fld.name()]
134+
outFeature[fieldName] = value
135+
writer.addFeature(outFeature)
90136

91-
fieldnames = [field.name() for field in provider.fields()]
92-
fieldnames.sort(key=len, reverse=False)
93-
fieldidx = [fieldnames.index(field.name()) for field in
94-
provider.fields()]
95-
print fieldidx
96-
for inFeat in features:
97-
progress.setPercentage(int(100 * nElement / nFeat))
98-
attrs = inFeat.attributes()
99-
expression = formula
100-
for idx in fieldidx:
101-
expression = expression.replace(unicode(fields[idx].name()),
102-
unicode(attrs[idx]))
103-
try:
104-
result = eval(expression)
105-
except Exception:
106-
result = None
107-
nElement += 1
108-
inGeom = inFeat.geometry()
109-
outFeat.setGeometry(inGeom)
110-
attrs = inFeat.attributes()
111-
attrs.append(result)
112-
outFeat.setAttributes(attrs)
113-
writer.addFeature(outFeat)
137+
current += 1
138+
progress.setPercentage(int(current * total))
114139
del writer
115140

141+
if not calculationSuccess:
142+
raise GeoAlgorithmExecutionException(
143+
'An error occured while evaluating the calculation '
144+
'string:\n' + error)
145+
146+
116147
def checkParameterValuesBeforeExecuting(self):
117-
# TODO check that formula is correct and fields exist
118-
pass
148+
newField = self.getParameterValue(self.NEW_FIELD)
149+
fieldName = self.getParameterValue(self.FIELD_NAME)
150+
if newField and len(fieldName) == 0:
151+
raise GeoAlgorithmExecutionException('Field name is not set. '
152+
'Please enter a field name')
153+
154+
outputName = self.getOutputValue(self.OUTPUT_LAYER)
155+
if outputName == '':
156+
raise GeoAlgorithmExecutionException('Output is not set. '
157+
'Please specify valid filename')
158+
159+
160+
def getCustomParametersDialog(self):
161+
return FieldsCalculatorDialog(self)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FILE(GLOB PY_FILES *.py)
2+
3+
FILE(GLOB UI_FILES *.ui)
4+
PYQT4_WRAP_UI(PYUI_FILES ${UI_FILES})
5+
6+
PLUGIN_INSTALL(processing ./algs/ui ${PY_FILES} ${PYUI_FILES})
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>FieldsCalculator</class>
4+
<widget class="QDialog" name="FieldsCalculator">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>681</width>
10+
<height>681</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Field calculator</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout">
17+
<item row="2" column="0">
18+
<widget class="QGroupBox" name="mNewFieldGroupBox">
19+
<property name="sizePolicy">
20+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
21+
<horstretch>0</horstretch>
22+
<verstretch>0</verstretch>
23+
</sizepolicy>
24+
</property>
25+
<property name="title">
26+
<string>Create a new field</string>
27+
</property>
28+
<property name="flat">
29+
<bool>true</bool>
30+
</property>
31+
<property name="checkable">
32+
<bool>true</bool>
33+
</property>
34+
<property name="checked">
35+
<bool>true</bool>
36+
</property>
37+
<layout class="QGridLayout">
38+
<property name="sizeConstraint">
39+
<enum>QLayout::SetMinimumSize</enum>
40+
</property>
41+
<property name="leftMargin">
42+
<number>3</number>
43+
</property>
44+
<property name="topMargin">
45+
<number>3</number>
46+
</property>
47+
<property name="rightMargin">
48+
<number>3</number>
49+
</property>
50+
<property name="bottomMargin">
51+
<number>0</number>
52+
</property>
53+
<property name="verticalSpacing">
54+
<number>3</number>
55+
</property>
56+
<item row="0" column="0">
57+
<widget class="QLabel" name="mFieldNameLabel">
58+
<property name="text">
59+
<string>Output field name</string>
60+
</property>
61+
<property name="buddy">
62+
<cstring>mOutputFieldNameLineEdit</cstring>
63+
</property>
64+
</widget>
65+
</item>
66+
<item row="0" column="1" colspan="3">
67+
<widget class="QLineEdit" name="mOutputFieldNameLineEdit"/>
68+
</item>
69+
<item row="1" column="0">
70+
<widget class="QLabel" name="mOutputFieldTypeLabel">
71+
<property name="text">
72+
<string>Output field type</string>
73+
</property>
74+
<property name="buddy">
75+
<cstring>mOutputFieldTypeComboBox</cstring>
76+
</property>
77+
</widget>
78+
</item>
79+
<item row="1" column="1" colspan="3">
80+
<widget class="QComboBox" name="mOutputFieldTypeComboBox"/>
81+
</item>
82+
<item row="2" column="0">
83+
<widget class="QLabel" name="mOutputFieldWidthLabel">
84+
<property name="text">
85+
<string>Output field width</string>
86+
</property>
87+
<property name="buddy">
88+
<cstring>mOutputFieldWidthSpinBox</cstring>
89+
</property>
90+
</widget>
91+
</item>
92+
<item row="2" column="1">
93+
<widget class="QSpinBox" name="mOutputFieldWidthSpinBox">
94+
<property name="toolTip">
95+
<string>Width of complete output. For example 123,456 means 6 as field width.</string>
96+
</property>
97+
<property name="minimum">
98+
<number>0</number>
99+
</property>
100+
<property name="value">
101+
<number>15</number>
102+
</property>
103+
</widget>
104+
</item>
105+
<item row="2" column="2">
106+
<widget class="QLabel" name="mOutputFieldPrecisionLabel">
107+
<property name="text">
108+
<string>Precision</string>
109+
</property>
110+
<property name="buddy">
111+
<cstring>mOutputFieldPrecisionSpinBox</cstring>
112+
</property>
113+
</widget>
114+
</item>
115+
<item row="2" column="3">
116+
<widget class="QSpinBox" name="mOutputFieldPrecisionSpinBox">
117+
<property name="value">
118+
<number>2</number>
119+
</property>
120+
</widget>
121+
</item>
122+
</layout>
123+
</widget>
124+
</item>
125+
<item row="5" column="0" colspan="2">
126+
<widget class="QDialogButtonBox" name="mButtonBox">
127+
<property name="sizePolicy">
128+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
129+
<horstretch>3</horstretch>
130+
<verstretch>0</verstretch>
131+
</sizepolicy>
132+
</property>
133+
<property name="orientation">
134+
<enum>Qt::Horizontal</enum>
135+
</property>
136+
<property name="standardButtons">
137+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
138+
</property>
139+
</widget>
140+
</item>
141+
<item row="0" column="0" colspan="2">
142+
<layout class="QHBoxLayout" name="horizontalLayout">
143+
<item>
144+
<widget class="QLabel" name="label">
145+
<property name="text">
146+
<string>Input layer</string>
147+
</property>
148+
</widget>
149+
</item>
150+
<item>
151+
<widget class="QComboBox" name="cmbInputLayer">
152+
<property name="sizePolicy">
153+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
154+
<horstretch>0</horstretch>
155+
<verstretch>0</verstretch>
156+
</sizepolicy>
157+
</property>
158+
</widget>
159+
</item>
160+
</layout>
161+
</item>
162+
<item row="2" column="1">
163+
<widget class="QGroupBox" name="mUpdateExistingGroupBox">
164+
<property name="title">
165+
<string>Update existing field</string>
166+
</property>
167+
<property name="flat">
168+
<bool>true</bool>
169+
</property>
170+
<property name="checkable">
171+
<bool>true</bool>
172+
</property>
173+
<property name="checked">
174+
<bool>false</bool>
175+
</property>
176+
<layout class="QVBoxLayout" name="verticalLayout">
177+
<item>
178+
<widget class="QComboBox" name="mExistingFieldComboBox"/>
179+
</item>
180+
</layout>
181+
</widget>
182+
</item>
183+
<item row="3" column="0" colspan="2">
184+
<widget class="QgsExpressionBuilderWidget" name="builder" native="true">
185+
<property name="autoFillBackground">
186+
<bool>false</bool>
187+
</property>
188+
</widget>
189+
</item>
190+
<item row="1" column="0" colspan="2">
191+
<layout class="QHBoxLayout" name="horizontalLayout_2">
192+
<item>
193+
<widget class="QLabel" name="label_2">
194+
<property name="text">
195+
<string>Output file</string>
196+
</property>
197+
</widget>
198+
</item>
199+
<item>
200+
<widget class="QLineEdit" name="leOutputFile"/>
201+
</item>
202+
<item>
203+
<widget class="QToolButton" name="btnBrowse">
204+
<property name="text">
205+
<string>...</string>
206+
</property>
207+
</widget>
208+
</item>
209+
</layout>
210+
</item>
211+
<item row="4" column="0" colspan="2">
212+
<widget class="QProgressBar" name="progressBar">
213+
<property name="value">
214+
<number>0</number>
215+
</property>
216+
</widget>
217+
</item>
218+
</layout>
219+
</widget>
220+
<customwidgets>
221+
<customwidget>
222+
<class>QgsExpressionBuilderWidget</class>
223+
<extends>QWidget</extends>
224+
<header>qgis.gui</header>
225+
<container>1</container>
226+
</customwidget>
227+
</customwidgets>
228+
<tabstops>
229+
<tabstop>mOutputFieldNameLineEdit</tabstop>
230+
<tabstop>mOutputFieldTypeComboBox</tabstop>
231+
<tabstop>mOutputFieldWidthSpinBox</tabstop>
232+
<tabstop>mOutputFieldPrecisionSpinBox</tabstop>
233+
<tabstop>mButtonBox</tabstop>
234+
</tabstops>
235+
<resources/>
236+
<connections>
237+
<connection>
238+
<sender>mButtonBox</sender>
239+
<signal>accepted()</signal>
240+
<receiver>FieldsCalculator</receiver>
241+
<slot>accept()</slot>
242+
<hints>
243+
<hint type="sourcelabel">
244+
<x>679</x>
245+
<y>559</y>
246+
</hint>
247+
<hint type="destinationlabel">
248+
<x>157</x>
249+
<y>274</y>
250+
</hint>
251+
</hints>
252+
</connection>
253+
<connection>
254+
<sender>mButtonBox</sender>
255+
<signal>rejected()</signal>
256+
<receiver>FieldsCalculator</receiver>
257+
<slot>reject()</slot>
258+
<hints>
259+
<hint type="sourcelabel">
260+
<x>679</x>
261+
<y>559</y>
262+
</hint>
263+
<hint type="destinationlabel">
264+
<x>286</x>
265+
<y>274</y>
266+
</hint>
267+
</hints>
268+
</connection>
269+
</connections>
270+
</ui>
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
FieldsCalculatorDialog.py
6+
---------------------
7+
Date : October 2013
8+
Copyright : (C) 2013 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'October 2013'
22+
__copyright__ = '(C) 2013, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
import re
30+
31+
from PyQt4.QtCore import *
32+
from PyQt4.QtGui import *
33+
from qgis.core import *
34+
from qgis.gui import *
35+
36+
from processing.core.ProcessingConfig import ProcessingConfig
37+
from processing.core.ProcessingLog import ProcessingLog
38+
from processing.core.GeoAlgorithmExecutionException import \
39+
GeoAlgorithmExecutionException
40+
from processing.gui.UnthreadedAlgorithmExecutor import \
41+
UnthreadedAlgorithmExecutor
42+
from processing.gui.Postprocessing import Postprocessing
43+
from processing.tools import dataobjects
44+
45+
from ui_DlgFieldsCalculator import Ui_FieldsCalculator
46+
47+
48+
class FieldsCalculatorDialog(QDialog, Ui_FieldsCalculator):
49+
def __init__(self, alg):
50+
QDialog.__init__(self)
51+
self.setupUi( self )
52+
53+
self.executed = False
54+
self.alg = alg
55+
self.layer = None
56+
57+
self.cmbInputLayer.currentIndexChanged.connect(self.updateLayer)
58+
self.btnBrowse.clicked.connect(self.selectFile)
59+
self.mNewFieldGroupBox.toggled.connect(self.toggleExistingGroup)
60+
self.mUpdateExistingGroupBox.toggled.connect(self.toggleNewGroup)
61+
self.mOutputFieldTypeComboBox.currentIndexChanged.connect(
62+
self.setupSpinboxes)
63+
64+
# Default values for field width and precision
65+
self.mOutputFieldWidthSpinBox.setValue(10)
66+
self.mOutputFieldPrecisionSpinBox.setValue(3)
67+
68+
# Output is a shapefile, so limit maximum field name length
69+
self.mOutputFieldNameLineEdit.setMaxLength(10)
70+
71+
self.manageGui()
72+
73+
def manageGui(self):
74+
self.mOutputFieldTypeComboBox.blockSignals(True)
75+
for t in self.alg.TYPE_NAMES:
76+
self.mOutputFieldTypeComboBox.addItem(t)
77+
self.mOutputFieldTypeComboBox.blockSignals(False)
78+
79+
self.cmbInputLayer.blockSignals(True)
80+
layers = dataobjects.getVectorLayers()
81+
for layer in layers:
82+
self.cmbInputLayer.addItem(layer.name())
83+
self.cmbInputLayer.blockSignals(False)
84+
85+
self.updateLayer()
86+
87+
def updateLayer(self):
88+
self.layer = dataobjects.getObject(self.cmbInputLayer.currentText())
89+
90+
self.builder.setLayer(self.layer)
91+
self.builder.loadFieldNames()
92+
93+
self.populateFields()
94+
#populateOutputFieldTypes()
95+
96+
def setupSpinboxes(self, index):
97+
if index != 0:
98+
self.mOutputFieldPrecisionSpinBox.setEnabled(False)
99+
else:
100+
self.mOutputFieldPrecisionSpinBox.setEnabled(True)
101+
102+
if index == 0:
103+
self.mOutputFieldWidthSpinBox.setRange(1, 20)
104+
self.mOutputFieldWidthSpinBox.setValue(10)
105+
self.mOutputFieldPrecisionSpinBox.setRange(0, 15)
106+
self.mOutputFieldPrecisionSpinBox.setValue(3)
107+
elif index == 1:
108+
self.mOutputFieldWidthSpinBox.setRange(1, 10)
109+
self.mOutputFieldWidthSpinBox.setValue(10)
110+
elif index == 2:
111+
self.mOutputFieldWidthSpinBox.setRange(1, 255)
112+
self.mOutputFieldWidthSpinBox.setValue(80)
113+
else:
114+
self.mOutputFieldWidthSpinBox.setEnabled(False)
115+
self.mOutputFieldPrecisionSpinBox.setEnabled(False)
116+
117+
def selectFile(self):
118+
output = self.alg.getOutputFromName('OUTPUT_LAYER')
119+
fileFilter = output.getFileFilter(self.alg)
120+
121+
settings = QSettings()
122+
if settings.contains('/Processing/LastOutputPath'):
123+
path = settings.value('/Processing/LastOutputPath')
124+
else:
125+
path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)
126+
lastEncoding = settings.value('/Processing/encoding', 'System')
127+
fileDialog = QgsEncodingFileDialog(self,
128+
self.tr('Save file'),
129+
path,
130+
fileFilter,
131+
lastEncoding)
132+
fileDialog.setFileMode(QFileDialog.AnyFile)
133+
fileDialog.setAcceptMode(QFileDialog.AcceptSave)
134+
fileDialog.setConfirmOverwrite(True)
135+
if fileDialog.exec_() == QDialog.Accepted:
136+
files = fileDialog.selectedFiles()
137+
encoding = unicode(fileDialog.encoding())
138+
output.encoding = encoding
139+
filename = unicode(files[0])
140+
selectedFileFilter = unicode(fileDialog.selectedNameFilter())
141+
if not filename.lower().endswith(
142+
tuple(re.findall("\*(\.[a-z]{1,5})", fileFilter))):
143+
ext = re.search("\*(\.[a-z]{1,5})", selectedFileFilter)
144+
if ext:
145+
filename = filename + ext.group(1)
146+
self.leOutputFile.setText(filename)
147+
settings.setValue('/Processing/LastOutputPath',
148+
os.path.dirname(filename))
149+
settings.setValue('/Processing/encoding', encoding)
150+
151+
def toggleExistingGroup(self, toggled):
152+
self.mUpdateExistingGroupBox.setChecked(not toggled)
153+
154+
def toggleNewGroup(self, toggled):
155+
self.mNewFieldGroupBox.setChecked(not toggled)
156+
157+
def populateFields(self):
158+
if self.layer is None:
159+
return
160+
161+
fields = self.layer.pendingFields()
162+
for f in fields:
163+
self.mExistingFieldComboBox.addItem(f.name())
164+
165+
def setParamValues(self):
166+
if self.mUpdateExistingGroupBox.isChecked():
167+
fieldName = self.mExistingFieldComboBox.currentText()
168+
else:
169+
fieldName = self.mOutputFieldNameLineEdit.text()
170+
171+
layer = dataobjects.getObjectFromName(self.cmbInputLayer.currentText())
172+
173+
self.alg.setParameterValue('INPUT_LAYER', layer)
174+
self.alg.setParameterValue('FIELD_NAME', fieldName)
175+
self.alg.setParameterValue('FIELD_TYPE',
176+
self.mOutputFieldTypeComboBox.currentIndex())
177+
self.alg.setParameterValue('FIELD_LENGTH',
178+
self.mOutputFieldWidthSpinBox.value())
179+
self.alg.setParameterValue('FIELD_PRECISION',
180+
self.mOutputFieldPrecisionSpinBox.value())
181+
self.alg.setParameterValue('NEW_FIELD',
182+
self.mNewFieldGroupBox.isChecked())
183+
self.alg.setParameterValue('FORMULA', self.builder.expressionText())
184+
self.alg.setOutputValue('OUTPUT_LAYER',
185+
self.leOutputFile.text())
186+
return True
187+
188+
def accept(self):
189+
keepOpen = ProcessingConfig.getSetting(
190+
ProcessingConfig.KEEP_DIALOG_OPEN)
191+
try:
192+
if self.setParamValues():
193+
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
194+
ProcessingLog.addToLog(ProcessingLog.LOG_ALGORITHM,
195+
self.alg.getAsCommand())
196+
ret = UnthreadedAlgorithmExecutor.runalg(self.alg, self)
197+
QApplication.restoreOverrideCursor()
198+
if ret:
199+
Postprocessing.handleAlgorithmResults(self.alg,
200+
self,
201+
not keepOpen)
202+
self.executed = True
203+
QDialog.reject(self)
204+
else:
205+
QMessageBox.critical(self,
206+
self.tr('Unable to execute algorithm'),
207+
self.tr('Wrong or missing parameter '
208+
'values'))
209+
return
210+
except GeoAlgorithmExecutionException, e:
211+
QApplication.restoreOverrideCursor()
212+
QMessageBox.critical(self, "Error",e.msg)
213+
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
214+
self.executed = False
215+
QDialog.reject(self)
216+
217+
def reject(self):
218+
self.executed = False
219+
QDialog.reject(self)
220+
221+
def setPercentage(self, i):
222+
self.progressBar.setValue(i)
223+
224+
def setText(self, text):
225+
pass
226+
227+
def error(self, text):
228+
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, text)

‎python/plugins/processing/algs/ui/__init__.py

Whitespace-only changes.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file 'python/plugins/processing/algs/ui/DlgFieldsCalculator.ui'
4+
#
5+
# Created: Thu Oct 24 16:06:48 2013
6+
# by: PyQt4 UI code generator 4.9.1
7+
#
8+
# WARNING! All changes made in this file will be lost!
9+
10+
from PyQt4 import QtCore, QtGui
11+
12+
try:
13+
_fromUtf8 = QtCore.QString.fromUtf8
14+
except AttributeError:
15+
_fromUtf8 = lambda s: s
16+
17+
class Ui_FieldsCalculator(object):
18+
def setupUi(self, FieldsCalculator):
19+
FieldsCalculator.setObjectName(_fromUtf8("FieldsCalculator"))
20+
FieldsCalculator.resize(681, 681)
21+
self.gridLayout = QtGui.QGridLayout(FieldsCalculator)
22+
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
23+
self.mNewFieldGroupBox = QtGui.QGroupBox(FieldsCalculator)
24+
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
25+
sizePolicy.setHorizontalStretch(0)
26+
sizePolicy.setVerticalStretch(0)
27+
sizePolicy.setHeightForWidth(self.mNewFieldGroupBox.sizePolicy().hasHeightForWidth())
28+
self.mNewFieldGroupBox.setSizePolicy(sizePolicy)
29+
self.mNewFieldGroupBox.setFlat(True)
30+
self.mNewFieldGroupBox.setCheckable(True)
31+
self.mNewFieldGroupBox.setChecked(True)
32+
self.mNewFieldGroupBox.setObjectName(_fromUtf8("mNewFieldGroupBox"))
33+
self.gridlayout = QtGui.QGridLayout(self.mNewFieldGroupBox)
34+
self.gridlayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
35+
self.gridlayout.setContentsMargins(3, 3, 3, 0)
36+
self.gridlayout.setVerticalSpacing(3)
37+
self.gridlayout.setObjectName(_fromUtf8("gridlayout"))
38+
self.mFieldNameLabel = QtGui.QLabel(self.mNewFieldGroupBox)
39+
self.mFieldNameLabel.setObjectName(_fromUtf8("mFieldNameLabel"))
40+
self.gridlayout.addWidget(self.mFieldNameLabel, 0, 0, 1, 1)
41+
self.mOutputFieldNameLineEdit = QtGui.QLineEdit(self.mNewFieldGroupBox)
42+
self.mOutputFieldNameLineEdit.setObjectName(_fromUtf8("mOutputFieldNameLineEdit"))
43+
self.gridlayout.addWidget(self.mOutputFieldNameLineEdit, 0, 1, 1, 3)
44+
self.mOutputFieldTypeLabel = QtGui.QLabel(self.mNewFieldGroupBox)
45+
self.mOutputFieldTypeLabel.setObjectName(_fromUtf8("mOutputFieldTypeLabel"))
46+
self.gridlayout.addWidget(self.mOutputFieldTypeLabel, 1, 0, 1, 1)
47+
self.mOutputFieldTypeComboBox = QtGui.QComboBox(self.mNewFieldGroupBox)
48+
self.mOutputFieldTypeComboBox.setObjectName(_fromUtf8("mOutputFieldTypeComboBox"))
49+
self.gridlayout.addWidget(self.mOutputFieldTypeComboBox, 1, 1, 1, 3)
50+
self.mOutputFieldWidthLabel = QtGui.QLabel(self.mNewFieldGroupBox)
51+
self.mOutputFieldWidthLabel.setObjectName(_fromUtf8("mOutputFieldWidthLabel"))
52+
self.gridlayout.addWidget(self.mOutputFieldWidthLabel, 2, 0, 1, 1)
53+
self.mOutputFieldWidthSpinBox = QtGui.QSpinBox(self.mNewFieldGroupBox)
54+
self.mOutputFieldWidthSpinBox.setMinimum(0)
55+
self.mOutputFieldWidthSpinBox.setProperty("value", 15)
56+
self.mOutputFieldWidthSpinBox.setObjectName(_fromUtf8("mOutputFieldWidthSpinBox"))
57+
self.gridlayout.addWidget(self.mOutputFieldWidthSpinBox, 2, 1, 1, 1)
58+
self.mOutputFieldPrecisionLabel = QtGui.QLabel(self.mNewFieldGroupBox)
59+
self.mOutputFieldPrecisionLabel.setObjectName(_fromUtf8("mOutputFieldPrecisionLabel"))
60+
self.gridlayout.addWidget(self.mOutputFieldPrecisionLabel, 2, 2, 1, 1)
61+
self.mOutputFieldPrecisionSpinBox = QtGui.QSpinBox(self.mNewFieldGroupBox)
62+
self.mOutputFieldPrecisionSpinBox.setProperty("value", 2)
63+
self.mOutputFieldPrecisionSpinBox.setObjectName(_fromUtf8("mOutputFieldPrecisionSpinBox"))
64+
self.gridlayout.addWidget(self.mOutputFieldPrecisionSpinBox, 2, 3, 1, 1)
65+
self.gridLayout.addWidget(self.mNewFieldGroupBox, 2, 0, 1, 1)
66+
self.mButtonBox = QtGui.QDialogButtonBox(FieldsCalculator)
67+
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
68+
sizePolicy.setHorizontalStretch(3)
69+
sizePolicy.setVerticalStretch(0)
70+
sizePolicy.setHeightForWidth(self.mButtonBox.sizePolicy().hasHeightForWidth())
71+
self.mButtonBox.setSizePolicy(sizePolicy)
72+
self.mButtonBox.setOrientation(QtCore.Qt.Horizontal)
73+
self.mButtonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
74+
self.mButtonBox.setObjectName(_fromUtf8("mButtonBox"))
75+
self.gridLayout.addWidget(self.mButtonBox, 5, 0, 1, 2)
76+
self.horizontalLayout = QtGui.QHBoxLayout()
77+
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
78+
self.label = QtGui.QLabel(FieldsCalculator)
79+
self.label.setObjectName(_fromUtf8("label"))
80+
self.horizontalLayout.addWidget(self.label)
81+
self.cmbInputLayer = QtGui.QComboBox(FieldsCalculator)
82+
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
83+
sizePolicy.setHorizontalStretch(0)
84+
sizePolicy.setVerticalStretch(0)
85+
sizePolicy.setHeightForWidth(self.cmbInputLayer.sizePolicy().hasHeightForWidth())
86+
self.cmbInputLayer.setSizePolicy(sizePolicy)
87+
self.cmbInputLayer.setObjectName(_fromUtf8("cmbInputLayer"))
88+
self.horizontalLayout.addWidget(self.cmbInputLayer)
89+
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 2)
90+
self.mUpdateExistingGroupBox = QtGui.QGroupBox(FieldsCalculator)
91+
self.mUpdateExistingGroupBox.setFlat(True)
92+
self.mUpdateExistingGroupBox.setCheckable(True)
93+
self.mUpdateExistingGroupBox.setChecked(False)
94+
self.mUpdateExistingGroupBox.setObjectName(_fromUtf8("mUpdateExistingGroupBox"))
95+
self.verticalLayout = QtGui.QVBoxLayout(self.mUpdateExistingGroupBox)
96+
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
97+
self.mExistingFieldComboBox = QtGui.QComboBox(self.mUpdateExistingGroupBox)
98+
self.mExistingFieldComboBox.setObjectName(_fromUtf8("mExistingFieldComboBox"))
99+
self.verticalLayout.addWidget(self.mExistingFieldComboBox)
100+
self.gridLayout.addWidget(self.mUpdateExistingGroupBox, 2, 1, 1, 1)
101+
self.builder = QgsExpressionBuilderWidget(FieldsCalculator)
102+
self.builder.setAutoFillBackground(False)
103+
self.builder.setObjectName(_fromUtf8("builder"))
104+
self.gridLayout.addWidget(self.builder, 3, 0, 1, 2)
105+
self.horizontalLayout_2 = QtGui.QHBoxLayout()
106+
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
107+
self.label_2 = QtGui.QLabel(FieldsCalculator)
108+
self.label_2.setObjectName(_fromUtf8("label_2"))
109+
self.horizontalLayout_2.addWidget(self.label_2)
110+
self.leOutputFile = QtGui.QLineEdit(FieldsCalculator)
111+
self.leOutputFile.setObjectName(_fromUtf8("leOutputFile"))
112+
self.horizontalLayout_2.addWidget(self.leOutputFile)
113+
self.btnBrowse = QtGui.QToolButton(FieldsCalculator)
114+
self.btnBrowse.setObjectName(_fromUtf8("btnBrowse"))
115+
self.horizontalLayout_2.addWidget(self.btnBrowse)
116+
self.gridLayout.addLayout(self.horizontalLayout_2, 1, 0, 1, 2)
117+
self.progressBar = QtGui.QProgressBar(FieldsCalculator)
118+
self.progressBar.setProperty("value", 0)
119+
self.progressBar.setObjectName(_fromUtf8("progressBar"))
120+
self.gridLayout.addWidget(self.progressBar, 4, 0, 1, 2)
121+
self.mFieldNameLabel.setBuddy(self.mOutputFieldNameLineEdit)
122+
self.mOutputFieldTypeLabel.setBuddy(self.mOutputFieldTypeComboBox)
123+
self.mOutputFieldWidthLabel.setBuddy(self.mOutputFieldWidthSpinBox)
124+
self.mOutputFieldPrecisionLabel.setBuddy(self.mOutputFieldPrecisionSpinBox)
125+
126+
self.retranslateUi(FieldsCalculator)
127+
QtCore.QObject.connect(self.mButtonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), FieldsCalculator.accept)
128+
QtCore.QObject.connect(self.mButtonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), FieldsCalculator.reject)
129+
QtCore.QMetaObject.connectSlotsByName(FieldsCalculator)
130+
FieldsCalculator.setTabOrder(self.mOutputFieldNameLineEdit, self.mOutputFieldTypeComboBox)
131+
FieldsCalculator.setTabOrder(self.mOutputFieldTypeComboBox, self.mOutputFieldWidthSpinBox)
132+
FieldsCalculator.setTabOrder(self.mOutputFieldWidthSpinBox, self.mOutputFieldPrecisionSpinBox)
133+
FieldsCalculator.setTabOrder(self.mOutputFieldPrecisionSpinBox, self.mButtonBox)
134+
135+
def retranslateUi(self, FieldsCalculator):
136+
FieldsCalculator.setWindowTitle(QtGui.QApplication.translate("FieldsCalculator", "Field calculator", None, QtGui.QApplication.UnicodeUTF8))
137+
self.mNewFieldGroupBox.setTitle(QtGui.QApplication.translate("FieldsCalculator", "Create a new field", None, QtGui.QApplication.UnicodeUTF8))
138+
self.mFieldNameLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Output field name", None, QtGui.QApplication.UnicodeUTF8))
139+
self.mOutputFieldTypeLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Output field type", None, QtGui.QApplication.UnicodeUTF8))
140+
self.mOutputFieldWidthLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Output field width", None, QtGui.QApplication.UnicodeUTF8))
141+
self.mOutputFieldWidthSpinBox.setToolTip(QtGui.QApplication.translate("FieldsCalculator", "Width of complete output. For example 123,456 means 6 as field width.", None, QtGui.QApplication.UnicodeUTF8))
142+
self.mOutputFieldPrecisionLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Precision", None, QtGui.QApplication.UnicodeUTF8))
143+
self.label.setText(QtGui.QApplication.translate("FieldsCalculator", "Input layer", None, QtGui.QApplication.UnicodeUTF8))
144+
self.mUpdateExistingGroupBox.setTitle(QtGui.QApplication.translate("FieldsCalculator", "Update existing field", None, QtGui.QApplication.UnicodeUTF8))
145+
self.label_2.setText(QtGui.QApplication.translate("FieldsCalculator", "Output file", None, QtGui.QApplication.UnicodeUTF8))
146+
self.btnBrowse.setText(QtGui.QApplication.translate("FieldsCalculator", "...", None, QtGui.QApplication.UnicodeUTF8))
147+
148+
from qgis.gui import QgsExpressionBuilderWidget

0 commit comments

Comments
 (0)
Please sign in to comment.