Skip to content

Commit

Permalink
Change QgsComposerItem to use opacity instead of transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 29, 2017
1 parent b2ba450 commit 3914651
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 81 deletions.
6 changes: 6 additions & 0 deletions doc/api_break.dox
Expand Up @@ -615,6 +615,12 @@ Use the corresponding methods in QgsComposerUtils instead.
instead.
- lockSymbolSize(), imageSizeConsideringRotation(), cornerPointOnRotatedAndScaledRect(),
sizeChangedByRotation() were removed. No replacement is offered for these methods.
- transparency() and setTransparency() were removed. Use itemOpacity() and setItemOpacity() instead.

QgsComposerItemCommand {#qgis_api_break_3_0_QgsComposerItemCommand}
----------------------

- ItemTransparency was removed. Use ItemOpacity instead.


QgsComposerLabel {#qgis_api_break_3_0_QgsComposerLabel}
Expand Down
13 changes: 2 additions & 11 deletions python/core/composer/qgscomposeritem.sip
Expand Up @@ -404,17 +404,8 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
*/
void setBlendMode( const QPainter::CompositionMode blendMode );

/** Returns the item's transparency
* @returns transparency as integer between 0 (transparent) and 255 (opaque)
* @see setTransparency
*/
int transparency() const;

/** Sets the item's transparency
* @param transparency integer between 0 (transparent) and 255 (opaque)
* @see transparency
*/
void setTransparency( const int transparency );
double itemOpacity() const;
void setItemOpacity( const double opacity );

/** Returns whether effects (e.g., blend modes) are enabled for the item
* @returns true if effects are enabled
Expand Down
2 changes: 1 addition & 1 deletion python/core/composer/qgscomposeritemcommand.sip
Expand Up @@ -112,7 +112,7 @@ class QgsComposerMergeCommand : QgsComposerItemCommand
ItemBackgroundColor,
ItemMove,
ItemRotation,
ItemTransparency,
ItemOpacity,
ItemZoomContent
};

Expand Down
1 change: 1 addition & 0 deletions python/core/composer/qgscomposerobject.sip
Expand Up @@ -30,6 +30,7 @@ class QgsComposerObject : QObject, QgsExpressionContextGenerator
ItemHeight, /*!< height of item */
ItemRotation, /*!< rotation of item */
Transparency, /*!< item transparency */
Opacity,
BlendMode, /*!< item blend mode */
ExcludeFromExports, /*!< exclude item from exports */
FrameColor, //!< Item frame color
Expand Down
28 changes: 14 additions & 14 deletions src/app/composer/qgscomposeritemwidget.cpp
Expand Up @@ -170,7 +170,7 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget *parent, QgsComposerItem *
connect( mItem, &QgsComposerItem::sizeChanged, this, &QgsComposerItemWidget::setValuesForGuiPositionElements );
connect( mItem, &QgsComposerObject::itemChanged, this, &QgsComposerItemWidget::setValuesForGuiNonPositionElements );

connect( mTransparencySlider, &QAbstractSlider::valueChanged, mTransparencySpnBx, &QSpinBox::setValue );
connect( mOpacitySlider, &QAbstractSlider::valueChanged, this, [ = ]( int value ) { mOpacitySpnBx->setValue( value / 10.0 ); } );

updateVariables();
connect( mVariableEditor, &QgsVariableEditorWidget::scopeChanged, this, &QgsComposerItemWidget::variablesChanged );
Expand Down Expand Up @@ -504,8 +504,8 @@ void QgsComposerItemWidget::setValuesForGuiNonPositionElements()
mBackgroundGroupBox->blockSignals( true );
mItemIdLineEdit->blockSignals( true );
mBlendModeCombo->blockSignals( true );
mTransparencySlider->blockSignals( true );
mTransparencySpnBx->blockSignals( true );
mOpacitySlider->blockSignals( true );
mOpacitySpnBx->blockSignals( true );
mFrameColorButton->blockSignals( true );
mFrameJoinStyleCombo->blockSignals( true );
mBackgroundColorButton->blockSignals( true );
Expand All @@ -520,8 +520,8 @@ void QgsComposerItemWidget::setValuesForGuiNonPositionElements()
mFrameGroupBox->setChecked( mItem->hasFrame() );
mBackgroundGroupBox->setChecked( mItem->hasBackground() );
mBlendModeCombo->setBlendMode( mItem->blendMode() );
mTransparencySlider->setValue( mItem->transparency() );
mTransparencySpnBx->setValue( mItem->transparency() );
mOpacitySlider->setValue( mItem->itemOpacity() * 1000.0 );
mOpacitySpnBx->setValue( mItem->itemOpacity() * 100.0 );
mItemRotationSpinBox->setValue( mItem->itemRotation( QgsComposerObject::OriginalValue ) );
mExcludeFromPrintsCheckBox->setChecked( mItem->excludeFromExports( QgsComposerObject::OriginalValue ) );

Expand All @@ -533,8 +533,8 @@ void QgsComposerItemWidget::setValuesForGuiNonPositionElements()
mBackgroundGroupBox->blockSignals( false );
mItemIdLineEdit->blockSignals( false );
mBlendModeCombo->blockSignals( false );
mTransparencySlider->blockSignals( false );
mTransparencySpnBx->blockSignals( false );
mOpacitySlider->blockSignals( false );
mOpacitySpnBx->blockSignals( false );
mItemRotationSpinBox->blockSignals( false );
mExcludeFromPrintsCheckBox->blockSignals( false );
}
Expand All @@ -546,7 +546,7 @@ void QgsComposerItemWidget::initializeDataDefinedButtons()
mConfigObject->initializeDataDefinedButton( mWidthDDBtn, QgsComposerObject::ItemWidth );
mConfigObject->initializeDataDefinedButton( mHeightDDBtn, QgsComposerObject::ItemHeight );
mConfigObject->initializeDataDefinedButton( mItemRotationDDBtn, QgsComposerObject::ItemRotation );
mConfigObject->initializeDataDefinedButton( mTransparencyDDBtn, QgsComposerObject::Transparency );
mConfigObject->initializeDataDefinedButton( mOpacityDDBtn, QgsComposerObject::Opacity );
mConfigObject->initializeDataDefinedButton( mBlendModeDDBtn, QgsComposerObject::BlendMode );
mConfigObject->initializeDataDefinedButton( mExcludePrintsDDBtn, QgsComposerObject::ExcludeFromExports );
mConfigObject->initializeDataDefinedButton( mItemFrameColorDDBtn, QgsComposerObject::FrameColor );
Expand Down Expand Up @@ -591,15 +591,15 @@ void QgsComposerItemWidget::on_mBlendModeCombo_currentIndexChanged( int index )
}
}

