Skip to content

Commit a61a15b

Browse files
committed
Move expression text replace to QgsExpression
- Update actions, html map tips, and annotations
1 parent 7298e73 commit a61a15b

File tree

6 files changed

+103
-115
lines changed

6 files changed

+103
-115
lines changed

python/core/qgsexpression.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public:
5050
//! (used by internal functions)
5151
QgsDistanceArea* geomCalculator();
5252

53+
/** This function currently replaces each expression between [% and %]
54+
in the string with the result of its evaluation on the feature
55+
passed as argument.
56+
57+
Additional substitutions can be passed through the substitutionMap
58+
parameter
59+
*/
60+
static QString replaceExpressionText( QString action, QgsFeature &feat,
61+
QgsVectorLayer* layer,
62+
const QMap<QString, QVariant> *substitutionMap = 0);
63+
5364
//
5465

5566
enum UnaryOperator

src/core/qgsattributeaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void QgsAttributeAction::doAction( int index, QgsFeature &feat,
9696
return;
9797

9898
// search for expressions while expanding actions
99-
QString expandedAction = expandAction( action.action(), feat, substitutionMap );
99+
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), feat, mLayer , substitutionMap );
100100
if ( expandedAction.isEmpty() )
101101
return;
102102

src/core/qgsexpression.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ QString QgsExpression::dump() const
999999
return mRootNode->dump();
10001000
}
10011001

1002+
10021003
void QgsExpression::toOgcFilter( QDomDocument &doc, QDomElement &element ) const
10031004
{
10041005
if ( !mRootNode )
@@ -1048,6 +1049,57 @@ void QgsExpression::acceptVisitor( QgsExpression::Visitor& v )
10481049
mRootNode->accept( v );
10491050
}
10501051

1052+
QString QgsExpression::replaceExpressionText( QString action, QgsFeature &feat,
1053+
QgsVectorLayer* layer,
1054+
const QMap<QString, QVariant> *substitutionMap )
1055+
{
1056+
QString expr_action;
1057+
1058+
int index = 0;
1059+
while ( index < action.size() )
1060+
{
1061+
QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" );
1062+
1063+
int pos = rx.indexIn( action, index );
1064+
if ( pos < 0 )
1065+
break;
1066+
1067+
int start = index;
1068+
index = pos + rx.matchedLength();
1069+
1070+
QString to_replace = rx.cap( 1 ).trimmed();
1071+
QgsDebugMsg( "Found expression: " + to_replace );
1072+
1073+
if ( substitutionMap && substitutionMap->contains( to_replace ) )
1074+
{
1075+
expr_action += action.mid( start, pos - start ) + substitutionMap->value( to_replace ).toString();
1076+
continue;
1077+
}
1078+
1079+
QgsExpression exp( to_replace );
1080+
if ( exp.hasParserError() )
1081+
{
1082+
QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() );
1083+
expr_action += action.mid( start, index - start );
1084+
continue;
1085+
}
1086+
1087+
QVariant result = exp.evaluate( &feat, layer->pendingFields() );
1088+
if ( exp.hasEvalError() )
1089+
{
1090+
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );
1091+
expr_action += action.mid( start, index - start );
1092+
continue;
1093+
}
1094+
1095+
QgsDebugMsg( "Expression result is: " + result.toString() );
1096+
expr_action += action.mid( start, pos - start ) + result.toString();
1097+
}
1098+
1099+
expr_action += action.mid( index );
1100+
return expr_action;
1101+
}
1102+
10511103

