@@ -545,12 +545,16 @@ void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables
545
545
class GetNamedProjectColor : public QgsScopedExpressionFunction
546
546
{
547
547
public:
548
- GetNamedProjectColor ()
548
+ GetNamedProjectColor ( const QgsProject* project )
549
549
: QgsScopedExpressionFunction( QStringLiteral( " project_color" ), 1 , QStringLiteral( " Color" ) )
550
+ , mProject ( project )
550
551
{
552
+ if ( !project )
553
+ return ;
554
+
551
555
// 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" ) );
554
558
555
559
// generate list from custom colors
556
560
int colorIndex = 0 ;
@@ -582,11 +586,12 @@ class GetNamedProjectColor : public QgsScopedExpressionFunction
582
586
583
587
QgsScopedExpressionFunction* clone () const override
584
588
{
585
- return new GetNamedProjectColor ();
589
+ return new GetNamedProjectColor ( mProject );
586
590
}
587
591
588
592
private:
589
593
594
+ const QgsProject* mProject = nullptr ;
590
595
QHash< QString, QColor > mColors ;
591
596
592
597
};
@@ -618,7 +623,7 @@ QgsExpressionContextScope* QgsExpressionContextUtils::projectScope( const QgsPro
618
623
scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_crs" ), projectCrs.authid (), true ) );
619
624
scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_crs_definition" ), projectCrs.toProj4 (), true ) );
620
625
621
- scope->addFunction ( QStringLiteral ( " project_color" ), new GetNamedProjectColor () );
626
+ scope->addFunction ( QStringLiteral ( " project_color" ), new GetNamedProjectColor ( project ) );
622
627
return scope;
623
628
}
624
629
@@ -951,7 +956,7 @@ QgsExpressionContext QgsExpressionContextUtils::createFeatureBasedContext( const
951
956
952
957
void QgsExpressionContextUtils::registerContextFunctions ()
953
958
{
954
- QgsExpression::registerFunction ( new GetNamedProjectColor () );
959
+ QgsExpression::registerFunction ( new GetNamedProjectColor ( nullptr ) );
955
960
}
956
961
957
962
bool QgsScopedExpressionFunction::usesGeometry ( const QgsExpression::NodeFunction* node ) const
0 commit comments