Skip to content

Commit

Permalink
Make QgsRenderContext available to more renderer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 86ace24 commit 57669e7
Show file tree
Hide file tree
Showing 41 changed files with 380 additions and 202 deletions.
6 changes: 3 additions & 3 deletions python/core/symbology-ng/qgscategorizedsymbolrendererv2.sip
Expand Up @@ -45,9 +45,9 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2

virtual ~QgsCategorizedSymbolRendererV2();

virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );

virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context );

virtual void startRender( QgsRenderContext& context, const QgsFields& fields );

Expand All @@ -64,7 +64,7 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
//! returns bitwise OR-ed capabilities of the renderer
virtual int capabilities();

virtual QgsSymbolV2List symbols();
virtual QgsSymbolV2List symbols( QgsRenderContext& context );
void updateSymbols( QgsSymbolV2 * sym );

const QgsCategoryList& categories() const;
Expand Down
6 changes: 3 additions & 3 deletions python/core/symbology-ng/qgsgraduatedsymbolrendererv2.sip
Expand Up @@ -87,9 +87,9 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2

virtual ~QgsGraduatedSymbolRendererV2();

virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );

virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context );

virtual void startRender( QgsRenderContext& context, const QgsFields& fields );

Expand All @@ -106,7 +106,7 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
//! returns bitwise OR-ed capabilities of the renderer
virtual int capabilities();

virtual QgsSymbolV2List symbols();
virtual QgsSymbolV2List symbols( QgsRenderContext& context );

QString classAttribute() const;
void setClassAttribute( QString attr );
Expand Down
4 changes: 2 additions & 2 deletions python/core/symbology-ng/qgsheatmaprenderer.sip
Expand Up @@ -13,8 +13,8 @@ class QgsHeatmapRenderer : QgsFeatureRendererV2
virtual void startRender( QgsRenderContext& context, const QgsFields& fields );
virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );
virtual void stopRender( QgsRenderContext& context );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2List symbols();
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );
virtual QgsSymbolV2List symbols( QgsRenderContext& context );
virtual QString dump() const;
virtual QList<QString> usedAttributes();
static QgsFeatureRendererV2* create( QDomElement& element ) /Factory/;
Expand Down
12 changes: 6 additions & 6 deletions python/core/symbology-ng/qgsinvertedpolygonrenderer.sip
Expand Up @@ -41,23 +41,23 @@ class QgsInvertedPolygonRenderer : QgsFeatureRendererV2
/** Proxy that will call this method on the embedded renderer. */
virtual int capabilities();
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2List symbols();
virtual QgsSymbolV2List symbols( QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feat );
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feat, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat );
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
/** Proxy that will call this method on the embedded renderer.
@note not available in python bindings
*/
// virtual QgsLegendSymbolList legendSymbolItems( double scaleDenominator = -1, QString rule = "" );
/** Proxy that will call this method on the embedded renderer. */
virtual bool willRenderFeature( QgsFeature& feat );
virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context );

/** Creates a renderer out of an XML, for loading*/
static QgsFeatureRendererV2* create( QDomElement& element ) /Factory/;
Expand Down
20 changes: 16 additions & 4 deletions python/core/symbology-ng/qgspointdisplacementrenderer.sip
Expand Up @@ -14,15 +14,27 @@ class QgsPointDisplacementRenderer : QgsFeatureRendererV2
/** Reimplemented from QgsFeatureRendererV2*/
bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );

QgsSymbolV2* symbolForFeature( QgsFeature& feature );
/** Partial proxy that will call this method on the embedded renderer. */
virtual QList<QString> usedAttributes();
/** Proxy that will call this method on the embedded renderer. */
virtual int capabilities();
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2List symbols( QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feat, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context );
/** Proxy that will call this method on the embedded renderer. */
virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context );

virtual void startRender( QgsRenderContext& context, const QgsFields& fields );

void stopRender( QgsRenderContext& context );

QList<QString> usedAttributes();
QgsSymbolV2List symbols();