void QgsComposerItemWidget::on_mTransparencySpnBx_valueChanged( int value )
void QgsComposerItemWidget::on_mOpacitySpnBx_valueChanged( double value )
{
mTransparencySlider->blockSignals( true );
mTransparencySlider->setValue( value );
mTransparencySlider->blockSignals( false );
mOpacitySlider->blockSignals( true );
mOpacitySlider->setValue( value * 10 );
mOpacitySlider->blockSignals( false );
if ( mItem )
{
mItem->beginCommand( tr( "Item transparency changed" ), QgsComposerMergeCommand::ItemTransparency );
mItem->setTransparency( value );
mItem->beginCommand( tr( "Item opacity changed" ), QgsComposerMergeCommand::ItemOpacity );
mItem->setItemOpacity( value / 100.0 );
mItem->endCommand();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposeritemwidget.h
Expand Up @@ -172,7 +172,7 @@ class QgsComposerItemWidget: public QWidget, private Ui::QgsComposerItemWidgetBa
void on_mLowerRightCheckBox_stateChanged( int state );

void on_mBlendModeCombo_currentIndexChanged( int index );
void on_mTransparencySpnBx_valueChanged( int value );
void on_mOpacitySpnBx_valueChanged( double value );

void on_mItemRotationSpinBox_valueChanged( double val );
void on_mExcludeFromPrintsCheckBox_toggled( bool checked );
Expand Down
35 changes: 20 additions & 15 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -60,7 +60,6 @@ QgsComposerItem::QgsComposerItem( QgsComposition *composition, bool manageZValue
, mEvaluatedItemRotation( 0 )
, mBlendMode( QPainter::CompositionMode_SourceOver )
, mEffectsEnabled( true )
, mTransparency( 0 )
, mExcludeFromExports( false )
, mEvaluatedExcludeFromExports( false )
, mLastUsedPositionMode( UpperLeft )
Expand Down Expand Up @@ -89,7 +88,6 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
, mEvaluatedItemRotation( 0 )
, mBlendMode( QPainter::CompositionMode_SourceOver )
, mEffectsEnabled( true )
, mTransparency( 0 )
, mExcludeFromExports( false )
, mEvaluatedExcludeFromExports( false )
, mLastUsedPositionMode( UpperLeft )
Expand Down Expand Up @@ -228,8 +226,8 @@ bool QgsComposerItem::_writeXml( QDomElement &itemElem, QDomDocument &doc ) cons
//blend mode
composerItemElem.setAttribute( QStringLiteral( "blendMode" ), QgsPainting::getBlendModeEnum( mBlendMode ) );

//transparency
composerItemElem.setAttribute( QStringLiteral( "transparency" ), QString::number( mTransparency ) );
//opacity
composerItemElem.setAttribute( QStringLiteral( "opacity" ), QString::number( mOpacity ) );

composerItemElem.setAttribute( QStringLiteral( "excludeFromExports" ), mExcludeFromExports );

Expand Down Expand Up @@ -386,8 +384,15 @@ bool QgsComposerItem::_readXml( const QDomElement &itemElem, const QDomDocument
//blend mode
setBlendMode( QgsPainting::getCompositionMode( static_cast< QgsPainting::BlendMode >( itemElem.attribute( QStringLiteral( "blendMode" ), QStringLiteral( "0" ) ).toUInt() ) ) );

//transparency
setTransparency( itemElem.attribute( QStringLiteral( "transparency" ), QStringLiteral( "0" ) ).toInt() );
//opacity
if ( itemElem.hasAttribute( QStringLiteral( "opacity" ) ) )
{
setItemOpacity( itemElem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1" ) ).toDouble() );
}
else
{
setItemOpacity( 1.0 - itemElem.attribute( QStringLiteral( "transparency" ), QStringLiteral( "0" ) ).toInt() / 100.0 );
}

mExcludeFromExports = itemElem.attribute( QStringLiteral( "excludeFromExports" ), QStringLiteral( "0" ) ).toInt();
mEvaluatedExcludeFromExports = mExcludeFromExports;
Expand Down Expand Up @@ -895,20 +900,20 @@ void QgsComposerItem::refreshBlendMode( const QgsExpressionContext &context )
mEffect->setCompositionMode( blendMode );
}

void QgsComposerItem::setTransparency( const int transparency )
void QgsComposerItem::setItemOpacity( const double opacity )
{
mTransparency = transparency;
mOpacity = opacity;
QgsExpressionContext context = createExpressionContext();
refreshTransparency( true, context );
refreshOpacity( true, context );
}

void QgsComposerItem::refreshTransparency( const bool updateItem, const QgsExpressionContext &context )
void QgsComposerItem::refreshOpacity( const bool updateItem, const QgsExpressionContext &context )
{
//data defined transparency set?
int transparency = mDataDefinedProperties.valueAsInt( QgsComposerObject::Transparency, context, mTransparency );
//data defined opacity set?
double opacity = mDataDefinedProperties.valueAsDouble( QgsComposerObject::Opacity, context, mOpacity * 100.0 );

// Set the QGraphicItem's opacity
setOpacity( 1. - ( transparency / 100. ) );
setOpacity( opacity / 100.0 );

if ( updateItem )
{
Expand Down Expand Up @@ -1140,9 +1145,9 @@ void QgsComposerItem::refreshDataDefinedProperty( const QgsComposerObject::DataD
{
refreshRotation( false, true, *evalContext );
}
if ( property == QgsComposerObject::Transparency || property == QgsComposerObject::AllProperties )
if ( property == QgsComposerObject::Opacity || property == QgsComposerObject::AllProperties )
{
refreshTransparency( false, *evalContext );
refreshOpacity( false, *evalContext );
}
if ( property == QgsComposerObject::BlendMode || property == QgsComposerObject::AllProperties )
{
Expand Down
44 changes: 24 additions & 20 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -364,30 +364,34 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
*/
void setBlendMode( const QPainter::CompositionMode blendMode );

/** Returns the item's transparency
* \returns transparency as integer between 0 (transparent) and 255 (opaque)
* \see setTransparency
/** Returns the item's opacity. This method should be used instead of
* QGraphicsItem::opacity() as any data defined overrides will be
* respected.
* \returns opacity as double between 1.0 (opaque) and 0 (transparent).
* \see setItemOpacity()
*/
int transparency() const { return mTransparency; }
double itemOpacity() const { return mOpacity; }

/** Sets the item's transparency
* \param transparency integer between 0 (transparent) and 255 (opaque)
* \see transparency
/** Sets the item's \a opacity. This method should be used instead of
* QGraphicsItem::setOpacity() as any data defined overrides will be
* respected.
* \param opacity double between 1.0 (opaque) and 0 (transparent).
* \see itemOpacity()
*/
void setTransparency( const int transparency );
void setItemOpacity( const double itemOpacity );

/** Returns whether effects (e.g., blend modes) are enabled for the item
* \returns true if effects are enabled
* \see setEffectsEnabled
* \see transparency
* \see itemOpacity()
* \see blendMode
*/
bool effectsEnabled() const { return mEffectsEnabled; }

/** Sets whether effects (e.g., blend modes) are enabled for the item
* \param effectsEnabled set to true to enable effects
* \see effectsEnabled
* \see setTransparency
* \see setItemOpacity()
* \see setBlendMode
*/
void setEffectsEnabled( const bool effectsEnabled );
Expand Down Expand Up @@ -621,8 +625,8 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
bool mEffectsEnabled;
QgsComposerEffect *mEffect = nullptr;

//! Item transparency
int mTransparency;
//! Item opacity, between 0 and 1
double mOpacity = 1.0;

//! Whether item should be excluded in exports
bool mExcludeFromExports;
Expand Down Expand Up @@ -734,25 +738,25 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
*/
void refreshRotation( const bool updateItem = true, const bool rotateAroundCenter = false, const QgsExpressionContext &context = QgsExpressionContext() );

/** Refresh item's transparency, considering data defined transparency
/** Refresh item's opacity, considering data defined opacity
* \param updateItem set to false to prevent the item being automatically updated
* after the transparency is set
* \param context expression context for evaulating data defined transparency
* after the opacity is set
* \param context expression context for evaulating data defined opacity
* \since QGIS 2.5
*/
void refreshTransparency( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );
void refreshOpacity( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );

/** Refresh item's frame color, considering data defined transparency
/** Refresh item's frame color, considering data defined colors
* \param updateItem set to false to prevent the item being automatically updated
* after the frame color is set
* \param context expression context for evaulating data defined transparency
* \param context expression context for evaulating data defined color
*/
void refreshFrameColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );

/** Refresh item's transparency, considering data defined transparency
/** Refresh item's background color, considering data defined colors
* \param updateItem set to false to prevent the item being automatically updated
* after the background color is set
* \param context expression context for evaulating data defined transparency
* \param context expression context for evaulating data defined color
*/
void refreshBackgroundColor( const bool updateItem = true, const QgsExpressionContext &context = QgsExpressionContext() );

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposeritemcommand.h
Expand Up @@ -154,7 +154,7 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
ItemBackgroundColor,
ItemMove,
ItemRotation,
ItemTransparency,
ItemOpacity, //!< Item opacity
ItemZoomContent
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposermultiframe.cpp
Expand Up @@ -213,7 +213,7 @@ QgsComposerFrame *QgsComposerMultiFrame::createNewFrame( QgsComposerFrame *curre
newFrame->setFrameStrokeColor( currentFrame->frameStrokeColor() );
newFrame->setFrameJoinStyle( currentFrame->frameJoinStyle() );
newFrame->setFrameStrokeWidth( currentFrame->frameStrokeWidth() );
newFrame->setTransparency( currentFrame->transparency() );
newFrame->setItemOpacity( currentFrame->itemOpacity() );
newFrame->setHideBackgroundIfEmpty( currentFrame->hideBackgroundIfEmpty() );

addFrame( newFrame, false );
Expand Down
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposerobject.cpp
Expand Up @@ -51,6 +51,7 @@ void QgsComposerObject::initPropertyDefinitions()
{ QgsComposerObject::ItemHeight, QgsPropertyDefinition( "dataDefinedHeight", QObject::tr( "Height" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsComposerObject::ItemRotation, QgsPropertyDefinition( "dataDefinedRotation", QObject::tr( "Rotation angle" ), QgsPropertyDefinition::Rotation ) },
{ QgsComposerObject::Transparency, QgsPropertyDefinition( "dataDefinedTransparency", QObject::tr( "Transparency" ), QgsPropertyDefinition::Transparency ) },
{ QgsComposerObject::Opacity, QgsPropertyDefinition( "dataDefinedOpacity", QObject::tr( "Opacity" ), QgsPropertyDefinition::Transparency ) },
{ QgsComposerObject::BlendMode, QgsPropertyDefinition( "dataDefinedBlendMode", QObject::tr( "Blend mode" ), QgsPropertyDefinition::BlendMode ) },
{ QgsComposerObject::ExcludeFromExports, QgsPropertyDefinition( "dataDefinedExcludeExports", QObject::tr( "Exclude item from exports" ), QgsPropertyDefinition::Boolean ) },
{ QgsComposerObject::FrameColor, QgsPropertyDefinition( "dataDefinedFrameColor", QObject::tr( "Frame color" ), QgsPropertyDefinition::ColorWithAlpha ) },
Expand Down Expand Up @@ -142,6 +143,14 @@ bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument
{
mDataDefinedProperties.readXml( propsNode.toElement(), sPropertyDefinitions );
}
if ( mDataDefinedProperties.isActive( QgsComposerObject::Transparency ) )
{
// upgrade transparency -> opacity
QString exp = mDataDefinedProperties.property( QgsComposerObject::Transparency ).asExpression();
exp = QStringLiteral( "100.0 - (%1)" ).arg( exp );
mDataDefinedProperties.setProperty( QgsComposerObject::Opacity, QgsProperty::fromExpression( exp ) );
mDataDefinedProperties.setProperty( QgsComposerObject::Transparency, QgsProperty() );
}

//custom properties
mCustomProperties.readXml( itemElem );
Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposerobject.h
Expand Up @@ -57,7 +57,8 @@ class CORE_EXPORT QgsComposerObject: public QObject, public QgsExpressionContext
ItemWidth, //!< Width of item
ItemHeight, //!< Height of item
ItemRotation, //!< Rotation of item
Transparency, //!< Item transparency
Transparency, //!< Item transparency (deprecated)
Opacity, //!< Item opacity
BlendMode, //!< Item blend mode
ExcludeFromExports, //!< Exclude item from exports
FrameColor, //!< Item frame color
Expand Down

0 comments on commit 3914651

Please sign in to comment.