Skip to content

Commit 675cf05

Browse files
author
wonder
committed
Improved labeling engine interface, now connected with QgsMapRenderer instead of individual layers.
Also fixes #2108. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12174 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9465459 commit 675cf05

12 files changed

+148
-93
lines changed

python/core/qgsmaprenderer.sip

+36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11

2+
3+
/** Labeling engine interface.
4+
* \note Added in QGIS v1.4
5+
*/
6+
class QgsLabelingEngineInterface
7+
{
8+
%TypeHeaderCode
9+
#include <qgsmaprenderer.h>
10+
%End
11+
12+
public:
13+
virtual ~QgsLabelingEngineInterface();
14+
15+
//! called when we're going to start with rendering
16+
virtual void init() = 0;
17+
//! called when starting rendering of a layer
18+
virtual int prepareLayer(QgsVectorLayer* layer, int& attrIndex) = 0;
19+
//! called for every feature
20+
virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
21+
//! called when the map is drawn and labels should be placed
22+
virtual void drawLabeling( QgsRenderContext& context ) = 0;
23+
//! called when we're done with rendering
24+
virtual void exit() = 0;
25+
26+
};
27+
28+
229
/**
330
* \class QgsMapRenderer
431
* \brief Class for rendering map layer set
@@ -114,6 +141,15 @@ class QgsMapRenderer : QObject
114141
//! Accessor for render context
115142
QgsRenderContext* rendererContext();
116143

144+
//! Labeling engine (NULL if there's no custom engine)
145+
//! \note Added in QGIS v1.4
146+
QgsLabelingEngineInterface* labelingEngine();
147+
148+
//! Set labeling engine. Previous engine (if any) is deleted.
149+
//! Takes ownership of the engine.
150+
//! Added in QGIS v1.4
151+
void setLabelingEngine(QgsLabelingEngineInterface* iface /Transfer/);
152+
117153
signals:
118154

119155
void drawingProgress(int current, int total);

python/core/qgsrendercontext.sip

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class QgsRenderContext
3232

3333
double rendererScale() const;
3434

35+
//! Added in QGIS v1.4
36+
QgsLabelingEngineInterface* labelingEngine();
37+
3538
//setters
3639

3740
/**Sets coordinate transformation. QgsRenderContext takes ownership and deletes if necessary*/
@@ -44,4 +47,6 @@ class QgsRenderContext
4447
void setRasterScaleFactor(double factor);
4548
void setRendererScale( double scale );
4649
void setPainter(QPainter* p);
50+
//! Added in QGIS v1.4
51+
void setLabelingEngine(QgsLabelingEngineInterface* iface);
4752
};

src/core/qgsmaprenderer.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ QgsMapRenderer::QgsMapRenderer()
5858
mDestCRS = new QgsCoordinateReferenceSystem( GEO_EPSG_CRS_ID, QgsCoordinateReferenceSystem::EpsgCrsId ); //WGS 84
5959

6060
mOutputUnits = QgsMapRenderer::Millimeters;
61+
62+
mLabelingEngine = NULL;
6163
}
6264

6365
QgsMapRenderer::~QgsMapRenderer()
6466
{
6567
delete mScaleCalculator;
6668
delete mDistArea;
6769
delete mDestCRS;
70+
delete mLabelingEngine;
6871
}
6972

7073

@@ -282,6 +285,10 @@ void QgsMapRenderer::render( QPainter* painter )
282285
mySameAsLastFlag = false;
283286
}
284287

288+
mRenderContext.setLabelingEngine(mLabelingEngine);
289+
if ( mLabelingEngine )
290+
mLabelingEngine->init();
291+
285292
// know we know if this render is just a repeat of the last time, we
286293
// can clear caches if it has changed
287294
if ( !mySameAsLastFlag )
@@ -572,6 +579,12 @@ void QgsMapRenderer::render( QPainter* painter )
572579
// make sure progress bar arrives at 100%!
573580
emit drawingProgress( 1, 1 );
574581

