Skip to content

Commit

Permalink
fix color component setting save
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed May 22, 2024
1 parent d33c1c0 commit bb97a02
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
34 changes: 11 additions & 23 deletions src/gui/qgscompoundcolorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgscompoundcolorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs

QList<QPair<QRadioButton *, QgsColorWidget::ColorComponent>> mRgbRadios;
QList<QPair<QRadioButton *, QgsColorWidget::ColorComponent>> mCmykRadios;
QButtonGroup *mCmykGroup = nullptr;
QButtonGroup *mRgbGroup = nullptr;

/**
* Saves all widget settings
Expand Down
35 changes: 24 additions & 11 deletions tests/src/gui/testqgscompoundcolorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,27 @@ void TestQgsCompoundColorWidget::testComponentSettings_data()
{
QTest::addColumn<int>( "settingsComponent" );
QTest::addColumn<QgsColorWidget::ColorComponent>( "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<QgsColorWidget::ColorComponent>( "newComponent" );
QTest::addColumn<int>( "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 );
Expand All @@ -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()
Expand Down

0 comments on commit bb97a02

Please sign in to comment.