Skip to content
Permalink
Browse files

Flip symbology from alpha/transparency to opacity

  • Loading branch information
nyalldawson committed May 30, 2017
1 parent 233a95e commit a3262240fe63e57b4849482075940b5c9f6bac2a
@@ -1837,6 +1837,12 @@ QProgressDialog argument will need to be updated using manually created connecti
signals and the updates to the progress dialog.


QgsRasterFillSymbolLayer {#qgis_api_break_3_0_QgsRasterFillSymbolLayer}
-----------------

- alpha() and setAlpha() were removed. Use opacity() and setOpacity() instead.


QgsRasterInterface {#qgis_api_break_3_0_QgsRasterInterface}
------------------

@@ -2108,6 +2114,7 @@ than an integer value
- usedAttributes() now requires a QgsRenderContext argument.
- setDataDefinedAngle(), dataDefinedAngle(), setDataDefinedSize(), dataDefinedSize(), setDataDefinedWidth() and dataDefinedWidth()
now work with QgsProperty objects instead of QgsDataDefined.
- alpha() and setAlpha() were removed. Use opacity() and setOpacity() instead.


QgsSymbolLayer (renamed from QgsSymbolLayerV2) {#qgis_api_break_3_0_QgsSymbolLayer}
@@ -2146,6 +2153,7 @@ QgsSymbolRenderContext (renamed from QgsSymbolV2RenderContext) {#qgis_api
- The constructor, setRenderHints() and renderHints() now accept and return a QgsSymbol::RenderHints flag rather
than an integer value
- fields() now returns a QgsFields value, not a pointer.
- alpha() and setAlpha() were removed. Use opacity() and setOpacity() instead.


QgsSymbolLayerUtils (renamed from QgsSymbolLayerUtilsV2) {#qgis_api_break_3_0_QgsSymbolLayerUtils}
@@ -550,16 +550,8 @@ class QgsRasterFillSymbolLayer: QgsImageFillSymbolLayer
*/
FillCoordinateMode coordinateMode() const;

/** Sets the opacity for the raster image used in the fill.
* @param alpha opacity value between 0 (fully transparent) and 1 (fully opaque)
* @see alpha
*/
void setAlpha( const double alpha );
/** The opacity for the raster image used in the fill.
* @returns opacity value between 0 (fully transparent) and 1 (fully opaque)
* @see setAlpha
*/
double alpha() const;
void setOpacity( const double opacity );
double opacity() const;

/** Sets the offset for the fill.
* @param offset offset for fill
@@ -175,10 +175,8 @@ class QgsSymbol
QgsMapUnitScale mapUnitScale() const;
void setMapUnitScale( const QgsMapUnitScale &scale );

//! Get alpha transparency 1 for opaque, 0 for invisible
qreal alpha() const;
//! Set alpha transparency 1 for opaque, 0 for invisible
void setAlpha( qreal alpha );
qreal opacity() const;
void setOpacity( qreal opacity );

/** Sets rendering hint flags for the symbol.
* @see renderHints()
@@ -303,17 +301,7 @@ class QgsSymbolRenderContext

public:

/** Constructor for QgsSymbolRenderContext
* @param c
* @param u
* @param alpha
* @param selected set to true if symbol should be drawn in a "selected" state
* @param renderHints flags controlling rendering behavior
* @param f
* @param fields
* @param mapUnitScale
*/
QgsSymbolRenderContext( QgsRenderContext &c, QgsUnitTypes::RenderUnit u, qreal alpha = 1.0, bool selected = false, QgsSymbol::RenderHints renderHints = 0, const QgsFeature *f = 0, const QgsFields &fields = QgsFields(), const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
QgsSymbolRenderContext( QgsRenderContext &c, QgsUnitTypes::RenderUnit u, qreal opacity = 1.0, bool selected = false, QgsSymbol::RenderHints renderHints = 0, const QgsFeature *f = 0, const QgsFields &fields = QgsFields(), const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
~QgsSymbolRenderContext();

QgsRenderContext &renderContext();
@@ -335,10 +323,8 @@ class QgsSymbolRenderContext
QgsMapUnitScale mapUnitScale() const;
void setMapUnitScale( const QgsMapUnitScale &scale );

//! Get alpha transparency 1 for opaque, 0 for invisible
qreal alpha() const;
//! Set alpha transparency 1 for opaque, 0 for invisible
void setAlpha( qreal alpha );
qreal opacity() const;
void setOpacity( qreal opacity );

bool selected() const;
void setSelected( bool selected );
@@ -35,7 +35,6 @@ class QgsSymbolsListWidget : QWidget
void saveSymbol();
void symbolAddedToStyle( const QString &name, QgsSymbol *symbol );
void on_mSymbolUnitWidget_changed();
void on_mTransparencySlider_valueChanged( int value );

//! Pupulates the groups combo box with available tags and smartgroups
void populateGroups();
@@ -1133,7 +1133,7 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/Line" ), cboStyleLine->currentText() );
QgsProject::instance()->writeEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/Fill" ), cboStyleFill->currentText() );
QgsProject::instance()->writeEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/ColorRamp" ), cboStyleColorRamp->currentText() );
QgsProject::instance()->writeEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/AlphaInt" ), ( int )( 255 - ( mTransparencySlider->value() * 2.55 ) ) );
QgsProject::instance()->writeEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/Opacity" ), 1.0 - mTransparencySlider->value() / 100.0 );
QgsProject::instance()->writeEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/RandomColors" ), cbxStyleRandomColors->isChecked() );
if ( mTreeProjectColors->isDirty() )
{
@@ -1687,7 +1687,15 @@ void QgsProjectProperties::populateStyles()
cbxStyleRandomColors->setChecked( QgsProject::instance()->readBoolEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/RandomColors" ), true ) );

// alpha transparency
int transparencyInt = ( 255 - QgsProject::instance()->readNumEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/AlphaInt" ), 255 ) ) / 2.55;
int transparencyInt = 255;
bool ok = false;
double alpha = QgsProject::instance()->readDoubleEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/AlphaInt" ), 255, &ok );
if ( ok )
transparencyInt = alpha;
double newOpacity = QgsProject::instance()->readDoubleEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/Opacity" ), 1.0, &ok );
if ( ok )
transparencyInt = 255 - newOpacity * 255.0;

mTransparencySlider->setValue( transparencyInt );
}

@@ -58,7 +58,7 @@ void QgsComposerMapOverview::createDefaultFrameSymbol()
properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "no" ) );
mFrameSymbol = QgsFillSymbol::createSimple( properties );
mFrameSymbol->setAlpha( 0.3 );
mFrameSymbol->setOpacity( 0.3 );
}

QgsComposerMapOverview::~QgsComposerMapOverview()
@@ -897,7 +897,7 @@ void QgsDxfExport::writeBlocks()
continue;

// if point symbol layer and no data defined properties: write block
QgsSymbolRenderContext ctx( ct, QgsUnitTypes::RenderMapUnits, slIt->second->alpha(), false, slIt->second->renderHints(), nullptr );
QgsSymbolRenderContext ctx( ct, QgsUnitTypes::RenderMapUnits, slIt->second->opacity(), false, slIt->second->renderHints(), nullptr );
ml->startRender( ctx );

// markers with data defined properties are inserted inline
@@ -232,20 +232,20 @@ QString QgsSimpleFillSymbolLayer::layerType() const
void QgsSimpleFillSymbolLayer::startRender( QgsSymbolRenderContext &context )
{
QColor fillColor = mColor;
fillColor.setAlphaF( context.alpha() * mColor.alphaF() );
fillColor.setAlphaF( context.opacity() * mColor.alphaF() );
mBrush = QBrush( fillColor, mBrushStyle );

QColor selColor = context.renderContext().selectionColor();
QColor selPenColor = selColor == mColor ? selColor : mStrokeColor;
if ( ! SELECTION_IS_OPAQUE ) selColor.setAlphaF( context.alpha() );
if ( ! SELECTION_IS_OPAQUE ) selColor.setAlphaF( context.opacity() );
mSelBrush = QBrush( selColor );
// N.B. unless a "selection line color" is implemented in addition to the "selection color" option
// this would mean symbols with "no fill" look the same whether or not they are selected
if ( SELECT_FILL_STYLE )
mSelBrush.setStyle( mBrushStyle );

QColor strokeColor = mStrokeColor;
strokeColor.setAlphaF( context.alpha() * mStrokeColor.alphaF() );
strokeColor.setAlphaF( context.opacity() * mStrokeColor.alphaF() );
mPen = QPen( strokeColor );
mSelPen = QPen( selPenColor );
mPen.setStyle( mStrokeStyle );
@@ -767,9 +767,9 @@ void QgsGradientFillSymbolLayer::applyGradient( const QgsSymbolRenderContext &co
{
//update alpha of gradient colors
QColor fillColor = color;
fillColor.setAlphaF( context.alpha() * fillColor.alphaF() );
fillColor.setAlphaF( context.opacity() * fillColor.alphaF() );
QColor fillColor2 = color2;
fillColor2.setAlphaF( context.alpha() * fillColor2.alphaF() );
fillColor2.setAlphaF( context.opacity() * fillColor2.alphaF() );

//rotate reference points
QPointF rotatedReferencePoint1 = !qgsDoubleNear( angle, 0.0 ) ? rotateReferencePoint( referencePoint1, angle ) : referencePoint1;
@@ -817,7 +817,7 @@ void QgsGradientFillSymbolLayer::applyGradient( const QgsSymbolRenderContext &co
{
//color ramp gradient
QgsGradientColorRamp *gradRamp = static_cast<QgsGradientColorRamp *>( gradientRamp );
gradRamp->addStopsToGradient( &gradient, context.alpha() );
gradRamp->addStopsToGradient( &gradient, context.opacity() );
}
else
{
@@ -833,7 +833,8 @@ void QgsGradientFillSymbolLayer::applyGradient( const QgsSymbolRenderContext &co
void QgsGradientFillSymbolLayer::startRender( QgsSymbolRenderContext &context )
{
QColor selColor = context.renderContext().selectionColor();
if ( ! SELECTION_IS_OPAQUE ) selColor.setAlphaF( context.alpha() );
if ( ! SELECTION_IS_OPAQUE )
selColor.setAlphaF( context.opacity() );
mSelBrush = QBrush( selColor );
}

@@ -1125,7 +1126,8 @@ void QgsShapeburstFillSymbolLayer::startRender( QgsSymbolRenderContext &context
{
//TODO - check this
QColor selColor = context.renderContext().selectionColor();
if ( ! SELECTION_IS_OPAQUE ) selColor.setAlphaF( context.alpha() );
if ( ! SELECTION_IS_OPAQUE )
selColor.setAlphaF( context.opacity() );
mSelBrush = QBrush( selColor );
}

@@ -1237,7 +1239,7 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList

//copy distance transform values back to QImage, shading by appropriate color ramp
dtArrayToQImage( dtArray, fillImage, mColorType == QgsShapeburstFillSymbolLayer::SimpleTwoColor ? mTwoColorGradientRamp : mGradientRamp,
context.alpha(), useWholeShape, outputPixelMaxDist );
context.opacity(), useWholeShape, outputPixelMaxDist );

//clean up some variables
delete [] dtArray;
@@ -1962,10 +1964,10 @@ void QgsSVGFillSymbolLayer::applyPattern( QBrush &brush, const QString &svgFileP
}

QTransform brushTransform;
if ( !qgsDoubleNear( context.alpha(), 1.0 ) )
if ( !qgsDoubleNear( context.opacity(), 1.0 ) )
{
QImage transparentImage = fitsInCache ? patternImage.copy() : mSvgPattern->copy();
QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.alpha() );
QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.opacity() );
brush.setTextureImage( transparentImage );
}
else
@@ -2771,10 +2773,10 @@ void QgsLinePatternFillSymbolLayer::applyPattern( const QgsSymbolRenderContext &
patternImage = patternImage.copy( xBuffer, yBuffer, patternImage.width() - 2 * xBuffer, patternImage.height() - 2 * yBuffer );

//set image to mBrush
if ( !qgsDoubleNear( context.alpha(), 1.0 ) )
if ( !qgsDoubleNear( context.opacity(), 1.0 ) )
{
QImage transparentImage = patternImage.copy();
QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.alpha() );
QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.opacity() );
brush.setTextureImage( transparentImage );
}
else
@@ -3185,10 +3187,10 @@ void QgsPointPatternFillSymbolLayer::applyPattern( const QgsSymbolRenderContext
mMarkerSymbol->stopRender( pointRenderContext );
}

if ( !qgsDoubleNear( context.alpha(), 1.0 ) )
if ( !qgsDoubleNear( context.opacity(), 1.0 ) )
{
QImage transparentImage = patternImage.copy();
QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.alpha() );
QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.opacity() );
brush.setTextureImage( transparentImage );
}
else
@@ -3418,7 +3420,7 @@ QColor QgsCentroidFillSymbolLayer::color() const

void QgsCentroidFillSymbolLayer::startRender( QgsSymbolRenderContext &context )
{
mMarker->setAlpha( context.alpha() );
mMarker->setOpacity( context.opacity() );
mMarker->startRender( context.renderContext(), context.fields() );

mCurrentFeatureId = -1;
@@ -3590,7 +3592,6 @@ QgsRasterFillSymbolLayer::QgsRasterFillSymbolLayer( const QString &imageFilePath
: QgsImageFillSymbolLayer()
, mImageFilePath( imageFilePath )
, mCoordinateMode( QgsRasterFillSymbolLayer::Feature )
, mAlpha( 1.0 )
, mOffsetUnit( QgsUnitTypes::RenderMillimeters )
, mWidth( 0.0 )
, mWidthUnit( QgsUnitTypes::RenderPixels )
@@ -3633,7 +3634,7 @@ QgsSymbolLayer *QgsRasterFillSymbolLayer::create( const QgsStringMap &properties
}
QgsRasterFillSymbolLayer *symbolLayer = new QgsRasterFillSymbolLayer( imagePath );
symbolLayer->setCoordinateMode( mode );
symbolLayer->setAlpha( alpha );
symbolLayer->setOpacity( alpha );
symbolLayer->setOffset( offset );
symbolLayer->setAngle( angle );
symbolLayer->setWidth( width );
@@ -3701,7 +3702,7 @@ void QgsRasterFillSymbolLayer::renderPolygon( const QPolygonF &points, QList<QPo

void QgsRasterFillSymbolLayer::startRender( QgsSymbolRenderContext &context )
{
applyPattern( mBrush, mImageFilePath, mWidth, mAlpha, context );
applyPattern( mBrush, mImageFilePath, mWidth, mOpacity, context );
}

void QgsRasterFillSymbolLayer::stopRender( QgsSymbolRenderContext &context )
@@ -3714,7 +3715,7 @@ QgsStringMap QgsRasterFillSymbolLayer::properties() const
QgsStringMap map;
map[QStringLiteral( "imageFile" )] = mImageFilePath;
map[QStringLiteral( "coordinate_mode" )] = QString::number( mCoordinateMode );
map[QStringLiteral( "alpha" )] = QString::number( mAlpha );
map[QStringLiteral( "alpha" )] = QString::number( mOpacity );
map[QStringLiteral( "offset" )] = QgsSymbolLayerUtils::encodePoint( mOffset );
map[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
map[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
@@ -3729,7 +3730,7 @@ QgsRasterFillSymbolLayer *QgsRasterFillSymbolLayer::clone() const
{
QgsRasterFillSymbolLayer *sl = new QgsRasterFillSymbolLayer( mImageFilePath );
sl->setCoordinateMode( mCoordinateMode );
sl->setAlpha( mAlpha );
sl->setOpacity( mOpacity );
sl->setOffset( mOffset );
sl->setOffsetUnit( mOffsetUnit );
sl->setOffsetMapUnitScale( mOffsetMapUnitScale );
@@ -3757,9 +3758,9 @@ void QgsRasterFillSymbolLayer::setCoordinateMode( const QgsRasterFillSymbolLayer
mCoordinateMode = mode;
}

void QgsRasterFillSymbolLayer::setAlpha( const double alpha )
void QgsRasterFillSymbolLayer::setOpacity( const double opacity )
{
mAlpha = alpha;
mOpacity = opacity;
}

void QgsRasterFillSymbolLayer::applyDataDefinedSettings( QgsSymbolRenderContext &context )
@@ -3769,10 +3770,10 @@ void QgsRasterFillSymbolLayer::applyDataDefinedSettings( QgsSymbolRenderContext

bool hasWidthExpression = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyWidth );
bool hasFileExpression = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyFile );
bool hasAlphaExpression = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyAlpha );
bool hasOpacityExpression = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyOpacity );
bool hasAngleExpression = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyAngle );

if ( !hasWidthExpression && !hasAngleExpression && !hasAlphaExpression && !hasFileExpression )
if ( !hasWidthExpression && !hasAngleExpression && !hasOpacityExpression && !hasFileExpression )
{
return; //no data defined settings
}
@@ -3786,7 +3787,7 @@ void QgsRasterFillSymbolLayer::applyDataDefinedSettings( QgsSymbolRenderContext
mNextAngle = nextAngle;
}

if ( !hasWidthExpression && !hasAlphaExpression && !hasFileExpression )
if ( !hasWidthExpression && !hasOpacityExpression && !hasFileExpression )
{
return; //nothing further to do
}
@@ -3797,19 +3798,19 @@ void QgsRasterFillSymbolLayer::applyDataDefinedSettings( QgsSymbolRenderContext
context.setOriginalValueVariable( mWidth );
width = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyWidth, context.renderContext().expressionContext(), width );
}
double alpha = mAlpha;
if ( hasAlphaExpression )
double opacity = mOpacity;
if ( hasOpacityExpression )
{
context.setOriginalValueVariable( mAlpha );
alpha = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyAlpha, context.renderContext().expressionContext(), alpha );
context.setOriginalValueVariable( mOpacity );
opacity = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyOpacity, context.renderContext().expressionContext(), opacity * 100 ) / 100.0;
}
QString file = mImageFilePath;
if ( hasFileExpression )
{
context.setOriginalValueVariable( mImageFilePath );
file = mDataDefinedProperties.valueAsString( QgsSymbolLayer::PropertyFile, context.renderContext().expressionContext(), file );
}
applyPattern( mBrush, file, width, alpha, context );
applyPattern( mBrush, file, width, opacity, context );
}

void QgsRasterFillSymbolLayer::applyPattern( QBrush &brush, const QString &imageFilePath, const double width, const double alpha, const QgsSymbolRenderContext &context )

0 comments on commit a326224

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