28
28
#include " qgscoordinatereferencesystem.h"
29
29
#include " qgslogger.h"
30
30
#include " qgsfontutils.h"
31
+ #include " qgsexpressioncontext.h"
31
32
32
33
#include < QPainter>
33
34
#include < QPen>
@@ -296,6 +297,7 @@ bool QgsComposerMapGrid::writeXML( QDomElement& elem, QDomDocument& doc ) const
296
297
297
298
mapGridElem.setAttribute ( " annotationFormat" , mGridAnnotationFormat );
298
299
mapGridElem.setAttribute ( " showAnnotation" , mShowGridAnnotation );
300
+ mapGridElem.setAttribute ( " annotationExpression" , mGridAnnotationExpressionString );
299
301
mapGridElem.setAttribute ( " leftAnnotationDisplay" , mLeftGridAnnotationDisplay );
300
302
mapGridElem.setAttribute ( " rightAnnotationDisplay" , mRightGridAnnotationDisplay );
301
303
mapGridElem.setAttribute ( " topAnnotationDisplay" , mTopGridAnnotationDisplay );
@@ -395,6 +397,8 @@ bool QgsComposerMapGrid::readXML( const QDomElement& itemElem, const QDomDocumen
395
397
// annotation
396
398
mShowGridAnnotation = ( itemElem.attribute ( " showAnnotation" , " 0" ) != " 0" );
397
399
mGridAnnotationFormat = QgsComposerMapGrid::AnnotationFormat ( itemElem.attribute ( " annotationFormat" , " 0" ).toInt () );
400
+ mGridAnnotationExpressionString = itemElem.attribute ( " annotationExpression" );
401
+ mGridAnnotationExpression .reset ();
398
402
mLeftGridAnnotationPosition = QgsComposerMapGrid::AnnotationPosition ( itemElem.attribute ( " leftAnnotationPosition" , " 0" ).toInt () );
399
403
mRightGridAnnotationPosition = QgsComposerMapGrid::AnnotationPosition ( itemElem.attribute ( " rightAnnotationPosition" , " 0" ).toInt () );
400
404
mTopGridAnnotationPosition = QgsComposerMapGrid::AnnotationPosition ( itemElem.attribute ( " topAnnotationPosition" , " 0" ).toInt () );
@@ -689,7 +693,7 @@ void QgsComposerMapGrid::draw( QPainter* p )
689
693
690
694
if ( mShowGridAnnotation )
691
695
{
692
- drawCoordinateAnnotations ( p, horizontalLines, verticalLines );
696
+ drawCoordinateAnnotations ( p, horizontalLines, verticalLines, context. expressionContext () );
693
697
}
694
698
}
695
699
@@ -1037,7 +1041,7 @@ void QgsComposerMapGrid::drawGridFrameLineBorder( QPainter* p, QgsComposerMapGri
1037
1041
}
1038
1042
}
1039
1043
1040
- void QgsComposerMapGrid::drawCoordinateAnnotations ( QPainter* p, const QList< QPair< double , QLineF > >& hLines, const QList< QPair< double , QLineF > >& vLines ) const
1044
+ void QgsComposerMapGrid::drawCoordinateAnnotations ( QPainter* p, const QList< QPair< double , QLineF > >& hLines, const QList< QPair< double , QLineF > >& vLines, QgsExpressionContext &expressionContext ) const
1041
1045
{
1042
1046
if ( !p )
1043
1047
{
@@ -1048,15 +1052,15 @@ void QgsComposerMapGrid::drawCoordinateAnnotations( QPainter* p, const QList< QP
1048
1052
QList< QPair< double , QLineF > >::const_iterator it = hLines.constBegin ();
1049
1053
for ( ; it != hLines.constEnd (); ++it )
1050
1054
{
1051
- currentAnnotationString = gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude );
1055
+ currentAnnotationString = gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude, expressionContext );
1052
1056
drawCoordinateAnnotation ( p, it->second .p1 (), currentAnnotationString, QgsComposerMapGrid::Latitude );
1053
1057
drawCoordinateAnnotation ( p, it->second .p2 (), currentAnnotationString, QgsComposerMapGrid::Latitude );
1054
1058
}
1055
1059
1056
1060
it = vLines.constBegin ();
1057
1061
for ( ; it != vLines.constEnd (); ++it )
1058
1062
{
1059
- currentAnnotationString = gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude );
1063
+ currentAnnotationString = gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude, expressionContext );
1060
1064
drawCoordinateAnnotation ( p, it->second .p1 (), currentAnnotationString, QgsComposerMapGrid::Longitude );
1061
1065
drawCoordinateAnnotation ( p, it->second .p2 (), currentAnnotationString, QgsComposerMapGrid::Longitude );
1062
1066
}
@@ -1369,7 +1373,7 @@ void QgsComposerMapGrid::drawAnnotation( QPainter* p, const QPointF& pos, int ro
1369
1373
p->restore ();
1370
1374
}
1371
1375
1372
- QString QgsComposerMapGrid::gridAnnotationString ( double value, QgsComposerMapGrid::AnnotationCoordinate coord ) const
1376
+ QString QgsComposerMapGrid::gridAnnotationString ( double value, QgsComposerMapGrid::AnnotationCoordinate coord, QgsExpressionContext &expressionContext ) const
1373
1377
{
1374
1378
// check if we are using degrees (ie, geographic crs)
1375
1379
bool geographic = false ;
@@ -1432,6 +1436,17 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
1432
1436
return QString::number ( qAbs ( value ), ' f' , mGridAnnotationPrecision ) + hemisphere;
1433
1437
}
1434
1438
}
1439
+ else if ( mGridAnnotationFormat == CustomFormat )
1440
+ {
1441
+ expressionContext.lastScope ()->setVariable ( " grid_number" , value );
1442
+ expressionContext.lastScope ()->setVariable ( " grid_axis" , coord == QgsComposerMapGrid::Longitude ? " x" : " y" );
1443
+ if ( !mGridAnnotationExpression .data () )
1444
+ {
1445
+ mGridAnnotationExpression .reset ( new QgsExpression ( mGridAnnotationExpressionString ) );
1446
+ mGridAnnotationExpression ->prepare ( &expressionContext );
1447
+ }
1448
+ return mGridAnnotationExpression ->evaluate ( &expressionContext ).toString ();
1449
+ }
1435
1450
1436
1451
QgsPoint p;
1437
1452
p.setX ( coord == QgsComposerMapGrid::Longitude ? value : 0 );
@@ -2008,6 +2023,9 @@ double QgsComposerMapGrid::maxExtension() const
2008
2023
}
2009
2024
2010
2025
const QgsMapSettings& ms = mComposerMap ->composition ()->mapSettings ();
2026
+
2027
+ QScopedPointer< QgsExpressionContext> expressionContext ( createExpressionContext () );
2028
+
2011
2029
QStringList coordStrings;
2012
2030
if ( mCRS .isValid () && mCRS != ms.destinationCrs () )
2013
2031
{
@@ -2030,12 +2048,12 @@ double QgsComposerMapGrid::maxExtension() const
2030
2048
QList< QPair< double , QPolygonF > >::const_iterator it = xGridLines.constBegin ();
2031
2049
for ( ; it != xGridLines.constEnd (); ++it )
2032
2050
{
2033
- coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude ) );
2051
+ coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude, *expressionContext ) );
2034
2052
}
2035
2053
it = yGridLines.constBegin ();
2036
2054
for ( ; it != yGridLines.constEnd (); ++it )
2037
2055
{
2038
- coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude ) );
2056
+ coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude, *expressionContext ) );
2039
2057
}
2040
2058
}
2041
2059
else
@@ -2052,13 +2070,13 @@ double QgsComposerMapGrid::maxExtension() const
2052
2070
QList< QPair< double , QLineF > >::const_iterator it = xLines.constBegin ();
2053
2071
for ( ; it != xLines.constEnd (); ++it )
2054
2072
{
2055
- coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude ) );
2073
+ coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Latitude, *expressionContext ) );
2056
2074
}
2057
2075
2058
2076
it = yLines.constBegin ();
2059
2077
for ( ; it != yLines.constEnd (); ++it )
2060
2078
{
2061
- coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude ) );
2079
+ coordStrings.append ( gridAnnotationString ( it->first , QgsComposerMapGrid::Longitude, *expressionContext ) );
2062
2080
}
2063
2081
}
2064
2082
@@ -2183,6 +2201,16 @@ QgsComposerMapGrid::FrameSideFlags QgsComposerMapGrid::frameSideFlags() const
2183
2201
return mGridFrameSides ;
2184
2202
}
2185
2203
2204
+ QgsExpressionContext* QgsComposerMapGrid::createExpressionContext () const
2205
+ {
2206
+ QgsExpressionContext* context = QgsComposerObject::createExpressionContext ();
2207
+ context->appendScope ( new QgsExpressionContextScope ( tr ( " Grid" ) ) );
2208
+ context->lastScope ()->setVariable ( " grid_number" , 0 );
2209
+ context->lastScope ()->setVariable ( " grid_axis" , " x" );
2210
+ context->setHighlightedVariables ( QStringList () << " grid_number" << " grid_axis" );
2211
+ return context;
2212
+ }
2213
+
2186
2214
bool QgsComposerMapGrid::testFrameSideFlag ( QgsComposerMapGrid::FrameSideFlag flag ) const
2187
2215
{
2188
2216
return mGridFrameSides .testFlag ( flag );
0 commit comments