20
20
#include < QSettings>
21
21
#include < QDate>
22
22
#include < QRegExp>
23
+ #include < QColor>
23
24
24
25
#include < math.h>
25
26
#include < limits>
30
31
#include " qgslogger.h"
31
32
#include " qgsogcutils.h"
32
33
#include " qgsvectorlayer.h"
34
+ #include " qgssymbollayerv2utils.h"
35
+ #include " qgsvectorcolorrampv2.h"
36
+ #include " qgsstylev2.h"
33
37
34
38
// from parser
35
39
extern QgsExpression::Node* parseExpression ( const QString& str, QString& parserErrorMsg );
@@ -975,6 +979,50 @@ static QVariant fcnFormatDate( const QVariantList& values, QgsFeature*, QgsExpre
975
979
return dt.toString ( format );
976
980
}
977
981
982
+ static QVariant fcnColorRgb ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
983
+ {
984
+ int red = getIntValue ( values.at ( 0 ), parent );
985
+ int green = getIntValue ( values.at ( 1 ), parent );
986
+ int blue = getIntValue ( values.at ( 2 ), parent );
987
+ QColor color = QColor ( red, green, blue );
988
+ if ( ! color.isValid () )
989
+ {
990
+ parent->setEvalErrorString ( QObject::tr ( " Cannot convert '%1:%2:%3' to color" ).arg ( red ).arg ( green ).arg ( blue ) );
991
+ color = QColor ( 0 , 0 , 0 );
992
+ }
993
+
994
+ return color.name ();
995
+ }
996
+
997
+ static QVariant fncColorRgba ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
998
+ {
999
+ int red = getIntValue ( values.at ( 0 ), parent );
1000
+ int green = getIntValue ( values.at ( 1 ), parent );
1001
+ int blue = getIntValue ( values.at ( 2 ), parent );
1002
+ int alpha = getIntValue ( values.at ( 3 ), parent );
1003
+ QColor color = QColor ( red, green, blue, alpha );
1004
+ if ( ! color.isValid () )
1005
+ {
1006
+ parent->setEvalErrorString ( QObject::tr ( " Cannot convert '%1:%2:%3:%4' to color" ).arg ( red ).arg ( green ).arg ( blue ).arg ( alpha ) );
1007
+ color = QColor ( 0 , 0 , 0 );
1008
+ }
1009
+ return QgsSymbolLayerV2Utils::encodeColor ( color );
1010
+ }
1011
+
1012
+ QVariant fcnRampColor ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
1013
+ {
1014
+ QString rampName = getStringValue ( values.at ( 0 ), parent );
1015
+ const QgsVectorColorRampV2 *mRamp = QgsStyleV2::defaultStyle ()->colorRampRef ( rampName );
1016
+ if ( ! mRamp )
1017
+ {
1018
+ parent->setEvalErrorString ( QObject::tr ( " \" %1\" is not a valid color ramp" ).arg ( rampName ) );
1019
+ return QColor ( 0 , 0 , 0 ).name ();
1020
+ }
1021
+ double value = getDoubleValue ( values.at ( 1 ), parent );
1022
+ QColor color = mRamp ->color ( value );
1023
+ return color.name ();
1024
+ }
1025
+
978
1026
static QVariant fcnSpecialColumn ( const QVariantList& values, QgsFeature* /* f*/ , QgsExpression* parent )
979
1027
{
980
1028
QString varName = getStringValue ( values.at ( 0 ), parent );
@@ -1008,13 +1056,15 @@ bool QgsExpression::unregisterFunction( QString name )
1008
1056
return false ;
1009
1057
}
1010
1058
1059
+
1060
+
1011
1061
QStringList QgsExpression::gmBuiltinFunctions;
1012
1062
1013
1063
const QStringList &QgsExpression::BuiltinFunctions ()
1014
1064
{
1015
1065
if ( gmBuiltinFunctions.isEmpty () )
1016
1066
{
1017
- gmBuiltinFunctions << " sqrt "
1067
+ gmBuiltinFunctions
1018
1068
<< " sqrt" << " cos" << " sin" << " tan"
1019
1069
<< " asin" << " acos" << " atan" << " atan2"
1020
1070
<< " exp" << " ln" << " log10" << " log"
@@ -1025,8 +1075,10 @@ const QStringList &QgsExpression::BuiltinFunctions()
1025
1075
<< " minute" << " second" << " lower" << " upper"
1026
1076
<< " title" << " length" << " replace" << " regexp_replace"
1027
1077
<< " substr" << " concat" << " strpos" << " left"
1028
- << " right" << " rpad" << " lpad" << " format_number"
1029
- << " format_date" << " xat" << " yat" << " $area"
1078
+ << " right" << " rpad" << " lpad"
1079
+ << " format_number" << " format_date"
1080
+ << " color_rgb" << " color_rgba" << " ramp_color"
1081
+ << " xat" << " yat" << " $area"
1030
1082
<< " $length" << " $perimeter" << " $x" << " $y"
1031
1083
<< " $rownum" << " $id" << " $scale" << " _specialcol_" ;
1032
1084
}
@@ -1087,6 +1139,9 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
1087
1139
<< new StaticFunction ( " format" , -1 , fcnFormatString, QObject::tr ( " String" ) )
1088
1140
<< new StaticFunction ( " format_number" , 2 , fcnFormatNumber, QObject::tr ( " String" ) )
1089
1141
<< new StaticFunction ( " format_date" , 2 , fcnFormatDate, QObject::tr ( " String" ) )
1142
+ << new StaticFunction ( " color_rgb" , 3 , fcnColorRgb, QObject::tr ( " Color" ) )
1143
+ << new StaticFunction ( " color_rgba" , 4 , fncColorRgba, QObject::tr ( " Color" ) )
1144
+ << new StaticFunction ( " ramp_color" , 2 , fcnRampColor, QObject::tr ( " Color" ) )
1090
1145
<< new StaticFunction ( " xat" , 1 , fcnXat, QObject::tr ( " Geometry" ), " " , true )
1091
1146
<< new StaticFunction ( " yat" , 1 , fcnYat, QObject::tr ( " Geometry" ), " " , true )
1092
1147
<< new StaticFunction ( " $area" , 0 , fcnGeomArea, QObject::tr ( " Geometry" ), " " , true )
0 commit comments