Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ColorWidget] Synchronize color model with color ramp one #58461

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/PyQt6/gui/auto_generated/qgscompoundcolorwidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ Sets whether opacity modification (transparency) is permitted
for the color dialog. Defaults to ``True``.

:param allowOpacity: set to ``False`` to disable opacity modification
%End

void setColorModelEditable( bool colorModelEditable );
%Docstring
Sets whether color model is editable or not

:param colorModelEditable: set to ``False`` to disable color model modification
Defaults to ``True``.

.. versionadded:: 3.40
%End

void setDiscarded( bool discarded );
Expand Down
10 changes: 10 additions & 0 deletions python/gui/auto_generated/qgscompoundcolorwidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ Sets whether opacity modification (transparency) is permitted
for the color dialog. Defaults to ``True``.

:param allowOpacity: set to ``False`` to disable opacity modification
%End

void setColorModelEditable( bool colorModelEditable );
%Docstring
Sets whether color model is editable or not

:param colorModelEditable: set to ``False`` to disable color model modification
Defaults to ``True``.

.. versionadded:: 3.40
%End

void setDiscarded( bool discarded );
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgscompoundcolorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ void QgsCompoundColorWidget::setAllowOpacity( const bool allowOpacity )
}
}

void QgsCompoundColorWidget::setColorModelEditable( bool colorModelEditable )
{
mColorModel->setVisible( colorModelEditable );
}

void QgsCompoundColorWidget::refreshSchemeComboBox()
{
mSchemeComboBox->blockSignals( true );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/qgscompoundcolorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgspanelwidget.h"
#include "ui_qgscompoundcolorwidget.h"
#include "qgis_gui.h"
#include "qgis.h"

class QgsScreenHelper;

Expand Down Expand Up @@ -67,6 +68,14 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs
*/
void setAllowOpacity( bool allowOpacity );

/**
* Sets whether color model is editable or not
* \param colorModelEditable set to FALSE to disable color model modification
* Defaults to TRUE.
* \since QGIS 3.40
*/
void setColorModelEditable( bool colorModelEditable );

/**
* Sets whether the widget's color has been "discarded" and the selected color should not
* be stored in the recent color list.
Expand Down
14 changes: 12 additions & 2 deletions src/gui/qgsgradientcolorrampdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ QgsGradientColorRampDialog::QgsGradientColorRampDialog( const QgsGradientColorRa
setupUi( this );
QgsGui::enableAutoGeometryRestore( this );

mColorWidget->setColorModelEditable( false );

mStopColorSpec->addItem( tr( "RGB" ), static_cast< int >( QColor::Spec::Rgb ) );
mStopColorSpec->addItem( tr( "HSV" ), static_cast< int >( QColor::Spec::Hsv ) );
mStopColorSpec->addItem( tr( "HSL" ), static_cast< int >( QColor::Spec::Hsl ) );
mStopColorSpec->addItem( tr( "CMYK" ), static_cast< int >( QColor::Spec::Cmyk ) );
mStopColorSpec->setCurrentIndex( mStopColorSpec->findData( static_cast< int >( ramp.colorSpec() ) ) );

mStopDirection->addItem( tr( "Clockwise" ), static_cast< int >( Qgis::AngularDirection::Clockwise ) );
Expand All @@ -66,11 +69,18 @@ QgsGradientColorRampDialog::QgsGradientColorRampDialog( const QgsGradientColorRa

connect( mStopColorSpec, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
{
mStopDirection->setEnabled( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) != QColor::Spec::Rgb );
const QColor::Spec colorSpec = static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() );
mStopDirection->setEnabled( colorSpec != QColor::Spec::Rgb );

if ( colorSpec != mColorWidget->color().spec() )
{
mColorWidget->setColor( mColorWidget->color().convertTo( colorSpec ) );
}

if ( mBlockChanges )
return;
mStopEditor->setSelectedStopColorSpec( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) );

mStopEditor->setSelectedStopColorSpec( colorSpec );
} );

connect( mStopDirection, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]
Expand Down
12 changes: 12 additions & 0 deletions tests/src/gui/testqgscompoundcolorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ void TestQgsCompoundColorWidget::testModelChange()

w.setColor( QColor( 1, 2, 3 ) );
QCOMPARE( w.mColorModel->currentData(), QColor::Rgb );

w.setColor( QColor::fromCmykF( 0.5, 0.1, 0.2, 0.3 ) );
QCOMPARE( w.mColorModel->currentData(), QColor::Cmyk );

w.setColor( QColor::fromHsvF( 0.5, 0.1, 0.2 ) );
QCOMPARE( w.mColorModel->currentData(), QColor::Rgb );

QVERIFY( w.mColorModel->isVisible() );
w.setColorModelEditable( false );
QVERIFY( !w.mColorModel->isVisible() );
w.setColorModelEditable( true );
QVERIFY( w.mColorModel->isVisible() );
}

void TestQgsCompoundColorWidget::testTabChange()
Expand Down
Loading