Skip to content

Commit

Permalink
[composer] Fix area calculation in expressions with OTF (fix #9791)
Browse files Browse the repository at this point in the history
Adds an argument to QgsExpression::replaceExpressionText to allow
passing an optional QgsDistanceArea to use during calculations.
Ensure that html and label composer items correctly specify this
argument.
  • Loading branch information
nyalldawson committed Oct 19, 2014
1 parent fad44d0 commit 0cb9854
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 13 deletions.
13 changes: 11 additions & 2 deletions python/core/qgsexpression.sip
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,26 @@ class QgsExpression
//! Sets the geometry calculator used in evaluation of expressions,
// instead of the default.
void setGeomCalculator( const QgsDistanceArea &calc );

/** 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
@param action
@param feat
@param layer
@param substitutionMap
@param distanceArea optional QgsDistanceArea. If specified, the QgsDistanceArea is used for distance
and area conversion
*/
static QString replaceExpressionText( const QString &action, const QgsFeature *feat,
QgsVectorLayer *layer,
const QMap<QString, QVariant> *substitutionMap = 0 );
const QMap<QString, QVariant> *substitutionMap = 0,
const QgsDistanceArea* distanceArea = 0
);

enum UnaryOperator
{
uoNot,
Expand Down
25 changes: 24 additions & 1 deletion src/core/composer/qgscomposerhtml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "qgsexpression.h"
#include "qgslogger.h"
#include "qgsnetworkcontentfetcher.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"

#include <QCoreApplication>
#include <QPainter>
Expand All @@ -42,9 +44,11 @@ QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands )
, mMaxBreakDistance( 10 )
, mExpressionFeature( 0 )
, mExpressionLayer( 0 )
, mDistanceArea( 0 )
, mEnableUserStylesheet( false )
, mFetcher( 0 )
{
mDistanceArea = new QgsDistanceArea();
mHtmlUnitsToMM = htmlUnitsToMM();
mWebPage = new QWebPage();
mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
Expand Down Expand Up @@ -92,14 +96,17 @@ QgsComposerHtml::QgsComposerHtml()
, mMaxBreakDistance( 10 )
, mExpressionFeature( 0 )
, mExpressionLayer( 0 )
, mDistanceArea( 0 )
, mFetcher( 0 )
{
mDistanceArea = new QgsDistanceArea();
mFetcher = new QgsNetworkContentFetcher();
connect( mFetcher, SIGNAL( finished() ), this, SLOT( frameLoaded() ) );
}

QgsComposerHtml::~QgsComposerHtml()
{
delete mDistanceArea;
delete mWebPage;
delete mRenderedPage;
mFetcher->deleteLater();
Expand Down Expand Up @@ -179,7 +186,7 @@ void QgsComposerHtml::loadHtml()
//evaluate expressions
if ( mEvaluateExpressions )
{
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, mExpressionFeature, mExpressionLayer );
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, mExpressionFeature, mExpressionLayer, 0, mDistanceArea );
}

mLoaded = false;
Expand Down Expand Up @@ -522,6 +529,22 @@ void QgsComposerHtml::setExpressionContext( QgsFeature* feature, QgsVectorLayer*
{
mExpressionFeature = feature;
mExpressionLayer = layer;

//setup distance area conversion
if ( layer )
{
mDistanceArea->setSourceCrs( layer->crs().srsid() );
}
else if ( mComposition )
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
}
if ( mComposition )
{
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
}
mDistanceArea->setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
}

void QgsComposerHtml::refreshExpressionContext()
Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposerhtml.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class QImage;
class QgsFeature;
class QgsVectorLayer;
class QgsNetworkContentFetcher;
class QgsDistanceArea;

