Skip to content

Commit c62c396

Browse files
committedApr 15, 2013
WIP
1 parent 632bfbb commit c62c396

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed
 

‎src/core/qgsexpression.cpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QSettings>
2121
#include <QDate>
2222
#include <QRegExp>
23+
#include <QColor>
2324

2425
#include <math.h>
2526
#include <limits>
@@ -30,6 +31,9 @@
3031
#include "qgslogger.h"
3132
#include "qgsogcutils.h"
3233
#include "qgsvectorlayer.h"
34+
#include "qgssymbollayerv2utils.h"
35+
#include "qgsvectorcolorrampv2.h"
36+
#include "qgsstylev2.h"
3337

3438
// from parser
3539
extern QgsExpression::Node* parseExpression( const QString& str, QString& parserErrorMsg );
@@ -975,6 +979,52 @@ static QVariant fcnFormatDate( const QVariantList& values, QgsFeature*, QgsExpre
975979
return dt.toString( format );
976980
}
977981

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+
9781028
static QVariant fcnSpecialColumn( const QVariantList& values, QgsFeature* /*f*/, QgsExpression* parent )
9791029
{
9801030
QString varName = getStringValue( values.at( 0 ), parent );
@@ -1008,13 +1058,15 @@ bool QgsExpression::unregisterFunction( QString name )
10081058
return false;
10091059
}
10101060

1061+
1062+
10111063
QStringList QgsExpression::gmBuiltinFunctions;
10121064

10131065
const QStringList &QgsExpression::BuiltinFunctions()
10141066
{
10151067
if ( gmBuiltinFunctions.isEmpty() )
10161068
{
1017-
gmBuiltinFunctions << "sqrt"
1069+
gmBuiltinFunctions
10181070
<< "sqrt" << "cos" << "sin" << "tan"
10191071
<< "asin" << "acos" << "atan" << "atan2"
10201072
<< "exp" << "ln" << "log10" << "log"
@@ -1025,8 +1077,10 @@ const QStringList &QgsExpression::BuiltinFunctions()
10251077
<< "minute" << "second" << "lower" << "upper"
10261078
<< "title" << "length" << "replace" << "regexp_replace"
10271079
<< "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"
10301084
<< "$length" << "$perimeter" << "$x" << "$y"
10311085
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
10321086
}
@@ -1087,6 +1141,10 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
10871141
<< new StaticFunction( "format", -1, fcnFormatString, QObject::tr( "String" ) )
10881142
<< new StaticFunction( "format_number", 2, fcnFormatNumber, QObject::tr( "String" ) )
10891143
<< 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" ) )
10901148
<< new StaticFunction( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
10911149
<< new StaticFunction( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
10921150
<< new StaticFunction( "$area", 0, fcnGeomArea, QObject::tr( "Geometry" ), "", true )

0 commit comments

Comments
 (0)
Please sign in to comment.