From 5deb18f91c8cbc80c369b334c45920a3f1e8101e Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Tue, 13 Aug 2024 15:41:06 +0200 Subject: [PATCH 1/8] ui(ColorWidget): Display CMYK components as percent --- .../gui/auto_additions/qgscolorwidgets.py | 6 +++ .../gui/auto_generated/qgscolorwidgets.sip.in | 13 +++++ python/gui/auto_additions/qgscolorwidgets.py | 6 +++ .../gui/auto_generated/qgscolorwidgets.sip.in | 13 +++++ src/gui/qgscolorwidgets.cpp | 47 +++++++++++-------- src/gui/qgscolorwidgets.h | 16 +++++++ tests/src/gui/testqgscompoundcolorwidget.cpp | 37 ++++++++++++++- 7 files changed, 117 insertions(+), 21 deletions(-) diff --git a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py index 27fc2aeb2a22..0ea5af920b8e 100644 --- a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py +++ b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py @@ -11,6 +11,12 @@ QgsColorWidget.Magenta = QgsColorWidget.ColorComponent.Magenta QgsColorWidget.Yellow = QgsColorWidget.ColorComponent.Yellow QgsColorWidget.Black = QgsColorWidget.ColorComponent.Black +# monkey patching scoped based enum +QgsColorWidget.ComponentUnit.Raw.__doc__ = "Raw values in the range 0-255" +QgsColorWidget.ComponentUnit.Percent.__doc__ = "Percent values in the range 0-100" +QgsColorWidget.ComponentUnit.Degree.__doc__ = "Degree values in the range 0-359" +QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Raw``: ' + QgsColorWidget.ComponentUnit.Raw.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ +# -- QgsColorRampWidget.Horizontal = QgsColorRampWidget.Orientation.Horizontal QgsColorRampWidget.Vertical = QgsColorRampWidget.Orientation.Vertical QgsColorTextWidget.HexRgb = QgsColorTextWidget.ColorTextFormat.HexRgb diff --git a/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in b/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in index 608bf506b7eb..b8968eec9328 100644 --- a/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in +++ b/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in @@ -40,6 +40,13 @@ set to a color with an ambiguous hue (e.g., black or white shades). Black }; + enum class ComponentUnit + { + Raw, + Percent, + Degree + }; + QgsColorWidget( QWidget *parent /TransferThis/ = 0, ColorComponent component = Multiple ); %Docstring Construct a new color widget. @@ -102,6 +109,12 @@ Create an icon for dragging colors :param color: for icon %End + static ComponentUnit componentUnit( ColorComponent component ); +%Docstring +Returns color ``component`` unit +%End + + public slots: virtual void setColor( const QColor &color, bool emitSignals = false ); diff --git a/python/gui/auto_additions/qgscolorwidgets.py b/python/gui/auto_additions/qgscolorwidgets.py index 07356589ae5a..22e7fc60a812 100644 --- a/python/gui/auto_additions/qgscolorwidgets.py +++ b/python/gui/auto_additions/qgscolorwidgets.py @@ -1,4 +1,10 @@ # The following has been generated automatically from src/gui/qgscolorwidgets.h +# monkey patching scoped based enum +QgsColorWidget.ComponentUnit.Raw.__doc__ = "Raw values in the range 0-255" +QgsColorWidget.ComponentUnit.Percent.__doc__ = "Percent values in the range 0-100" +QgsColorWidget.ComponentUnit.Degree.__doc__ = "Degree values in the range 0-359" +QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Raw``: ' + QgsColorWidget.ComponentUnit.Raw.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ +# -- QgsColorTextWidget.ColorTextFormat.baseClass = QgsColorTextWidget try: QgsColorWidget.__attribute_docs__ = {'colorChanged': "Emitted when the widget's color changes\n\n:param color: new widget color\n", 'hovered': 'Emitted when mouse hovers over widget.\n'} diff --git a/python/gui/auto_generated/qgscolorwidgets.sip.in b/python/gui/auto_generated/qgscolorwidgets.sip.in index 523e69f22554..32c6335a5d45 100644 --- a/python/gui/auto_generated/qgscolorwidgets.sip.in +++ b/python/gui/auto_generated/qgscolorwidgets.sip.in @@ -40,6 +40,13 @@ set to a color with an ambiguous hue (e.g., black or white shades). Black }; + enum class ComponentUnit + { + Raw, + Percent, + Degree + }; + QgsColorWidget( QWidget *parent /TransferThis/ = 0, ColorComponent component = Multiple ); %Docstring Construct a new color widget. @@ -102,6 +109,12 @@ Create an icon for dragging colors :param color: for icon %End + static ComponentUnit componentUnit( ColorComponent component ); +%Docstring +Returns color ``component`` unit +%End + + public slots: virtual void setColor( const QColor &color, bool emitSignals = false ); diff --git a/src/gui/qgscolorwidgets.cpp b/src/gui/qgscolorwidgets.cpp index 9e7c72738d14..620108b36d39 100644 --- a/src/gui/qgscolorwidgets.cpp +++ b/src/gui/qgscolorwidgets.cpp @@ -93,6 +93,26 @@ QPixmap QgsColorWidget::createDragIcon( const QColor &color ) return pixmap; } +QgsColorWidget::ComponentUnit QgsColorWidget::componentUnit( ColorComponent component ) +{ + switch ( component ) + { + case QgsColorWidget::Hue: + return ComponentUnit::Degree; + case QgsColorWidget::Saturation: + case QgsColorWidget::Value: + case QgsColorWidget::Alpha: + case QgsColorWidget::Cyan: + case QgsColorWidget::Magenta: + case QgsColorWidget::Yellow: + case QgsColorWidget::Black: + return ComponentUnit::Percent; + + default: + return ComponentUnit::Raw; + } +} + int QgsColorWidget::componentValue( const QgsColorWidget::ColorComponent component ) const { return static_cast( std::round( componentValueF( component ) * static_cast( componentRange( component ) ) ) ); @@ -1447,15 +1467,6 @@ QgsColorSliderWidget::QgsColorSliderWidget( QWidget *parent, const ColorComponen mSpinBox->setMinimum( 0 ); mSpinBox->setMaximum( convertRealToDisplay( 1.f ) ); mSpinBox->setValue( convertRealToDisplay( componentValueF() ) ); - if ( component == QgsColorWidget::Hue ) - { - //degrees suffix for hue - mSpinBox->setSuffix( QChar( 176 ) ); - } - else if ( component == QgsColorWidget::Saturation || component == QgsColorWidget::Value || component == QgsColorWidget::Alpha ) - { - mSpinBox->setSuffix( tr( "%" ) ); - } hLayout->addWidget( mSpinBox ); setLayout( hLayout ); @@ -1468,15 +1479,13 @@ void QgsColorSliderWidget::setComponent( const QgsColorWidget::ColorComponent co { QgsColorWidget::setComponent( component ); mRampWidget->setComponent( component ); - mSpinBox->setMaximum( convertRealToDisplay( componentRange() ) ); - if ( component == QgsColorWidget::Hue ) + mSpinBox->setMaximum( convertRealToDisplay( static_cast( componentRange() ) ) ); + if ( componentUnit( component ) == ComponentUnit::Degree ) { - //degrees suffix for hue mSpinBox->setSuffix( QChar( 176 ) ); } - else if ( component == QgsColorWidget::Saturation || component == QgsColorWidget::Value || component == QgsColorWidget::Alpha ) + else if ( componentUnit( component ) == ComponentUnit::Percent ) { - //saturation, value and alpha are in % mSpinBox->setSuffix( tr( "%" ) ); } else @@ -1529,13 +1538,11 @@ void QgsColorSliderWidget::rampChanged( float value ) float QgsColorSliderWidget::convertRealToDisplay( const float realValue ) const { - //scale saturation, value or alpha to 0->100 range. This makes more sense for users - //for whom "255" is a totally arbitrary value! - if ( mComponent == QgsColorWidget::Saturation || mComponent == QgsColorWidget::Value || mComponent == QgsColorWidget::Alpha ) + if ( componentUnit( mComponent ) == ComponentUnit::Percent ) { return realValue * 100.f; } - else if ( mComponent == QgsColorWidget::Hue ) + else if ( componentUnit( mComponent ) == ComponentUnit::Degree ) { return realValue * HUE_MAX; } @@ -1547,11 +1554,11 @@ float QgsColorSliderWidget::convertRealToDisplay( const float realValue ) const float QgsColorSliderWidget::convertDisplayToReal( const float displayValue ) const { - if ( mComponent == QgsColorWidget::Saturation || mComponent == QgsColorWidget::Value || mComponent == QgsColorWidget::Alpha ) + if ( componentUnit( mComponent ) == ComponentUnit::Percent ) { return displayValue / 100.f; } - else if ( mComponent == QgsColorWidget::Hue ) + else if ( componentUnit( mComponent ) == ComponentUnit::Degree ) { return displayValue / HUE_MAX; } diff --git a/src/gui/qgscolorwidgets.h b/src/gui/qgscolorwidgets.h index 2d8cd2a0441a..88ad9edb0ff9 100644 --- a/src/gui/qgscolorwidgets.h +++ b/src/gui/qgscolorwidgets.h @@ -60,6 +60,16 @@ class GUI_EXPORT QgsColorWidget : public QWidget Black //!< Black component (based on CMYK model) of color }; + /** + * Specified the color component unit + */ + enum class ComponentUnit + { + Raw, //!< Raw values in the range 0-255 + Percent, //!< Percent values in the range 0-100 + Degree //!< Degree values in the range 0-359 + }; + /** * Construct a new color widget. * \param parent parent QWidget for the widget @@ -107,6 +117,12 @@ class GUI_EXPORT QgsColorWidget : public QWidget */ static QPixmap createDragIcon( const QColor &color ); + /** + * Returns color \a component unit + */ + static ComponentUnit componentUnit( ColorComponent component ); + + public slots: /** diff --git a/tests/src/gui/testqgscompoundcolorwidget.cpp b/tests/src/gui/testqgscompoundcolorwidget.cpp index 16613e117530..e8e692525d91 100644 --- a/tests/src/gui/testqgscompoundcolorwidget.cpp +++ b/tests/src/gui/testqgscompoundcolorwidget.cpp @@ -47,6 +47,7 @@ class TestQgsCompoundColorWidget : public QgsTest void testTabChange(); void testInvalidColor(); void testSliderWidgets(); + void testSliderWidgetsCmyk(); }; void TestQgsCompoundColorWidget::initTestCase() @@ -245,7 +246,7 @@ void TestQgsCompoundColorWidget::testInvalidColor() void TestQgsCompoundColorWidget::testSliderWidgets() { - QgsCompoundColorWidget w( nullptr, QColor::fromRgbF( 0.12f, 0.34f, 0.56, 0.78 ) ); + QgsCompoundColorWidget w( nullptr, QColor::fromRgbF( 0.12f, 0.34f, 0.56f, 0.78f ) ); w.setVisible( true ); // TODO QGIS 4 remove the nolint instructions, QColor was qreal (double) and is now float @@ -286,6 +287,40 @@ void TestQgsCompoundColorWidget::testSliderWidgets() // NOLINTEND(bugprone-narrowing-conversions) } +void TestQgsCompoundColorWidget::testSliderWidgetsCmyk() +{ + QgsCompoundColorWidget w( nullptr, QColor::fromCmykF( 0.12f, 0.34f, 0.56f, 0.78f, 0.91f ) ); + w.setVisible( true ); + + QCOMPARE( w.mCyanSlider->mSpinBox->value(), 12 ); + compareFloat( w.mCyanSlider->mRampWidget->color().cyanF(), 0.12f ); + QCOMPARE( w.mMagentaSlider->mSpinBox->value(), 34 ); + compareFloat( w.mMagentaSlider->mRampWidget->color().magentaF(), 0.34f ); + QCOMPARE( w.mYellowSlider->mSpinBox->value(), 56 ); + compareFloat( w.mYellowSlider->mRampWidget->color().yellowF(), 0.56f ); + QCOMPARE( w.mBlackSlider->mSpinBox->value(), 78 ); + compareFloat( w.mBlackSlider->mRampWidget->color().blackF(), 0.78f ); + QCOMPARE( w.mAlphaSlider->mSpinBox->value(), 91 ); + compareFloat( w.mAlphaSlider->mRampWidget->color().alphaF(), 0.91f ); + + w.mCyanSlider->mRampWidget->setColorFromPoint( QPointF( static_cast( w.mCyanSlider->mRampWidget->width() ) / 2.f, 0.f ) ); + w.mMagentaSlider->mRampWidget->setColorFromPoint( QPointF( static_cast( w.mMagentaSlider->mRampWidget->width() ) / 2.f, 0.f ) ); + w.mYellowSlider->mRampWidget->setColorFromPoint( QPointF( static_cast( w.mYellowSlider->mRampWidget->width() ) / 2.f, 0.f ) ); + w.mBlackSlider->mRampWidget->setColorFromPoint( QPointF( static_cast( w.mBlackSlider->mRampWidget->width() ) / 2.f, 0.f ) ); + w.mAlphaSlider->mRampWidget->setColorFromPoint( QPointF( static_cast( w.mAlphaSlider->mRampWidget->width() ) / 2.f, 0.f ) ); + + QCOMPARE( w.mCyanSlider->mSpinBox->value(), 50 ); + compareFloat( w.mCyanSlider->mRampWidget->color().cyanF(), 0.5f ); + QCOMPARE( w.mMagentaSlider->mSpinBox->value(), 50 ); + compareFloat( w.mMagentaSlider->mRampWidget->color().magentaF(), 0.5f ); + QCOMPARE( w.mYellowSlider->mSpinBox->value(), 50 ); + compareFloat( w.mYellowSlider->mRampWidget->color().yellowF(), 0.5f ); + QCOMPARE( w.mBlackSlider->mSpinBox->value(), 50 ); + compareFloat( w.mBlackSlider->mRampWidget->color().blackF(), 0.5f ); + QCOMPARE( w.mAlphaSlider->mSpinBox->value(), 50 ); + compareFloat( w.mAlphaSlider->mRampWidget->color().alphaF(), 0.5f ); +} + QGSTEST_MAIN( TestQgsCompoundColorWidget ) #include "testqgscompoundcolorwidget.moc" From bdb835d16498e71a578b6bce6bade537970bdfc6 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 14 Aug 2024 10:04:46 +0200 Subject: [PATCH 2/8] fix(ColorWidget): Add missing Q_ENUM --- python/PyQt6/gui/auto_additions/qgscolorwidgets.py | 1 + python/gui/auto_additions/qgscolorwidgets.py | 1 + src/gui/qgscolorwidgets.h | 1 + 3 files changed, 3 insertions(+) diff --git a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py index 0ea5af920b8e..4fa6e17dc227 100644 --- a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py +++ b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py @@ -17,6 +17,7 @@ QgsColorWidget.ComponentUnit.Degree.__doc__ = "Degree values in the range 0-359" QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Raw``: ' + QgsColorWidget.ComponentUnit.Raw.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ # -- +QgsColorWidget.ComponentUnit.baseClass = QgsColorWidget QgsColorRampWidget.Horizontal = QgsColorRampWidget.Orientation.Horizontal QgsColorRampWidget.Vertical = QgsColorRampWidget.Orientation.Vertical QgsColorTextWidget.HexRgb = QgsColorTextWidget.ColorTextFormat.HexRgb diff --git a/python/gui/auto_additions/qgscolorwidgets.py b/python/gui/auto_additions/qgscolorwidgets.py index 22e7fc60a812..0096780251e1 100644 --- a/python/gui/auto_additions/qgscolorwidgets.py +++ b/python/gui/auto_additions/qgscolorwidgets.py @@ -5,6 +5,7 @@ QgsColorWidget.ComponentUnit.Degree.__doc__ = "Degree values in the range 0-359" QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Raw``: ' + QgsColorWidget.ComponentUnit.Raw.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ # -- +QgsColorWidget.ComponentUnit.baseClass = QgsColorWidget QgsColorTextWidget.ColorTextFormat.baseClass = QgsColorTextWidget try: QgsColorWidget.__attribute_docs__ = {'colorChanged': "Emitted when the widget's color changes\n\n:param color: new widget color\n", 'hovered': 'Emitted when mouse hovers over widget.\n'} diff --git a/src/gui/qgscolorwidgets.h b/src/gui/qgscolorwidgets.h index 88ad9edb0ff9..f702ce28e0d4 100644 --- a/src/gui/qgscolorwidgets.h +++ b/src/gui/qgscolorwidgets.h @@ -69,6 +69,7 @@ class GUI_EXPORT QgsColorWidget : public QWidget Percent, //!< Percent values in the range 0-100 Degree //!< Degree values in the range 0-359 }; + Q_ENUM( ComponentUnit ) /** * Construct a new color widget. From 7ccfda75720d0e97cd26bc46b863ab26dac74881 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 14 Aug 2024 10:05:52 +0200 Subject: [PATCH 3/8] fix(ColorWidget): Remove switch default instruction --- src/gui/qgscolorwidgets.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/gui/qgscolorwidgets.cpp b/src/gui/qgscolorwidgets.cpp index 620108b36d39..708c44592e25 100644 --- a/src/gui/qgscolorwidgets.cpp +++ b/src/gui/qgscolorwidgets.cpp @@ -108,7 +108,10 @@ QgsColorWidget::ComponentUnit QgsColorWidget::componentUnit( ColorComponent comp case QgsColorWidget::Black: return ComponentUnit::Percent; - default: + case QgsColorWidget::Multiple: + case QgsColorWidget::Red: + case QgsColorWidget::Green: + case QgsColorWidget::Blue: return ComponentUnit::Raw; } } From c24b83509050151544158177781bfa2ca32f1dd1 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 14 Aug 2024 13:45:35 +0200 Subject: [PATCH 4/8] style(ColorWidget): make clang-tidy happy --- tests/src/gui/testqgscompoundcolorwidget.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/src/gui/testqgscompoundcolorwidget.cpp b/tests/src/gui/testqgscompoundcolorwidget.cpp index e8e692525d91..88dbeb8a0bc3 100644 --- a/tests/src/gui/testqgscompoundcolorwidget.cpp +++ b/tests/src/gui/testqgscompoundcolorwidget.cpp @@ -292,6 +292,8 @@ void TestQgsCompoundColorWidget::testSliderWidgetsCmyk() QgsCompoundColorWidget w( nullptr, QColor::fromCmykF( 0.12f, 0.34f, 0.56f, 0.78f, 0.91f ) ); w.setVisible( true ); + // TODO QGIS 4 remove the nolint instructions, QColor was qreal (double) and is now float + // NOLINTBEGIN(bugprone-narrowing-conversions) QCOMPARE( w.mCyanSlider->mSpinBox->value(), 12 ); compareFloat( w.mCyanSlider->mRampWidget->color().cyanF(), 0.12f ); QCOMPARE( w.mMagentaSlider->mSpinBox->value(), 34 ); @@ -319,6 +321,7 @@ void TestQgsCompoundColorWidget::testSliderWidgetsCmyk() compareFloat( w.mBlackSlider->mRampWidget->color().blackF(), 0.5f ); QCOMPARE( w.mAlphaSlider->mSpinBox->value(), 50 ); compareFloat( w.mAlphaSlider->mRampWidget->color().alphaF(), 0.5f ); + // NOLINTEND(bugprone-narrowing-conversions) } From a24779fdebc1d77cbbc4acde8f4e800c83ec4714 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 21 Aug 2024 13:46:45 +0200 Subject: [PATCH 5/8] fix(ColorWidget): Run sipify since new static wrapper --- python/PyQt6/gui/auto_additions/qgscolorwidgets.py | 1 + python/gui/auto_additions/qgscolorwidgets.py | 1 + 2 files changed, 2 insertions(+) diff --git a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py index 4fa6e17dc227..29f8cb7768b6 100644 --- a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py +++ b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py @@ -38,5 +38,6 @@ except NameError: pass QgsColorWidget.createDragIcon = staticmethod(QgsColorWidget.createDragIcon) +QgsColorWidget.componentUnit = staticmethod(QgsColorWidget.componentUnit) QgsColorWidget.alterColor = staticmethod(QgsColorWidget.alterColor) QgsColorWidget.alterColorF = staticmethod(QgsColorWidget.alterColorF) diff --git a/python/gui/auto_additions/qgscolorwidgets.py b/python/gui/auto_additions/qgscolorwidgets.py index 0096780251e1..d5ff0f57d5ac 100644 --- a/python/gui/auto_additions/qgscolorwidgets.py +++ b/python/gui/auto_additions/qgscolorwidgets.py @@ -20,5 +20,6 @@ except NameError: pass QgsColorWidget.createDragIcon = staticmethod(QgsColorWidget.createDragIcon) +QgsColorWidget.componentUnit = staticmethod(QgsColorWidget.componentUnit) QgsColorWidget.alterColor = staticmethod(QgsColorWidget.alterColor) QgsColorWidget.alterColorF = staticmethod(QgsColorWidget.alterColorF) From 1a47480c2bd9c4ed63dffefd83c9c9a916dfb37a Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Wed, 21 Aug 2024 14:17:33 +0200 Subject: [PATCH 6/8] style(ColorWidgets): Make cppcheck happy --- src/gui/qgscolorwidgets.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gui/qgscolorwidgets.cpp b/src/gui/qgscolorwidgets.cpp index 708c44592e25..b9d846e36eb3 100644 --- a/src/gui/qgscolorwidgets.cpp +++ b/src/gui/qgscolorwidgets.cpp @@ -114,6 +114,8 @@ QgsColorWidget::ComponentUnit QgsColorWidget::componentUnit( ColorComponent comp case QgsColorWidget::Blue: return ComponentUnit::Raw; } + + BUILTIN_UNREACHABLE } int QgsColorWidget::componentValue( const QgsColorWidget::ColorComponent component ) const From 3b7a3c0060f2f04a031d0508979c83381a6785a9 Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Thu, 22 Aug 2024 09:58:55 +0200 Subject: [PATCH 7/8] style(ColorWidgets): Rename Raw enum to Scaled0to255 --- python/PyQt6/gui/auto_additions/qgscolorwidgets.py | 4 ++-- python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in | 2 +- python/gui/auto_additions/qgscolorwidgets.py | 4 ++-- python/gui/auto_generated/qgscolorwidgets.sip.in | 2 +- src/gui/qgscolorwidgets.cpp | 2 +- src/gui/qgscolorwidgets.h | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py index 29f8cb7768b6..98adb7ba6b43 100644 --- a/python/PyQt6/gui/auto_additions/qgscolorwidgets.py +++ b/python/PyQt6/gui/auto_additions/qgscolorwidgets.py @@ -12,10 +12,10 @@ QgsColorWidget.Yellow = QgsColorWidget.ColorComponent.Yellow QgsColorWidget.Black = QgsColorWidget.ColorComponent.Black # monkey patching scoped based enum -QgsColorWidget.ComponentUnit.Raw.__doc__ = "Raw values in the range 0-255" +QgsColorWidget.ComponentUnit.Scaled0to255.__doc__ = "Values in the range 0-255" QgsColorWidget.ComponentUnit.Percent.__doc__ = "Percent values in the range 0-100" QgsColorWidget.ComponentUnit.Degree.__doc__ = "Degree values in the range 0-359" -QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Raw``: ' + QgsColorWidget.ComponentUnit.Raw.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ +QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Scaled0to255``: ' + QgsColorWidget.ComponentUnit.Scaled0to255.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ # -- QgsColorWidget.ComponentUnit.baseClass = QgsColorWidget QgsColorRampWidget.Horizontal = QgsColorRampWidget.Orientation.Horizontal diff --git a/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in b/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in index b8968eec9328..d6c6d0d2b13f 100644 --- a/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in +++ b/python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in @@ -42,7 +42,7 @@ set to a color with an ambiguous hue (e.g., black or white shades). enum class ComponentUnit { - Raw, + Scaled0to255, Percent, Degree }; diff --git a/python/gui/auto_additions/qgscolorwidgets.py b/python/gui/auto_additions/qgscolorwidgets.py index d5ff0f57d5ac..495f6b8847cb 100644 --- a/python/gui/auto_additions/qgscolorwidgets.py +++ b/python/gui/auto_additions/qgscolorwidgets.py @@ -1,9 +1,9 @@ # The following has been generated automatically from src/gui/qgscolorwidgets.h # monkey patching scoped based enum -QgsColorWidget.ComponentUnit.Raw.__doc__ = "Raw values in the range 0-255" +QgsColorWidget.ComponentUnit.Scaled0to255.__doc__ = "Values in the range 0-255" QgsColorWidget.ComponentUnit.Percent.__doc__ = "Percent values in the range 0-100" QgsColorWidget.ComponentUnit.Degree.__doc__ = "Degree values in the range 0-359" -QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Raw``: ' + QgsColorWidget.ComponentUnit.Raw.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ +QgsColorWidget.ComponentUnit.__doc__ = "Specified the color component unit\n\n" + '* ``Scaled0to255``: ' + QgsColorWidget.ComponentUnit.Scaled0to255.__doc__ + '\n' + '* ``Percent``: ' + QgsColorWidget.ComponentUnit.Percent.__doc__ + '\n' + '* ``Degree``: ' + QgsColorWidget.ComponentUnit.Degree.__doc__ # -- QgsColorWidget.ComponentUnit.baseClass = QgsColorWidget QgsColorTextWidget.ColorTextFormat.baseClass = QgsColorTextWidget diff --git a/python/gui/auto_generated/qgscolorwidgets.sip.in b/python/gui/auto_generated/qgscolorwidgets.sip.in index 32c6335a5d45..752505ac2b2e 100644 --- a/python/gui/auto_generated/qgscolorwidgets.sip.in +++ b/python/gui/auto_generated/qgscolorwidgets.sip.in @@ -42,7 +42,7 @@ set to a color with an ambiguous hue (e.g., black or white shades). enum class ComponentUnit { - Raw, + Scaled0to255, Percent, Degree }; diff --git a/src/gui/qgscolorwidgets.cpp b/src/gui/qgscolorwidgets.cpp index b9d846e36eb3..ab1ec1d55e8c 100644 --- a/src/gui/qgscolorwidgets.cpp +++ b/src/gui/qgscolorwidgets.cpp @@ -112,7 +112,7 @@ QgsColorWidget::ComponentUnit QgsColorWidget::componentUnit( ColorComponent comp case QgsColorWidget::Red: case QgsColorWidget::Green: case QgsColorWidget::Blue: - return ComponentUnit::Raw; + return ComponentUnit::Scaled0to255; } BUILTIN_UNREACHABLE diff --git a/src/gui/qgscolorwidgets.h b/src/gui/qgscolorwidgets.h index f702ce28e0d4..8abb076ba8ef 100644 --- a/src/gui/qgscolorwidgets.h +++ b/src/gui/qgscolorwidgets.h @@ -65,7 +65,7 @@ class GUI_EXPORT QgsColorWidget : public QWidget */ enum class ComponentUnit { - Raw, //!< Raw values in the range 0-255 + Scaled0to255, //!< Values in the range 0-255 Percent, //!< Percent values in the range 0-100 Degree //!< Degree values in the range 0-359 }; From dac3d308f10b5b38c4f0166105a879022fc7eb8d Mon Sep 17 00:00:00 2001 From: Julien Cabieces Date: Thu, 22 Aug 2024 10:12:16 +0200 Subject: [PATCH 8/8] style(ColorWidget): Remplace if/else with switch --- src/gui/qgscolorwidgets.cpp | 66 ++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/gui/qgscolorwidgets.cpp b/src/gui/qgscolorwidgets.cpp index ab1ec1d55e8c..b81e141a7142 100644 --- a/src/gui/qgscolorwidgets.cpp +++ b/src/gui/qgscolorwidgets.cpp @@ -1485,18 +1485,20 @@ void QgsColorSliderWidget::setComponent( const QgsColorWidget::ColorComponent co QgsColorWidget::setComponent( component ); mRampWidget->setComponent( component ); mSpinBox->setMaximum( convertRealToDisplay( static_cast( componentRange() ) ) ); - if ( componentUnit( component ) == ComponentUnit::Degree ) - { - mSpinBox->setSuffix( QChar( 176 ) ); - } - else if ( componentUnit( component ) == ComponentUnit::Percent ) - { - mSpinBox->setSuffix( tr( "%" ) ); - } - else + + switch ( componentUnit( component ) ) { - //clear suffix - mSpinBox->setSuffix( QString() ); + case ComponentUnit::Degree: + mSpinBox->setSuffix( QChar( 176 ) ); + break; + + case ComponentUnit::Percent: + mSpinBox->setSuffix( tr( "%" ) ); + break; + + case ComponentUnit::Scaled0to255: + //clear suffix + mSpinBox->setSuffix( QString() ); } } @@ -1543,34 +1545,36 @@ void QgsColorSliderWidget::rampChanged( float value ) float QgsColorSliderWidget::convertRealToDisplay( const float realValue ) const { - if ( componentUnit( mComponent ) == ComponentUnit::Percent ) - { - return realValue * 100.f; - } - else if ( componentUnit( mComponent ) == ComponentUnit::Degree ) - { - return realValue * HUE_MAX; - } - else + switch ( componentUnit( mComponent ) ) { - return realValue * 255.f; + case ComponentUnit::Percent: + return realValue * 100.f; + + case ComponentUnit::Degree: + return realValue * HUE_MAX; + + case ComponentUnit::Scaled0to255: + return realValue * 255.f; } + + BUILTIN_UNREACHABLE } float QgsColorSliderWidget::convertDisplayToReal( const float displayValue ) const { - if ( componentUnit( mComponent ) == ComponentUnit::Percent ) - { - return displayValue / 100.f; - } - else if ( componentUnit( mComponent ) == ComponentUnit::Degree ) + switch ( componentUnit( mComponent ) ) { - return displayValue / HUE_MAX; - } - else - { - return displayValue / 255.f; + case ComponentUnit::Percent: + return displayValue / 100.f; + + case ComponentUnit::Degree: + return displayValue / HUE_MAX; + + case ComponentUnit::Scaled0to255: + return displayValue / 255.f; } + + BUILTIN_UNREACHABLE } //