Skip to content

Commit

Permalink
Improved labeling engine interface, now connected with QgsMapRenderer…
Browse files Browse the repository at this point in the history
… instead of individual layers.

Also fixes #2108.


git-svn-id: http://svn.osgeo.org/qgis/trunk@12174 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Nov 18, 2009
1 parent ae722e7 commit c1676d5
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 93 deletions.
36 changes: 36 additions & 0 deletions python/core/qgsmaprenderer.sip
@@ -1,4 +1,31 @@


/** Labeling engine interface.
* \note Added in QGIS v1.4
*/
class QgsLabelingEngineInterface
{
%TypeHeaderCode
#include <qgsmaprenderer.h>
%End

public:
virtual ~QgsLabelingEngineInterface();

//! called when we're going to start with rendering
virtual void init() = 0;
//! called when starting rendering of a layer
virtual int prepareLayer(QgsVectorLayer* layer, int& attrIndex) = 0;
//! called for every feature
virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
//! called when the map is drawn and labels should be placed
virtual void drawLabeling( QgsRenderContext& context ) = 0;
//! called when we're done with rendering
virtual void exit() = 0;

};


/**
* \class QgsMapRenderer
* \brief Class for rendering map layer set
Expand Down Expand Up @@ -114,6 +141,15 @@ class QgsMapRenderer : QObject
//! Accessor for render context
QgsRenderContext* rendererContext();

//! Labeling engine (NULL if there's no custom engine)
//! \note Added in QGIS v1.4
QgsLabelingEngineInterface* labelingEngine();

//! Set labeling engine. Previous engine (if any) is deleted.
//! Takes ownership of the engine.
//! Added in QGIS v1.4
void setLabelingEngine(QgsLabelingEngineInterface* iface /Transfer/);

signals:

void drawingProgress(int current, int total);
Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsrendercontext.sip
Expand Up @@ -32,6 +32,9 @@ class QgsRenderContext

double rendererScale() const;

//! Added in QGIS v1.4
QgsLabelingEngineInterface* labelingEngine();

//setters

/**Sets coordinate transformation. QgsRenderContext takes ownership and deletes if necessary*/
Expand All @@ -44,4 +47,6 @@ class QgsRenderContext
void setRasterScaleFactor(double factor);
void setRendererScale( double scale );
void setPainter(QPainter* p);
//! Added in QGIS v1.4
void setLabelingEngine(QgsLabelingEngineInterface* iface);
};
21 changes: 21 additions & 0 deletions src/core/qgsmaprenderer.cpp
Expand Up @@ -58,13 +58,16 @@ QgsMapRenderer::QgsMapRenderer()
mDestCRS = new QgsCoordinateReferenceSystem( GEO_EPSG_CRS_ID, QgsCoordinateReferenceSystem::EpsgCrsId ); //WGS 84

mOutputUnits = QgsMapRenderer::Millimeters;

mLabelingEngine = NULL;
}

QgsMapRenderer::~QgsMapRenderer()
{
delete mScaleCalculator;
delete mDistArea;
delete mDestCRS;
delete mLabelingEngine;
}


Expand Down Expand Up @@ -282,6 +285,10 @@ void QgsMapRenderer::render( QPainter* painter )
mySameAsLastFlag = false;
}

mRenderContext.setLabelingEngine(mLabelingEngine);
if ( mLabelingEngine )
mLabelingEngine->init();

// know we know if this render is just a repeat of the last time, we
// can clear caches if it has changed
if ( !mySameAsLastFlag )
Expand Down Expand Up @@ -572,6 +579,12 @@ void QgsMapRenderer::render( QPainter* painter )
// make sure progress bar arrives at 100%!
emit drawingProgress( 1, 1 );

if ( mLabelingEngine )
{
mLabelingEngine->drawLabeling( mRenderContext );
mLabelingEngine->exit();
}

QgsDebugMsg( "Rendering completed in (seconds): " + QString( "%1" ).arg( renderTime.elapsed() / 1000.0 ) );

mDrawing = false;
Expand Down Expand Up @@ -1041,3 +1054,11 @@ bool QgsMapRenderer::writeXML( QDomNode & theNode, QDomDocument & theDoc )