10521104
QgsExpression::Node* QgsExpression::Node::createFromOgcFilter( QDomElement &element, QString &errorMessage )
10531105
{

src/core/qgsexpression.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QDomDocument>
2424

2525
#include "qgsfield.h"
26+
#include "qgsvectorlayer.h"
2627

2728
class QgsDistanceArea;
2829
class QgsFeature;
@@ -132,6 +133,17 @@ class CORE_EXPORT QgsExpression
132133
//! (used by internal functions)
133134
QgsDistanceArea* geomCalculator() { if ( !mCalc ) initGeomCalculator(); return mCalc; }
134135

136+
/** This function currently replaces each expression between [% and %]
137+
in the string with the result of its evaluation on the feature
138+
passed as argument.
139+
140+
Additional substitutions can be passed through the substitutionMap
141+
parameter
142+
*/
143+
static QString replaceExpressionText( QString action, QgsFeature &feat,
144+
QgsVectorLayer* layer,
145+
const QMap<QString, QVariant> *substitutionMap = 0 );
146+
135147
//
136148

137149
enum UnaryOperator

src/gui/qgshtmlannotationitem.cpp

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ QgsHtmlAnnotationItem::QgsHtmlAnnotationItem( QgsMapCanvas* canvas, QgsVectorLay
4242
mWidgetContainer = new QGraphicsProxyWidget( this );
4343
mWidgetContainer->setWidget( mWebView );
4444

45-
QObject::connect( mWebView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javascript()));
45+
QObject::connect( mWebView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared() ), this, SLOT( javascript() ) );
4646

4747
if ( mVectorLayer && mMapCanvas )
4848
{
@@ -86,36 +86,36 @@ void QgsHtmlAnnotationItem::setMapPosition( const QgsPoint& pos )
8686

8787
void QgsHtmlAnnotationItem::paint( QPainter * painter )
8888
{
89-
Q_UNUSED( painter );
89+
Q_UNUSED( painter );
9090
}
9191

9292
void QgsHtmlAnnotationItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
9393
{
9494
Q_UNUSED( option );
9595
Q_UNUSED( widget );
96-
if ( !painter || !mWidgetContainer )
97-
{
98-
return;
99-
}
96+
if ( !painter || !mWidgetContainer )
97+
{
98+
return;
99+
}
100100

101-
drawFrame( painter );
102-
if ( mMapPositionFixed )
103-
{
104-
drawMarkerSymbol( painter );
105-
}
101+
drawFrame( painter );
102+
if ( mMapPositionFixed )
103+
{
104+
drawMarkerSymbol( painter );
105+
}
106106

107-
mWidgetContainer->setGeometry( QRectF( mOffsetFromReferencePoint.x() + mFrameBorderWidth / 2.0, mOffsetFromReferencePoint.y()
108-
+ mFrameBorderWidth / 2.0, mFrameSize.width() - mFrameBorderWidth, mFrameSize.height()
109-
- mFrameBorderWidth ) );
110-
if (data(1).toString() == "composer")
111-
{
112-
mWidgetContainer->widget()->render( painter, mOffsetFromReferencePoint.toPoint());
113-
}
107+
mWidgetContainer->setGeometry( QRectF( mOffsetFromReferencePoint.x() + mFrameBorderWidth / 2.0, mOffsetFromReferencePoint.y()
108+
+ mFrameBorderWidth / 2.0, mFrameSize.width() - mFrameBorderWidth, mFrameSize.height()
109+
- mFrameBorderWidth ) );
110+
if ( data( 1 ).toString() == "composer" )
111+
{
112+
mWidgetContainer->widget()->render( painter, mOffsetFromReferencePoint.toPoint() );
113+
}
114114

115-
if ( isSelected() )
116-
{
117-
drawSelectionBoxes( painter );
118-
}
115+
if ( isSelected() )
116+
{
117+
drawSelectionBoxes( painter );
118+
}
119119
}
120120

121121
QSizeF QgsHtmlAnnotationItem::minimumFrameSize() const
@@ -167,7 +167,7 @@ void QgsHtmlAnnotationItem::readXML( const QDomDocument& doc, const QDomElement&
167167
}
168168
mHasAssociatedFeature = itemElem.attribute( "hasFeature", "0" ).toInt();
169169
mFeatureId = itemElem.attribute( "feature", "0" ).toInt();
170-
mHtmlFile = itemElem.attribute( "htmlfile", "");
170+
mHtmlFile = itemElem.attribute( "htmlfile", "" );
171171
QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" );
172172
if ( !annotationElem.isNull() )
173173
{
@@ -210,7 +210,7 @@ void QgsHtmlAnnotationItem::setFeatureForMapPosition()
210210
mFeatureId = currentFeatureId;
211211
mFeature = currentFeature;
212212

213-
QString newtext = replaceText( mHtmlSource, vectorLayer(), mFeature );
213+
QString newtext = QgsExpression::replaceExpressionText( mHtmlSource, mFeature, vectorLayer() );
214214
mWebView->setHtml( newtext );
215215

216216
}
@@ -227,51 +227,8 @@ void QgsHtmlAnnotationItem::updateVisibility()
227227

228228
void QgsHtmlAnnotationItem::javascript()
229229
{
230-
QWebFrame *frame = mWebView->page()->mainFrame();
231-
frame->addToJavaScriptWindowObject("canvas", mMapCanvas );
232-
}
233-
234-
QString QgsHtmlAnnotationItem::replaceText( QString displayText, QgsVectorLayer *layer, QgsFeature &feat )
235-
{
236-
QString expr_action;
237-
238-
int index = 0;
239-
while ( index < displayText.size() )
240-
{
241-
QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" );
242-
243-
int pos = rx.indexIn( displayText, index );
244-
if ( pos < 0 )
245-
break;
246-
247-
int start = index;
248-
index = pos + rx.matchedLength();
249-
250-
QString to_replace = rx.cap( 1 ).trimmed();
251-
QgsDebugMsg( "Found expression: " + to_replace );
252-
253-
QgsExpression exp( to_replace );
254-
if ( exp.hasParserError() )
255-
{
256-
QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() );
257-
expr_action += displayText.mid( start, index - start );
258-
continue;
259-
}
260-
261-
QVariant result = exp.evaluate( &feat, layer->pendingFields() );
262-
if ( exp.hasEvalError() )
263-
{
264-
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );
265-
expr_action += displayText.mid( start, index - start );
266-
continue;
267-
}
268-
269-
QgsDebugMsg( "Expression result is: " + result.toString() );
270-
expr_action += displayText.mid( start, pos - start ) + result.toString();
271-
}
272-
273-
expr_action += displayText.mid( index );
274-
return expr_action;
230+
QWebFrame *frame = mWebView->page()->mainFrame();
231+
frame->addToJavaScriptWindowObject( "canvas", mMapCanvas );
275232
}
276233