582+
if ( mLabelingEngine )
583+
{
584+
mLabelingEngine->drawLabeling( mRenderContext );
585+
mLabelingEngine->exit();
586+
}
587+
575588
QgsDebugMsg( "Rendering completed in (seconds): " + QString( "%1" ).arg( renderTime.elapsed() / 1000.0 ) );
576589

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

10421055
return true;
10431056
}
1057+
1058+
void QgsMapRenderer::setLabelingEngine(QgsLabelingEngineInterface* iface)
1059+
{
1060+
if (mLabelingEngine)
1061+
delete mLabelingEngine;
1062+
1063+
mLabelingEngine = iface;
1064+
}

src/core/qgsmaprenderer.h

+37
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ class QgsScaleCalculator;
3434
class QgsCoordinateReferenceSystem;
3535
class QgsDistanceArea;
3636
class QgsOverlayObjectPositionManager;
37+
class QgsVectorLayer;
38+
class QgsFeature;
39+
40+
/** Labeling engine interface.
41+
* \note Added in QGIS v1.4
42+
*/
43+
class QgsLabelingEngineInterface
44+
{
45+
public:
46+
virtual ~QgsLabelingEngineInterface() {}
47+
48+
//! called when we're going to start with rendering
49+
virtual void init() = 0;
50+
//! called when starting rendering of a layer
51+
virtual int prepareLayer(QgsVectorLayer* layer, int& attrIndex) = 0;
52+
//! called for every feature
53+
virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
54+
//! called when the map is drawn and labels should be placed
55+
virtual void drawLabeling( QgsRenderContext& context ) = 0;
56+
//! called when we're done with rendering
57+
virtual void exit() = 0;
58+
59+
};
60+
61+
3762

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

174+
//! Labeling engine (NULL if there's no custom engine)
175+
//! \note Added in QGIS v1.4
176+
QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
177+
178+
//! Set labeling engine. Previous engine (if any) is deleted.
179+
//! Takes ownership of the engine.
180+
//! Added in QGIS v1.4
181+
void setLabelingEngine(QgsLabelingEngineInterface* iface);
182+
149183
signals:
150184

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

233267
//!Output units
234268
OutputUnits mOutputUnits;
269+
270+
//! Labeling engine (NULL by default)
271+
QgsLabelingEngineInterface* mLabelingEngine;
235272
};
236273

237274
#endif

src/core/qgsrendercontext.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818

1919
#include "qgsrendercontext.h"
2020

21-
QgsRenderContext::QgsRenderContext(): mPainter( 0 ), mCoordTransform( 0 ), mDrawEditingInformation( false ), mForceVectorOutput( true ), mRenderingStopped( false ), mScaleFactor( 1.0 ), mRasterScaleFactor( 1.0 )
21+
QgsRenderContext::QgsRenderContext()
22+
: mPainter( 0 ),
23+
mCoordTransform( 0 ),
24+
mDrawEditingInformation( false ),
25+
mForceVectorOutput( true ),
26+
mRenderingStopped( false ),
27+
mScaleFactor( 1.0 ),
28+
mRasterScaleFactor( 1.0 ),
29+
mLabelingEngine( NULL )
2230
{
2331

2432
}

src/core/qgsrendercontext.h

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
class QPainter;
2626

27+
class QgsLabelingEngineInterface;
28+
2729
/** \ingroup core
2830
* Contains information about the context of a rendering operation.
2931
* The context of a rendering operation defines properties such as
@@ -58,6 +60,9 @@ class CORE_EXPORT QgsRenderContext
5860

5961
double rendererScale() const {return mRendererScale;}
6062

63+
//! Added in QGIS v1.4
64+
QgsLabelingEngineInterface* labelingEngine() const { return mLabelingEngine; }
65+
6166
//setters
6267

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

7481
private:
7582

@@ -100,6 +107,9 @@ class CORE_EXPORT QgsRenderContext
100107

101108
/**Map scale*/
102109
double mRendererScale;
110+
111+
/**Labeling engine (can be NULL)*/
112+
QgsLabelingEngineInterface* mLabelingEngine;
103113
};
104114

