Skip to content

Commit

Permalink
add translation strings for expression builder
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Nov 27, 2011
1 parent 8841d32 commit 8a7929e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 89 deletions.
100 changes: 54 additions & 46 deletions src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,46 +370,53 @@ static QVariant fcnGeomPerimeter( const QVariantList& , QgsFeature* f, QgsExpres
return QVariant( calc->measurePerimeter( f->geometry() ) );
}

typedef QgsExpression::FunctionDef FnDef;

FnDef QgsExpression::BuiltinFunctions[] =
{
// math
FnDef( "sqrt", 1, fcnSqrt, "Math" ),
FnDef( "sin", 1, fcnSin, "Math" ),
FnDef( "cos", 1, fcnCos, "Math" ),
FnDef( "tan", 1, fcnTan, "Math" ),
FnDef( "asin", 1, fcnAsin, "Math" ),
FnDef( "acos", 1, fcnAcos, "Math" ),
FnDef( "atan", 1, fcnAtan, "Math" ),
FnDef( "atan2", 2, fcnAtan2, "Math" ),
FnDef( "exp", 1, fcnExp, "Math" ),
FnDef( "ln", 1, fcnLn, "Math" ),
FnDef( "log10", 1, fcnLog10, "Math" ),
FnDef( "log", 2, fcnLog, "Math" ),
// casts
FnDef( "toint", 1, fcnToInt, "Conversions" ),
FnDef( "toreal", 1, fcnToReal, "Conversions" ),
FnDef( "tostring", 1, fcnToString, "Conversions" ),
// string manipulation
FnDef( "lower", 1, fcnLower, "String" ),
FnDef( "upper", 1, fcnUpper, "String" ),
FnDef( "length", 1, fcnLength, "String" ),
FnDef( "replace", 3, fcnReplace, "String" ),
FnDef( "regexp_replace", 3, fcnRegexpReplace, "String" ),
FnDef( "substr", 3, fcnSubstr, "String" ),
// geometry accessors
FnDef( "xat", 1, fcnXat, "Geometry", "", true ),
FnDef( "yat", 1, fcnYat, "Geometry", "", true ),
FnDef( "$area", 0, fcnGeomArea, "Geometry", "", true ),
FnDef( "$length", 0, fcnGeomLength, "Geometry", "", true ),
FnDef( "$perimeter", 0, fcnGeomPerimeter, "Geometry", "", true ),
FnDef( "$x", 0, fcnX, "Geometry", "", true ),
FnDef( "$y", 0, fcnY, "Geometry", "" , true ),
// special columns
FnDef( "$rownum", 0, fcnRowNumber, "Record" ),
FnDef( "$id", 0, fcnFeatureId, "Record" )
};
QList<QgsExpression::FunctionDef> QgsExpression::gmBuiltinFunctions;

