Skip to content

Commit

Permalink
ui(ColorWidget): Display CMYK components as percent
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Aug 13, 2024
1 parent 62c7648 commit 6b97709
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 21 deletions.
6 changes: 6 additions & 0 deletions python/PyQt6/gui/auto_additions/qgscolorwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions python/PyQt6/gui/auto_generated/qgscolorwidgets.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions python/gui/auto_additions/qgscolorwidgets.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# 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
13 changes: 13 additions & 0 deletions python/gui/auto_generated/qgscolorwidgets.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down
47 changes: 27 additions & 20 deletions src/gui/qgscolorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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<int>( std::round( componentValueF( component ) * static_cast<float>( componentRange( component ) ) ) );
Expand Down Expand Up @@ -1445,15 +1465,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 );

Expand All @@ -1466,15 +1477,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<float>( 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
Expand Down Expand Up @@ -1527,13 +1536,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;
}
Expand All @@ -1545,11 +1552,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;
}
Expand Down
16 changes: 16 additions & 0 deletions src/gui/qgscolorwidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

/**
Expand Down
37 changes: 36 additions & 1 deletion tests/src/gui/testqgscompoundcolorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TestQgsCompoundColorWidget : public QgsTest
void testModelChange();
void testTabChange();
void testSliderWidgets();
void testSliderWidgetsCmyk();
};

void TestQgsCompoundColorWidget::initTestCase()
Expand Down Expand Up @@ -232,7 +233,7 @@ void TestQgsCompoundColorWidget::testTabChange()

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 );

QCOMPARE( w.mRedSlider->mSpinBox->value(), 30.6 );
Expand Down Expand Up @@ -270,6 +271,40 @@ void TestQgsCompoundColorWidget::testSliderWidgets()
compareFloat( w.mValueSlider->mRampWidget->color().greenF(), 0.5f );
}

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<float>( w.mCyanSlider->mRampWidget->width() ) / 2.f, 0.f ) );
w.mMagentaSlider->mRampWidget->setColorFromPoint( QPointF( static_cast<float>( w.mMagentaSlider->mRampWidget->width() ) / 2.f, 0.f ) );
w.mYellowSlider->mRampWidget->setColorFromPoint( QPointF( static_cast<float>( w.mYellowSlider->mRampWidget->width() ) / 2.f, 0.f ) );
w.mBlackSlider->mRampWidget->setColorFromPoint( QPointF( static_cast<float>( w.mBlackSlider->mRampWidget->width() ) / 2.f, 0.f ) );
w.mAlphaSlider->mRampWidget->setColorFromPoint( QPointF( static_cast<float>( 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"

0 comments on commit 6b97709

Please sign in to comment.