Skip to content
Permalink
Browse files

WIP

  • Loading branch information
ccrook committed Apr 15, 2013
1 parent 632bfbb commit c62c396a5fbe14385a9b92db8a25f067a9ece599
Showing with 61 additions and 3 deletions.
  1. +61 −3 src/core/qgsexpression.cpp
@@ -20,6 +20,7 @@
#include <QSettings>
#include <QDate>
#include <QRegExp>
#include <QColor>

#include <math.h>
#include <limits>
@@ -30,6 +31,9 @@
#include "qgslogger.h"
#include "qgsogcutils.h"
#include "qgsvectorlayer.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgsstylev2.h"

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


static QVariant fncColorFromName( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
QString name = getStringValue( values.at( 0 ), parent );
QColor color = QColor( name );
if ( ! color.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to double" ).arg( name ) );
color = QColor( 0, 0, 0 );
}
return QgsSymbolLayerV2Utils::encodeColor( color );
}

static QVariant fncColorRgb( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
int red = getIntValue( values.at( 0 ), parent );
int green = getIntValue( values.at( 1 ), parent );
int blue = getIntValue( values.at( 2 ), parent );
QColor color = QColor( red, green, blue );
return QgsSymbolLayerV2Utils::encodeColor( color );
}


static QVariant fncColorRgba( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
int red = getIntValue( values.at( 0 ), parent );
int green = getIntValue( values.at( 1 ), parent );
int blue = getIntValue( values.at( 2 ), parent );
int alpha = getIntValue( values.at( 3 ), parent );
QColor color = QColor( red, green, blue, alpha );
return QgsSymbolLayerV2Utils::encodeColor( color );
}

QVariant fncColorFromRamp( const QVariantList &values, QgsFeature *, QgsExpression *parent )
{
QString rampName = getStringValue( values.at( 0 ), parent );
const QgsVectorColorRampV2 *mRamp = QgsStyleV2::defaultStyle()->colorRampRef( rampName );
if ( ! mRamp )
{
parent->setEvalErrorString( QObject::tr( "\"%1\" is not a valid color ramp" ).arg( rampName ) );
return QgsSymbolLayerV2Utils::encodeColor( QColor( 0, 0, 0 ) );
}
double value = getDoubleValue( values.at( 1 ), parent );
return QgsSymbolLayerV2Utils::encodeColor( mRamp->color( value ) );
}

static QVariant fcnSpecialColumn( const QVariantList& values, QgsFeature* /*f*/, QgsExpression* parent )
{
QString varName = getStringValue( values.at( 0 ), parent );
@@ -1008,13 +1058,15 @@ bool QgsExpression::unregisterFunction( QString name )
return false;
}



QStringList QgsExpression::gmBuiltinFunctions;

const QStringList &QgsExpression::BuiltinFunctions()
{
if ( gmBuiltinFunctions.isEmpty() )
{
gmBuiltinFunctions << "sqrt"
gmBuiltinFunctions
<< "sqrt" << "cos" << "sin" << "tan"
<< "asin" << "acos" << "atan" << "atan2"
<< "exp" << "ln" << "log10" << "log"
@@ -1025,8 +1077,10 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "minute" << "second" << "lower" << "upper"
<< "title" << "length" << "replace" << "regexp_replace"
<< "substr" << "concat" << "strpos" << "left"
<< "right" << "rpad" << "lpad" << "format_number"
<< "format_date" << "xat" << "yat" << "$area"
<< "right" << "rpad" << "lpad"
<< "format_number" << "format_date"
<< "color_rgb" << "color_rgba" << "color_from_name" << "color_from_ramp"
<< "xat" << "yat" << "$area"
<< "$length" << "$perimeter" << "$x" << "$y"
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
}
@@ -1087,6 +1141,10 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "format", -1, fcnFormatString, QObject::tr( "String" ) )
<< new StaticFunction( "format_number", 2, fcnFormatNumber, QObject::tr( "String" ) )
<< new StaticFunction( "format_date", 2, fcnFormatDate, QObject::tr( "String" ) )
<< new StaticFunction( "color_rgb", 3, fncColorRgb, QObject::tr( "Color" ), QObject::tr( "Usage: color_rgb(red,green,blue) - rgb values from 0 to 255" ) )
<< new StaticFunction( "color_rgba", 4, fncColorRgba, QObject::tr( "Color" ), QObject::tr( "Usage: color_rgba(red,green,blue,alpha) - rgba values from 0 to 255\"" ) )
<< new StaticFunction( "color_from_name", 1, fncColorFromName, QObject::tr( "Color" ), QObject::tr( "Usage: color_from_name(name) - examples \"#FF0000\", \"red\"" ) )
<< new StaticFunction( "color_from_ramp", 1, fncColorFromRamp, QObject::tr( "Color" ), QObject::tr( "Usage: color_from_ramp(name) - type=ramp name, value=0..1" ) )
<< new StaticFunction( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
<< new StaticFunction( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
<< new StaticFunction( "$area", 0, fcnGeomArea, QObject::tr( "Geometry" ), "", true )

0 comments on commit c62c396

Please sign in to comment.
You can’t perform that action at this time.