Skip to content

Commit

Permalink
feat(OverrideButton): Support also project_color_object function
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Sep 5, 2024
1 parent 6e8d657 commit 6d4bc26
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
27 changes: 16 additions & 11 deletions src/gui/qgspropertyoverridebutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ void QgsPropertyOverrideButton::aboutToShowMenu()
QPixmap icon = QgsColorButton::createMenuIcon( color.first, mDefinition.standardTemplate() == QgsPropertyDefinition::ColorWithAlpha );
QAction *act = mColorsMenu->addAction( color.second );
act->setIcon( icon );
if ( mProperty.propertyType() == Qgis::PropertyType::Expression && hasExp && mExpressionString == QStringLiteral( "project_color('%1')" ).arg( color.second ) )
if ( mProperty.propertyType() == Qgis::PropertyType::Expression && hasExp && getColor() == color.second )
{
act->setCheckable( true );
act->setChecked( true );
Expand Down Expand Up @@ -643,9 +643,9 @@ void QgsPropertyOverrideButton::menuActionTriggered( QAction *action )
}
else if ( mColorsMenu->actions().contains( action ) ) // a color name clicked
{
if ( mExpressionString != QStringLiteral( "project_color('%1')" ).arg( action->text() ) )
if ( getColor() != action->text() )
{
mExpressionString = QStringLiteral( "project_color('%1')" ).arg( action->text() );
mExpressionString = QStringLiteral( "project_color_object('%1')" ).arg( action->text() );
}
mProperty.setExpressionString( mExpressionString );
mProperty.setTransformer( nullptr );
Expand Down Expand Up @@ -769,12 +769,11 @@ void QgsPropertyOverrideButton::updateGui()
{
icon = mProperty.isActive() ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefineExpressionOn.svg" ) ) : QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefineExpression.svg" ) );

const thread_local QRegularExpression rx( QStringLiteral( "^project_color\\('(.*)'\\)$" ) );
QRegularExpressionMatch match = rx.match( mExpressionString );
if ( match.hasMatch() )
const QString colorName = getColor();
if ( !colorName.isEmpty() )
{
icon = mProperty.isActive() ? QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefineColorOn.svg" ) ) : QgsApplication::getThemeIcon( QStringLiteral( "/mIconDataDefineColor.svg" ) );
deftip = match.captured( 1 );
deftip = colorName;
deftype = tr( "project color" );
}
else
Expand Down Expand Up @@ -925,11 +924,10 @@ void QgsPropertyOverrideButton::updateSiblingWidgets( bool state )
{
if ( state && mProperty.isProjectColor() )
{
const thread_local QRegularExpression rx( QStringLiteral( "^project_color\\('(.*)'\\)$" ) );
QRegularExpressionMatch match = rx.match( mExpressionString );
if ( match.hasMatch() )
const QString colorName = getColor();
if ( !colorName.isEmpty() )
{
cb->linkToProjectColor( match.captured( 1 ) );
cb->linkToProjectColor( colorName );
}
}
else
Expand Down Expand Up @@ -986,3 +984,10 @@ void QgsPropertyOverrideButton::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#data-defined" ) );
}

QString QgsPropertyOverrideButton::getColor() const
{
const thread_local QRegularExpression rx( QStringLiteral( "^project_color(_object|)\\('(.*)'\\)$" ) );
QRegularExpressionMatch match = rx.match( mExpressionString );
return match.hasMatch() ? match.captured( 2 ) : QString();
}
2 changes: 2 additions & 0 deletions src/gui/qgspropertyoverridebutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
*/
void setActivePrivate( bool active );

// Returns color name if current expression is a reference to a color
QString getColor() const;

int mPropertyKey = -1;

Expand Down
12 changes: 9 additions & 3 deletions tests/src/python/test_qgspropertyoverridebutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,24 @@ def testProjectColor(self):

button.menuActionTriggered(color_action.menu().actions()[1])
self.assertTrue(button.toProperty().isActive())
self.assertEqual(button.toProperty().asExpression(), 'project_color(\'burnt marigold\')')
self.assertEqual(button.toProperty().asExpression(), 'project_color_object(\'burnt marigold\')')

button.menuActionTriggered(color_action.menu().actions()[0])
self.assertTrue(button.toProperty().isActive())
self.assertEqual(button.toProperty().asExpression(), 'project_color(\'color 1\')')
self.assertEqual(button.toProperty().asExpression(), 'project_color_object(\'color 1\')')

button.setToProperty(QgsProperty.fromExpression('project_color(\'burnt marigold\')'))
button.setToProperty(QgsProperty.fromExpression('project_color_object(\'burnt marigold\')'))
button.aboutToShowMenu()
color_action = [a for a in button.menu().actions() if a.text() == 'Color'][0]
self.assertTrue(color_action.isChecked())
self.assertEqual([a.isChecked() for a in color_action.menu().actions()], [False, True])

button.setToProperty(QgsProperty.fromExpression('project_color(\'color 1\')'))
button.aboutToShowMenu()
color_action = [a for a in button.menu().actions() if a.text() == 'Color'][0]
self.assertTrue(color_action.isChecked())
self.assertEqual([a.isChecked() for a in color_action.menu().actions()], [True, False])

# should also see color menu for ColorNoAlpha properties
definition = QgsPropertyDefinition('test', 'test', QgsPropertyDefinition.StandardPropertyTemplate.ColorNoAlpha)
button = QgsPropertyOverrideButton()
Expand Down

0 comments on commit 6d4bc26

Please sign in to comment.