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,52 @@ static QVariant fcnFormatDate( const QVariantList& values, QgsFeature*, QgsExpre
975
979
return dt.toString ( format );
976
980
}
977
981
982
+
983
+ static QVariant fncColorFromName ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
984
+ {
985
+ QString name = getStringValue ( values.at ( 0 ), parent );
986
+ QColor color = QColor ( name );
987
+ if ( ! color.isValid () )
988
+ {
989
+ parent->setEvalErrorString ( QObject::tr ( " Cannot convert '%1' to double" ).arg ( name ) );
990
+ color = QColor ( 0 , 0 , 0 );
991
+ }
992
+ return QgsSymbolLayerV2Utils::encodeColor ( color );
993
+ }
994
+
995
+ static QVariant fncColorRgb ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
996
+ {
997
+ int red = getIntValue ( values.at ( 0 ), parent );
998
+ int green = getIntValue ( values.at ( 1 ), parent );
999
+ int blue = getIntValue ( values.at ( 2 ), parent );
1000
+ QColor color = QColor ( red, green, blue );
1001
+ return QgsSymbolLayerV2Utils::encodeColor ( color );
1002
+ }
1003
+
1004
+
1005
+ static QVariant fncColorRgba ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
1006
+ {
1007
+ int red = getIntValue ( values.at ( 0 ), parent );
1008
+ int green = getIntValue ( values.at ( 1 ), parent );
1009
+ int blue = getIntValue ( values.at ( 2 ), parent );
1010
+ int alpha = getIntValue ( values.at ( 3 ), parent );
1011
+ QColor color = QColor ( red, green, blue, alpha );
1012
+ return QgsSymbolLayerV2Utils::encodeColor ( color );
1013
+ }
1014
+
1015
+ QVariant fncColorFromRamp ( const QVariantList &values, QgsFeature *, QgsExpression *parent )
1016
+ {
1017
+ QString rampName = getStringValue ( values.at ( 0 ), parent );
1018
+ const QgsVectorColorRampV2 *mRamp = QgsStyleV2::defaultStyle ()->colorRampRef ( rampName );
1019
+ if ( ! mRamp )
1020
+ {
1021
+ parent->setEvalErrorString ( QObject::tr ( " \" %1\" is not a valid color ramp" ).arg ( rampName ) );
1022
+ return QgsSymbolLayerV2Utils::encodeColor ( QColor ( 0 , 0 , 0 ) );
1023
+ }
1024
+ double value = getDoubleValue ( values.at ( 1 ), parent );
1025
+ return QgsSymbolLayerV2Utils::encodeColor ( mRamp ->color ( value ) );
1026
+ }
1027
+
978
1028
static QVariant fcnSpecialColumn ( const QVariantList& values, QgsFeature* /* f*/ , QgsExpression* parent )
979
1029
{
980
1030
QString varName = getStringValue ( values.at ( 0 ), parent );
@@ -1008,13 +1058,15 @@ bool QgsExpression::unregisterFunction( QString name )
1008
1058
return false ;
1009
1059
}
1010
1060
1061
+
1062
+
1011
1063
QStringList QgsExpression::gmBuiltinFunctions;
1012
1064
1013
1065
const QStringList &QgsExpression::BuiltinFunctions ()
1014
1066
{
1015
1067
if ( gmBuiltinFunctions.isEmpty () )
1016
1068
{
1017
- gmBuiltinFunctions << " sqrt "
1069
+ gmBuiltinFunctions
1018
1070
<< " sqrt" << " cos" << " sin" << " tan"
1019
1071
<< " asin" << " acos" << " atan" << " atan2"
1020
1072
<< " exp" << " ln" << " log10" << " log"
@@ -1025,8 +1077,10 @@ const QStringList &QgsExpression::BuiltinFunctions()
1025
1077
<< " minute" << " second" << " lower" << " upper"
1026
1078
<< " title" << " length" << " replace" << " regexp_replace"
1027
1079
<< " substr" << " concat" << " strpos" << " left"
1028
- << " right" << " rpad" << " lpad" << " format_number"
1029
- << " format_date" << " xat" << " yat" << " $area"
1080
+ << " right" << " rpad" << " lpad"
1081
+ << " format_number" << " format_date"
1082
+ << " color_rgb" << " color_rgba" << " color_from_name" << " color_from_ramp"
1083
+ << " xat" << " yat" << " $area"
1030
1084
<< " $length" << " $perimeter" << " $x" << " $y"
1031
1085
<< " $rownum" << " $id" << " $scale" << " _specialcol_" ;
1032
1086
}
@@ -1087,6 +1141,10 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
1087
1141
<< new StaticFunction ( " format" , -1 , fcnFormatString, QObject::tr ( " String" ) )
1088
1142
<< new StaticFunction ( " format_number" , 2 , fcnFormatNumber, QObject::tr ( " String" ) )
1089
1143
<< new StaticFunction ( " format_date" , 2 , fcnFormatDate, QObject::tr ( " String" ) )
1144
+ << new StaticFunction ( " color_rgb" , 3 , fncColorRgb, QObject::tr ( " Color" ), QObject::tr ( " Usage: color_rgb(red,green,blue) - rgb values from 0 to 255" ) )
1145
+ << new StaticFunction ( " color_rgba" , 4 , fncColorRgba, QObject::tr ( " Color" ), QObject::tr ( " Usage: color_rgba(red,green,blue,alpha) - rgba values from 0 to 255\" " ) )
1146
+ << new StaticFunction ( " color_from_name" , 1 , fncColorFromName, QObject::tr ( " Color" ), QObject::tr ( " Usage: color_from_name(name) - examples \" #FF0000\" , \" red\" " ) )
1147
+ << new StaticFunction ( " color_from_ramp" , 1 , fncColorFromRamp, QObject::tr ( " Color" ), QObject::tr ( " Usage: color_from_ramp(name) - type=ramp name, value=0..1" ) )
1090
1148
<< new StaticFunction ( " xat" , 1 , fcnXat, QObject::tr ( " Geometry" ), " " , true )
1091
1149
<< new StaticFunction ( " yat" , 1 , fcnYat, QObject::tr ( " Geometry" ), " " , true )
1092
1150
<< new StaticFunction ( " $area" , 0 , fcnGeomArea, QObject::tr ( " Geometry" ), " " , true )
0 commit comments