@@ -787,6 +787,18 @@ static QVariant fcnRound( const QVariantList& values , QgsFeature *f, QgsExpress
787
787
return QVariant ();
788
788
}
789
789
790
+ static QVariant fcnScale ( const QVariantList&, QgsFeature*, QgsExpression* parent )
791
+ {
792
+ return QVariant ( parent->scale () );
793
+ }
794
+
795
+ static QVariant fcnFormatNumber ( const QVariantList& values, QgsFeature*, QgsExpression* parent )
796
+ {
797
+ double value = getDoubleValue ( values.at ( 0 ), parent );
798
+ int places = getIntValue ( values.at ( 1 ), parent );
799
+ return QString ( " %L1" ).arg ( value, 0 , ' f' , places );
800
+ }
801
+
790
802
QList<QgsExpression::FunctionDef> QgsExpression::gmBuiltinFunctions;
791
803
792
804
const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions ()
@@ -842,6 +854,7 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
842
854
<< FunctionDef ( " right" , 2 , fcnRight, QObject::tr ( " String" ) )
843
855
<< FunctionDef ( " rpad" , 3 , fcnRPad, QObject::tr ( " String" ) )
844
856
<< FunctionDef ( " lpad" , 3 , fcnLPad, QObject::tr ( " String" ) )
857
+ << FunctionDef ( " format_number" , 2 , fcnFormatNumber, QObject::tr ( " String" ) )
845
858
846
859
// geometry accessors
847
860
<< FunctionDef ( " xat" , 1 , fcnXat, QObject::tr ( " Geometry" ), " " , true )
@@ -854,6 +867,7 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
854
867
// special columns
855
868
<< FunctionDef ( " $rownum" , 0 , fcnRowNumber, QObject::tr ( " Record" ) )
856
869
<< FunctionDef ( " $id" , 0 , fcnFeatureId, QObject::tr ( " Record" ) )
870
+ << FunctionDef ( " $scale" , 0 , fcnScale, QObject::tr ( " Record" ) )
857
871
;
858
872
}
859
873
@@ -887,7 +901,7 @@ QgsExpression::QgsExpression( const QString& expr )
887
901
: mExpression( expr )
888
902
, mRowNumber( 0 )
889
903
, mScale( 0 )
890
-
904
+
891
905
{
892
906
initGeomCalculator ();
893
907
@@ -902,7 +916,6 @@ QgsExpression::QgsExpression( const QString& expr )
902
916
QgsExpression::~QgsExpression ()
903
917
{
904
918
delete mRootNode ;
905
- delete mCalc ;
906
919
}
907
920
908
921
QStringList QgsExpression::referencedColumns ()
@@ -1041,6 +1054,56 @@ void QgsExpression::acceptVisitor( QgsExpression::Visitor& v )
1041
1054
mRootNode ->accept ( v );
1042
1055
}
1043
1056
1057
+ QString QgsExpression::replaceExpressionText ( QString action, QgsFeature &feat,
1058
+ QgsVectorLayer* layer,
1059
+ const QMap<QString, QVariant> *substitutionMap )
1060
+ {
1061
+ QString expr_action;
1062
+
1063
+ int index = 0 ;
1064
+ while ( index < action.size () )
1065
+ {
1066
+ QRegExp rx = QRegExp ( " \\ [%([^\\ ]]+)%\\ ]" );
1067
+
1068
+ int pos = rx.indexIn ( action, index );
1069
+ if ( pos < 0 )
1070
+ break ;
1071
+
1072
+ int start = index;
1073
+ index = pos + rx.matchedLength ();
1074
+ QString to_replace = rx.cap ( 1 ).trimmed ();
1075
+ QgsDebugMsg ( " Found expression: " + to_replace );
1076
+
1077
+ if ( substitutionMap && substitutionMap->contains ( to_replace ) )
1078
+ {
1079
+ expr_action += action.mid ( start, pos - start ) + substitutionMap->value ( to_replace ).toString ();
1080
+ continue ;
1081
+ }
1082
+
1083
+ QgsExpression exp ( to_replace );
1084
+ if ( exp.hasParserError () )
1085
+ {
1086
+ QgsDebugMsg ( " Expression parser error: " + exp.parserErrorString () );
1087
+ expr_action += action.mid ( start, index - start );
1088
+ continue ;
1089
+ }
1090
+
1091
+ QVariant result = exp.evaluate ( &feat, layer->pendingFields () );
1092
+ if ( exp.hasEvalError () )
1093
+ {
1094
+ QgsDebugMsg ( " Expression parser eval error: " + exp.evalErrorString () );
1095
+ expr_action += action.mid ( start, index - start );
1096
+ continue ;
1097
+ }
1098
+
1099
+ QgsDebugMsg ( " Expression result is: " + result.toString () );
1100
+ expr_action += action.mid ( start, pos - start ) + result.toString ();
1101
+ }
1102
+
1103
+ expr_action += action.mid ( index );
1104
+ return expr_action;
1105
+ }
1106
+
1044
1107
1045
1108
QgsExpression::Node* QgsExpression::Node::createFromOgcFilter ( QDomElement &element, QString &errorMessage )
1046
1109
{
0 commit comments