Skip to content
Permalink
Browse files

Move expression text replace to QgsExpression

    - Update actions, html map tips, and annotations
  • Loading branch information
NathanW2 committed Sep 2, 2012
1 parent 7298e73 commit a61a15bb1e90abe667e3f0d2e9bf4bc076f5b46a
@@ -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<QString, QVariant> *substitutionMap = 0);

//

enum UnaryOperator
@@ -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;

@@ -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<QString, QVariant> *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 )
{
@@ -23,6 +23,7 @@
#include <QDomDocument>

#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<QString, QVariant> *substitutionMap = 0 );

//

enum UnaryOperator
@@ -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 );
}


@@ -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;
}

0 comments on commit a61a15b

Please sign in to comment.
You can’t perform that action at this time.