From bb97a02b0d743887465b30737082cf36576ba71f Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 22 May 2024 19:37:55 +0200 Subject: [PATCH] fix color component setting save --- src/gui/qgscompoundcolorwidget.cpp | 34 ++++++------------- src/gui/qgscompoundcolorwidget.h | 2 ++ tests/src/gui/testqgscompoundcolorwidget.cpp | 35 ++++++++++++++------ 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/gui/qgscompoundcolorwidget.cpp b/src/gui/qgscompoundcolorwidget.cpp index d225c42df344..baf74d196fe1 100644 --- a/src/gui/qgscompoundcolorwidget.cpp +++ b/src/gui/qgscompoundcolorwidget.cpp @@ -60,18 +60,18 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor &c { mBlackRadio, QgsColorWidget::ColorComponent::Black } }; - QButtonGroup *rgbGroup = new QButtonGroup( this ); + mRgbGroup = new QButtonGroup( this ); int i = 0; for ( auto colorRadio : mRgbRadios ) - rgbGroup->addButton( colorRadio.first, i++ ); + mRgbGroup->addButton( colorRadio.first, i++ ); - QButtonGroup *cmykGroup = new QButtonGroup( this ); + mCmykGroup = new QButtonGroup( this ); i = 0; for ( auto colorRadio : mCmykRadios ) - cmykGroup->addButton( colorRadio.first, i++ ); + mCmykGroup->addButton( colorRadio.first, i++ ); - connect( rgbGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onRgbButtonGroupToggled ); - connect( cmykGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onCmykButtonGroupToggled ); + connect( mRgbGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onRgbButtonGroupToggled ); + connect( mCmykGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onCmykButtonGroupToggled ); connect( mAddColorToSchemeButton, &QPushButton::clicked, this, &QgsCompoundColorWidget::mAddColorToSchemeButton_clicked ); connect( mAddCustomColorButton, &QPushButton::clicked, this, &QgsCompoundColorWidget::mAddCustomColorButton_clicked ); connect( mSampleButton, &QPushButton::clicked, this, &QgsCompoundColorWidget::mSampleButton_clicked ); @@ -274,11 +274,11 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor &c // restore active Rgb/Cmyk component radio button const int activeRgbRadio = settings.value( QStringLiteral( "Windows/ColorDialog/activeComponent" ), 2 ).toInt(); - if ( QAbstractButton *rgbRadio = rgbGroup->button( activeRgbRadio ) ) + if ( QAbstractButton *rgbRadio = mRgbGroup->button( activeRgbRadio ) ) rgbRadio->setChecked( true ); const int activeCmykRadio = settings.value( QStringLiteral( "Windows/ColorDialog/activeCmykComponent" ), 0 ).toInt(); - if ( QAbstractButton *cmykRadio = cmykGroup->button( activeCmykRadio ) ) + if ( QAbstractButton *cmykRadio = mCmykGroup->button( activeCmykRadio ) ) cmykRadio->setChecked( true ); const int currentTab = settings.value( QStringLiteral( "Windows/ColorDialog/activeTab" ), 0 ).toInt(); @@ -668,21 +668,9 @@ void QgsCompoundColorWidget::saveSettings() QgsSettings settings; - //record active component - int activeRadio = 0; - if ( mHueRadio->isChecked() ) - activeRadio = 0; - if ( mSaturationRadio->isChecked() ) - activeRadio = 1; - if ( mValueRadio->isChecked() ) - activeRadio = 2; - if ( mRedRadio->isChecked() ) - activeRadio = 3; - if ( mGreenRadio->isChecked() ) - activeRadio = 4; - if ( mBlueRadio->isChecked() ) - activeRadio = 5; - settings.setValue( QStringLiteral( "Windows/ColorDialog/activeComponent" ), activeRadio ); + // record active component + settings.setValue( QStringLiteral( "Windows/ColorDialog/activeComponent" ), mRgbGroup->checkedId() ); + settings.setValue( QStringLiteral( "Windows/ColorDialog/activeCmykComponent" ), mCmykGroup->checkedId() ); //record current scheme settings.setValue( QStringLiteral( "Windows/ColorDialog/activeScheme" ), mSchemeComboBox->currentIndex() ); diff --git a/src/gui/qgscompoundcolorwidget.h b/src/gui/qgscompoundcolorwidget.h index 9ae19c2cc5e5..f9a20af4de00 100644 --- a/src/gui/qgscompoundcolorwidget.h +++ b/src/gui/qgscompoundcolorwidget.h @@ -187,6 +187,8 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs QList> mRgbRadios; QList> mCmykRadios; + QButtonGroup *mCmykGroup = nullptr; + QButtonGroup *mRgbGroup = nullptr; /** * Saves all widget settings diff --git a/tests/src/gui/testqgscompoundcolorwidget.cpp b/tests/src/gui/testqgscompoundcolorwidget.cpp index 584f1e81fb76..11bd9a7ff0b2 100644 --- a/tests/src/gui/testqgscompoundcolorwidget.cpp +++ b/tests/src/gui/testqgscompoundcolorwidget.cpp @@ -98,23 +98,27 @@ void TestQgsCompoundColorWidget::testComponentSettings_data() { QTest::addColumn( "settingsComponent" ); QTest::addColumn( "expectedComponent" ); - - QTest::newRow( "hue" ) << 0 << QgsColorWidget::ColorComponent::Hue; - QTest::newRow( "saturation" ) << 1 << QgsColorWidget::ColorComponent::Saturation; - QTest::newRow( "value" ) << 2 << QgsColorWidget::ColorComponent::Value; - QTest::newRow( "red" ) << 3 << QgsColorWidget::ColorComponent::Red; - QTest::newRow( "green" ) << 4 << QgsColorWidget::ColorComponent::Green; - QTest::newRow( "blue" ) << 5 << QgsColorWidget::ColorComponent::Blue; - QTest::newRow( "cyan" ) << 0 << QgsColorWidget::ColorComponent::Cyan; - QTest::newRow( "magenta" ) << 1 << QgsColorWidget::ColorComponent::Magenta; - QTest::newRow( "yellow" ) << 2 << QgsColorWidget::ColorComponent::Yellow; - QTest::newRow( "black" ) << 3 << QgsColorWidget::ColorComponent::Black; + QTest::addColumn( "newComponent" ); + QTest::addColumn( "newSettingsComponent" ); + + QTest::newRow( "hue" ) << 0 << QgsColorWidget::ColorComponent::Hue << QgsColorWidget::ColorComponent::Saturation << 1; + QTest::newRow( "saturation" ) << 1 << QgsColorWidget::ColorComponent::Saturation << QgsColorWidget::ColorComponent::Value << 2; + QTest::newRow( "value" ) << 2 << QgsColorWidget::ColorComponent::Value << QgsColorWidget::ColorComponent::Red << 3; + QTest::newRow( "red" ) << 3 << QgsColorWidget::ColorComponent::Red << QgsColorWidget::ColorComponent::Green << 4; + QTest::newRow( "green" ) << 4 << QgsColorWidget::ColorComponent::Green << QgsColorWidget::ColorComponent::Blue << 5; + QTest::newRow( "blue" ) << 5 << QgsColorWidget::ColorComponent::Blue << QgsColorWidget::ColorComponent::Hue << 0; + QTest::newRow( "cyan" ) << 0 << QgsColorWidget::ColorComponent::Cyan << QgsColorWidget::ColorComponent::Magenta << 1; + QTest::newRow( "magenta" ) << 1 << QgsColorWidget::ColorComponent::Magenta << QgsColorWidget::ColorComponent::Yellow << 2; + QTest::newRow( "yellow" ) << 2 << QgsColorWidget::ColorComponent::Yellow << QgsColorWidget::ColorComponent::Black << 3; + QTest::newRow( "black" ) << 3 << QgsColorWidget::ColorComponent::Black << QgsColorWidget::ColorComponent::Cyan << 0; } void TestQgsCompoundColorWidget::testComponentSettings() { QFETCH( int, settingsComponent ); QFETCH( QgsColorWidget::ColorComponent, expectedComponent ); + QFETCH( QgsColorWidget::ColorComponent, newComponent ); + QFETCH( int, newSettingsComponent ); QgsSettings().setValue( QgsColorWidget::colorSpec( expectedComponent ) == QColor::Cmyk ? QStringLiteral( "Windows/ColorDialog/activeCmykComponent" ) : QStringLiteral( "Windows/ColorDialog/activeComponent" ), settingsComponent ); @@ -125,6 +129,15 @@ void TestQgsCompoundColorWidget::testComponentSettings() QCOMPARE( w.mColorBox->component(), expectedComponent ); QCOMPARE( w.mVerticalRamp->component(), expectedComponent ); + + ( QgsColorWidget::colorSpec( expectedComponent ) == QColor::Cmyk ? w.mCmykRadios : w.mRgbRadios ).at( newSettingsComponent ).first->setChecked( true ); + QCOMPARE( w.mColorBox->component(), newComponent ); + QCOMPARE( w.mVerticalRamp->component(), newComponent ); + + w.saveSettings(); + const int newValue = QgsSettings().value( QgsColorWidget::colorSpec( expectedComponent ) == QColor::Cmyk ? + QStringLiteral( "Windows/ColorDialog/activeCmykComponent" ) : QStringLiteral( "Windows/ColorDialog/activeComponent" ), -1 ).toInt(); + QCOMPARE( newValue, newSettingsComponent ); } void TestQgsCompoundColorWidget::testComponentChange()