Skip to content

Commit

Permalink
Move expression text replace to QgsExpression
Browse files Browse the repository at this point in the history
    - Update actions, html map tips, and annotations
  • Loading branch information
NathanW2 committed Sep 2, 2012
1 parent 7298e73 commit a61a15b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 115 deletions.
11 changes: 11 additions & 0 deletions python/core/qgsexpression.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsattributeaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
52 changes: 52 additions & 0 deletions src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ QString QgsExpression::dump() const
return mRootNode->dump();
}


void QgsExpression::toOgcFilter( QDomDocument &doc, QDomElement &element ) const
{
if ( !mRootNode )
Expand Down Expand Up @@ -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 )
{
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QDomDocument>

#include "qgsfield.h"
#include "qgsvectorlayer.h"

class QgsDistanceArea;
class QgsFeature;
Expand Down Expand Up @@ -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
Expand Down
95 changes: 26 additions & 69 deletions src/gui/qgshtmlannotationitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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 );

}
Expand All @@ -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 );
}


Expand Down
46 changes: 1 addition & 45 deletions src/gui/qgsmaptip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.