Skip to content
Permalink
Browse files

Fix #9658 (crash with diagrams)

  • Loading branch information
wonder-sk committed Feb 28, 2014
1 parent 3b1e192 commit 3a317a061e0f2be9726b66f7f5caca304948fba7
@@ -24,6 +24,9 @@ class QgsDiagram
virtual QSizeF diagramSize( const QgsFeature& feature, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) = 0;

protected:
QgsDiagram();
QgsDiagram( const QgsDiagram& other );

/** Changes the pen width to match the current settings and rendering context
* @param pen The pen to modify
* @param s The settings that specify the pen width
@@ -55,7 +55,7 @@ class QgsLabelingEngineInterface
//! @note: this method was added in version 1.9
virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
//! adds a diagram layer to the labeling engine
virtual int addDiagramLayer( QgsVectorLayer* layer, QgsDiagramLayerSettings* s );
virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s );
//! called for every feature
virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() ) = 0;
//! called for every diagram feature
@@ -20,6 +20,19 @@
#include <QPainter>



QgsDiagram::QgsDiagram()
{

}

QgsDiagram::QgsDiagram( const QgsDiagram& other )
{
Q_UNUSED( other );
// do not copy the cached expression map - the expressions need to be created and prepared with getExpression(...) call
}


void QgsDiagram::clearCache()
{
QMapIterator<QString, QgsExpression*> i( mExpressions );
@@ -54,6 +54,9 @@ class CORE_EXPORT QgsDiagram
virtual QSizeF diagramSize( const QgsFeature& feature, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) = 0;

protected:
QgsDiagram();
QgsDiagram( const QgsDiagram& other );

/** Changes the pen width to match the current settings and rendering context
* @param pen The pen to modify
* @param s The settings that specify the pen width
@@ -88,6 +88,7 @@ class CORE_EXPORT QgsDiagramLayerSettings
const QgsCoordinateTransform* ct;
const QgsMapToPixel* xform;
QList<QgsPalGeometry*> geometries;
QgsFields fields;

int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
@@ -92,7 +92,7 @@ class CORE_EXPORT QgsLabelingEngineInterface
//! @note: this method was added in version 1.9
virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
//! adds a diagram layer to the labeling engine
virtual int addDiagramLayer( QgsVectorLayer* layer, QgsDiagramLayerSettings* s )
virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s )
{ Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
//! called for every feature
virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() ) = 0;
@@ -118,13 +118,12 @@ class QgsPalGeometry : public PalGeometry

QFontMetricsF* getLabelFontMetrics() { return mFontMetrics; }

void setDiagramAttributes( const QgsAttributes& attrs, const QgsFields* fields ) { mDiagramAttributes = attrs; mDiagramFields = fields; }
void setDiagramAttributes( const QgsAttributes& attrs ) { mDiagramAttributes = attrs; }
const QgsAttributes& diagramAttributes() { return mDiagramAttributes; }

void feature( QgsFeature& feature )
{
feature.setFeatureId( mId );
feature.setFields( mDiagramFields, false );
feature.setAttributes( mDiagramAttributes );
feature.setValid( true );
}
@@ -147,7 +146,6 @@ class QgsPalGeometry : public PalGeometry

/**Stores attribute values for diagram rendering*/
QgsAttributes mDiagramAttributes;
const QgsFields* mDiagramFields;
};

#endif //QGSPALGEOMETRY_H
@@ -3342,20 +3342,25 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QStringList& attrNames,
return 1; // init successful
}

