Skip to content

Commit d0fcc95

Browse files
committed
More labeling engine refactoring
- QgsPalLabeling now internally uses new engine - label/diagram providers can hook into rendering loop to avoid extra feature loops - map rendering uses the new engine instead of QgsPalLabeling This code has been funded by Tuscany Region (Italy) - SITA (CIG: 63526840AE) and commissioned to Gis3W s.a.s.
1 parent 8100495 commit d0fcc95

16 files changed

+503
-1257
lines changed

python/core/qgsmaprenderer.sip

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ class QgsLabelingEngineInterface
4747
//! called when starting rendering of a layer
4848
virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
4949
//! returns PAL layer settings for a registered layer
50-
virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
50+
//! @deprecated since 2.12 - if direct access to QgsPalLayerSettings is necessary, use QgsPalLayerSettings::fromLayer()
51+
virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0 /Deprecated/;
5152
//! adds a diagram layer to the labeling engine
52-
virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s );
53+
//! @note added in QGIS 2.12
54+
virtual int prepareDiagramLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx );
55+
//! adds a diagram layer to the labeling engine
56+
//! @deprecated since 2.12 - use prepareDiagramLayer()
57+
virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s ) /Deprecated/;
5358
//! called for every feature
5459
virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext(), QString dxfLayer = QString::null ) = 0;
5560
//! called for every diagram feature

python/core/qgspallabeling.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ class QgsPalLabeling : QgsLabelingEngineInterface
699699

700700
bool isShowingCandidates() const;
701701
void setShowingCandidates( bool showing );
702-
const QList<QgsLabelCandidate>& candidates();
702+
//! @deprecated since 2.12
703+
const QList<QgsLabelCandidate>& candidates() /Deprecated/;
703704

704705
bool isShowingShadowRectangles() const;
705706
void setShowingShadowRectangles( bool showing );

src/core/qgslabelingenginev2.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgslogger.h"
1919
#include "qgspalgeometry.h"
20+
#include "qgsproject.h"
2021

2122
#include "feature.h"
2223
#include "labelposition.h"
@@ -33,9 +34,8 @@ static bool _palIsCancelled( void* ctx )
3334
}
3435

3536

36-
QgsLabelingEngineV2::QgsLabelingEngineV2( const QgsMapSettings& mapSettings )
37-
: mMapSettings( mapSettings )
38-
, mFlags( RenderOutlineLabels | UsePartialCandidates )
37+
QgsLabelingEngineV2::QgsLabelingEngineV2()
38+
: mFlags( RenderOutlineLabels | UsePartialCandidates )
3939
, mSearchMethod( QgsPalLabeling::Chain )
4040
, mCandPoint( 8 )
4141
, mCandLine( 8 )
@@ -57,6 +57,15 @@ void QgsLabelingEngineV2::addProvider( QgsAbstractLabelProvider* provider )
5757
mProviders << provider;
5858
}
5959