//! create a renderer from XML element
static QgsFeatureRendererV2* create( QDomElement& symbologyElem ) /Factory/;
QDomElement save( QDomDocument& doc );
Expand Down
46 changes: 40 additions & 6 deletions python/core/symbology-ng/qgsrendererv2.sip
Expand Up @@ -65,15 +65,32 @@ class QgsFeatureRendererV2
* @param feature feature
* @return returns pointer to symbol or 0 if symbol was not found
*/
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature ) = 0;
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature ) /Deprecated/;

/** To be overridden
* @param feature feature
* @param context render context
* @return returns pointer to symbol or 0 if symbol was not found
* @note added in QGIS 2.12
*/
//TODO - make pure virtual when above method is removed
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );

/**
* Return symbol for feature. The difference compared to symbolForFeature() is that it returns original
* symbol which can be used as an identifier for renderer's rule - the former may return a temporary replacement
* of a symbol for use in rendering.
* @note added in 2.6
*/
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature ) /Deprecated/;

/**
* Return symbol for feature. The difference compared to symbolForFeature() is that it returns original
* symbol which can be used as an identifier for renderer's rule - the former may return a temporary replacement
* of a symbol for use in rendering.
* @note added in 2.12
*/
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context );

virtual void startRender( QgsRenderContext& context, const QgsFields& fields ) = 0;

Expand Down Expand Up @@ -108,7 +125,7 @@ class QgsFeatureRendererV2
virtual int capabilities();

//! for symbol levels
virtual QgsSymbolV2List symbols() = 0;
virtual QgsSymbolV2List symbols( QgsRenderContext& context ) = 0;

bool usingSymbolLevels() const;
void setUsingSymbolLevels( bool usingSymbolLevels );
Expand Down Expand Up @@ -180,17 +197,34 @@ class QgsFeatureRendererV2
//! return whether the renderer will render a feature or not.
//! Must be called between startRender() and stopRender() calls.
//! Default implementation uses symbolForFeature().
virtual bool willRenderFeature( QgsFeature& feat );
virtual bool willRenderFeature( QgsFeature& feat ) /Deprecated/;

/** Returns whether the renderer will render a feature or not.
* Must be called between startRender() and stopRender() calls.
* Default implementation uses symbolForFeature().
* @note added in QGIS 2.12
*/
virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext& context );

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat ) /Deprecated/;

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext& context );

//! Equivalent of originalSymbolsForFeature() call
//! extended to support renderers that may use more symbols per feature - similar to symbolsForFeature()
//! @note added in 2.6
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat ) /Deprecated/;

//! Equivalent of originalSymbolsForFeature() call
//! extended to support renderers that may use more symbols per feature - similar to symbolsForFeature()
//! @note added in 2.6
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat );
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext& context );

