Change QgsComposerItem to use opacity instead of transparency

This commit is contained in:
Nyall Dawson 2017-05-29 15:32:58 +10:00
parent b2ba450d46
commit 39146517d2
15 changed files with 116 additions and 81 deletions

View File

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

View File

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

View File

@ -112,7 +112,7 @@ class QgsComposerMergeCommand : QgsComposerItemCommand
ItemBackgroundColor,
ItemMove,
ItemRotation,
ItemTransparency,
ItemOpacity,
ItemZoomContent
};

View File

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

View File

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

View File

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

View File

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

View File

@ -364,22 +364,26 @@ 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; }
@ -387,7 +391,7 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
/** 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() );

View File

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

View File

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

View File

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

View File

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

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>290</width>
<height>1017</height>
<width>309</width>
<height>1135</height>
</rect>
</property>
<property name="windowTitle">
@ -658,37 +658,55 @@
<item row="1" column="0">
<widget class="QLabel" name="labelTransparency">
<property name="text">
<string>Transparency</string>
<string>Opacity</string>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QSlider" name="mTransparencySlider">
<widget class="QSlider" name="mOpacitySlider">
<property name="maximum">
<number>1000</number>
</property>
<property name="singleStep">
<number>10</number>
</property>
<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="QgsSpinBox" name="mTransparencySpnBx">
<widget class="QgsDoubleSpinBox" name="mOpacitySpnBx">
<property name="minimumSize">
<size>
<width>80</width>
<width>100</width>
<height>25</height>
</size>
</property>
<property name="suffix">
<string> %</string>
</property>
<property name="decimals">
<number>1</number>
</property>
<property name="maximum">
<number>100</number>
<double>100.000000000000000</double>
</property>
<property name="value">
<double>100.000000000000000</double>
</property>
</widget>
</item>
<item>
<widget class="QgsPropertyOverrideButton" name="mTransparencyDDBtn">
<widget class="QgsPropertyOverrideButton" name="mOpacityDDBtn">
<property name="text">
<string>...</string>
</property>
@ -841,9 +859,9 @@
<tabstop>groupRendering</tabstop>
<tabstop>mBlendModeCombo</tabstop>
<tabstop>mBlendModeDDBtn</tabstop>
<tabstop>mTransparencySlider</tabstop>
<tabstop>mTransparencySpnBx</tabstop>
<tabstop>mTransparencyDDBtn</tabstop>
<tabstop>mOpacitySlider</tabstop>
<tabstop>mOpacitySpnBx</tabstop>
<tabstop>mOpacityDDBtn</tabstop>
<tabstop>mExcludeFromPrintsCheckBox</tabstop>
<tabstop>mExcludePrintsDDBtn</tabstop>
</tabstops>

View File

@ -111,7 +111,7 @@ void TestQgsComposerEffects::blend_modes()
void TestQgsComposerEffects::transparency()
{
mComposerRect2->setTransparency( 50 );
mComposerRect2->setItemOpacity( 0.5 );
QgsCompositionChecker checker( QStringLiteral( "composereffects_transparency" ), mComposition );
checker.setControlPathPrefix( QStringLiteral( "composer_effects" ) );

View File

@ -109,7 +109,7 @@ void TestQgsComposerMultiFrame::addFrame()
frame1->setBackgroundEnabled( true );
frame1->setBackgroundColor( QColor( Qt::green ) );
frame1->setBlendMode( QPainter::CompositionMode_ColorBurn );
frame1->setTransparency( 50 );
frame1->setItemOpacity( 0.5 );
QgsComposerFrame *frame2 = htmlItem->createNewFrame( frame1, QPointF( 50, 55 ), QSizeF( 70, 120 ) );
@ -126,7 +126,7 @@ void TestQgsComposerMultiFrame::addFrame()
QCOMPARE( frame2->hasBackground(), frame1->hasBackground() );
QCOMPARE( frame2->backgroundColor(), frame1->backgroundColor() );
QCOMPARE( frame2->blendMode(), frame1->blendMode() );
QCOMPARE( frame2->transparency(), frame1->transparency() );
QCOMPARE( frame2->itemOpacity(), frame1->itemOpacity() );
//check non-inherited properties
QVERIFY( !frame2->hidePageIfEmpty() );
@ -252,10 +252,10 @@ void TestQgsComposerMultiFrame::undoRedo()
mComposition->endMultiFrameCommand();
//another frame command
frame1->beginCommand( QStringLiteral( "bgcolor" ), QgsComposerMergeCommand::ItemTransparency );
frame1->beginCommand( QStringLiteral( "bgcolor" ), QgsComposerMergeCommand::ItemOpacity );
frame1->setBackgroundColor( QColor( 255, 255, 0 ) );
frame1->endCommand();
frame1->beginCommand( QStringLiteral( "bgcolor" ), QgsComposerMergeCommand::ItemTransparency );
frame1->beginCommand( QStringLiteral( "bgcolor" ), QgsComposerMergeCommand::ItemOpacity );
frame1->setBackgroundColor( QColor( 255, 0, 0 ) );
frame1->endCommand();