60+
void QgsLabelingEngineV2::removeProvider( QgsAbstractLabelProvider* provider )
61+
{
62+
int idx = mProviders.indexOf( provider );
63+
if ( idx >= 0 )
64+
{
65+
delete mProviders.takeAt( idx );
66+
}
67+
}
68+
6069
void QgsLabelingEngineV2::run( QgsRenderContext& context )
6170
{
6271
pal::Pal p;
@@ -146,7 +155,7 @@ void QgsLabelingEngineV2::run( QgsRenderContext& context )
146155
l->setUpsidedownLabels( upsdnlabels );
147156

148157

149-
QList<QgsLabelFeature*> features = provider->labelFeatures( mMapSettings, context );
158+
QList<QgsLabelFeature*> features = provider->labelFeatures( context );
150159

151160
foreach ( QgsLabelFeature* feature, features )
152161
{
@@ -286,6 +295,40 @@ QgsLabelingResults* QgsLabelingEngineV2::takeResults()
286295
return res;
287296
}
288297

298+
299+
void QgsLabelingEngineV2::readSettingsFromProject()
300+
{
301+
bool saved = false;
302+
QgsProject* prj = QgsProject::instance();
303+
mSearchMethod = ( QgsPalLabeling::Search )( prj->readNumEntry( "PAL", "/SearchMethod", ( int ) QgsPalLabeling::Chain, &saved ) );
304+
mCandPoint = prj->readNumEntry( "PAL", "/CandidatesPoint", 8, &saved );
305+
mCandLine = prj->readNumEntry( "PAL", "/CandidatesLine", 8, &saved );
306+
mCandPolygon = prj->readNumEntry( "PAL", "/CandidatesPolygon", 8, &saved );
307+
308+
mFlags = 0;
309+
if ( prj->readBoolEntry( "PAL", "/ShowingCandidates", false, &saved ) ) mFlags |= DrawCandidates;
310+
if ( prj->readBoolEntry( "PAL", "/DrawRectOnly", false, &saved ) ) mFlags |= DrawLabelRectOnly;
311+
if ( prj->readBoolEntry( "PAL", "/ShowingShadowRects", false, &saved ) ) mFlags |= DrawShadowRects;
312+
if ( prj->readBoolEntry( "PAL", "/ShowingAllLabels", false, &saved ) ) mFlags |= UseAllLabels;
313+
if ( prj->readBoolEntry( "PAL", "/ShowingPartialsLabels", true, &saved ) ) mFlags |= UsePartialCandidates;
314+
if ( prj->readBoolEntry( "PAL", "/DrawOutlineLabels", true, &saved ) ) mFlags |= RenderOutlineLabels;
315+
}
316+
317+
void QgsLabelingEngineV2::writeSettingsToProject()
318+
{
319+
QgsProject::instance()->writeEntry( "PAL", "/SearchMethod", ( int )mSearchMethod );
320+
QgsProject::instance()->writeEntry( "PAL", "/CandidatesPoint", mCandPoint );
321+
QgsProject::instance()->writeEntry( "PAL", "/CandidatesLine", mCandLine );
322+
QgsProject::instance()->writeEntry( "PAL", "/CandidatesPolygon", mCandPolygon );
323+
324+
QgsProject::instance()->writeEntry( "PAL", "/ShowingCandidates", mFlags.testFlag( DrawCandidates ) );
325+
QgsProject::instance()->writeEntry( "PAL", "/DrawRectOnly", mFlags.testFlag( DrawLabelRectOnly ) );
326+
QgsProject::instance()->writeEntry( "PAL", "/ShowingShadowRects", mFlags.testFlag( DrawShadowRects ) );
327+
QgsProject::instance()->writeEntry( "PAL", "/ShowingAllLabels", mFlags.testFlag( UseAllLabels ) );
328+
QgsProject::instance()->writeEntry( "PAL", "/ShowingPartialsLabels", mFlags.testFlag( UsePartialCandidates ) );
329+
QgsProject::instance()->writeEntry( "PAL", "/DrawOutlineLabels", mFlags.testFlag( RenderOutlineLabels ) );
330+
}
331+
289332
QgsAbstractLabelProvider* QgsLabelingEngineV2::providerById( const QString& id )
290333
{
291334
Q_FOREACH ( QgsAbstractLabelProvider* provider, mProviders )

src/core/qgslabelingenginev2.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class CORE_EXPORT QgsAbstractLabelProvider
164164
virtual QString id() const = 0;
165165

166166
//! Return list of labels
167-
virtual QList<QgsLabelFeature*> labelFeatures( const QgsMapSettings& mapSettings, const QgsRenderContext& context ) = 0;
167+
virtual QList<QgsLabelFeature*> labelFeatures( const QgsRenderContext& context ) = 0;
168168

169169
//! draw this label at the position determined by the labeling engine
170170
virtual void drawLabel( QgsRenderContext& context, pal::LabelPosition* label ) const = 0;
@@ -210,7 +210,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
210210
class CORE_EXPORT QgsLabelingEngineV2
211211
{
212212
public:
213-
QgsLabelingEngineV2( const QgsMapSettings& mapSettings );
213+
QgsLabelingEngineV2();
214214
~QgsLabelingEngineV2();
215215

216216
enum Flag
@@ -224,9 +224,18 @@ class CORE_EXPORT QgsLabelingEngineV2
224224
};
225225
Q_DECLARE_FLAGS( Flags, Flag )
226226

227+
void setMapSettings( const QgsMapSettings& mapSettings ) { mMapSettings = mapSettings; }
228+
const QgsMapSettings& mapSettings() const { return mMapSettings; }
229+
227230
//! Add provider of label features. Takes ownership of the provider
228231
void addProvider( QgsAbstractLabelProvider* provider );
229232

233+
//! Remove provider if the provider's initialization failed. Provider instance is deleted.
234+
void removeProvider( QgsAbstractLabelProvider* provider );
235+
236+
//! Lookup provider by its ID
237+
QgsAbstractLabelProvider* providerById( const QString& id );
238+
230239
//! compute the labeling with given map settings and providers
231240
void run( QgsRenderContext& context );
232241

@@ -238,15 +247,17 @@ class CORE_EXPORT QgsLabelingEngineV2
238247

239248
void setFlags( Flags flags ) { mFlags = flags; }
240249
Flags flags() const { return mFlags; }
250+
bool testFlag( Flag f ) const { return mFlags.testFlag( f ); }
251+
void setFlag( Flag f, bool enabled ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
241252

242253
void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon ) { candPoint = mCandPoint; candLine = mCandLine; candPolygon = mCandPolygon; }
243254
void setNumCandidatePositions( int candPoint, int candLine, int candPolygon ) { mCandPoint = candPoint; mCandLine = candLine; mCandPolygon = candPolygon; }
244255

245256
void setSearchMethod( QgsPalLabeling::Search s ) { mSearchMethod = s; }
246257
QgsPalLabeling::Search searchMethod() const { return mSearchMethod; }
247258

248-
protected:
249-
QgsAbstractLabelProvider* providerById( const QString& id );
259+
void readSettingsFromProject();
260+
void writeSettingsToProject();
250261

251262
protected:
252263
QgsMapSettings mMapSettings;

src/core/qgsmaprenderer.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ class CORE_EXPORT QgsLabelingEngineInterface
8484
//! called when starting rendering of a layer
8585
virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
8686
//! returns PAL layer settings for a registered layer
87-
virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
87+
//! @deprecated since 2.12 - if direct access to QgsPalLayerSettings is necessary, use QgsPalLayerSettings::fromLayer()
88+
Q_DECL_DEPRECATED virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
8889
//! adds a diagram layer to the labeling engine
89-
virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s )
90+
//! @note added in QGIS 2.12
91+
virtual int prepareDiagramLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx )
92+
{ Q_UNUSED( layer ); Q_UNUSED( attrNames ); Q_UNUSED( ctx ); return 0; }
93+
//! adds a diagram layer to the labeling engine
94+
//! @deprecated since 2.12 - use prepareDiagramLayer()
95+
Q_DECL_DEPRECATED virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s )
9096
{ Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
9197
//! called for every feature
9298
virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext(), QString dxfLayer = QString::null ) = 0;

