Skip to content

Commit

Permalink
Flip QgsDrawSourceEffect from transparency to opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 29, 2017
1 parent 89c2e85 commit 6f4c549
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 67 deletions.
6 changes: 6 additions & 0 deletions doc/api_break.dox
Expand Up @@ -1032,6 +1032,12 @@ willUseEllipsoid() to determine whether ellipsoidal calculations will be perform
- convertMeasurement was removed. Use QgsUnitTypes for conversion instead.


QgsDrawSourceEffect {#qgis_api_break_3_0_QgsDrawSourceEffect}
-------------------

- setTransparency and transparency were removed. Use setOpacity and opacity instead.


QgsDxfExport {#qgis_api_break_3_0_QgsDxfExport}
------------

Expand Down
18 changes: 9 additions & 9 deletions python/core/effects/qgspainteffect.sip
Expand Up @@ -311,20 +311,20 @@ class QgsDrawSourceEffect : QgsPaintEffect
virtual QgsStringMap properties() const;
virtual void readProperties( const QgsStringMap &props );

void setTransparency( const double transparency );
void setOpacity( const double opacity );
%Docstring
Sets the transparency for the effect
\param transparency double between 0 and 1 inclusive, where 0 is fully opaque
and 1 is fully transparent
.. seealso:: transparency
Sets the ``opacity`` for the effect.
\param transparency double between 0 and 1 inclusive, where 0 is fully transparent
and 1 is fully opaque
.. seealso:: opacity()
%End

double transparency() const;
double opacity() const;
%Docstring
Returns the transparency for the effect
:return: transparency value between 0 and 1 inclusive, where 0 is fully opaque
and 1 is fully transparent
.. seealso:: setTransparency
:return: opacity value between 0 and 1 inclusive, where 0 is fully transparent
and 1 is fully opaque
.. seealso:: setOpacity()
:rtype: float
%End

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooloffsetpointsymbol.cpp
Expand Up @@ -174,7 +174,7 @@ void QgsMapToolOffsetPointSymbol::createPreviewItem( QgsMarkerSymbol *markerSymb
}

mOffsetItem = new QgsPointMarkerItem( mCanvas );
mOffsetItem->setTransparency( 0.3 );
mOffsetItem->setOpacity( 0.7 );
mOffsetItem->setSymbol( markerSymbol->clone() );
}

Expand Down
10 changes: 5 additions & 5 deletions src/app/qgspointmarkeritem.cpp
Expand Up @@ -64,7 +64,7 @@ void QgsPointMarkerItem::paint( QPainter *painter )

QgsRenderContext rc = renderContext( painter );

bool useEffect = !qgsDoubleNear( mOpacityEffect->transparency(), 0.0 );
bool useEffect = !qgsDoubleNear( mOpacityEffect->opacity(), 1.0 );
if ( useEffect )
{
//use a paint effect to reduce opacity. If we directly set the opacity on the painter, then the symbol will NOT
Expand Down Expand Up @@ -113,13 +113,13 @@ void QgsPointMarkerItem::updateSize()
setRect( r );
}

void QgsPointMarkerItem::setTransparency( double transparency )
void QgsPointMarkerItem::setOpacity( double opacity )
{
mOpacityEffect->setTransparency( transparency );
mOpacityEffect->setOpacity( opacity );
}

double QgsPointMarkerItem::transparency() const
double QgsPointMarkerItem::opacity() const
{
return mOpacityEffect->transparency();
return mOpacityEffect->opacity();
}

20 changes: 10 additions & 10 deletions src/app/qgspointmarkeritem.h
Expand Up @@ -75,19 +75,19 @@ class APP_EXPORT QgsPointMarkerItem: public QgsMapCanvasItem
*/
void updateSize();

/** Sets the transparency for the marker.
* \param transparency double between 0 and 1 inclusive, where 0 is fully opaque
* and 1 is fully transparent
* \see transparency()
/** Sets the \a opacity for the marker.
* \param opacity double between 0 and 1 inclusive, where 0 is fully transparent
* and 1 is fully opaque
* \see opacity()
*/
void setTransparency( double transparency );
void setOpacity( double opacity );

/** Returns the transparency for the marker.
* \returns transparency value between 0 and 1 inclusive, where 0 is fully opaque
* and 1 is fully transparent
* \see setTransparency()
/** Returns the opacity for the marker.
* \returns opacity value between 0 and 1 inclusive, where 0 is fully transparent
* and 1 is fully opaque
* \see setOpacity()
*/
double transparency() const;
double opacity() const;

private:

Expand Down
24 changes: 17 additions & 7 deletions src/core/effects/qgspainteffect.cpp
Expand Up @@ -249,7 +249,6 @@ QRectF QgsPaintEffect::imageBoundingRect( const QgsRenderContext &context ) cons

QgsDrawSourceEffect::QgsDrawSourceEffect()
: QgsPaintEffect()
, mTransparency( 0.0 )
, mBlendMode( QPainter::CompositionMode_SourceOver )
{

Expand All @@ -269,7 +268,7 @@ void QgsDrawSourceEffect::draw( QgsRenderContext &context )

QPainter *painter = context.painter();

if ( mBlendMode == QPainter::CompositionMode_SourceOver && qgsDoubleNear( mTransparency, 0.0 ) )
if ( mBlendMode == QPainter::CompositionMode_SourceOver && qgsDoubleNear( mOpacity, 1.0 ) )
{
//just draw unmodified source
drawSource( *painter );
Expand All @@ -278,7 +277,7 @@ void QgsDrawSourceEffect::draw( QgsRenderContext &context )
{
//rasterize source and apply modifications
QImage image = sourceAsImage( context )->copy();
QgsImageOperation::multiplyOpacity( image, 1.0 - mTransparency );
QgsImageOperation::multiplyOpacity( image, mOpacity );
painter->save();
painter->setCompositionMode( mBlendMode );
painter->drawImage( imageOffset( context ), image );
Expand All @@ -297,7 +296,7 @@ QgsStringMap QgsDrawSourceEffect::properties() const
props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
props.insert( QStringLiteral( "transparency" ), QString::number( mTransparency ) );
props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
return props;
}

Expand All @@ -309,10 +308,21 @@ void QgsDrawSourceEffect::readProperties( const QgsStringMap &props )
{
mBlendMode = mode;
}
double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
if ( ok )
if ( props.contains( QStringLiteral( "transparency" ) ) )
{
double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
if ( ok )
{
mOpacity = 1.0 - transparency;
}
}
else
{
mTransparency = transparency;
double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
if ( ok )
{
mOpacity = opacity;
}
}
mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
Expand Down
20 changes: 10 additions & 10 deletions src/core/effects/qgspainteffect.h
Expand Up @@ -312,19 +312,19 @@ class CORE_EXPORT QgsDrawSourceEffect : public QgsPaintEffect
virtual QgsStringMap properties() const override;
virtual void readProperties( const QgsStringMap &props ) override;

/** Sets the transparency for the effect
* \param transparency double between 0 and 1 inclusive, where 0 is fully opaque
* and 1 is fully transparent
* \see transparency
/** Sets the \a opacity for the effect.
* \param transparency double between 0 and 1 inclusive, where 0 is fully transparent
* and 1 is fully opaque
* \see opacity()
*/
void setTransparency( const double transparency ) { mTransparency = transparency; }
void setOpacity( const double opacity ) { mOpacity = opacity; }

/** Returns the transparency for the effect
* \returns transparency value between 0 and 1 inclusive, where 0 is fully opaque
* and 1 is fully transparent
* \see setTransparency
* \returns opacity value between 0 and 1 inclusive, where 0 is fully transparent
* and 1 is fully opaque
* \see setOpacity()
*/
double transparency() const { return mTransparency; }
double opacity() const { return mOpacity; }

/** Sets the blend mode for the effect
* \param mode blend mode used for drawing the source on to a destination
Expand All @@ -346,7 +346,7 @@ class CORE_EXPORT QgsDrawSourceEffect : public QgsPaintEffect

private:

double mTransparency;
double mOpacity = 1.0;
QPainter::CompositionMode mBlendMode;
};

Expand Down
23 changes: 12 additions & 11 deletions src/gui/effects/qgspainteffectwidget.cpp
Expand Up @@ -35,6 +35,7 @@ QgsDrawSourceWidget::QgsDrawSourceWidget( QWidget *parent )
, mEffect( nullptr )
{
setupUi( this );
mOpacitySpnBx->setClearValue( 100.0 );
initGui();
}

Expand All @@ -57,8 +58,8 @@ void QgsDrawSourceWidget::initGui()

blockSignals( true );

mTransparencySpnBx->setValue( mEffect->transparency() * 100.0 );
mTransparencySlider->setValue( mEffect->transparency() * 1000.0 );
mOpacitySpnBx->setValue( mEffect->opacity() * 100.0 );
mOpacitySlider->setValue( mEffect->opacity() * 1000.0 );
mBlendCmbBx->setBlendMode( mEffect->blendMode() );
mDrawModeComboBox->setDrawMode( mEffect->drawMode() );

Expand All @@ -67,22 +68,22 @@ void QgsDrawSourceWidget::initGui()

void QgsDrawSourceWidget::blockSignals( const bool block )
{
mTransparencySlider->blockSignals( block );
mTransparencySpnBx->blockSignals( block );
mOpacitySlider->blockSignals( block );
mOpacitySpnBx->blockSignals( block );
mBlendCmbBx->blockSignals( block );
mDrawModeComboBox->blockSignals( block );
}

void QgsDrawSourceWidget::on_mTransparencySpnBx_valueChanged( double value )
void QgsDrawSourceWidget::on_mOpacitySpnBx_valueChanged( double value )
{
if ( !mEffect )
return;

mTransparencySlider->blockSignals( true );
mTransparencySlider->setValue( value * 10.0 );
mTransparencySlider->blockSignals( false );
mOpacitySlider->blockSignals( true );
mOpacitySlider->setValue( value * 10.0 );
mOpacitySlider->blockSignals( false );

mEffect->setTransparency( value / 100.0 );
mEffect->setOpacity( value / 100.0 );
emit changed();
}

Expand All @@ -108,9 +109,9 @@ void QgsDrawSourceWidget::on_mBlendCmbBx_currentIndexChanged( int index )
emit changed();
}

void QgsDrawSourceWidget::on_mTransparencySlider_valueChanged( int value )
void QgsDrawSourceWidget::on_mOpacitySlider_valueChanged( int value )
{
mTransparencySpnBx->setValue( value / 10.0 );
mOpacitySpnBx->setValue( value / 10.0 );
}


Expand Down
4 changes: 2 additions & 2 deletions src/gui/effects/qgspainteffectwidget.h
Expand Up @@ -84,10 +84,10 @@ class GUI_EXPORT QgsDrawSourceWidget : public QgsPaintEffectWidget, private Ui::

private slots:

void on_mTransparencySpnBx_valueChanged( double value );
void on_mOpacitySpnBx_valueChanged( double value );
void on_mDrawModeComboBox_currentIndexChanged( int index );
void on_mBlendCmbBx_currentIndexChanged( int index );
void on_mTransparencySlider_valueChanged( int value );
void on_mOpacitySlider_valueChanged( int value );

};

Expand Down
23 changes: 16 additions & 7 deletions src/ui/effects/widget_drawsource.ui
Expand Up @@ -44,14 +44,14 @@
<item row="0" column="0">
<widget class="QLabel" name="label_28">
<property name="text">
<string>Transparency</string>
<string>Opacity</string>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_28">
<item>
<widget class="QSlider" name="mTransparencySlider">
<widget class="QSlider" name="mOpacitySlider">
<property name="enabled">
<bool>true</bool>
</property>
Expand All @@ -76,18 +76,24 @@
<property name="pageStep">
<number>100</number>
</property>
<property name="value">
<number>1000</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QgsDoubleSpinBox" name="mTransparencySpnBx">
<widget class="QgsDoubleSpinBox" name="mOpacitySpnBx">
<property name="enabled">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="suffix">
<string> %</string>
Expand All @@ -98,6 +104,9 @@
<property name="maximum">
<double>100.000000000000000</double>
</property>
<property name="value">
<double>100.000000000000000</double>
</property>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -150,8 +159,8 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mTransparencySlider</tabstop>
<tabstop>mTransparencySpnBx</tabstop>
<tabstop>mOpacitySlider</tabstop>
<tabstop>mOpacitySpnBx</tabstop>
<tabstop>mBlendCmbBx</tabstop>
<tabstop>mDrawModeComboBox</tabstop>
</tabstops>
Expand Down
10 changes: 5 additions & 5 deletions tests/src/core/testqgspainteffect.cpp
Expand Up @@ -291,8 +291,8 @@ void TestQgsPaintEffect::drawSource()
QVERIFY( effect );
effect->setBlendMode( QPainter::CompositionMode_ColorBurn );
QCOMPARE( effect->blendMode(), QPainter::CompositionMode_ColorBurn );
effect->setTransparency( 0.5 );
QCOMPARE( effect->transparency(), 0.5 );
effect->setOpacity( 0.5 );
QCOMPARE( effect->opacity(), 0.5 );
effect->setEnabled( false );
QCOMPARE( effect->enabled(), false );
effect->setDrawMode( QgsPaintEffect::Modifier );
Expand All @@ -302,7 +302,7 @@ void TestQgsPaintEffect::drawSource()
QgsDrawSourceEffect *copy = new QgsDrawSourceEffect( *effect );
QVERIFY( copy );
QCOMPARE( copy->blendMode(), effect->blendMode() );
QCOMPARE( copy->transparency(), effect->transparency() );
QCOMPARE( copy->opacity(), effect->opacity() );
QCOMPARE( copy->enabled(), effect->enabled() );
QCOMPARE( copy->drawMode(), effect->drawMode() );
delete copy;
Expand All @@ -312,7 +312,7 @@ void TestQgsPaintEffect::drawSource()
QgsDrawSourceEffect *cloneCast = dynamic_cast<QgsDrawSourceEffect * >( clone );
QVERIFY( cloneCast );
QCOMPARE( cloneCast->blendMode(), effect->blendMode() );
QCOMPARE( cloneCast->transparency(), effect->transparency() );
QCOMPARE( cloneCast->opacity(), effect->opacity() );
QCOMPARE( cloneCast->enabled(), effect->enabled() );
QCOMPARE( cloneCast->drawMode(), effect->drawMode() );
delete cloneCast;
Expand All @@ -323,7 +323,7 @@ void TestQgsPaintEffect::drawSource()
QgsDrawSourceEffect *readCast = dynamic_cast<QgsDrawSourceEffect * >( readEffect );
QVERIFY( readCast );
QCOMPARE( readCast->blendMode(), effect->blendMode() );
QCOMPARE( readCast->transparency(), effect->transparency() );
QCOMPARE( readCast->opacity(), effect->opacity() );
QCOMPARE( readCast->enabled(), effect->enabled() );
QCOMPARE( readCast->drawMode(), effect->drawMode() );
delete readCast;
Expand Down

0 comments on commit 6f4c549

Please sign in to comment.