Skip to content

Commit

Permalink
Disable linked color buttons when a project color is set for a property
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 11, 2019
1 parent eecfe50 commit e569331
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
26 changes: 26 additions & 0 deletions python/gui/auto_generated/qgspropertyoverridebutton.sip.in
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 *
Expand Down
20 changes: 20 additions & 0 deletions src/gui/qgspropertyoverridebutton.cpp
Expand Up @@ -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 )
{
Expand Down
25 changes: 25 additions & 0 deletions src/gui/qgspropertyoverridebutton.h
Expand Up @@ -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
Expand All @@ -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).
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions tests/src/python/test_qgspropertyoverridebutton.py
Expand Up @@ -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.