Skip to content

Commit

Permalink
Fix(ColorWidget): Update current component on color model change
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed May 23, 2024
1 parent a2924f3 commit 1ad2b4b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
29 changes: 15 additions & 14 deletions src/gui/qgscompoundcolorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor &c
for ( auto colorRadio : mCmykRadios )
mCmykGroup->addButton( colorRadio.first, i++ );

connect( mRgbGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onRgbButtonGroupToggled );
connect( mCmykGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onCmykButtonGroupToggled );
connect( mRgbGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onColorButtonGroupToggled );
connect( mCmykGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onColorButtonGroupToggled );
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 All @@ -87,6 +87,8 @@ QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor &c
setColor( this->color().toCmyk() );
else
setColor( this->color().toRgb() );

updateComponent();
} );

if ( widgetLayout == LayoutVertical )
Expand Down Expand Up @@ -883,28 +885,27 @@ void QgsCompoundColorWidget::keyPressEvent( QKeyEvent *e )
}


void QgsCompoundColorWidget::onColorButtonGroupToggled( const QList<QPair<QRadioButton *, QgsColorWidget::ColorComponent>> &colorRadios,
const QColor::Spec colorSpec, const int id, const bool checked )
void QgsCompoundColorWidget::updateComponent()
{
if ( checked && id >= 0 && id < colorRadios.count() && static_cast<QColor::Spec>( mColorModel->currentData().toInt() ) == colorSpec )
const bool isCmyk = mColorModel->currentData().toInt() == QColor::Spec::Cmyk;
const auto radios = isCmyk ? mCmykRadios : mRgbRadios;
const QButtonGroup *group = isCmyk ? mCmykGroup : mRgbGroup;

const int id = group->checkedId();
if ( id >= 0 && id < radios.count() )
{
const QgsColorWidget::ColorComponent component = colorRadios.at( id ).second;
const QgsColorWidget::ColorComponent component = radios.at( group->checkedId() ).second;
mColorBox->setComponent( component );
mVerticalRamp->setComponent( component );
}
}

void QgsCompoundColorWidget::onRgbButtonGroupToggled( int id, bool checked )
{
onColorButtonGroupToggled( mRgbRadios, QColor::Rgb, id, checked );
}

void QgsCompoundColorWidget::onCmykButtonGroupToggled( int id, bool checked )
void QgsCompoundColorWidget::onColorButtonGroupToggled( int, bool checked )
{
onColorButtonGroupToggled( mCmykRadios, QColor::Cmyk, id, checked );
if ( checked )
updateComponent();
}


void QgsCompoundColorWidget::mAddColorToSchemeButton_clicked()
{
mSchemeList->addColor( mColorPreview->color(), QgsSymbolLayerUtils::colorToName( mColorPreview->color() ) );
Expand Down
19 changes: 7 additions & 12 deletions src/gui/qgscompoundcolorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs

private slots:

void onRgbButtonGroupToggled( int id, bool checked );
void onCmykButtonGroupToggled( int id, bool checked );
void onColorButtonGroupToggled( int, bool checked );

void mAddColorToSchemeButton_clicked();

Expand Down Expand Up @@ -175,6 +174,12 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs

static QScreen *findScreenAt( QPoint pos );

/**
* Helper method to update current widget display with current component according to
* color model and selected color component radio button
*/
void updateComponent();

QgsScreenHelper *mScreenHelper = nullptr;

bool mAllowAlpha = true;
Expand Down Expand Up @@ -230,16 +235,6 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs
//! Updates the state of actions for the current selected scheme
void updateActionsForCurrentScheme();

/**
* Helper method to implement slots called when color radio button has been toggled
* \param colorRadios related to the toggled button
* \param colorSpec color type of the toggled button
* \param id of the toggled button
* \param checked TRUE is the button is checked
*/
void onColorButtonGroupToggled( const QList<QPair<QRadioButton *, QgsColorWidget::ColorComponent>> &colorRadios,
const QColor::Spec colorSpec, const int id, const bool checked );

friend class TestQgsCompoundColorWidget;
};

Expand Down

0 comments on commit 1ad2b4b

Please sign in to comment.