class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
{
Expand Down Expand Up @@ -242,6 +243,8 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame

QgsFeature* mExpressionFeature;
QgsVectorLayer* mExpressionLayer;
QgsDistanceArea* mDistanceArea;

QString mUserStylesheet;
bool mEnableUserStylesheet;

Expand Down
40 changes: 34 additions & 6 deletions src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "qgsexpression.h"
#include "qgsnetworkaccessmanager.h"
#include "qgscomposermodel.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"

#include <QCoreApplication>
#include <QDate>
Expand All @@ -32,12 +34,20 @@
#include <QWebPage>
#include <QEventLoop>

QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ):
QgsComposerItem( composition ), mHtmlState( 0 ), mHtmlUnitsToMM( 1.0 ),
mHtmlLoaded( false ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ),
mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop ),
mExpressionFeature( 0 ), mExpressionLayer( 0 )
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
: QgsComposerItem( composition )
, mHtmlState( 0 )
, mHtmlUnitsToMM( 1.0 )
, mHtmlLoaded( false )
, mMargin( 1.0 )
, mFontColor( QColor( 0, 0, 0 ) )
, mHAlignment( Qt::AlignLeft )
, mVAlignment( Qt::AlignTop )
, mExpressionFeature( 0 )
, mExpressionLayer( 0 )
, mDistanceArea( 0 )
{
mDistanceArea = new QgsDistanceArea();
mHtmlUnitsToMM = htmlUnitsToMM();

//get default composer font from settings
Expand Down Expand Up @@ -71,6 +81,7 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ):

QgsComposerLabel::~QgsComposerLabel()
{
delete mDistanceArea;
}

void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
Expand Down Expand Up @@ -221,6 +232,23 @@ void QgsComposerLabel::setExpressionContext( QgsFeature* feature, QgsVectorLayer
mExpressionFeature = feature;
mExpressionLayer = layer;
mSubstitutions = substitutions;

//setup distance area conversion
if ( layer )
{
mDistanceArea->setSourceCrs( layer->crs().srsid() );
}
else if ( mComposition )
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
}
if ( mComposition )
{
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
}
mDistanceArea->setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );

// Force label to redraw -- fixes label printing for labels with blend modes when used with atlas
update();
}
Expand Down Expand Up @@ -248,7 +276,7 @@ QString QgsComposerLabel::displayText() const
replaceDateText( displayText );
QMap<QString, QVariant> subs = mSubstitutions;
subs[ "$page" ] = QVariant(( int )mComposition->itemPageNumber( this ) + 1 );
return QgsExpression::replaceExpressionText( displayText, mExpressionFeature, mExpressionLayer, &subs );
return QgsExpression::replaceExpressionText( displayText, mExpressionFeature, mExpressionLayer, &subs, mDistanceArea );
}

void QgsComposerLabel::replaceDateText( QString& text ) const
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class QgsVectorLayer;
class QgsFeature;
class QgsDistanceArea;

/** \ingroup MapComposer
* A label that can be placed onto a map composition.
Expand Down Expand Up @@ -143,8 +144,7 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
QgsFeature* mExpressionFeature;
QgsVectorLayer* mExpressionLayer;
QMap<QString, QVariant> mSubstitutions;


QgsDistanceArea* mDistanceArea;
};

#endif
Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ void QgsExpression::acceptVisitor( QgsExpression::Visitor& v ) const

QString QgsExpression::replaceExpressionText( const QString &action, const QgsFeature *feat,
QgsVectorLayer *layer,
const QMap<QString, QVariant> *substitutionMap )
const QMap<QString, QVariant> *substitutionMap , const QgsDistanceArea *distanceArea )
{
QString expr_action;

Expand Down Expand Up @@ -2030,6 +2030,12 @@ QString QgsExpression::replaceExpressionText( const QString &action, const QgsFe
continue;
}

if ( distanceArea )
{
//if QgsDistanceArea specified for area/distance conversion, use it
exp.setGeomCalculator( *distanceArea );
}

QVariant result;
if ( layer )
{
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,18 @@ class CORE_EXPORT QgsExpression
Additional substitutions can be passed through the substitutionMap
parameter
@param action
@param feat
@param layer
@param substitutionMap
@param distanceArea optional QgsDistanceArea. If specified, the QgsDistanceArea is used for distance
and area conversion
*/
static QString replaceExpressionText( const QString &action, const QgsFeature *feat,
QgsVectorLayer *layer,
const QMap<QString, QVariant> *substitutionMap = 0 );
const QMap<QString, QVariant> *substitutionMap = 0,
const QgsDistanceArea* distanceArea = 0
);
enum UnaryOperator
{
uoNot,
Expand Down

0 comments on commit 0cb9854

Please sign in to comment.