From 358f3eb8e73318fb3c4ded95742e1632819c7cc4 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Thu, 22 Aug 2024 11:47:53 +0200 Subject: [PATCH 1/4] fix(ColorWidget): Synchronize color model with color ramp one Fixes #58324 --- .../auto_generated/qgscompoundcolorwidget.sip.in | 10 ++++++++++ .../auto_generated/qgscompoundcolorwidget.sip.in | 10 ++++++++++ src/gui/qgscompoundcolorwidget.cpp | 5 +++++ src/gui/qgscompoundcolorwidget.h | 9 +++++++++ src/gui/qgsgradientcolorrampdialog.cpp | 14 ++++++++++++-- tests/src/gui/testqgscompoundcolorwidget.cpp | 12 ++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) 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..1984669418bb 100644 --- a/src/gui/qgsgradientcolorrampdialog.cpp +++ b/src/gui/qgsgradientcolorrampdialog.cpp @@ -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 ) ); @@ -66,11 +69,18 @@ QgsGradientColorRampDialog::QgsGradientColorRampDialog( const QgsGradientColorRa 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( 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( &QComboBox::currentIndexChanged ), this, [ = ] 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() From 5e88de8b53000d60da9ba6deb77987053fbe9cf4 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Mon, 26 Aug 2024 08:54:41 +0200 Subject: [PATCH 2/4] style(ColorWidget): Use switch to properly support direction or not --- src/gui/qgsgradientcolorrampdialog.cpp | 24 ++++++++++++++++++++---- src/gui/qgsgradientcolorrampdialog.h | 3 +++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/gui/qgsgradientcolorrampdialog.cpp b/src/gui/qgsgradientcolorrampdialog.cpp index 1984669418bb..91a867d2ccbd 100644 --- a/src/gui/qgsgradientcolorrampdialog.cpp +++ b/src/gui/qgsgradientcolorrampdialog.cpp @@ -65,12 +65,12 @@ QgsGradientColorRampDialog::QgsGradientColorRampDialog( const QgsGradientColorRa 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( supportDirection( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) ) ); connect( mStopColorSpec, static_cast( &QComboBox::currentIndexChanged ), this, [ = ] { const QColor::Spec colorSpec = static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ); - mStopDirection->setEnabled( colorSpec != QColor::Spec::Rgb ); + mStopDirection->setEnabled( supportDirection( colorSpec ) ); if ( colorSpec != mColorWidget->color().spec() ) { @@ -369,7 +369,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() ) && supportDirection( mStopEditor->selectedStop().colorSpec() ) ); updatePlot(); } @@ -539,6 +539,22 @@ void QgsGradientColorRampDialog::addMarkersForColor( double x, const QColor &col addPlotMarker( x, color.alphaF(), color, isSelected && mCurrentPlotColorComponent == 3 ); } +bool QgsGradientColorRampDialog::supportDirection( 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; + } +} + void QgsGradientColorRampDialog::updatePlot() { // remove existing markers @@ -619,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() ) && supportDirection( mStopEditor->selectedStop().colorSpec() ) ); updateColorButtons(); updatePlot(); diff --git a/src/gui/qgsgradientcolorrampdialog.h b/src/gui/qgsgradientcolorrampdialog.h index 049093297165..3cc43cf6f178 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 supportDirection( QColor::Spec colorSpec ); }; From c02e8bebe4e4e29c0590c97d2b6195453221a858 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Mon, 26 Aug 2024 17:44:48 +0200 Subject: [PATCH 3/4] style(ColorWidget): make cppcheck happy --- src/gui/qgsgradientcolorrampdialog.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gui/qgsgradientcolorrampdialog.cpp b/src/gui/qgsgradientcolorrampdialog.cpp index 91a867d2ccbd..fbfd9c201625 100644 --- a/src/gui/qgsgradientcolorrampdialog.cpp +++ b/src/gui/qgsgradientcolorrampdialog.cpp @@ -553,6 +553,8 @@ bool QgsGradientColorRampDialog::supportDirection( QColor::Spec colorSpec ) case QColor::Spec::Hsl: return true; } + + BUILTIN_UNREACHABLE } void QgsGradientColorRampDialog::updatePlot() From 28b3ef00f4b8635efa24de0959f9d329b7dd98c1 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Mon, 26 Aug 2024 17:50:36 +0200 Subject: [PATCH 4/4] style(ColorWidget): s/supportDirection/hasDirection --- src/gui/qgsgradientcolorrampdialog.cpp | 12 +++++------- src/gui/qgsgradientcolorrampdialog.h | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/gui/qgsgradientcolorrampdialog.cpp b/src/gui/qgsgradientcolorrampdialog.cpp index fbfd9c201625..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" @@ -65,12 +63,12 @@ QgsGradientColorRampDialog::QgsGradientColorRampDialog( const QgsGradientColorRa mStopDirection->addItem( tr( "Counterclockwise" ), static_cast< int >( Qgis::AngularDirection::CounterClockwise ) ); mStopDirection->setCurrentIndex( mStopColorSpec->findData( static_cast< int >( ramp.direction() ) ) ); - mStopDirection->setEnabled( supportDirection( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) ) ); + mStopDirection->setEnabled( hasDirection( static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ) ) ); connect( mStopColorSpec, static_cast( &QComboBox::currentIndexChanged ), this, [ = ] { const QColor::Spec colorSpec = static_cast< QColor::Spec>( mStopColorSpec->currentData().toInt() ); - mStopDirection->setEnabled( supportDirection( colorSpec ) ); + mStopDirection->setEnabled( hasDirection( colorSpec ) ); if ( colorSpec != mColorWidget->color().spec() ) { @@ -369,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() ) && supportDirection( mStopEditor->selectedStop().colorSpec() ) ); + mStopDirection->setEnabled( !( stop.offset == 0 && stop.color == mRamp.color1() ) && hasDirection( mStopEditor->selectedStop().colorSpec() ) ); updatePlot(); } @@ -539,7 +537,7 @@ void QgsGradientColorRampDialog::addMarkersForColor( double x, const QColor &col addPlotMarker( x, color.alphaF(), color, isSelected && mCurrentPlotColorComponent == 3 ); } -bool QgsGradientColorRampDialog::supportDirection( QColor::Spec colorSpec ) +bool QgsGradientColorRampDialog::hasDirection( QColor::Spec colorSpec ) { switch ( colorSpec ) { @@ -637,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() ) && supportDirection( mStopEditor->selectedStop().colorSpec() ) ); + 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 3cc43cf6f178..ddabdc26cade 100644 --- a/src/gui/qgsgradientcolorrampdialog.h +++ b/src/gui/qgsgradientcolorrampdialog.h @@ -123,7 +123,7 @@ class GUI_EXPORT QgsGradientColorRampDialog : public QDialog, private Ui::QgsGra void addMarkersForColor( double x, const QColor &color, bool isSelected = false ); // Returns TRUE if it's possible to edit direction regarding given colorSpec - static bool supportDirection( QColor::Spec colorSpec ); + static bool hasDirection( QColor::Spec colorSpec ); };