Skip to content
Permalink
Browse files

Change QgsComposerItem to use opacity instead of transparency

  • Loading branch information
nyalldawson committed May 29, 2017
1 parent b2ba450 commit 39146517d27fb2fc03bf405b346f2cf703fde6bc
@@ -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}
@@ -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
@@ -112,7 +112,7 @@ class QgsComposerMergeCommand : QgsComposerItemCommand
ItemBackgroundColor,
ItemMove,
ItemRotation,
ItemTransparency,
ItemOpacity,
ItemZoomContent
};

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

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

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

@@ -154,7 +154,7 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
ItemBackgroundColor,
ItemMove,
ItemRotation,
ItemTransparency,
ItemOpacity, //!< Item opacity
ItemZoomContent
};

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

0 comments on commit 3914651

Please sign in to comment.
You can’t perform that action at this time.