Flip QgsDrawSourceEffect from transparency to opacity

This commit is contained in:
Nyall Dawson 2017-05-29 16:36:35 +10:00
parent 89c2e85aa9
commit 6f4c5496c7
11 changed files with 93 additions and 67 deletions

View File

@ -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}
------------

View File

@ -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

View File

@ -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() );
}

View File

@ -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
@ -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();
}

View File

@ -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:

View File

@ -249,7 +249,6 @@ QRectF QgsPaintEffect::imageBoundingRect( const QgsRenderContext &context ) cons
QgsDrawSourceEffect::QgsDrawSourceEffect()
: QgsPaintEffect()
, mTransparency( 0.0 )
, mBlendMode( QPainter::CompositionMode_SourceOver )
{
@ -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 );
@ -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 );
@ -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;
}
@ -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" ) ) )
{
mTransparency = transparency;
double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
if ( ok )
{
mOpacity = 1.0 - transparency;
}
}
else
{
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() );

View File

@ -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
@ -346,7 +346,7 @@ class CORE_EXPORT QgsDrawSourceEffect : public QgsPaintEffect
private:
double mTransparency;
double mOpacity = 1.0;
QPainter::CompositionMode mBlendMode;
};

View File

@ -35,6 +35,7 @@ QgsDrawSourceWidget::QgsDrawSourceWidget( QWidget *parent )
, mEffect( nullptr )
{
setupUi( this );
mOpacitySpnBx->setClearValue( 100.0 );
initGui();
}
@ -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() );
@ -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();
}
@ -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 );
}

View File

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

View File

@ -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>
@ -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>
@ -98,6 +104,9 @@
<property name="maximum">
<double>100.000000000000000</double>
</property>
<property name="value">
<double>100.000000000000000</double>
</property>
</widget>
</item>
</layout>
@ -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>

View File

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