/** Allows for a renderer to modify the extent of a feature request prior to rendering
* @param extent reference to request's filter extent. Modify extent to change the
Expand Down
20 changes: 10 additions & 10 deletions python/core/symbology-ng/qgsrulebasedrendererv2.sip
Expand Up @@ -60,12 +60,12 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
~Rule();
QString dump( int offset = 0 ) const;
QSet<QString> usedAttributes();
QgsSymbolV2List symbols();
QgsSymbolV2List symbols( const QgsRenderContext& context = QgsRenderContext() );
//! @note not available in python bindings
// QgsLegendSymbolList legendSymbolItems();
//! @note added in 2.6
QgsLegendSymbolListV2 legendSymbolItemsV2( int currentLevel = -1 ) const;
bool isFilterOK( QgsFeature& f ) const;
bool isFilterOK( QgsFeature& f, QgsRenderContext* context = 0 ) const;
bool isScaleOK( double scale ) const;

QgsSymbolV2* symbol();
Expand Down Expand Up @@ -117,13 +117,13 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
bool renderFeature( QgsRuleBasedRendererV2::FeatureToRender& featToRender, QgsRenderContext& context, QgsRuleBasedRendererV2::RenderQueue& renderQueue );

//! only tell whether a feature will be rendered without actually rendering it
bool willRenderFeature( QgsFeature& feat );
bool willRenderFeature( QgsFeature& feat, QgsRenderContext* context = 0);

//! tell which symbols will be used to render the feature
QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext* context = 0 );

//! tell which rules will be used to render the feature
QList<QgsRuleBasedRendererV2::Rule*> rulesForFeature( QgsFeature& feat );
QList<QgsRuleBasedRendererV2::Rule*> rulesForFeature( QgsFeature& feat, QgsRenderContext* context = 0 );

void stopRender( QgsRenderContext& context );

Expand Down Expand Up @@ -171,7 +171,7 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
~QgsRuleBasedRendererV2();

//! return symbol for current feature. Should not be used individually: there could be more symbols for a feature
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext &context );

virtual bool renderFeature( QgsFeature& feature, QgsRenderContext& context, int layer = -1, bool selected = false, bool drawVertexMarker = false );

Expand All @@ -189,7 +189,7 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2

static QgsFeatureRendererV2* createFromSld( QDomElement& element, QGis::GeometryType geomType ) /Factory/;

virtual QgsSymbolV2List symbols();
virtual QgsSymbolV2List symbols( QgsRenderContext &context );

//! store renderer info to XML element
virtual QDomElement save( QDomDocument& doc );
Expand Down Expand Up @@ -223,14 +223,14 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2

//! return whether the renderer will render a feature or not.
//! Must be called between startRender() and stopRender() calls.
virtual bool willRenderFeature( QgsFeature& feat );
virtual bool willRenderFeature( QgsFeature& feat, QgsRenderContext &context );

//! return list of symbols used for rendering the feature.
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
//! to use symbolForFeature()
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat, QgsRenderContext &context );

virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat );
virtual QgsSymbolV2List originalSymbolsForFeature( QgsFeature& feat, QgsRenderContext &context );

//! returns bitwise OR-ed capabilities of the renderer
virtual int capabilities();
Expand Down
6 changes: 3 additions & 3 deletions python/core/symbology-ng/qgssinglesymbolrendererv2.sip
Expand Up @@ -9,9 +9,9 @@ class QgsSingleSymbolRendererV2 : QgsFeatureRendererV2

virtual ~QgsSingleSymbolRendererV2();

virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature, QgsRenderContext& context );

virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature );
virtual QgsSymbolV2* originalSymbolForFeature( QgsFeature& feature, QgsRenderContext& context );

This comment has been minimized.

Copy link
@slarosa

slarosa Sep 2, 2015

Member

@nyalldawson these changes for symbol renderer seem to break the API. Shouldn't be reimplemented the virtual QgsSymbolV2* symbolForFeature( QgsFeature& feature ); method as well?

The following code does not work anymore:

vl = iface.activeLayer()
f = QgsFeature()
symbol = vl.rendererV2().symbolForFeature(f)

I also see the symboloForFeature( QgsFeature& feature ) method is deprecated in QgsFeatureRendererV2, but it is supposed to work in qgis 2.12, isn't it?

thanks.

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Sep 2, 2015

Author Collaborator

@slarosa It should work - the deprecated method calls the new symbolForFeature( feature, context ) method, which is the one which is reimplemented by all the renderers.

What happens when you run your code? What's the actual error?

This comment has been minimized.

Copy link
@slarosa

slarosa Sep 3, 2015

Member

@nyalldawson I am getting a type error:

TypeError: QgsCategorizedSymbolRendererV2.symbolForFeature(QgsFeature, QgsRenderContext): not enough arguments


virtual void startRender( QgsRenderContext& context, const QgsFields& fields );

Expand Down Expand Up @@ -41,7 +41,7 @@ class QgsSingleSymbolRendererV2 : QgsFeatureRendererV2
//! returns bitwise OR-ed capabilities of the renderer
virtual int capabilities();

virtual QgsSymbolV2List symbols();
virtual QgsSymbolV2List symbols( QgsRenderContext& context );

//! create renderer from XML element
static QgsFeatureRendererV2* create( QDomElement& element ) /Factory/;
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolrotatepointsymbols.cpp
Expand Up @@ -117,7 +117,7 @@ void QgsMapToolRotatePointSymbols::canvasPressEvent( QMouseEvent *e )
if ( renderer->capabilities() & QgsFeatureRendererV2::MoreSymbolsPerFeature )
{
//could be multiple symbols for this feature, so check them all
foreach ( QgsSymbolV2* s, renderer->originalSymbolsForFeature( pointFeature ) )
foreach ( QgsSymbolV2* s, renderer->originalSymbolsForFeature( pointFeature, context ) )
{
if ( s && s->type() == QgsSymbolV2::Marker )
{
Expand All @@ -135,7 +135,7 @@ void QgsMapToolRotatePointSymbols::canvasPressEvent( QMouseEvent *e )
}
else
{
QgsSymbolV2* s = renderer->originalSymbolForFeature( pointFeature );
QgsSymbolV2* s = renderer->originalSymbolForFeature( pointFeature, context );
if ( s && s->type() == QgsSymbolV2::Marker )
{
markerSymbol = static_cast< QgsMarkerSymbolV2* >( s );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolselectutils.cpp
Expand Up @@ -159,7 +159,7 @@ void QgsMapToolSelectUtils::setSelectFeatures( QgsMapCanvas* canvas,
while ( fit.nextFeature( f ) )
{
// make sure to only use features that are visible
if ( r && !r->willRenderFeature( f ) )
if ( r && !r->willRenderFeature( f, context ) )
continue;

const QgsGeometry* g = f.constGeometry();
Expand Down
18 changes: 10 additions & 8 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -554,10 +554,11 @@ void QgsDxfExport::writeTables()
writeGroup( 2, "TABLES" );

// Iterate through all layers and get symbol layer pointers
QgsRenderContext context = renderContext();
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > slList;
if ( mSymbologyExport != NoSymbology )
{
slList = symbolLayers();
slList = symbolLayers( context );
}

// Line types
Expand Down Expand Up @@ -813,11 +814,13 @@ void QgsDxfExport::writeBlocks()
writeGroup( 100, "AcDbBlockEnd" );
}

QgsRenderContext ct = renderContext();

// Iterate through all layers and get symbol layer pointers
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > slList;
if ( mSymbologyExport != NoSymbology )
{
slList = symbolLayers();
slList = symbolLayers( ct );
}

QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > >::const_iterator slIt = slList.constBegin();
Expand All @@ -828,7 +831,6 @@ void QgsDxfExport::writeBlocks()
continue;

// if point symbol layer and no data defined properties: write block
QgsRenderContext ct;
QgsSymbolV2RenderContext ctx( ct, QgsSymbolV2::MapUnit, slIt->second->alpha(), false, slIt->second->renderHints(), 0 );
ml->startRender( ctx );

Expand Down Expand Up @@ -941,7 +943,7 @@ void QgsDxfExport::writeEntities()
}
else
{
QgsSymbolV2List symbolList = renderer->symbolsForFeature( fet );
QgsSymbolV2List symbolList = renderer->symbolsForFeature( fet, ctx );
if ( symbolList.size() < 1 )
{
continue;
Expand Down Expand Up @@ -1021,7 +1023,7 @@ void QgsDxfExport::writeEntitiesSymbolLevels( QgsVectorLayer* layer )
QgsSymbolV2* featureSymbol = 0;
while ( fit.nextFeature( fet ) )
{
featureSymbol = renderer->symbolForFeature( fet );
featureSymbol = renderer->symbolForFeature( fet, ctx );
if ( !featureSymbol )
{
continue;
Expand All @@ -1037,7 +1039,7 @@ void QgsDxfExport::writeEntitiesSymbolLevels( QgsVectorLayer* layer )

// find out order
QgsSymbolV2LevelOrder levels;
QgsSymbolV2List symbols = renderer->symbols();
QgsSymbolV2List symbols = renderer->symbols( ctx );
for ( int i = 0; i < symbols.count(); i++ )
{
QgsSymbolV2* sym = symbols[i];
Expand Down Expand Up @@ -3818,7 +3820,7 @@ double QgsDxfExport::mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::O
return scaleDenominator * QGis::fromUnitToUnitFactor( QGis::Meters, mapUnits ) / 1000.0;
}

QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > QgsDxfExport::symbolLayers()
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > QgsDxfExport::symbolLayers( QgsRenderContext &context )
{
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > symbolLayers;

Expand All @@ -3840,7 +3842,7 @@ QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > QgsDxfExport::symbolLayers()
}

// get all symbols
QgsSymbolV2List symbols = r->symbols();
QgsSymbolV2List symbols = r->symbols( context );
QgsSymbolV2List::iterator symbolIt = symbols.begin();
for ( ; symbolIt != symbols.end(); ++symbolIt )
{
Expand Down

0 comments on commit 57669e7

Please sign in to comment.