From a61a15bb1e90abe667e3f0d2e9bf4bc076f5b46a Mon Sep 17 00:00:00 2001 From: Nathan Woodrow Date: Tue, 21 Aug 2012 07:42:22 +1000 Subject: [PATCH] Move expression text replace to QgsExpression - Update actions, html map tips, and annotations --- python/core/qgsexpression.sip | 11 ++++ src/core/qgsattributeaction.cpp | 2 +- src/core/qgsexpression.cpp | 52 +++++++++++++++++ src/core/qgsexpression.h | 12 ++++ src/gui/qgshtmlannotationitem.cpp | 95 +++++++++---------------------- src/gui/qgsmaptip.cpp | 46 +-------------- 6 files changed, 103 insertions(+), 115 deletions(-) diff --git a/python/core/qgsexpression.sip b/python/core/qgsexpression.sip index 38204c3e5582..240bc7b35c23 100644 --- a/python/core/qgsexpression.sip +++ b/python/core/qgsexpression.sip @@ -50,6 +50,17 @@ public: //! (used by internal functions) QgsDistanceArea* geomCalculator(); + /** This function currently replaces each expression between [% and %] + in the string with the result of its evaluation on the feature + passed as argument. + + Additional substitutions can be passed through the substitutionMap + parameter + */ + static QString replaceExpressionText( QString action, QgsFeature &feat, + QgsVectorLayer* layer, + const QMap *substitutionMap = 0); + // enum UnaryOperator diff --git a/src/core/qgsattributeaction.cpp b/src/core/qgsattributeaction.cpp index 71a4ee3f0df8..b46674e4cc12 100644 --- a/src/core/qgsattributeaction.cpp +++ b/src/core/qgsattributeaction.cpp @@ -96,7 +96,7 @@ void QgsAttributeAction::doAction( int index, QgsFeature &feat, return; // search for expressions while expanding actions - QString expandedAction = expandAction( action.action(), feat, substitutionMap ); + QString expandedAction = QgsExpression::replaceExpressionText( action.action(), feat, mLayer , substitutionMap ); if ( expandedAction.isEmpty() ) return; diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index c59cf777568e..86207eea3462 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -999,6 +999,7 @@ QString QgsExpression::dump() const return mRootNode->dump(); } + void QgsExpression::toOgcFilter( QDomDocument &doc, QDomElement &element ) const { if ( !mRootNode ) @@ -1048,6 +1049,57 @@ void QgsExpression::acceptVisitor( QgsExpression::Visitor& v ) mRootNode->accept( v ); } +QString QgsExpression::replaceExpressionText( QString action, QgsFeature &feat, + QgsVectorLayer* layer, + const QMap *substitutionMap ) +{ + QString expr_action; + + int index = 0; + while ( index < action.size() ) + { + QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" ); + + int pos = rx.indexIn( action, index ); + if ( pos < 0 ) + break; + + int start = index; + index = pos + rx.matchedLength(); + + QString to_replace = rx.cap( 1 ).trimmed(); + QgsDebugMsg( "Found expression: " + to_replace ); + + if ( substitutionMap && substitutionMap->contains( to_replace ) ) + { + expr_action += action.mid( start, pos - start ) + substitutionMap->value( to_replace ).toString(); + continue; + } + + QgsExpression exp( to_replace ); + if ( exp.hasParserError() ) + { + QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() ); + expr_action += action.mid( start, index - start ); + continue; + } + + QVariant result = exp.evaluate( &feat, layer->pendingFields() ); + if ( exp.hasEvalError() ) + { + QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() ); + expr_action += action.mid( start, index - start ); + continue; + } + + QgsDebugMsg( "Expression result is: " + result.toString() ); + expr_action += action.mid( start, pos - start ) + result.toString(); + } + + expr_action += action.mid( index ); + return expr_action; +} + QgsExpression::Node* QgsExpression::Node::createFromOgcFilter( QDomElement &element, QString &errorMessage ) { diff --git a/src/core/qgsexpression.h b/src/core/qgsexpression.h index 1e488046f1e4..cece66124a4a 100644 --- a/src/core/qgsexpression.h +++ b/src/core/qgsexpression.h @@ -23,6 +23,7 @@ #include #include "qgsfield.h" +#include "qgsvectorlayer.h" class QgsDistanceArea; class QgsFeature; @@ -132,6 +133,17 @@ class CORE_EXPORT QgsExpression //! (used by internal functions) QgsDistanceArea* geomCalculator() { if ( !mCalc ) initGeomCalculator(); return mCalc; } + /** This function currently replaces each expression between [% and %] + in the string with the result of its evaluation on the feature + passed as argument. + + Additional substitutions can be passed through the substitutionMap + parameter + */ + static QString replaceExpressionText( QString action, QgsFeature &feat, + QgsVectorLayer* layer, + const QMap *substitutionMap = 0 ); + // enum UnaryOperator diff --git a/src/gui/qgshtmlannotationitem.cpp b/src/gui/qgshtmlannotationitem.cpp index f837688f293b..b04a5aa7bdf2 100644 --- a/src/gui/qgshtmlannotationitem.cpp +++ b/src/gui/qgshtmlannotationitem.cpp @@ -42,7 +42,7 @@ QgsHtmlAnnotationItem::QgsHtmlAnnotationItem( QgsMapCanvas* canvas, QgsVectorLay mWidgetContainer = new QGraphicsProxyWidget( this ); mWidgetContainer->setWidget( mWebView ); - QObject::connect( mWebView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javascript())); + QObject::connect( mWebView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared() ), this, SLOT( javascript() ) ); if ( mVectorLayer && mMapCanvas ) { @@ -86,36 +86,36 @@ void QgsHtmlAnnotationItem::setMapPosition( const QgsPoint& pos ) void QgsHtmlAnnotationItem::paint( QPainter * painter ) { - Q_UNUSED( painter ); + Q_UNUSED( painter ); } void QgsHtmlAnnotationItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) { Q_UNUSED( option ); Q_UNUSED( widget ); - if ( !painter || !mWidgetContainer ) - { - return; - } + if ( !painter || !mWidgetContainer ) + { + return; + } - drawFrame( painter ); - if ( mMapPositionFixed ) - { - drawMarkerSymbol( painter ); - } + drawFrame( painter ); + if ( mMapPositionFixed ) + { + drawMarkerSymbol( painter ); + } - mWidgetContainer->setGeometry( QRectF( mOffsetFromReferencePoint.x() + mFrameBorderWidth / 2.0, mOffsetFromReferencePoint.y() - + mFrameBorderWidth / 2.0, mFrameSize.width() - mFrameBorderWidth, mFrameSize.height() - - mFrameBorderWidth ) ); - if (data(1).toString() == "composer") - { - mWidgetContainer->widget()->render( painter, mOffsetFromReferencePoint.toPoint()); - } + mWidgetContainer->setGeometry( QRectF( mOffsetFromReferencePoint.x() + mFrameBorderWidth / 2.0, mOffsetFromReferencePoint.y() + + mFrameBorderWidth / 2.0, mFrameSize.width() - mFrameBorderWidth, mFrameSize.height() + - mFrameBorderWidth ) ); + if ( data( 1 ).toString() == "composer" ) + { + mWidgetContainer->widget()->render( painter, mOffsetFromReferencePoint.toPoint() ); + } - if ( isSelected() ) - { - drawSelectionBoxes( painter ); - } + if ( isSelected() ) + { + drawSelectionBoxes( painter ); + } } QSizeF QgsHtmlAnnotationItem::minimumFrameSize() const @@ -167,7 +167,7 @@ void QgsHtmlAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& } mHasAssociatedFeature = itemElem.attribute( "hasFeature", "0" ).toInt(); mFeatureId = itemElem.attribute( "feature", "0" ).toInt(); - mHtmlFile = itemElem.attribute( "htmlfile", ""); + mHtmlFile = itemElem.attribute( "htmlfile", "" ); QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" ); if ( !annotationElem.isNull() ) { @@ -210,7 +210,7 @@ void QgsHtmlAnnotationItem::setFeatureForMapPosition() mFeatureId = currentFeatureId; mFeature = currentFeature; - QString newtext = replaceText( mHtmlSource, vectorLayer(), mFeature ); + QString newtext = QgsExpression::replaceExpressionText( mHtmlSource, mFeature, vectorLayer() ); mWebView->setHtml( newtext ); } @@ -227,51 +227,8 @@ void QgsHtmlAnnotationItem::updateVisibility() void QgsHtmlAnnotationItem::javascript() { - QWebFrame *frame = mWebView->page()->mainFrame(); - frame->addToJavaScriptWindowObject("canvas", mMapCanvas ); -} - -QString QgsHtmlAnnotationItem::replaceText( QString displayText, QgsVectorLayer *layer, QgsFeature &feat ) -{ - QString expr_action; - - int index = 0; - while ( index < displayText.size() ) - { - QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" ); - - int pos = rx.indexIn( displayText, index ); - if ( pos < 0 ) - break; - - int start = index; - index = pos + rx.matchedLength(); - - QString to_replace = rx.cap( 1 ).trimmed(); - QgsDebugMsg( "Found expression: " + to_replace ); - - QgsExpression exp( to_replace ); - if ( exp.hasParserError() ) - { - QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() ); - expr_action += displayText.mid( start, index - start ); - continue; - } - - QVariant result = exp.evaluate( &feat, layer->pendingFields() ); - if ( exp.hasEvalError() ) - { - QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() ); - expr_action += displayText.mid( start, index - start ); - continue; - } - - QgsDebugMsg( "Expression result is: " + result.toString() ); - expr_action += displayText.mid( start, pos - start ) + result.toString(); - } - - expr_action += displayText.mid( index ); - return expr_action; + QWebFrame *frame = mWebView->page()->mainFrame(); + frame->addToJavaScriptWindowObject( "canvas", mMapCanvas ); } diff --git a/src/gui/qgsmaptip.cpp b/src/gui/qgsmaptip.cpp index 9034295aab81..cf91845dfca5 100644 --- a/src/gui/qgsmaptip.cpp +++ b/src/gui/qgsmaptip.cpp @@ -101,51 +101,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsM int idx = vlayer->fieldNameIndex( vlayer->displayField() ); if ( idx < 0 ) - return replaceText( vlayer->displayField(), vlayer, feature ); + return QgsExpression::replaceExpressionText( vlayer->displayField(), feature, vlayer ); else return feature.attributeMap().value( idx, "" ).toString(); - -} - -QString QgsMapTip::replaceText( QString displayText, QgsVectorLayer *layer, QgsFeature &feat ) -{ - QString expr_action; - - int index = 0; - while ( index < displayText.size() ) - { - QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" ); - - int pos = rx.indexIn( displayText, index ); - if ( pos < 0 ) - break; - - int start = index; - index = pos + rx.matchedLength(); - - QString to_replace = rx.cap( 1 ).trimmed(); - QgsDebugMsg( "Found expression: " + to_replace ); - - QgsExpression exp( to_replace ); - if ( exp.hasParserError() ) - { - QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() ); - expr_action += displayText.mid( start, index - start ); - continue; - } - - QVariant result = exp.evaluate( &feat, layer->pendingFields() ); - if ( exp.hasEvalError() ) - { - QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() ); - expr_action += displayText.mid( start, index - start ); - continue; - } - - QgsDebugMsg( "Expression result is: " + result.toString() ); - expr_action += displayText.mid( start, pos - start ) + result.toString(); - } - - expr_action += displayText.mid( index ); - return expr_action; }