Skip to content
Permalink
Browse files

Disable linked color buttons when a project color is set for a property

  • Loading branch information
nyalldawson committed Jan 11, 2019
1 parent eecfe50 commit e569331abbb8c603e8ed649566fb701b2a7bcbba
@@ -31,6 +31,13 @@ and layouts.
%End
public:

enum Flag
{
FlagDisableCheckedWidgetOnlyWhenProjectColorSet,
};
typedef QFlags<QgsPropertyOverrideButton::Flag> Flags;


QgsPropertyOverrideButton( QWidget *parent /TransferThis/ = 0,
const QgsVectorLayer *layer = 0 );
%Docstring
@@ -40,6 +47,23 @@ Constructor for QgsPropertyOverrideButton.
:param layer: associated vector layer
%End

Flags flags() const;
%Docstring
Returns the button's flags, which control the button behavior.

.. seealso:: :py:func:`setFlags`

.. versionadded:: 3.6
%End

void setFlags( QgsPropertyOverrideButton::Flags flags );
%Docstring
Sets the button's ``flags``, which control the button behavior.

.. seealso:: :py:func:`flags`

.. versionadded:: 3.6
%End

void init( int propertyKey,
const QgsProperty &property,
@@ -241,6 +265,8 @@ Emitted when creating a new auxiliary field

};

QFlags<QgsPropertyOverrideButton::Flag> operator|(QgsPropertyOverrideButton::Flag f1, QFlags<QgsPropertyOverrideButton::Flag> f2);


/************************************************************************
* This file has been generated automatically from *
@@ -829,8 +829,28 @@ void QgsPropertyOverrideButton::setActivePrivate( bool active )
}
}

QgsPropertyOverrideButton::Flags QgsPropertyOverrideButton::flags() const
{
return mFlags;
}

void QgsPropertyOverrideButton::setFlags( Flags flags )
{
mFlags = flags;
}

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;
}
}

Q_FOREACH ( const SiblingWidget &sw, mSiblingWidgets )
{
@@ -54,6 +54,13 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton

public:

//! Flags controlling button behavior
enum Flag
{
FlagDisableCheckedWidgetOnlyWhenProjectColorSet = 1 << 1, //!< Indicates that registered widgets will only be disabled when the property is set to follow a project color
};
Q_DECLARE_FLAGS( Flags, Flag )

/**
* Constructor for QgsPropertyOverrideButton.
* \param parent parent widget
@@ -62,6 +69,21 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
QgsPropertyOverrideButton( QWidget *parent SIP_TRANSFERTHIS = nullptr,
const QgsVectorLayer *layer = nullptr );

/**
* Returns the button's flags, which control the button behavior.
*
* \see setFlags()
* \since QGIS 3.6
*/
Flags flags() const;

/**
* Sets the button's \a flags, which control the button behavior.
*
* \see flags()
* \since QGIS 3.6
*/
void setFlags( QgsPropertyOverrideButton::Flags flags );

/**
* Initialize a newly constructed property button (useful if button was included in a UI layout).
@@ -322,11 +344,14 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton

std::shared_ptr< QgsSymbol > mSymbol;

Flags mFlags = nullptr;

private slots:

void showHelp();
void updateSiblingWidgets( bool state );
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsPropertyOverrideButton::Flags )

#endif // QGSPROPERTYOVERRIDEBUTTON_H
@@ -85,6 +85,35 @@ def testProjectColor(self):
self.assertNotIn('Project Color', [a.text() for a in button.menu().actions()])
self.assertNotIn('Color', [a.text() for a in button.menu().actions()])

def testLinkedColorButton(self):
definition = QgsPropertyDefinition('test', 'test', QgsPropertyDefinition.ColorWithAlpha)
button = QgsPropertyOverrideButton()
button.init(0, QgsProperty(), definition)
cb = QgsColorButton()
button.registerEnabledWidget(cb, False)

# set button to a non color property
button.setToProperty(QgsProperty.fromValue('#ff0000'))
self.assertFalse(cb.isEnabled())
button.setActive(False)
self.assertTrue(cb.isEnabled())

# set button to a color property
button.setToProperty(QgsProperty.fromExpression('project_color(\'Cthulhu\'s delight\')'))
self.assertFalse(cb.isEnabled())
button.setActive(False)
self.assertTrue(cb.isEnabled())

# test with FlagDisableCheckedWidgetOnlyWhenProjectColorSet set
button.setFlags(QgsPropertyOverrideButton.FlagDisableCheckedWidgetOnlyWhenProjectColorSet)
button.setToProperty(QgsProperty.fromValue('#ff0000'))
self.assertTrue(cb.isEnabled())
button.setActive(False)
self.assertTrue(cb.isEnabled())
button.setToProperty(QgsProperty.fromExpression('project_color(\'Cthulhu\'s delight\')'))
self.assertFalse(cb.isEnabled())
button.setActive(False)
self.assertTrue(cb.isEnabled())

if __name__ == '__main__':
unittest.main()

0 comments on commit e569331

Please sign in to comment.
You can’t perform that action at this time.