const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
{
if ( gmBuiltinFunctions.isEmpty() )
{
// math
gmBuiltinFunctions
<< FunctionDef( "sqrt", 1, fcnSqrt, QObject::tr( "Math" ) )
<< FunctionDef( "sin", 1, fcnSin, QObject::tr( "Math" ) )
<< FunctionDef( "cos", 1, fcnCos, QObject::tr( "Math" ) )
<< FunctionDef( "tan", 1, fcnTan, QObject::tr( "Math" ) )
<< FunctionDef( "asin", 1, fcnAsin, QObject::tr( "Math" ) )
<< FunctionDef( "acos", 1, fcnAcos, QObject::tr( "Math" ) )
<< FunctionDef( "atan", 1, fcnAtan, QObject::tr( "Math" ) )
<< FunctionDef( "atan2", 2, fcnAtan2, QObject::tr( "Math" ) )
<< FunctionDef( "exp", 1, fcnExp, QObject::tr( "Math" ) )
<< FunctionDef( "ln", 1, fcnLn, QObject::tr( "Math" ) )
<< FunctionDef( "log10", 1, fcnLog10, QObject::tr( "Math" ) )
<< FunctionDef( "log", 2, fcnLog, QObject::tr( "Math" ) )
// casts
<< FunctionDef( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
<< FunctionDef( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )
<< FunctionDef( "tostring", 1, fcnToString, QObject::tr( "Conversions" ) )
// string manipulation
<< FunctionDef( "lower", 1, fcnLower, QObject::tr( "String" ) )
<< FunctionDef( "upper", 1, fcnUpper, QObject::tr( "String" ) )
<< FunctionDef( "length", 1, fcnLength, QObject::tr( "String" ) )
<< FunctionDef( "replace", 3, fcnReplace, QObject::tr( "String" ) )
<< FunctionDef( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )
<< FunctionDef( "substr", 3, fcnSubstr, QObject::tr( "String" ) )
// geometry accessors
<< FunctionDef( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "$area", 0, fcnGeomArea, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "$length", 0, fcnGeomLength, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "$perimeter", 0, fcnGeomPerimeter, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "$x", 0, fcnX, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "$y", 0, fcnY, QObject::tr( "Geometry" ), "" , true )
// special columns
<< FunctionDef( "$rownum", 0, fcnRowNumber, QObject::tr( "Record" ) )
<< FunctionDef( "$id", 0, fcnFeatureId, QObject::tr( "Record" ) )
;
}

return gmBuiltinFunctions;
}


bool QgsExpression::isFunctionName( QString name )
Expand All @@ -422,15 +429,15 @@ int QgsExpression::functionIndex( QString name )
int count = functionCount();
for ( int i = 0; i < count; i++ )
{
if ( QString::compare( name, BuiltinFunctions[i].mName, Qt::CaseInsensitive ) == 0 )
if ( QString::compare( name, BuiltinFunctions()[i].mName, Qt::CaseInsensitive ) == 0 )
return i;
}
return -1;
}

int QgsExpression::functionCount()
{
return ( sizeof( BuiltinFunctions ) / sizeof( FunctionDef ) );
return BuiltinFunctions().size();
}


Expand Down Expand Up @@ -527,7 +534,8 @@ QVariant QgsExpression::evaluate( QgsFeature* f, const QgsFieldMap& fields )

QString QgsExpression::dump() const
{
if ( !mRootNode ) return "(no root)";
if ( !mRootNode )
return QObject::tr( "(no root)" );

return mRootNode->dump();
}
Expand Down Expand Up @@ -861,7 +869,7 @@ QString QgsExpression::NodeInOperator::dump() const