int QgsPalLabeling::addDiagramLayer( QgsVectorLayer* layer, QgsDiagramLayerSettings *s )
int QgsPalLabeling::addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings *s )
{
Layer* l = mPal->addLayer( layer->id().append( "d" ).toUtf8().data(), -1, -1, pal::Arrangement( s->placement ), METER, s->priority, s->obstacle, true, true );
l->setArrangementFlags( s->placementFlags );

s->palLayer = l;
s->ct = 0;
mActiveDiagramLayers.insert( layer->id(), *s );
// initialize the local copy
QgsDiagramLayerSettings& s2 = mActiveDiagramLayers[layer->id()];

s2.palLayer = l;
s2.ct = 0;
if ( mMapSettings->hasCrsTransformEnabled() )
s->ct = new QgsCoordinateTransform( layer->crs(), mMapSettings->destinationCrs() );
s2.ct = new QgsCoordinateTransform( layer->crs(), mMapSettings->destinationCrs() );

s->xform = &mMapSettings->mapToPixel();
mActiveDiagramLayers.insert( layer->id(), *s );
s2.xform = &mMapSettings->mapToPixel();

s2.fields = layer->pendingFields();

mActiveDiagramLayers[ layer->id()].renderer = layer->diagramRenderer()->clone();
s2.renderer = layer->diagramRenderer()->clone();

return 1;
}
@@ -3409,7 +3414,7 @@ void QgsPalLabeling::registerDiagramFeature( const QString& layerID, QgsFeature&
}

//append the diagram attributes to lbl
lbl->setDiagramAttributes( feat.attributes(), feat.fields() );
lbl->setDiagramAttributes( feat.attributes() );
}

// feature to the layer
@@ -3934,6 +3939,7 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
{
if ( QString( dit.key() + "d" ) == layerName )
{
feature.setFields( &dit.value().fields );
palGeometry->feature( feature );
QgsPoint outPt = xform.transform(( *it )->getX(), ( *it )->getY() );
dit.value().renderer->renderDiagram( feature, context, QPointF( outPt.x(), outPt.y() ) );
@@ -744,7 +744,7 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
//! hook called when drawing layer before issuing select()
virtual int prepareLayer( QgsVectorLayer* layer, QStringList &attrNames, QgsRenderContext& ctx );
//! adds a diagram layer to the labeling engine
virtual int addDiagramLayer( QgsVectorLayer* layer, QgsDiagramLayerSettings *s );
virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings *s );
//! hook called when drawing for every feature in a layer
virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() );
virtual void registerDiagramFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() );
@@ -445,16 +445,17 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList

mDiagrams = true;

QgsDiagramLayerSettings diagSettings = *layer->diagramLayerSettings();
const QgsDiagramRendererV2* diagRenderer = layer->diagramRenderer();
const QgsDiagramLayerSettings* diagSettings = layer->diagramLayerSettings();

mContext.labelingEngine()->addDiagramLayer( layer, &diagSettings );
mContext.labelingEngine()->addDiagramLayer( layer, diagSettings ); // will make internal copy of diagSettings + initialize it

//add attributes needed by the diagram renderer
QList<QString> att = layer->diagramRenderer()->diagramAttributes();
QList<QString> att = diagRenderer->diagramAttributes();
QList<QString>::const_iterator attIt = att.constBegin();
for ( ; attIt != att.constEnd(); ++attIt )
{
QgsExpression* expression = layer->diagramRenderer()->diagram()->getExpression( *attIt, &mFields );
QgsExpression* expression = diagRenderer->diagram()->getExpression( *attIt, &mFields );
QStringList columns = expression->referencedColumns();
QStringList::const_iterator columnsIterator = columns.constBegin();
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
@@ -469,7 +470,7 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
{
if ( linearlyInterpolatedDiagramRenderer->classificationAttributeIsExpression() )
{
QgsExpression* expression = layer->diagramRenderer()->diagram()->getExpression( linearlyInterpolatedDiagramRenderer->classificationAttributeExpression(), &mFields );
QgsExpression* expression = diagRenderer->diagram()->getExpression( linearlyInterpolatedDiagramRenderer->classificationAttributeExpression(), &mFields );
QStringList columns = expression->referencedColumns();
QStringList::const_iterator columnsIterator = columns.constBegin();
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
@@ -487,8 +488,8 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
}

//and the ones needed for data defined diagram positions
if ( diagSettings.xPosColumn != -1 )
attributeNames << mFields.at( diagSettings.xPosColumn ).name();
if ( diagSettings.yPosColumn != -1 )
attributeNames << mFields.at( diagSettings.yPosColumn ).name();
if ( diagSettings->xPosColumn != -1 )
attributeNames << mFields.at( diagSettings->xPosColumn ).name();
if ( diagSettings->yPosColumn != -1 )
attributeNames << mFields.at( diagSettings->yPosColumn ).name();
}

0 comments on commit 3a317a0

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