diff --git a/python/PyQt6/gui/auto_generated/qgscompoundcolorwidget.sip.in b/python/PyQt6/gui/auto_generated/qgscompoundcolorwidget.sip.in index eab6751d1b7c..c821a9d1a19b 100644 --- a/python/PyQt6/gui/auto_generated/qgscompoundcolorwidget.sip.in +++ b/python/PyQt6/gui/auto_generated/qgscompoundcolorwidget.sip.in @@ -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 ); diff --git a/python/gui/auto_generated/qgscompoundcolorwidget.sip.in b/python/gui/auto_generated/qgscompoundcolorwidget.sip.in index 89631feb8119..825aa7df3b41 100644 --- a/python/gui/auto_generated/qgscompoundcolorwidget.sip.in +++ b/python/gui/auto_generated/qgscompoundcolorwidget.sip.in @@ -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 ); diff --git a/src/gui/qgscompoundcolorwidget.cpp b/src/gui/qgscompoundcolorwidget.cpp index 98d96a64e702..0bec380690ab 100644 --- a/src/gui/qgscompoundcolorwidget.cpp +++ b/src/gui/qgscompoundcolorwidget.cpp @@ -346,6 +346,11 @@ void QgsCompoundColorWidget::setAllowOpacity( const bool allowOpacity ) } } +void QgsCompoundColorWidget::setColorModelEditable( bool colorModelEditable ) +{ + mColorModel->setVisible( colorModelEditable ); +} + void QgsCompoundColorWidget::refreshSchemeComboBox() { mSchemeComboBox->blockSignals( true ); diff --git a/src/gui/qgscompoundcolorwidget.h b/src/gui/qgscompoundcolorwidget.h index a99683d78873..1975d1d6a509 100644 --- a/src/gui/qgscompoundcolorwidget.h +++ b/src/gui/qgscompoundcolorwidget.h @@ -20,6 +20,7 @@ #include "qgspanelwidget.h" #include "ui_qgscompoundcolorwidget.h" #include "qgis_gui.h" +#include "qgis.h" class QgsScreenHelper; @@ -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. diff --git a/src/gui/qgsgradientcolorrampdialog.cpp b/src/gui/qgsgradientcolorrampdialog.cpp index 4d9923d4b0d1..2ce7ea97fbdd 100644 --- a/src/gui/qgsgradientcolorrampdialog.cpp +++ b/src/gui/qgsgradientcolorrampdialog.cpp @@ -15,9 +15,7 @@ #include "qgsgradientcolorrampdialog.h" -#include "qgscolorramp.h" #include "qgsdialog.h" -#include "qgscolordialog.h" #include "qgscptcityarchive.h" #include "qgssettings.h" #include "qgsgui.h" @@ -53,24 +51,34 @@ 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 ) ); mStopDirection->addItem( tr( "Counterclockwise" ), static_cast< int >( Qgis::AngularDirection::CounterClockwise ) ); mStopDirection->setCurrentIndex( mStopColorSpec->findData( static_cast< int >( ramp.direction() ) ) ); - mStopDirection->setEnabled( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) != QColor::Spec::Rgb ); + mStopDirection->setEnabled( hasDirection( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) ) ); connect( mStopColorSpec, static_cast( &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( hasDirection( colorSpec ) ); + + 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( &QComboBox::currentIndexChanged ), this, [ = ] @@ -359,7 +367,7 @@ void QgsGradientColorRampDialog::selectedStopChanged( const QgsGradientStop &sto // first stop cannot have color spec or direction set mStopColorSpec->setEnabled( !( stop.offset == 0 && stop.color == mRamp.color1() ) ); - mStopDirection->setEnabled( !( stop.offset == 0 && stop.color == mRamp.color1() ) && mStopEditor->selectedStop().colorSpec() != QColor::Rgb ); + mStopDirection->setEnabled( !( stop.offset == 0 && stop.color == mRamp.color1() ) && hasDirection( mStopEditor->selectedStop().colorSpec() ) ); updatePlot(); } @@ -529,6 +537,24 @@ void QgsGradientColorRampDialog::addMarkersForColor( double x, const QColor &col addPlotMarker( x, color.alphaF(), color, isSelected && mCurrentPlotColorComponent == 3 ); } +bool QgsGradientColorRampDialog::hasDirection( QColor::Spec colorSpec ) +{ + switch ( colorSpec ) + { + case QColor::Spec::Rgb: + case QColor::Spec::ExtendedRgb: + case QColor::Spec::Cmyk: + case QColor::Spec::Invalid: + return false; + + case QColor::Spec::Hsv: + case QColor::Spec::Hsl: + return true; + } + + BUILTIN_UNREACHABLE +} + void QgsGradientColorRampDialog::updatePlot() { // remove existing markers @@ -609,7 +635,7 @@ void QgsGradientColorRampDialog::updateRampFromStopEditor() // first stop cannot have color spec or direction set mStopColorSpec->setEnabled( !( mStopEditor->selectedStop().offset == 0 && mStopEditor->selectedStop().color == mRamp.color1() ) ); - mStopDirection->setEnabled( !( mStopEditor->selectedStop().offset == 0 && mStopEditor->selectedStop().color == mRamp.color1() ) && mStopEditor->selectedStop().colorSpec() != QColor::Rgb ); + mStopDirection->setEnabled( !( mStopEditor->selectedStop().offset == 0 && mStopEditor->selectedStop().color == mRamp.color1() ) && hasDirection( mStopEditor->selectedStop().colorSpec() ) ); updateColorButtons(); updatePlot(); diff --git a/src/gui/qgsgradientcolorrampdialog.h b/src/gui/qgsgradientcolorrampdialog.h index 049093297165..ddabdc26cade 100644 --- a/src/gui/qgsgradientcolorrampdialog.h +++ b/src/gui/qgsgradientcolorrampdialog.h @@ -121,6 +121,9 @@ class GUI_EXPORT QgsGradientColorRampDialog : public QDialog, private Ui::QgsGra void updatePlot(); void addPlotMarker( double x, double y, const QColor &color, bool isSelected = false ); void addMarkersForColor( double x, const QColor &color, bool isSelected = false ); + + // Returns TRUE if it's possible to edit direction regarding given colorSpec + static bool hasDirection( QColor::Spec colorSpec ); }; diff --git a/tests/src/gui/testqgscompoundcolorwidget.cpp b/tests/src/gui/testqgscompoundcolorwidget.cpp index 16613e117530..90bb2ef7886a 100644 --- a/tests/src/gui/testqgscompoundcolorwidget.cpp +++ b/tests/src/gui/testqgscompoundcolorwidget.cpp @@ -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()