src/core/qgsmaprenderercustompainterjob.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ void QgsMapRendererCustomPainterJob::start()
8585
if ( mSettings.testFlag( QgsMapSettings::DrawLabeling ) )
8686
{
8787
#ifdef LABELING_V2
88-
mLabelingEngineV2 = new QgsLabelingEngineV2( mSettings );
88+
mLabelingEngineV2 = new QgsLabelingEngineV2();
89+
mLabelingEngineV2->readSettingsFromProject();
90+
mLabelingEngineV2->setMapSettings( mSettings );
8991
#else
9092
mLabelingEngine = new QgsPalLabeling;
9193
mLabelingEngine->loadEngineSettings();

src/core/qgsmaprendererparalleljob.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ void QgsMapRendererParallelJob::start()
6464
if ( mSettings.testFlag( QgsMapSettings::DrawLabeling ) )
6565
{
6666
#ifdef LABELING_V2
67-
mLabelingEngineV2 = new QgsLabelingEngineV2( mSettings );
67+
mLabelingEngineV2 = new QgsLabelingEngineV2();
68+
mLabelingEngineV2->readSettingsFromProject();
69+
mLabelingEngineV2->setMapSettings( mSettings );
6870
#else
6971
mLabelingEngine = new QgsPalLabeling;
7072
mLabelingEngine->loadEngineSettings();

0 commit comments

Comments
 (0)