QVariant QgsExpression::NodeFunction::eval( QgsExpression* parent, QgsFeature* f )
{
const FunctionDef& fd = BuiltinFunctions[mFnIndex];
const FunctionDef& fd = BuiltinFunctions()[mFnIndex];

// evaluate arguments
QVariantList argValues;
Expand Down Expand Up @@ -900,7 +908,7 @@ bool QgsExpression::NodeFunction::prepare( QgsExpression* parent, const QgsField

QString QgsExpression::NodeFunction::dump() const
{
const FnDef& fd = BuiltinFunctions[mFnIndex];
const FunctionDef& fd = BuiltinFunctions()[mFnIndex];
if ( fd.mParams == 0 )
return fd.mName; // special column
else
Expand Down Expand Up @@ -930,7 +938,7 @@ QString QgsExpression::NodeLiteral::dump() const
case QVariant::Int: return QString::number( mValue.toInt() );
case QVariant::Double: return QString::number( mValue.toDouble() );
case QVariant::String: return QString( "'%1'" ).arg( mValue.toString() );
default: return QString( "[unsupported type;%1; value:%2]" ).arg( mValue.typeName() ).arg( mValue.toString() );
default: return QObject::tr( "[unsupported type;%1; value:%2]" ).arg( mValue.typeName() ).arg( mValue.toString() );
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/core/qgsexpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <QStringList>
#include <QVariant>
#include <QList>

#include "qgsfield.h"

Expand Down Expand Up @@ -122,7 +123,7 @@ class CORE_EXPORT QgsExpression

//! Return calculator used for distance and area calculations
//! (used by internal functions)
QgsDistanceArea* geomCalculator() { if ( mCalc == NULL ) initGeomCalculator(); return mCalc; }
QgsDistanceArea* geomCalculator() { if ( !mCalc ) initGeomCalculator(); return mCalc; }

//

Expand Down Expand Up @@ -186,7 +187,8 @@ class CORE_EXPORT QgsExpression
QString mHelpText;
};

static FunctionDef BuiltinFunctions[];
static const QList<FunctionDef> &BuiltinFunctions();
static QList<FunctionDef> gmBuiltinFunctions;

// tells whether the identifier is a name of existing function
static bool isFunctionName( QString name );
Expand Down Expand Up @@ -301,7 +303,7 @@ class CORE_EXPORT QgsExpression
virtual QVariant eval( QgsExpression* parent, QgsFeature* f );
virtual QString dump() const;
virtual QStringList referencedColumns() const { QStringList lst; if ( !mArgs ) return lst; foreach( Node* n, mArgs->list() ) lst.append( n->referencedColumns() ); return lst; }
virtual bool needsGeometry() const { bool needs = BuiltinFunctions[mFnIndex].mUsesGeometry; if ( mArgs ) { foreach( Node* n, mArgs->list() ) needs |= n->needsGeometry(); } return needs; }
virtual bool needsGeometry() const { bool needs = BuiltinFunctions()[mFnIndex].mUsesGeometry; if ( mArgs ) { foreach( Node* n, mArgs->list() ) needs |= n->needsGeometry(); } return needs; }
protected:
//QString mName;
int mFnIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsexpressionparser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ expression:
exp_error("Function is not known");
YYERROR;
}
if (QgsExpression::BuiltinFunctions[fnIndex].mParams != $3->count())
if (QgsExpression::BuiltinFunctions()[fnIndex].mParams != $3->count())
{
exp_error("Function is called with wrong number of arguments");
YYERROR;
Expand Down
79 changes: 41 additions & 38 deletions src/gui/qgsexpressionbuilderwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,41 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )


// TODO Can we move this stuff to QgsExpression, like the functions?
registerItem( "Operators", "+", " + " );
registerItem( "Operators", "-", " -" );
registerItem( "Operators", "*", " * " );
registerItem( "Operators", "/", " / " );
registerItem( "Operators", "%", " % " );
registerItem( "Operators", "^", " ^ " );
registerItem( "Operators", "=", " = " );
registerItem( "Operators", ">", " > " );
registerItem( "Operators", "<", " < " );
registerItem( "Operators", "<>", " <> " );
registerItem( "Operators", "<=", " <= " );
registerItem( "Operators", ">=", " >= " );
registerItem( "Operators", "||", " || ", "<b>|| (String Concatenation)</b> "
"<br> Joins two values together into a string "
"<br> <i>Usage:</i><br>'Dia' || Diameter" );
registerItem( "Operators", "LIKE", " LIKE " );
registerItem( "Operators", "ILIKE", " ILIKE " );
registerItem( "Operators", "IS", " IS NOT " );
registerItem( "Operators", "OR", " OR " );
registerItem( "Operators", "AND", " AND " );
registerItem( "Operators", "NOT", " NOT " );
registerItem( tr( "Operators" ), "+", " + " );
registerItem( tr( "Operators" ), "-", " -" );
registerItem( tr( "Operators" ), "*", " * " );
registerItem( tr( "Operators" ), "/", " / " );
registerItem( tr( "Operators" ), "%", " % " );
registerItem( tr( "Operators" ), "^", " ^ " );
registerItem( tr( "Operators" ), "=", " = " );
registerItem( tr( "Operators" ), ">", " > " );
registerItem( tr( "Operators" ), "<", " < " );
registerItem( tr( "Operators" ), "<>", " <> " );
registerItem( tr( "Operators" ), "<=", " <= " );
registerItem( tr( "Operators" ), ">=", " >= " );
registerItem( tr( "Operators" ), "||", " || ",
QString( "<b>|| %1</b><br><i>%2</i><br><i>%3:</i>%4" )
.arg( tr( "(String Concatenation)" ) )
.arg( tr( "Joins two values together into a string" ) )
.arg( tr( "Usage" ) )
.arg( tr( "'Dia' || Diameter" ) ) );
registerItem( tr( "Operators" ), "LIKE", " LIKE " );
registerItem( tr( "Operators" ), "ILIKE", " ILIKE " );
registerItem( tr( "Operators" ), "IS", " IS NOT " );
registerItem( tr( "Operators" ), "OR", " OR " );
registerItem( tr( "Operators" ), "AND", " AND " );
registerItem( tr( "Operators" ), "NOT", " NOT " );


// Load the fuctions from the QgsExpression class
int count = QgsExpression::functionCount();
for ( int i = 0; i < count; i++ )
{
QgsExpression::FunctionDef func = QgsExpression::BuiltinFunctions[i];
QgsExpression::FunctionDef func = QgsExpression::BuiltinFunctions()[i];
QString name = func.mName;
if ( func.mParams >= 1 )
name += "(";
registerItem( func.mGroup, func.mName, " " + name + " ");
registerItem( func.mGroup, func.mName, " " + name + " " );
};
}

