Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Restore separate handling of label component opacity
...in a way that avoids unnecessary detachments of the
settings objects
  • Loading branch information
nyalldawson committed Oct 24, 2016
1 parent 5495f20 commit 074ae42
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
8 changes: 5 additions & 3 deletions src/core/qgspallabeling.cpp
Expand Up @@ -4087,10 +4087,12 @@ void QgsPalLabeling::drawLabelBuffer( QgsRenderContext& context,
QPainterPath path;
path.setFillRule( Qt::WindingFill );
path.addText( 0, 0, format.scaledFont( context ), component.text() );
QPen pen( buffer.color() );
QColor bufferColor = buffer.color();
bufferColor.setAlphaF( buffer.opacity() );
QPen pen( bufferColor );
pen.setWidthF( penSize );
pen.setJoinStyle( buffer.joinStyle() );
QColor tmpColor( buffer.color() );
QColor tmpColor( bufferColor );
// honor pref for whether to fill buffer interior
if ( !buffer.fillBufferInterior() )
{
Expand Down Expand Up @@ -4240,7 +4242,7 @@ void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,

QgsSymbolLayer* symShdwL = QgsSvgMarkerSymbolLayer::create( shdwmap );
QgsSvgMarkerSymbolLayer* svgShdwM = static_cast<QgsSvgMarkerSymbolLayer*>( symShdwL );
QgsSymbolRenderContext svgShdwContext( shdwContext, QgsUnitTypes::RenderUnknownUnit, shadow.opacity() );
QgsSymbolRenderContext svgShdwContext( shdwContext, QgsUnitTypes::RenderUnknownUnit, background.opacity() );

double svgSize = QgsTextRenderer::scaleToPixelContext( sizeOut, context, background.sizeUnit(),
true, background.sizeMapUnitScale() );
Expand Down
65 changes: 33 additions & 32 deletions src/core/qgstextrenderer.cpp
Expand Up @@ -123,12 +123,12 @@ void QgsTextBufferSettings::setFillBufferInterior( bool fill )

double QgsTextBufferSettings::opacity() const
{
return d->color.alphaF();
return d->opacity;
}

void QgsTextBufferSettings::setOpacity( double opacity )
{
d->color.setAlphaF( opacity );
d->opacity = opacity;
}

Qt::PenJoinStyle QgsTextBufferSettings::joinStyle() const
Expand Down Expand Up @@ -197,11 +197,11 @@ void QgsTextBufferSettings::readFromLayer( QgsVectorLayer* layer )
d->color = _readColor( layer, "labeling/bufferColor", Qt::white, false );
if ( layer->customProperty( "labeling/bufferOpacity" ).toString().isEmpty() )
{
d->color.setAlphaF( 1 - layer->customProperty( "labeling/bufferTransp" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - layer->customProperty( "labeling/bufferTransp" ).toInt() / 100.0 ); //0 -100
}
else
{
d->color.setAlphaF( layer->customProperty( "labeling/bufferOpacity" ).toDouble() );
d->opacity = ( layer->customProperty( "labeling/bufferOpacity" ).toDouble() );
}
d->blendMode = QgsPainting::getCompositionMode(
static_cast< QgsPainting::BlendMode >( layer->customProperty( "labeling/bufferBlendMode", QVariant( QgsPainting::BlendNormal ) ).toUInt() ) );
Expand All @@ -218,7 +218,7 @@ void QgsTextBufferSettings::writeToLayer( QgsVectorLayer* layer ) const
layer->setCustomProperty( "labeling/bufferSizeMapUnitScale", QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
_writeColor( layer, "labeling/bufferColor", d->color );
layer->setCustomProperty( "labeling/bufferNoFill", !d->fillBufferInterior );
layer->setCustomProperty( "labeling/bufferOpacity", d->color.alphaF() );
layer->setCustomProperty( "labeling/bufferOpacity", d->opacity );
layer->setCustomProperty( "labeling/bufferJoinStyle", static_cast< unsigned int >( d->joinStyle ) );
layer->setCustomProperty( "labeling/bufferBlendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
}
Expand Down Expand Up @@ -270,11 +270,11 @@ void QgsTextBufferSettings::readXml( const QDomElement& elem )

if ( !textBufferElem.hasAttribute( "bufferOpacity" ) )
{
d->color.setAlphaF( 1 - textBufferElem.attribute( "bufferTransp" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - textBufferElem.attribute( "bufferTransp" ).toInt() / 100.0 ); //0 -100
}
else
{
d->color.setAlphaF( textBufferElem.attribute( "bufferOpacity" ).toDouble() );
d->opacity = ( textBufferElem.attribute( "bufferOpacity" ).toDouble() );
}

d->blendMode = QgsPainting::getCompositionMode(
Expand All @@ -293,7 +293,7 @@ QDomElement QgsTextBufferSettings::writeXml( QDomDocument& doc ) const
textBufferElem.setAttribute( "bufferSizeMapUnitScale", QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
textBufferElem.setAttribute( "bufferColor", QgsSymbolLayerUtils::encodeColor( d->color ) );
textBufferElem.setAttribute( "bufferNoFill", !d->fillBufferInterior );
textBufferElem.setAttribute( "bufferOpacity", d->color.alphaF() );
textBufferElem.setAttribute( "bufferOpacity", d->opacity );
textBufferElem.setAttribute( "bufferJoinStyle", static_cast< unsigned int >( d->joinStyle ) );
textBufferElem.setAttribute( "bufferBlendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
return textBufferElem;
Expand Down Expand Up @@ -478,12 +478,12 @@ void QgsTextBackgroundSettings::setRadiiMapUnitScale( const QgsMapUnitScale &sca

double QgsTextBackgroundSettings::opacity() const
{
return d->fillColor.alphaF();
return d->opacity;
}

void QgsTextBackgroundSettings::setOpacity( double opacity )
{
d->fillColor.setAlphaF( opacity );
d->opacity = opacity;
}

QPainter::CompositionMode QgsTextBackgroundSettings::blendMode() const
Expand Down Expand Up @@ -660,11 +660,11 @@ void QgsTextBackgroundSettings::readFromLayer( QgsVectorLayer* layer )

if ( layer->customProperty( "labeling/shapeOpacity" ).toString().isEmpty() )
{
d->fillColor.setAlphaF( 1 - layer->customProperty( "labeling/shapeTransparency" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - layer->customProperty( "labeling/shapeTransparency" ).toInt() / 100.0 ); //0 -100
}
else
{
d->fillColor.setAlphaF( layer->customProperty( "labeling/shapeOpacity" ).toDouble() );
d->opacity = ( layer->customProperty( "labeling/shapeOpacity" ).toDouble() );
}
d->blendMode = QgsPainting::getCompositionMode(
static_cast< QgsPainting::BlendMode >( layer->customProperty( "labeling/shapeBlendMode", QVariant( QgsPainting::BlendNormal ) ).toUInt() ) );
Expand Down Expand Up @@ -696,7 +696,7 @@ void QgsTextBackgroundSettings::writeToLayer( QgsVectorLayer* layer ) const
layer->setCustomProperty( "labeling/shapeBorderWidthUnit", QgsUnitTypes::encodeUnit( d->borderWidthUnits ) );
layer->setCustomProperty( "labeling/shapeBorderWidthMapUnitScale", QgsSymbolLayerUtils::encodeMapUnitScale( d->borderWidthMapUnitScale ) );
layer->setCustomProperty( "labeling/shapeJoinStyle", static_cast< unsigned int >( d->joinStyle ) );
layer->setCustomProperty( "labeling/shapeOpacity", d->fillColor.alphaF() );
layer->setCustomProperty( "labeling/shapeOpacity", d->opacity );
layer->setCustomProperty( "labeling/shapeBlendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
}

Expand Down Expand Up @@ -804,11 +804,11 @@ void QgsTextBackgroundSettings::readXml( const QDomElement& elem )

if ( !backgroundElem.hasAttribute( "shapeOpacity" ) )
{
d->fillColor.setAlphaF( 1 - backgroundElem.attribute( "shapeTransparency" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - backgroundElem.attribute( "shapeTransparency" ).toInt() / 100.0 ); //0 -100
}
else
{
d->fillColor.setAlphaF( backgroundElem.attribute( "shapeOpacity" ).toDouble() );
d->opacity = ( backgroundElem.attribute( "shapeOpacity" ).toDouble() );
}

d->blendMode = QgsPainting::getCompositionMode(
Expand Down Expand Up @@ -843,7 +843,7 @@ QDomElement QgsTextBackgroundSettings::writeXml( QDomDocument& doc ) const
backgroundElem.setAttribute( "shapeBorderWidthUnit", QgsUnitTypes::encodeUnit( d->borderWidthUnits ) );
backgroundElem.setAttribute( "shapeBorderWidthMapUnitScale", QgsSymbolLayerUtils::encodeMapUnitScale( d->borderWidthMapUnitScale ) );
backgroundElem.setAttribute( "shapeJoinStyle", static_cast< unsigned int >( d->joinStyle ) );
backgroundElem.setAttribute( "shapeOpacity", d->fillColor.alphaF() );
backgroundElem.setAttribute( "shapeOpacity", d->opacity );
backgroundElem.setAttribute( "shapeBlendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
return backgroundElem;
}
Expand Down Expand Up @@ -987,12 +987,12 @@ void QgsTextShadowSettings::setBlurAlphaOnly( bool alphaOnly )

double QgsTextShadowSettings::opacity() const
{
return d->color.alphaF();
return d->opacity;
}

void QgsTextShadowSettings::setOpacity( double opacity )
{
d->color.setAlphaF( opacity );
d->opacity = opacity;
}

int QgsTextShadowSettings::scale() const
Expand Down Expand Up @@ -1077,11 +1077,11 @@ void QgsTextShadowSettings::readFromLayer( QgsVectorLayer* layer )

if ( layer->customProperty( "labeling/shadowOpacity" ).toString().isEmpty() )
{
d->color.setAlphaF( 1 - layer->customProperty( "labeling/shadowTransparency" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - layer->customProperty( "labeling/shadowTransparency" ).toInt() / 100.0 ); //0 -100
}
else
{
d->color.setAlphaF( layer->customProperty( "labeling/shadowOpacity" ).toDouble() );
d->opacity = ( layer->customProperty( "labeling/shadowOpacity" ).toDouble() );
}
d->scale = layer->customProperty( "labeling/shadowScale", QVariant( 100 ) ).toInt();
d->color = _readColor( layer, "labeling/shadowColor", Qt::black, false );
Expand All @@ -1102,7 +1102,7 @@ void QgsTextShadowSettings::writeToLayer( QgsVectorLayer* layer ) const
layer->setCustomProperty( "labeling/shadowRadiusUnit", QgsUnitTypes::encodeUnit( d->radiusUnits ) );
layer->setCustomProperty( "labeling/shadowRadiusMapUnitScale", QgsSymbolLayerUtils::encodeMapUnitScale( d->radiusMapUnitScale ) );
layer->setCustomProperty( "labeling/shadowRadiusAlphaOnly", d->radiusAlphaOnly );
layer->setCustomProperty( "labeling/shadowOpacity", d->color.alphaF() );
layer->setCustomProperty( "labeling/shadowOpacity", d->opacity );
layer->setCustomProperty( "labeling/shadowScale", d->scale );
_writeColor( layer, "labeling/shadowColor", d->color, false );
layer->setCustomProperty( "labeling/shadowBlendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
Expand Down Expand Up @@ -1162,11 +1162,11 @@ void QgsTextShadowSettings::readXml( const QDomElement& elem )

if ( !shadowElem.hasAttribute( "shadowOpacity" ) )
{
d->color.setAlphaF( 1 - shadowElem.attribute( "shadowTransparency" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - shadowElem.attribute( "shadowTransparency" ).toInt() / 100.0 ); //0 -100
}
else
{
d->color.setAlphaF( shadowElem.attribute( "shadowOpacity" ).toDouble() );
d->opacity = ( shadowElem.attribute( "shadowOpacity" ).toDouble() );
}
d->scale = shadowElem.attribute( "shadowScale", "100" ).toInt();
d->color = QgsSymbolLayerUtils::decodeColor( shadowElem.attribute( "shadowColor", QgsSymbolLayerUtils::encodeColor( Qt::black ) ) );
Expand All @@ -1188,7 +1188,7 @@ QDomElement QgsTextShadowSettings::writeXml( QDomDocument& doc ) const
shadowElem.setAttribute( "shadowRadiusUnit", QgsUnitTypes::encodeUnit( d->radiusUnits ) );
shadowElem.setAttribute( "shadowRadiusMapUnitScale", QgsSymbolLayerUtils::encodeMapUnitScale( d->radiusMapUnitScale ) );
shadowElem.setAttribute( "shadowRadiusAlphaOnly", d->radiusAlphaOnly );
shadowElem.setAttribute( "shadowOpacity", d->color.alphaF() );
shadowElem.setAttribute( "shadowOpacity", d->opacity );
shadowElem.setAttribute( "shadowScale", d->scale );
shadowElem.setAttribute( "shadowColor", QgsSymbolLayerUtils::encodeColor( d->color ) );
shadowElem.setAttribute( "shadowBlendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
Expand Down Expand Up @@ -1308,12 +1308,12 @@ void QgsTextFormat::setColor( const QColor &color )

double QgsTextFormat::opacity() const
{
return d->textColor.alphaF();
return d->opacity;
}

void QgsTextFormat::setOpacity( double opacity )
{
d->textColor.setAlphaF( opacity );
d->opacity = opacity;
}

QPainter::CompositionMode QgsTextFormat::blendMode() const
Expand Down Expand Up @@ -1401,11 +1401,11 @@ void QgsTextFormat::readFromLayer( QgsVectorLayer* layer )
d->textColor = _readColor( layer, "labeling/textColor", Qt::black, false );
if ( layer->customProperty( "labeling/textOpacity" ).toString().isEmpty() )
{
d->textColor.setAlphaF( 1 - layer->customProperty( "labeling/textTransp" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - layer->customProperty( "labeling/textTransp" ).toInt() / 100.0 ); //0 -100
}
else
{
d->textColor.setAlphaF( layer->customProperty( "labeling/textOpacity" ).toDouble() );
d->opacity = ( layer->customProperty( "labeling/textOpacity" ).toDouble() );
}
d->blendMode = QgsPainting::getCompositionMode(
static_cast< QgsPainting::BlendMode >( layer->customProperty( "labeling/blendMode", QVariant( QgsPainting::BlendNormal ) ).toUInt() ) );
Expand All @@ -1428,10 +1428,11 @@ void QgsTextFormat::writeToLayer( QgsVectorLayer* layer ) const
layer->setCustomProperty( "labeling/fontStrikeout", d->textFont.strikeOut() );
layer->setCustomProperty( "labeling/fontUnderline", d->textFont.underline() );
_writeColor( layer, "labeling/textColor", d->textColor );
layer->setCustomProperty( "labeling/textOpacity", d->opacity );
layer->setCustomProperty( "labeling/fontCapitals", static_cast< unsigned int >( d->textFont.capitalization() ) );
layer->setCustomProperty( "labeling/fontLetterSpacing", d->textFont.letterSpacing() );
layer->setCustomProperty( "labeling/fontWordSpacing", d->textFont.wordSpacing() );
layer->setCustomProperty( "labeling/textOpacity", d->textColor.alphaF() );
layer->setCustomProperty( "labeling/textOpacity", d->opacity );
layer->setCustomProperty( "labeling/blendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
layer->setCustomProperty( "labeling/multilineHeight", d->multilineHeight );

Expand Down Expand Up @@ -1505,11 +1506,11 @@ void QgsTextFormat::readXml( const QDomElement& elem )
d->textColor = QgsSymbolLayerUtils::decodeColor( textStyleElem.attribute( "textColor", QgsSymbolLayerUtils::encodeColor( Qt::black ) ) );
if ( !textStyleElem.hasAttribute( "textOpacity" ) )
{
d->textColor.setAlphaF( 1 - textStyleElem.attribute( "textTransp" ).toInt() / 100.0 ); //0 -100
d->opacity = ( 1 - textStyleElem.attribute( "textTransp" ).toInt() / 100.0 ); //0 -100
}
else
{
d->textColor.setAlphaF( textStyleElem.attribute( "textOpacity" ).toDouble() );
d->opacity = ( textStyleElem.attribute( "textOpacity" ).toDouble() );
}
d->blendMode = QgsPainting::getCompositionMode(
static_cast< QgsPainting::BlendMode >( textStyleElem.attribute( "blendMode", QString::number( QgsPainting::BlendNormal ) ).toUInt() ) );
Expand Down Expand Up @@ -1567,7 +1568,7 @@ QDomElement QgsTextFormat::writeXml( QDomDocument& doc ) const
textStyleElem.setAttribute( "fontCapitals", static_cast< unsigned int >( d->textFont.capitalization() ) );
textStyleElem.setAttribute( "fontLetterSpacing", d->textFont.letterSpacing() );
textStyleElem.setAttribute( "fontWordSpacing", d->textFont.wordSpacing() );
textStyleElem.setAttribute( "textOpacity", d->textColor.alphaF() );
textStyleElem.setAttribute( "textOpacity", d->opacity );
textStyleElem.setAttribute( "blendMode", QgsPainting::getBlendModeEnum( d->blendMode ) );
textStyleElem.setAttribute( "multilineHeight", d->multilineHeight );

Expand Down
12 changes: 12 additions & 0 deletions src/core/qgstextrenderer_p.h
Expand Up @@ -45,6 +45,7 @@ class CORE_EXPORT QgsTextBufferSettingsPrivate : public QSharedData
, size( 1 )
, sizeUnit( QgsUnitTypes::RenderMillimeters )
, color( Qt::white )
, opacity( 1.0 )
, fillBufferInterior( false )
, joinStyle( Qt::RoundJoin )
, blendMode( QPainter::CompositionMode_SourceOver )
Expand All @@ -58,6 +59,7 @@ class CORE_EXPORT QgsTextBufferSettingsPrivate : public QSharedData
, sizeUnit( other.sizeUnit )
, sizeMapUnitScale( other.sizeMapUnitScale )
, color( other.color )
, opacity( other.opacity )
, fillBufferInterior( other.fillBufferInterior )
, joinStyle( other.joinStyle )
, blendMode( other.blendMode )
Expand All @@ -71,6 +73,7 @@ class CORE_EXPORT QgsTextBufferSettingsPrivate : public QSharedData
QgsUnitTypes::RenderUnit sizeUnit;
QgsMapUnitScale sizeMapUnitScale;
QColor color;
double opacity;
bool fillBufferInterior;
Qt::PenJoinStyle joinStyle;
QPainter::CompositionMode blendMode;
Expand All @@ -96,6 +99,7 @@ class CORE_EXPORT QgsTextBackgroundSettingsPrivate : public QSharedData
, blendMode( QPainter::CompositionMode_SourceOver )
, fillColor( Qt::white )
, borderColor( Qt::darkGray )
, opacity( 1.0 )
, borderWidth( 0.0 )
, borderWidthUnits( QgsUnitTypes::RenderMillimeters )
, joinStyle( Qt::BevelJoin )
Expand All @@ -122,6 +126,7 @@ class CORE_EXPORT QgsTextBackgroundSettingsPrivate : public QSharedData
, blendMode( other.blendMode )
, fillColor( other.fillColor )
, borderColor( other.borderColor )
, opacity( other.opacity )
, borderWidth( other.borderWidth )
, borderWidthUnits( other.borderWidthUnits )
, borderWidthMapUnitScale( other.borderWidthMapUnitScale )
Expand Down Expand Up @@ -149,6 +154,7 @@ class CORE_EXPORT QgsTextBackgroundSettingsPrivate : public QSharedData
QPainter::CompositionMode blendMode;
QColor fillColor;
QColor borderColor;
double opacity;
double borderWidth;
QgsUnitTypes::RenderUnit borderWidthUnits;
QgsMapUnitScale borderWidthMapUnitScale;
Expand All @@ -173,6 +179,7 @@ class CORE_EXPORT QgsTextShadowSettingsPrivate : public QSharedData
, radiusAlphaOnly( false )
, scale( 100 )
, color( QColor( 0, 0, 0, 76 ) )
, opacity( 0.7 )
, blendMode( QPainter::CompositionMode_Multiply )
{

Expand All @@ -193,6 +200,7 @@ class CORE_EXPORT QgsTextShadowSettingsPrivate : public QSharedData
, radiusAlphaOnly( other.radiusAlphaOnly )
, scale( other.scale )
, color( other.color )
, opacity( other.opacity )
, blendMode( other.blendMode )
{
}
Expand All @@ -212,6 +220,7 @@ class CORE_EXPORT QgsTextShadowSettingsPrivate : public QSharedData
bool radiusAlphaOnly;
int scale;
QColor color;
double opacity;
QPainter::CompositionMode blendMode;
};

Expand All @@ -225,6 +234,7 @@ class CORE_EXPORT QgsTextSettingsPrivate : public QSharedData
, fontSizeUnits( QgsUnitTypes::RenderPoints )
, fontSize( 10 )
, textColor( Qt::black )
, opacity( 1.0 )
, blendMode( QPainter::CompositionMode_SourceOver )
, multilineHeight( 1.0 )
{
Expand All @@ -238,6 +248,7 @@ class CORE_EXPORT QgsTextSettingsPrivate : public QSharedData
, fontSizeMapUnitScale( other.fontSizeMapUnitScale )
, fontSize( other.fontSize )
, textColor( other.textColor )
, opacity( other.opacity )
, blendMode( other.blendMode )
, multilineHeight( other.multilineHeight )
{
Expand All @@ -251,6 +262,7 @@ class CORE_EXPORT QgsTextSettingsPrivate : public QSharedData
QgsMapUnitScale fontSizeMapUnitScale;
double fontSize; //may differ from size in textFont due to units (eg size in map units)
QColor textColor;
double opacity;
QPainter::CompositionMode blendMode;
double multilineHeight; //0.0 to 10.0, leading between lines as multiplyer of line height

Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsvectorlayerlabelprovider.cpp
Expand Up @@ -727,7 +727,9 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
QPainter textp;
textp.begin( &textPict );
textp.setPen( Qt::NoPen );
textp.setBrush( tmpLyr.format().color() );
QColor textColor = tmpLyr.format().color();
textColor.setAlphaF( tmpLyr.format().opacity() );
textp.setBrush( textColor );
textp.drawPath( path );
// TODO: why are some font settings lost on drawPicture() when using drawText() inside QPicture?
// e.g. some capitalization options, but not others
Expand Down Expand Up @@ -764,7 +766,9 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
{
// draw text as text (for SVG and PDF exports)
painter->setFont( tmpLyr.format().scaledFont( context ) );
painter->setPen( tmpLyr.format().color() );
QColor textColor = tmpLyr.format().color();
textColor.setAlphaF( tmpLyr.format().opacity() );
painter->setPen( textColor );
painter->setRenderHint( QPainter::TextAntialiasing );
painter->drawText( 0, 0, component.text() );
}
Expand Down

0 comments on commit 074ae42

Please sign in to comment.