return true;
}

void QgsMapRenderer::setLabelingEngine(QgsLabelingEngineInterface* iface)
{
if (mLabelingEngine)
delete mLabelingEngine;

mLabelingEngine = iface;
}
37 changes: 37 additions & 0 deletions src/core/qgsmaprenderer.h
Expand Up @@ -34,6 +34,31 @@ class QgsScaleCalculator;
class QgsCoordinateReferenceSystem;
class QgsDistanceArea;
class QgsOverlayObjectPositionManager;
class QgsVectorLayer;
class QgsFeature;

/** Labeling engine interface.
* \note Added in QGIS v1.4
*/
class QgsLabelingEngineInterface
{
public:
virtual ~QgsLabelingEngineInterface() {}

//! called when we're going to start with rendering
virtual void init() = 0;
//! called when starting rendering of a layer
virtual int prepareLayer(QgsVectorLayer* layer, int& attrIndex) = 0;
//! called for every feature
virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
//! called when the map is drawn and labels should be placed
virtual void drawLabeling( QgsRenderContext& context ) = 0;
//! called when we're done with rendering
virtual void exit() = 0;

};



/** \ingroup core
* A non GUI class for rendering a map layer set onto a QPainter.
Expand Down Expand Up @@ -146,6 +171,15 @@ class CORE_EXPORT QgsMapRenderer : public QObject
//! Accessor for render context
QgsRenderContext* rendererContext() {return &mRenderContext;}

//! Labeling engine (NULL if there's no custom engine)
//! \note Added in QGIS v1.4
QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }

//! Set labeling engine. Previous engine (if any) is deleted.
//! Takes ownership of the engine.
//! Added in QGIS v1.4
void setLabelingEngine(QgsLabelingEngineInterface* iface);

signals:

void drawingProgress( int current, int total );
Expand Down Expand Up @@ -232,6 +266,9 @@ class CORE_EXPORT QgsMapRenderer : public QObject

//!Output units
OutputUnits mOutputUnits;

//! Labeling engine (NULL by default)
QgsLabelingEngineInterface* mLabelingEngine;
};

#endif
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsrendercontext.cpp
Expand Up @@ -18,7 +18,15 @@

#include "qgsrendercontext.h"

QgsRenderContext::QgsRenderContext(): mPainter( 0 ), mCoordTransform( 0 ), mDrawEditingInformation( false ), mForceVectorOutput( true ), mRenderingStopped( false ), mScaleFactor( 1.0 ), mRasterScaleFactor( 1.0 )
QgsRenderContext::QgsRenderContext()
: mPainter( 0 ),
mCoordTransform( 0 ),
mDrawEditingInformation( false ),
mForceVectorOutput( true ),
mRenderingStopped( false ),
mScaleFactor( 1.0 ),
mRasterScaleFactor( 1.0 ),
mLabelingEngine( NULL )
{

}
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsrendercontext.h
Expand Up @@ -24,6 +24,8 @@

class QPainter;

class QgsLabelingEngineInterface;

/** \ingroup core
* Contains information about the context of a rendering operation.
* The context of a rendering operation defines properties such as
Expand Down Expand Up @@ -58,6 +60,9 @@ class CORE_EXPORT QgsRenderContext

double rendererScale() const {return mRendererScale;}

//! Added in QGIS v1.4
QgsLabelingEngineInterface* labelingEngine() const { return mLabelingEngine; }

//setters

/**Sets coordinate transformation. QgsRenderContext takes ownership and deletes if necessary*/
Expand All @@ -70,6 +75,8 @@ class CORE_EXPORT QgsRenderContext
void setRasterScaleFactor( double factor ) {mRasterScaleFactor = factor;}
void setRendererScale( double scale ) {mRendererScale = scale;}
void setPainter( QPainter* p ) {mPainter = p;}
//! Added in QGIS v1.4
void setLabelingEngine(QgsLabelingEngineInterface* iface) { mLabelingEngine = iface; }

private:

Expand Down Expand Up @@ -100,6 +107,9 @@ class CORE_EXPORT QgsRenderContext

/**Map scale*/
double mRendererScale;

/**Labeling engine (can be NULL)*/
QgsLabelingEngineInterface* mLabelingEngine;
};

