Skip to content

Commit ee9ab82

Browse files
committedOct 10, 2011
Added expression output preview area. Thanks to Andy Bates for the idea
1 parent 651940c commit ee9ab82

File tree

3 files changed

+131
-6
lines changed

3 files changed

+131
-6
lines changed
 

‎src/gui/qgsexpressionbuilder.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,26 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
193193
{
194194
QString text = this->txtExpressionString->toPlainText();
195195
QgsExpression exp( text );
196-
if ( exp.hasParserError())
196+
197+
// TODO We could do this without a layer to.
198+
// maybe just calling exp.evaluate()?
199+
if ( chkLive->isChecked() && mLayer )
200+
{
201+
QgsFeature feature;
202+
mLayer->featureAtId( 0 , feature );
203+
QVariant value = exp.evaluate( &feature, mLayer->pendingFields() );
204+
205+
if (!exp.hasEvalError())
206+
lblPreview->setText( value.toString() );
207+
}
208+
209+
if ( exp.hasParserError() || exp.hasEvalError())
197210
{
198211
this->txtExpressionString->setStyleSheet("background-color: rgba(255, 6, 10, 75);");
199-
this->txtExpressionString->setToolTip(exp.parserErrorString());
212+
QString tooltip = "<b>Parser Error:</b> <br>" + exp.parserErrorString();
213+
if (exp.hasEvalError())
214+
tooltip += "<br><br> <b>Eval Error:</b> <br>" + exp.evalErrorString();
215+
this->txtExpressionString->setToolTip(tooltip);
200216
emit expressionParsed(false);
201217
}
202218
else
@@ -216,6 +232,24 @@ void QgsExpressionBuilderWidget::on_txtSearchEdit_textChanged()
216232
expressionTree->expandAll();
217233
}
218234

235+
void QgsExpressionBuilderWidget::on_btnUpdatePreview_clicked()
236+
{
237+
QString text = this->txtExpressionString->toPlainText();
238+
QgsExpression exp( text );
239+
240+
// TODO We could do this without a layer maybe just calling exp.evaluate()?
241+
if ( mLayer )
242+
{
243+
QgsFeature feature;
244+
mLayer->featureAtId( 0 , feature );
245+
QVariant value = exp.evaluate( &feature, mLayer->pendingFields() );
246+
247+
if (!exp.hasEvalError())
248+
lblPreview->setText( value.toString() );
249+
}
250+
}
251+
252+
219253
void QgsExpressionBuilderWidget::showContextMenu( const QPoint & pt)
220254
{
221255
QModelIndex idx = expressionTree->indexAt( pt );

‎src/gui/qgsexpressionbuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class QgsExpressionItemSearhProxy : public QSortFilterProxyModel
3636
if (source_parent == qobject_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem()->index())
3737
return true;
3838

39-
QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
39+
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
4040
}
4141
};
4242

@@ -141,6 +141,7 @@ public slots:
141141
void on_expressionTree_doubleClicked(const QModelIndex &index);
142142
void on_txtExpressionString_textChanged();
143143
void on_txtSearchEdit_textChanged();
144+
void on_btnUpdatePreview_clicked();
144145
void showContextMenu( const QPoint & );
145146
void loadSampleValues();
146147
void loadAllValues();

‎src/ui/qgsexpressionbuilder.ui

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,90 @@
212212
</property>
213213
</widget>
214214
</item>
215+
<item row="1" column="0">
216+
<layout class="QHBoxLayout" name="horizontalLayout_3">
217+
<property name="spacing">
218+
<number>3</number>
219+
</property>
220+
<property name="topMargin">
221+
<number>0</number>
222+
</property>
223+
<item>
224+
<widget class="QCheckBox" name="chkLive">
225+
<property name="sizePolicy">
226+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
227+
<horstretch>0</horstretch>
228+
<verstretch>0</verstretch>
229+
</sizepolicy>
230+
</property>
231+
<property name="toolTip">
232+
<string>If enabled the output preview will be generated &lt;br&gt; as the expression string is updated.</string>
233+
</property>
234+
<property name="text">
235+
<string>Live preview</string>
236+
</property>
237+
<property name="checked">
238+
<bool>true</bool>
239+
</property>
240+
</widget>
241+
</item>
242+
<item>
243+
<widget class="QPushButton" name="btnUpdatePreview">
244+
<property name="sizePolicy">
245+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
246+
<horstretch>0</horstretch>
247+
<verstretch>0</verstretch>
248+
</sizepolicy>
249+
</property>
250+
<property name="font">
251+
<font>
252+
<pointsize>7</pointsize>
253+
</font>
254+
</property>
255+
<property name="toolTip">
256+
<string>If Live preview is unchecked this button can be &lt;br&gt; used to render a output preview.</string>
257+
</property>
258+
<property name="text">
259+
<string>Update Preview</string>
260+
</property>
261+
</widget>
262+
</item>
263+
<item>
264+
<widget class="QLabel" name="label_2">
265+
<property name="sizePolicy">
266+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
267+
<horstretch>0</horstretch>
268+
<verstretch>0</verstretch>
269+
</sizepolicy>
270+
</property>
271+
<property name="toolTip">
272+
<string>Output preview is generated &lt;br&gt; using the first feature from the layer.</string>
273+
</property>
274+
<property name="text">
275+
<string>Output preview: </string>
276+
</property>
277+
</widget>
278+
</item>
279+
<item>
280+
<widget class="QLabel" name="lblPreview">
281+
<property name="font">
282+
<font>
283+
<weight>50</weight>
284+
<italic>true</italic>
285+
<bold>false</bold>
286+
<underline>false</underline>
287+
</font>
288+
</property>
289+
<property name="toolTip">
290+
<string>Output preview is generated &lt;br&gt; using the first feature from the layer.</string>
291+
</property>
292+
<property name="text">
293+
<string/>
294+
</property>
295+
</widget>
296+
</item>
297+
</layout>
298+
</item>
215299
</layout>
216300
</widget>
217301
</item>
@@ -222,6 +306,9 @@
222306
<property name="title">
223307
<string>Function List</string>
224308
</property>
309+
<property name="flat">
310+
<bool>true</bool>
311+
</property>
225312
<layout class="QGridLayout" name="gridLayout_6">
226313
<property name="margin">
227314
<number>0</number>
@@ -260,7 +347,10 @@
260347
<number>0</number>
261348
</property>
262349
<property name="topMargin">
263-
<number>0</number>
350+
<number>2</number>
351+
</property>
352+
<property name="bottomMargin">
353+
<number>2</number>
264354
</property>
265355
<item>
266356
<widget class="QLabel" name="label">
@@ -304,7 +394,7 @@
304394
<item>
305395
<widget class="QLabel" name="mValueListLabel">
306396
<property name="text">
307-
<string>&lt;b&gt;Field Values&lt;/b&gt;</string>
397+
<string>Field Values</string>
308398
</property>
309399
</widget>
310400
</item>
@@ -345,7 +435,7 @@
345435
<string>Function Help</string>
346436
</property>
347437
<property name="flat">
348-
<bool>false</bool>
438+
<bool>true</bool>
349439
</property>
350440
<layout class="QGridLayout" name="gridLayout_5">
351441
<property name="margin">

0 commit comments

Comments
 (0)
Please sign in to comment.