Skip to content

Commit 17efc96

Browse files
committedJan 9, 2017
Remove some core QgsProject::instance() uses
1 parent 0f42ea7 commit 17efc96

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed
 

‎src/core/qgsexpressioncontext.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,16 @@ void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables
545545
class GetNamedProjectColor : public QgsScopedExpressionFunction
546546
{
547547
public:
548-
GetNamedProjectColor()
548+
GetNamedProjectColor( const QgsProject* project )
549549
: QgsScopedExpressionFunction( QStringLiteral( "project_color" ), 1, QStringLiteral( "Color" ) )
550+
, mProject( project )
550551
{
552+
if ( !project )
553+
return;
554+
551555
//build up color list from project. Do this in advance for speed
552-
QStringList colorStrings = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ) );
553-
QStringList colorLabels = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ) );
556+
QStringList colorStrings = project->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ) );
557+
QStringList colorLabels = project->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ) );
554558

555559
//generate list from custom colors
556560
int colorIndex = 0;
@@ -582,11 +586,12 @@ class GetNamedProjectColor : public QgsScopedExpressionFunction
582586

583587
QgsScopedExpressionFunction* clone() const override
584588
{
585-
return new GetNamedProjectColor();
589+
return new GetNamedProjectColor( mProject );
586590
}
587591

588592
private:
589593

594+
const QgsProject* mProject = nullptr;
590595
QHash< QString, QColor > mColors;
591596

592597
};
@@ -618,7 +623,7 @@ QgsExpressionContextScope* QgsExpressionContextUtils::projectScope( const QgsPro
618623
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true ) );
619624
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true ) );
620625

621-
scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor() );
626+
scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor( project ) );
622627
return scope;
623628
}
624629

@@ -951,7 +956,7 @@ QgsExpressionContext QgsExpressionContextUtils::createFeatureBasedContext( const
951956

952957
void QgsExpressionContextUtils::registerContextFunctions()
953958
{
954-
QgsExpression::registerFunction( new GetNamedProjectColor() );
959+
QgsExpression::registerFunction( new GetNamedProjectColor( nullptr ) );
955960
}
956961

957962
bool QgsScopedExpressionFunction::usesGeometry( const QgsExpression::NodeFunction* node ) const

0 commit comments

Comments
 (0)
Please sign in to comment.