#endif
25 changes: 9 additions & 16 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -109,8 +109,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
mVertexMarkerOnlyForSelection( false ),
mFetching( false ),
mRendererV2( NULL ),
mUsingRendererV2( false ),
mLabelingEngine( NULL )
mUsingRendererV2( false )
{
mActions = new QgsAttributeAction;

Expand Down Expand Up @@ -686,7 +685,7 @@ void QgsVectorLayer::drawRendererV2( QgsRenderContext& rendererContext, bool lab
mRendererV2->renderFeature( fet, rendererContext );

if ( labeling )
mLabelingEngine->registerFeature( this, fet );
rendererContext.labelingEngine()->registerFeature( this, fet );
}

mRendererV2->stopRender( rendererContext );
Expand Down Expand Up @@ -725,7 +724,7 @@ void QgsVectorLayer::drawRendererV2Levels( QgsRenderContext& rendererContext, bo
features[sym].append( fet );

if ( labeling )
mLabelingEngine->registerFeature( this, fet );
rendererContext.labelingEngine()->registerFeature( this, fet );
}

// find out the order
Expand Down Expand Up @@ -799,10 +798,10 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
}

bool labeling = FALSE;
if ( mLabelingEngine )
if ( rendererContext.labelingEngine() )
{
int attrIndex;
if ( mLabelingEngine->prepareLayer( this, attrIndex ) )
if ( rendererContext.labelingEngine()->prepareLayer( this, attrIndex ) )
{
if ( !attributes.contains( attrIndex ) )
attributes << attrIndex;
Expand Down Expand Up @@ -859,10 +858,10 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
QgsAttributeList attributes = mRenderer->classificationAttributes();

bool labeling = FALSE;
if ( mLabelingEngine )
if ( rendererContext.labelingEngine() )
{
int attrIndex;
if ( mLabelingEngine->prepareLayer( this, attrIndex ) )
if ( rendererContext.labelingEngine()->prepareLayer( this, attrIndex ) )
{
if ( !attributes.contains( attrIndex ) )
attributes << attrIndex;
Expand Down Expand Up @@ -934,9 +933,9 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
//double scale = rendererContext.scaleFactor() / markerScaleFactor;
drawFeature( rendererContext, fet, &marker );

if ( labeling && mLabelingEngine )
if ( labeling )
{
mLabelingEngine->registerFeature( this, fet );
rendererContext.labelingEngine()->registerFeature( this, fet );
}

++featureCount;
Expand Down Expand Up @@ -2256,12 +2255,6 @@ bool QgsVectorLayer::hasLabelsEnabled( void ) const
return mLabelOn;
}

void QgsVectorLayer::setLabelingEngine( QgsLabelingEngineInterface* engine )
{
mLabelingEngine = engine;
}


bool QgsVectorLayer::startEditing()
{
if ( !mDataProvider )
Expand Down
17 changes: 0 additions & 17 deletions src/core/qgsvectorlayer.h
Expand Up @@ -53,17 +53,6 @@ typedef QList<int> QgsAttributeList;
typedef QSet<int> QgsFeatureIds;
typedef QSet<int> QgsAttributeIds;

class QgsLabelingEngineInterface
{
public:
virtual ~QgsLabelingEngineInterface() {}
virtual int prepareLayer( QgsVectorLayer* layer, int& attrIndex ) = 0;
virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
//void calculateLabeling() = 0;
//void drawLabeling(QgsRenderContext& context) = 0;
};



/** \ingroup core
* Vector layer backed by a data source provider.
Expand Down Expand Up @@ -362,9 +351,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Label is on */
bool hasLabelsEnabled( void ) const;

/** Assign a custom labeling engine with layer. Added in v1.4 */
void setLabelingEngine( QgsLabelingEngineInterface* engine );

/** Returns true if the provider is in editing mode */
virtual bool isEditable() const;

Expand Down Expand Up @@ -747,9 +733,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Label */
QgsLabel *mLabel;

QgsLabelingEngineInterface* mLabelingEngine;


/** Display labels */
bool mLabelOn;

Expand Down

0 comments on commit c1676d5

Please sign in to comment.