diff --git a/src/gui/qgspropertyoverridebutton.cpp b/src/gui/qgspropertyoverridebutton.cpp index 927780959f42..232a5bda3686 100644 --- a/src/gui/qgspropertyoverridebutton.cpp +++ b/src/gui/qgspropertyoverridebutton.cpp @@ -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 ); @@ -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 ); @@ -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 @@ -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 @@ -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(); +} diff --git a/src/gui/qgspropertyoverridebutton.h b/src/gui/qgspropertyoverridebutton.h index 30482403d3be..3ef21f18ec32 100644 --- a/src/gui/qgspropertyoverridebutton.h +++ b/src/gui/qgspropertyoverridebutton.h @@ -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; diff --git a/tests/src/python/test_qgspropertyoverridebutton.py b/tests/src/python/test_qgspropertyoverridebutton.py index c37b29cadab2..29ce4ae8a8c4 100644 --- a/tests/src/python/test_qgspropertyoverridebutton.py +++ b/tests/src/python/test_qgspropertyoverridebutton.py @@ -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()