Skip to content

Commit

Permalink
[FEATURE][diagrams] Paint effect support for diagram renderer
Browse files Browse the repository at this point in the history
Allows for diagrams to use paint effects, including drop shadows,
outer glows, etc...

Sponsored by SLYR
  • Loading branch information
nyalldawson committed Nov 24, 2019
1 parent 204bd47 commit e8ec004
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 121 deletions.
21 changes: 21 additions & 0 deletions python/core/auto_generated/qgsdiagramrenderer.sip.in
Expand Up @@ -376,6 +376,7 @@ QgsDiagramLayerSettings stores settings which control how ALL diagrams within a
%Docstring
Constructor for QgsDiagramSettings
%End
~QgsDiagramSettings();

QgsDiagramSettings( const QgsDiagramSettings &other );
%Docstring
Expand Down Expand Up @@ -582,6 +583,26 @@ Sets whether the diagram axis should be shown.

.. seealso:: :py:func:`setAxisLineSymbol`

.. versionadded:: 3.12
%End

QgsPaintEffect *paintEffect() const;
%Docstring
Returns the paint effect to use while rendering diagrams.

.. seealso:: :py:func:`setPaintEffect`

.. versionadded:: 3.12
%End

void setPaintEffect( QgsPaintEffect *effect /Transfer/ );
%Docstring
Sets the paint ``effect`` to use while rendering diagrams.

Ownership of ``effect`` is transferred to the settings.

.. seealso:: :py:func:`paintEffect`

.. versionadded:: 3.12
%End

Expand Down
13 changes: 13 additions & 0 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -44,6 +44,7 @@
#include "qgsauxiliarystorage.h"
#include "qgsexpressioncontextutils.h"
#include "qgspropertytransformer.h"
#include "qgspainteffectregistry.h"

#include <QList>
#include <QMessageBox>
Expand Down Expand Up @@ -238,6 +239,8 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );
}

mPaintEffect.reset( QgsPaintEffectRegistry::defaultStack() );

const QgsDiagramRenderer *dr = layer->diagramRenderer();
if ( !dr ) //no diagram renderer yet, insert reasonable default
{
Expand Down Expand Up @@ -284,6 +287,7 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
mBackgroundColorButton->setColor( QColor( 255, 255, 255, 255 ) );
//force a refresh of widget status to match diagram type
mDiagramTypeComboBox_currentIndexChanged( mDiagramTypeComboBox->currentIndex() );

}
else // already a diagram renderer present
{
Expand Down Expand Up @@ -329,6 +333,9 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
mLabelPlacementComboBox->setCurrentIndex( 1 );
}

if ( settingList.at( 0 ).paintEffect() )
mPaintEffect.reset( settingList.at( 0 ).paintEffect()->clone() );

mAngleOffsetComboBox->setCurrentIndex( mAngleOffsetComboBox->findData( settingList.at( 0 ).rotationOffset ) );
mAngleDirectionComboBox->setCurrentIndex( mAngleDirectionComboBox->findData( settingList.at( 0 ).direction() ) );

Expand Down Expand Up @@ -483,6 +490,7 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
}
}
}
mPaintEffectWidget->setPaintEffect( mPaintEffect.get() );

connect( mAddAttributeExpression, &QPushButton::clicked, this, &QgsDiagramProperties::showAddAttributeExpressionDialog );
registerDataDefinedButton( mBackgroundColorDDBtn, QgsDiagramLayerSettings::BackgroundColor );
Expand Down Expand Up @@ -841,6 +849,11 @@ void QgsDiagramProperties::apply()
ds.setSpacingUnit( mBarSpacingUnitComboBox->unit() );
ds.setSpacingMapUnitScale( mBarSpacingUnitComboBox->getMapUnitScale() );

if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect.get() ) )
ds.setPaintEffect( mPaintEffect->clone() );
else
ds.setPaintEffect( nullptr );

QgsDiagramRenderer *renderer = nullptr;
if ( mFixedSizeRadio->isChecked() )
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsdiagramproperties.h
Expand Up @@ -68,6 +68,8 @@ class APP_EXPORT QgsDiagramProperties : public QWidget, private Ui::QgsDiagramPr
//! Polygon placement button group
QButtonGroup *mPlacePolygonBtnGrp = nullptr;

std::unique_ptr< QgsPaintEffect> mPaintEffect;

enum Columns
{
ColumnAttributeExpression = 0,
Expand Down
38 changes: 34 additions & 4 deletions src/core/qgsdiagramrenderer.cpp
Expand Up @@ -24,6 +24,9 @@
#include "qgslayertreemodellegendnode.h"
#include "qgsfontutils.h"
#include "qgssymbollayerutils.h"
#include "qgspainteffectregistry.h"
#include "qgspainteffect.h"
#include "qgsapplication.h"

#include <QDomElement>
#include <QPainter>
Expand Down Expand Up @@ -330,6 +333,12 @@ void QgsDiagramSettings::readXml( const QDomElement &elem, const QgsReadWriteCon
categoryLabels.append( *catIt );
}
}

QDomElement effectElem = elem.firstChildElement( QStringLiteral( "effect" ) );
if ( !effectElem.isNull() )
setPaintEffect( QgsApplication::paintEffectRegistry()->createEffect( effectElem ) );
else
setPaintEffect( QgsApplication::paintEffectRegistry()->defaultStack() );
}

