@@ -787,6 +787,18 @@ static QVariant fcnRound( const QVariantList& values , QgsFeature *f, QgsExpress
787787 return QVariant ();
788788}
789789
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+
790802QList<QgsExpression::FunctionDef> QgsExpression::gmBuiltinFunctions;
791803
792804const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions ()
@@ -842,6 +854,7 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
842854 << FunctionDef ( " right" , 2 , fcnRight, QObject::tr ( " String" ) )
843855 << FunctionDef ( " rpad" , 3 , fcnRPad, QObject::tr ( " String" ) )
844856 << FunctionDef ( " lpad" , 3 , fcnLPad, QObject::tr ( " String" ) )
857+ << FunctionDef ( " format_number" , 2 , fcnFormatNumber, QObject::tr ( " String" ) )
845858
846859 // geometry accessors
847860 << FunctionDef ( " xat" , 1 , fcnXat, QObject::tr ( " Geometry" ), " " , true )
@@ -854,6 +867,7 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
854867 // special columns
855868 << FunctionDef ( " $rownum" , 0 , fcnRowNumber, QObject::tr ( " Record" ) )
856869 << FunctionDef ( " $id" , 0 , fcnFeatureId, QObject::tr ( " Record" ) )
870+ << FunctionDef ( " $scale" , 0 , fcnScale, QObject::tr ( " Record" ) )
857871 ;
858872 }
859873
@@ -887,7 +901,7 @@ QgsExpression::QgsExpression( const QString& expr )
887901 : mExpression( expr )
888902 , mRowNumber( 0 )
889903 , mScale( 0 )
890-
904+
891905{
892906 initGeomCalculator ();
893907
@@ -902,7 +916,6 @@ QgsExpression::QgsExpression( const QString& expr )
902916QgsExpression::~QgsExpression ()
903917{
904918 delete mRootNode ;
905- delete mCalc ;
906919}
907920
908921QStringList QgsExpression::referencedColumns ()
@@ -1041,6 +1054,56 @@ void QgsExpression::acceptVisitor( QgsExpression::Visitor& v )
10411054 mRootNode ->accept ( v );
10421055}
10431056
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+
10441107
10451108QgsExpression::Node* QgsExpression::Node::createFromOgcFilter ( QDomElement &element, QString &errorMessage )
10461109{
0 commit comments