Move method for detecting project colors to QgsProperty

This commit is contained in:
Nyall Dawson 2019-01-12 12:45:13 +10:00
parent da53f145f2
commit ac10769809
5 changed files with 39 additions and 7 deletions

View File

@ -311,6 +311,13 @@ Returns true if preparation was successful.
%Docstring
Returns the set of any fields referenced by the property for a specified
expression context.
%End
bool isProjectColor() const;
%Docstring
Returns true if the property is set to a linked project color.
.. versionadded:: 3.6
%End
QVariant value( const QgsExpressionContext &context, const QVariant &defaultValue = QVariant(), bool *ok /Out/ = 0 ) const;

View File

@ -418,6 +418,13 @@ QSet<QString> QgsProperty::referencedFields( const QgsExpressionContext &context
return QSet<QString>();
}
bool QgsProperty::isProjectColor() const
{
QRegularExpression rx( QStringLiteral( "^project_color\\('.*'\\)$" ) );
return d->type == QgsProperty::ExpressionBasedProperty && !d->expressionString.isEmpty()
&& rx.match( d->expressionString ).hasMatch();
}
QVariant QgsProperty::propertyValue( const QgsExpressionContext &context, const QVariant &defaultValue, bool *ok ) const
{
if ( ok )

View File

@ -352,6 +352,13 @@ class CORE_EXPORT QgsProperty
*/
QSet< QString > referencedFields( const QgsExpressionContext &context = QgsExpressionContext() ) const;
/**
* Returns true if the property is set to a linked project color.
*
* \since QGIS 3.6
*/
bool isProjectColor() const;
/**
* Calculates the current value of the property, including any transforms which are set for the property
* \param context QgsExpressionContext to evaluate the property for. The variables and functions contained

View File

@ -854,13 +854,7 @@ void QgsPropertyOverrideButton::updateSiblingWidgets( bool state )
{
if ( state && mFlags & FlagDisableCheckedWidgetOnlyWhenProjectColorSet )
{
state = false;
QRegularExpression rx( QStringLiteral( "^project_color\\('.*'\\)$" ) );
if ( mProperty.propertyType() == QgsProperty::ExpressionBasedProperty && !mExpressionString.isEmpty()
&& rx.match( mExpressionString ).hasMatch() )
{
state = true;
}
state = mProperty.isProjectColor();
}
Q_FOREACH ( const SiblingWidget &sw, mSiblingWidgets )

View File

@ -92,6 +92,7 @@ class TestQgsProperty : public QObject
void collectionStack(); //test for QgsPropertyCollectionStack
void curveTransform();
void asVariant();
void isProjectColor();
private:
@ -1778,6 +1779,22 @@ void TestQgsProperty::asVariant()
QCOMPARE( fromVar.field(), QStringLiteral( "field1" ) );
}
void TestQgsProperty::isProjectColor()
{
QgsProperty p = QgsProperty::fromValue( 3, true );
QVERIFY( !p.isProjectColor() );
p = QgsProperty::fromField( QStringLiteral( "blah" ), true );
QVERIFY( !p.isProjectColor() );
p = QgsProperty::fromExpression( QStringLiteral( "1+2" ), true );
QVERIFY( !p.isProjectColor() );
p = QgsProperty::fromExpression( QStringLiteral( "project_color('mine')" ), true );
QVERIFY( p.isProjectColor() );
p = QgsProperty::fromExpression( QStringLiteral( "project_color('burnt pineapple Skin 76')" ), true );
QVERIFY( p.isProjectColor() );
p.setActive( false );
QVERIFY( p.isProjectColor() );
}
void TestQgsProperty::checkCurveResult( const QList<QgsPointXY> &controlPoints, const QVector<double> &x, const QVector<double> &y )
{
// build transform