void QgsDiagramSettings::writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const
Expand Down Expand Up @@ -398,10 +407,6 @@ void QgsDiagramSettings::writeXml( QDomElement &rendererElem, QDomDocument &doc,
case Up:
categoryElem.setAttribute( QStringLiteral( "diagramOrientation" ), QStringLiteral( "Up" ) );
break;

default:
categoryElem.setAttribute( QStringLiteral( "diagramOrientation" ), QStringLiteral( "Up" ) );
break;
}

categoryElem.setAttribute( QStringLiteral( "barWidth" ), QString::number( barWidth ) );
Expand All @@ -425,6 +430,9 @@ void QgsDiagramSettings::writeXml( QDomElement &rendererElem, QDomDocument &doc,
axisSymbolElem.appendChild( symbolElem );
categoryElem.appendChild( axisSymbolElem );

if ( mPaintEffect && !QgsPaintEffectRegistry::isDefaultStack( mPaintEffect.get() ) )
mPaintEffect->saveProperties( doc, categoryElem );

rendererElem.appendChild( categoryElem );
}

Expand Down Expand Up @@ -474,6 +482,13 @@ void QgsDiagramRenderer::renderDiagram( const QgsFeature &feature, QgsRenderCont
s.rotationOffset = properties.valueAsDouble( QgsDiagramLayerSettings::StartAngle, c.expressionContext(), s.rotationOffset );
}

QgsPaintEffect *effect = s.paintEffect();
std::unique_ptr< QgsEffectPainter > effectPainter;
if ( effect && effect->enabled() )
{
effectPainter = qgis::make_unique< QgsEffectPainter >( c, effect );
}

mDiagram->renderDiagram( feature, c, s, pos );
}

Expand Down Expand Up @@ -807,11 +822,24 @@ void QgsDiagramSettings::setShowAxis( bool showAxis )
mShowAxis = showAxis;
}

QgsPaintEffect *QgsDiagramSettings::paintEffect() const
{
return mPaintEffect.get();
}

void QgsDiagramSettings::setPaintEffect( QgsPaintEffect *effect )
{
if ( effect != mPaintEffect.get() )
mPaintEffect.reset( effect );
}

QgsDiagramSettings::QgsDiagramSettings()
: mAxisLineSymbol( qgis::make_unique< QgsLineSymbol >() )
{
}

QgsDiagramSettings::~QgsDiagramSettings() = default;

QgsDiagramSettings::QgsDiagramSettings( const QgsDiagramSettings &other )
: enabled( other.enabled )
, font( other.font )
Expand Down Expand Up @@ -842,6 +870,7 @@ QgsDiagramSettings::QgsDiagramSettings( const QgsDiagramSettings &other )
, mDirection( other.mDirection )
, mShowAxis( other.mShowAxis )
, mAxisLineSymbol( other.mAxisLineSymbol ? other.mAxisLineSymbol->clone() : nullptr )
, mPaintEffect( other.mPaintEffect ? other.mPaintEffect->clone() : nullptr )
{

}
Expand Down Expand Up @@ -877,6 +906,7 @@ QgsDiagramSettings &QgsDiagramSettings::operator=( const QgsDiagramSettings &oth
mDirection = other.mDirection;
mAxisLineSymbol.reset( other.mAxisLineSymbol ? other.mAxisLineSymbol->clone() : nullptr );
mShowAxis = other.mShowAxis;
mPaintEffect.reset( other.mPaintEffect ? other.mPaintEffect->clone() : nullptr );
return *this;
}

Expand Down
23 changes: 23 additions & 0 deletions src/core/qgsdiagramrenderer.h
Expand Up @@ -44,6 +44,7 @@ class QgsReadWriteContext;
class QgsVectorLayer;
class QgsLayerTreeModelLegendNode;
class QgsLayerTreeLayer;
class QgsPaintEffect;

namespace pal { class Layer; } SIP_SKIP

Expand Down Expand Up @@ -407,6 +408,7 @@ class CORE_EXPORT QgsDiagramSettings

//! Constructor for QgsDiagramSettings
QgsDiagramSettings();
~QgsDiagramSettings();

//! Copy constructor
QgsDiagramSettings( const QgsDiagramSettings &other );
Expand Down Expand Up @@ -618,6 +620,26 @@ class CORE_EXPORT QgsDiagramSettings
*/
void setShowAxis( bool showAxis );

/**
* Returns the paint effect to use while rendering diagrams.
*
* \see setPaintEffect()
*
* \since QGIS 3.12
*/
QgsPaintEffect *paintEffect() const;

/**
* Sets the paint \a effect to use while rendering diagrams.
*
* Ownership of \a effect is transferred to the settings.
*
* \see paintEffect()
*
* \since QGIS 3.12
*/
void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );

private:

double mSpacing = 0;
Expand All @@ -627,6 +649,7 @@ class CORE_EXPORT QgsDiagramSettings

bool mShowAxis = false;
std::unique_ptr< QgsLineSymbol > mAxisLineSymbol;
std::unique_ptr< QgsPaintEffect > mPaintEffect;

};

Expand Down

0 comments on commit e8ec004

Please sign in to comment.