105115
#endif

src/core/qgsvectorlayer.cpp

+9-16
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
109109
mVertexMarkerOnlyForSelection( false ),
110110
mFetching( false ),
111111
mRendererV2( NULL ),
112-
mUsingRendererV2( false ),
113-
mLabelingEngine( NULL )
112+
mUsingRendererV2( false )
114113
{
115114
mActions = new QgsAttributeAction;
116115

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

688687
if ( labeling )
689-
mLabelingEngine->registerFeature( this, fet );
688+
rendererContext.labelingEngine()->registerFeature( this, fet );
690689
}
691690

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

727726
if ( labeling )
728-
mLabelingEngine->registerFeature( this, fet );
727+
rendererContext.labelingEngine()->registerFeature( this, fet );
729728
}
730729

731730
// find out the order
@@ -799,10 +798,10 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
799798
}
800799

801800
bool labeling = FALSE;
802-
if ( mLabelingEngine )
801+
if ( rendererContext.labelingEngine() )
803802
{
804803
int attrIndex;
805-
if ( mLabelingEngine->prepareLayer( this, attrIndex ) )
804+
if ( rendererContext.labelingEngine()->prepareLayer( this, attrIndex ) )
806805
{
807806
if ( !attributes.contains( attrIndex ) )
808807
attributes << attrIndex;
@@ -859,10 +858,10 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
859858
QgsAttributeList attributes = mRenderer->classificationAttributes();
860859

861860
bool labeling = FALSE;
862-
if ( mLabelingEngine )
861+
if ( rendererContext.labelingEngine() )
863862
{
864863
int attrIndex;
865-
if ( mLabelingEngine->prepareLayer( this, attrIndex ) )
864+
if ( rendererContext.labelingEngine()->prepareLayer( this, attrIndex ) )
866865
{
867866
if ( !attributes.contains( attrIndex ) )
868867
attributes << attrIndex;
@@ -934,9 +933,9 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
934933
//double scale = rendererContext.scaleFactor() / markerScaleFactor;
935934
drawFeature( rendererContext, fet, &marker );
936935

937-
if ( labeling && mLabelingEngine )
936+
if ( labeling )
938937
{
939-
mLabelingEngine->registerFeature( this, fet );
938+
rendererContext.labelingEngine()->registerFeature( this, fet );
940939
}
941940

942941
++featureCount;
@@ -2256,12 +2255,6 @@ bool QgsVectorLayer::hasLabelsEnabled( void ) const
22562255
return mLabelOn;
22572256
}
22582257

2259-
void QgsVectorLayer::setLabelingEngine( QgsLabelingEngineInterface* engine )
2260-
{
2261-
mLabelingEngine = engine;
2262-
}
2263-
2264-
22652258
bool QgsVectorLayer::startEditing()
22662259
{
22672260
if ( !mDataProvider )

src/core/qgsvectorlayer.h

-17
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ typedef QList<int> QgsAttributeList;
5353
typedef QSet<int> QgsFeatureIds;
5454
typedef QSet<int> QgsAttributeIds;
5555

56-
class QgsLabelingEngineInterface
57-
{
58-
public:
59-
virtual ~QgsLabelingEngineInterface() {}
60-
virtual int prepareLayer( QgsVectorLayer* layer, int& attrIndex ) = 0;
61-
virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
62-
//void calculateLabeling() = 0;
63-
//void drawLabeling(QgsRenderContext& context) = 0;
64-
};
65-
66-
6756

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

365-
/** Assign a custom labeling engine with layer. Added in v1.4 */
366-
void setLabelingEngine( QgsLabelingEngineInterface* engine );
367-
368354
/** Returns true if the provider is in editing mode */
369355
virtual bool isEditable() const;
370356

@@ -747,9 +733,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
747733
/** Label */
748734
QgsLabel *mLabel;
749735

750-
QgsLabelingEngineInterface* mLabelingEngine;
751-
752-
753736
/** Display labels */
754737
bool mLabelOn;
755738

0 commit comments

Comments
 (0)