277234

src/gui/qgsmaptip.cpp

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,51 +101,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsM
101101

102102
int idx = vlayer->fieldNameIndex( vlayer->displayField() );
103103
if ( idx < 0 )
104-
return replaceText( vlayer->displayField(), vlayer, feature );
104+
return QgsExpression::replaceExpressionText( vlayer->displayField(), feature, vlayer );
105105
else
106106
return feature.attributeMap().value( idx, "" ).toString();
107-
108-
}
109-
110-
QString QgsMapTip::replaceText( QString displayText, QgsVectorLayer *layer, QgsFeature &feat )
111-
{
112-
QString expr_action;
113-
114-
int index = 0;
115-
while ( index < displayText.size() )
116-
{
117-
QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" );
118-
119-
int pos = rx.indexIn( displayText, index );
120-
if ( pos < 0 )
121-
break;
122-
123-
int start = index;
124-
index = pos + rx.matchedLength();
125-
126-
QString to_replace = rx.cap( 1 ).trimmed();
127-
QgsDebugMsg( "Found expression: " + to_replace );
128-
129-
QgsExpression exp( to_replace );
130-
if ( exp.hasParserError() )
131-
{
132-
QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() );
133-
expr_action += displayText.mid( start, index - start );
134-
continue;
135-
}
136-
137-
QVariant result = exp.evaluate( &feat, layer->pendingFields() );
138-
if ( exp.hasEvalError() )
139-
{
140-
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );
141-
expr_action += displayText.mid( start, index - start );
142-
continue;
143-
}
144-
145-
QgsDebugMsg( "Expression result is: " + result.toString() );
146-
expr_action += displayText.mid( start, pos - start ) + result.toString();
147-
}
148-
149-
expr_action += displayText.mid( index );
150-
return expr_action;
151107
}

0 commit comments

Comments
 (0)