Expand Down Expand Up @@ -153,7 +156,7 @@ void QgsExpressionBuilderWidget::loadFieldNames()
for ( ; fieldIt != fieldMap.constEnd(); ++fieldIt )
{
QString fieldName = fieldIt.value().name();
registerItem( "Fields", fieldName, " " + fieldName + " ", "", QgsExpressionItem::Field );
registerItem( tr( "Fields" ), fieldName, " " + fieldName + " ", "", QgsExpressionItem::Field );
}
}

Expand Down Expand Up @@ -238,31 +241,31 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
{
if ( !mFeature.isValid() )
{
mLayer->select( mLayer->pendingAllAttributesList() );
mLayer->nextFeature( mFeature );
mLayer->select( mLayer->pendingAllAttributesList() );
mLayer->nextFeature( mFeature );
}

if ( mFeature.isValid() )
{
QVariant value = exp.evaluate( &mFeature, mLayer->pendingFields() );
if ( !exp.hasEvalError() )
lblPreview->setText( value.toString() );
QVariant value = exp.evaluate( &mFeature, mLayer->pendingFields() );
if ( !exp.hasEvalError() )
lblPreview->setText( value.toString() );
}
else
{
// The feautre is invaild because we don't have one but that doesn't mean user can't
// build a expression string. They just get no preview.
lblPreview->setText("");
// The feautre is invaild because we don't have one but that doesn't mean user can't
// build a expression string. They just get no preview.
lblPreview->setText( "" );
}
}

if ( exp.hasParserError() || exp.hasEvalError() )
{
QString tooltip = "<b>Parser Error:</b> <br>" + exp.parserErrorString();
QString tooltip = QString( "<b>%1:</b><br>%2" ).arg( tr( "Parser Error" ) ).arg( exp.parserErrorString() );
if ( exp.hasEvalError() )
tooltip += "<br><br> <b>Eval Error:</b> <br>" + exp.evalErrorString();
tooltip += QString( "<br><br><b>%1:</b><br>%2" ).arg( tr( "Eval Error" ) ).arg( exp.evalErrorString() );

lblPreview->setText( "Expression is invaild <a href=""more"">(more info)</a>" );
lblPreview->setText( tr( "Expression is invalid <a href=""more"">(more info)</a>" ) );
lblPreview->setStyleSheet( "color: rgba(255, 6, 10, 255);" );
txtExpressionString->setToolTip( tooltip );
lblPreview->setToolTip( tooltip );
Expand Down Expand Up @@ -291,7 +294,7 @@ void QgsExpressionBuilderWidget::on_lblPreview_linkActivated( QString link )
{
Q_UNUSED( link );
QgsMessageViewer * mv = new QgsMessageViewer( this );
mv->setWindowTitle( "More info on expression error" );
mv->setWindowTitle( tr( "More info on expression error" ) );
mv->setMessageAsHtml( txtExpressionString->toolTip() );
mv->exec();
}
Expand Down Expand Up @@ -395,13 +398,13 @@ QString QgsExpressionBuilderWidget::loadFunctionHelp( QgsExpressionItem* functio
// translate this for us message
if ( !lang.contains( "en_" ) )
{
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
}

}
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
helpContents = tr( "This help file does not exist for your language:<p><b>%1</b><p>If you would like to create it, contact the QGIS development team" )
helpContents = tr( "This help file does not exist for your language:<p><b>%1</b><p>If you would like to create it, contact the QGIS development team" )
.arg( fullHelpPath );
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/helpviewer/qgshelpviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void QgsHelpViewer::loadContext( const QString &contextId )
// translate this for us message
if ( !lang.contains( "en_" ) )
{
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
helpContents = "<i>" + tr( "This help file is not available in your language %1. If you would like to translate it, please contact the QGIS development team." ).arg( lang ) + "</i><hr />";
}
}
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
Expand Down

0 comments on commit 8a7929e

Please sign in to comment.