Skip to content

Commit

Permalink
Move method for detecting project colors to QgsProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 12, 2019
1 parent da53f14 commit ac10769
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsproperty.sip.in
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsproperty.cpp
Expand Up @@ -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 )
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsproperty.h
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions src/gui/qgspropertyoverridebutton.cpp
Expand Up @@ -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 )
Expand Down
17 changes: 17 additions & 0 deletions tests/src/core/testqgsproperty.cpp
Expand Up @@ -92,6 +92,7 @@ class TestQgsProperty : public QObject
void collectionStack(); //test for QgsPropertyCollectionStack
void curveTransform();
void asVariant();
void isProjectColor();

private:

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac10769

Please sign in to comment.