Skip to content

Commit d49f7ac

Browse files
committedNov 14, 2013
Refactoring of QgsPalLabeling
The idea was not to expose the labeling engine instance used for rendering to other parts of the application and keep it as an implementation detail within renderer jobs. Resulting placement is kept in a newly created class QgsLabelingResults which is passed from the renderer job's labeling engine to map canvas where it is available for map tools. Global labeling settings are now kept only in QgsProject (there is no instance of QgsPalLabeling which would stay alive all the time).
1 parent cfc2a48 commit d49f7ac

26 files changed

+275
-170
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,9 +3643,6 @@ bool QgisApp::addProject( QString projectFile )
36433643
}
36443644
}
36453645

3646-
// load PAL engine settings
3647-
mMapCanvas->labelingEngine()->loadEngineSettings();
3648-
36493646
emit projectRead(); // let plug-ins know that we've read in a new
36503647
// project so that they can check any project
36513648
// specific plug-in state
@@ -4336,7 +4333,7 @@ void QgisApp::labeling()
43364333

43374334
QDialog *dlg = new QDialog( this );
43384335
dlg->setWindowTitle( tr( "Layer labeling settings" ) );
4339-
QgsLabelingGui *labelingGui = new QgsLabelingGui( mMapCanvas->labelingEngine(), vlayer, mMapCanvas, dlg );
4336+
QgsLabelingGui *labelingGui = new QgsLabelingGui( vlayer, mMapCanvas, dlg );
43404337
labelingGui->init(); // load QgsPalLayerSettings for layer
43414338
labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
43424339
QVBoxLayout *layout = new QVBoxLayout( dlg );

‎src/app/qgisapp.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class QgsMapCanvas;
5252
class QgsMapLayer;
5353
class QgsMapTip;
5454
class QgsMapTool;
55-
class QgsPalLabeling;
5655
class QgsPoint;
5756
class QgsProviderRegistry;
5857
class QgsPythonUtils;
@@ -1506,8 +1505,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
15061505

15071506
QgsMessageLogViewer *mLogViewer;
15081507

1509-
QgsPalLabeling* mLBL;
1510-
15111508
//! project changed
15121509
void projectChanged( const QDomDocument & );
15131510

‎src/app/qgsdiagramproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ void QgsDiagramProperties::on_mDiagramAttributesTreeWidget_itemDoubleClicked( QT
469469

470470
void QgsDiagramProperties::on_mEngineSettingsButton_clicked()
471471
{
472-
QgsLabelEngineConfigDialog dlg( QgisApp::instance()->mapCanvas()->labelingEngine(), this );
472+
QgsLabelEngineConfigDialog dlg( this );
473473
dlg.exec();
474474
}
475475

‎src/app/qgslabelengineconfigdialog.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,53 @@
1919

2020
#include <QPushButton>
2121

22-
QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QgsPalLabeling* lbl, QWidget* parent )
23-
: QDialog( parent ), mLBL( lbl )
22+
QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QWidget* parent )
23+
: QDialog( parent )
2424
{
2525
setupUi( this );
2626

2727
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( onOK() ) );
2828
connect( buttonBox->button( QDialogButtonBox::RestoreDefaults ), SIGNAL( clicked() ),
2929
this, SLOT( setDefaults() ) );
3030

31+
QgsPalLabeling lbl;
32+
lbl.loadEngineSettings();
33+
3134
// search method
32-
cboSearchMethod->setCurrentIndex( mLBL->searchMethod() );
35+
cboSearchMethod->setCurrentIndex( lbl.searchMethod() );
3336

3437
// candidate numbers
3538
int candPoint, candLine, candPolygon;
36-
mLBL->numCandidatePositions( candPoint, candLine, candPolygon );
39+
lbl.numCandidatePositions( candPoint, candLine, candPolygon );
3740
spinCandPoint->setValue( candPoint );
3841
spinCandLine->setValue( candLine );
3942
spinCandPolygon->setValue( candPolygon );
4043

41-
chkShowCandidates->setChecked( mLBL->isShowingCandidates() );
42-
chkShowAllLabels->setChecked( mLBL->isShowingAllLabels() );
43-
mShadowDebugRectChkBox->setChecked( mLBL->isShowingShadowRectangles() );
44+
chkShowCandidates->setChecked( lbl.isShowingCandidates() );
45+
chkShowAllLabels->setChecked( lbl.isShowingAllLabels() );
46+
mShadowDebugRectChkBox->setChecked( lbl.isShowingShadowRectangles() );
4447

45-
chkShowPartialsLabels->setChecked( mLBL-> isShowingPartialsLabels() );
48+
chkShowPartialsLabels->setChecked( lbl.isShowingPartialsLabels() );
4649
}
4750

4851

4952
void QgsLabelEngineConfigDialog::onOK()
5053
{
54+
QgsPalLabeling lbl;
55+
5156
// save
52-
mLBL->setSearchMethod(( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );
57+
lbl.setSearchMethod(( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );
5358

54-
mLBL->setNumCandidatePositions( spinCandPoint->value(),
59+
lbl.setNumCandidatePositions( spinCandPoint->value(),
5560
spinCandLine->value(),
5661
spinCandPolygon->value() );
5762

58-
mLBL->setShowingCandidates( chkShowCandidates->isChecked() );
59-
mLBL->setShowingShadowRectangles( mShadowDebugRectChkBox->isChecked() );
60-
mLBL->setShowingAllLabels( chkShowAllLabels->isChecked() );
61-
mLBL->setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );
63+
lbl.setShowingCandidates( chkShowCandidates->isChecked() );
64+
lbl.setShowingShadowRectangles( mShadowDebugRectChkBox->isChecked() );
65+
lbl.setShowingAllLabels( chkShowAllLabels->isChecked() );
66+
lbl.setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );
6267

63-
mLBL->saveEngineSettings();
68+
lbl.saveEngineSettings();
6469

6570
accept();
6671
}

‎src/app/qgslabelengineconfigdialog.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,19 @@
1919

2020
#include "ui_qgsengineconfigdialog.h"
2121

22-
class QgsPalLabeling;
2322

2423
class APP_EXPORT QgsLabelEngineConfigDialog : public QDialog, private Ui::QgsEngineConfigDialog
2524
{
2625
Q_OBJECT
2726
public:
28-
QgsLabelEngineConfigDialog( QgsPalLabeling* lbl, QWidget* parent = NULL );
27+
QgsLabelEngineConfigDialog( QWidget* parent = NULL );
2928

3029
public slots:
3130
void onOK();
3231
/** @note Added in QGIS 1.9 */
3332
void setDefaults();
3433

3534
protected:
36-
QgsPalLabeling* mLBL;
3735
};
3836

3937
#endif // QGSLABELENGINECONFIGDIALOG_H

‎src/app/qgslabelinggui.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
#include <QSettings>
4646

4747

48-
QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent )
49-
: QWidget( parent ), mLBL( lbl ), mLayer( layer ), mMapCanvas( mapCanvas )
48+
QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent )
49+
: QWidget( parent ), mLayer( layer ), mMapCanvas( mapCanvas )
5050
{
5151
if ( !layer )
5252
return;
@@ -1171,7 +1171,7 @@ void QgsLabelingGui::setPreviewBackground( QColor color )
11711171

11721172
void QgsLabelingGui::showEngineConfigDialog()
11731173
{
1174-
QgsLabelEngineConfigDialog dlg( mLBL, this );
1174+
QgsLabelEngineConfigDialog dlg( this );
11751175
dlg.exec();
11761176
}
11771177

‎src/app/qgslabelinggui.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
3333
Q_OBJECT
3434

3535
public:
36-
QgsLabelingGui( QgsPalLabeling *lbl, QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent );
36+
QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent );
3737
~QgsLabelingGui();
3838

3939
QgsPalLayerSettings layerSettings();
@@ -96,7 +96,6 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
9696
void updateFont( QFont font );
9797

9898
private:
99-
QgsPalLabeling* mLBL;
10099
QgsVectorLayer* mLayer;
101100
QgsMapCanvas* mMapCanvas;
102101
QFontDatabase mFontDB;

‎src/app/qgslabelpropertydialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include <QSettings>
2929

3030

31-
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QgsPalLabeling* labeling, QWidget * parent, Qt::WindowFlags f ):
32-
QDialog( parent, f ), mLabeling( labeling ), mLabelFont( labelFont ), mCurLabelField( -1 )
31+
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent, Qt::WindowFlags f ):
32+
QDialog( parent, f ), mLabelFont( labelFont ), mCurLabelField( -1 )
3333
{
3434
setupUi( this );
3535
fillHaliComboBox();
@@ -65,7 +65,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
6565

6666
blockElementSignals( true );
6767

68-
QgsPalLayerSettings& layerSettings = mLabeling->layer( layerId );
68+
QgsPalLayerSettings layerSettings = QgsPalLayerSettings::fromLayer( vlayer );
6969

7070
//get label field and fill line edit
7171
if ( layerSettings.isExpression && !labelText.isNull() )

‎src/app/qgslabelpropertydialog.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
3030
{
3131
Q_OBJECT
3232
public:
33-
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QgsPalLabeling* labeling, QWidget * parent = 0, Qt::WindowFlags f = 0 );
33+
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent = 0, Qt::WindowFlags f = 0 );
3434
~QgsLabelPropertyDialog();
3535

3636
/**Returns properties changed by the user*/
@@ -86,8 +86,6 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
8686
/**Insert changed value into mChangedProperties*/
8787
void insertChangedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant value );
8888

89-
QgsPalLabeling* mLabeling;
90-
9189
QgsAttributeMap mChangedProperties;
9290
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* > mDataDefinedProperties;
9391
QFont mLabelFont;

‎src/app/qgsmaptoolchangelabelproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QMouseEvent *e )
6161
labeltext = mCurrentLabelPos.labelText;
6262
}
6363

64-
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, mCanvas->labelingEngine() );
64+
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, 0 );
6565
if ( d.exec() == QDialog::Accepted )
6666
{
6767
const QgsAttributeMap& changes = d.changedProperties();

‎src/app/qgsmaptoollabel.cpp

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QMouseEvent>
2727

2828
QgsMapToolLabel::QgsMapToolLabel( QgsMapCanvas* canvas ): QgsMapTool( canvas ), mLabelRubberBand( 0 ), mFeatureRubberBand( 0 ), mFixPointRubberBand( 0 )
29+
, mCurrentLayer( 0 )
2930
{
3031
}
3132

@@ -39,10 +40,10 @@ QgsMapToolLabel::~QgsMapToolLabel()
3940
bool QgsMapToolLabel::labelAtPosition( QMouseEvent* e, QgsLabelPosition& p )
4041
{
4142
QgsPoint pt = toMapCoordinates( e->pos() );
42-
QgsLabelingEngineInterface* labelingEngine = mCanvas->labelingEngine();
43-
if ( labelingEngine )
43+
const QgsLabelingResults* labelingResults = mCanvas->labelingResults();
44+
if ( labelingResults )
4445
{
45-
QList<QgsLabelPosition> labelPosList = labelingEngine->labelsAtPosition( pt );
46+
QList<QgsLabelPosition> labelPosList = labelingResults->labelsAtPosition( pt );
4647
QList<QgsLabelPosition>::const_iterator posIt = labelPosList.constBegin();
4748
if ( posIt != labelPosList.constEnd() )
4849
{
@@ -120,7 +121,7 @@ void QgsMapToolLabel::deleteRubberBands()
120121

121122
QgsVectorLayer* QgsMapToolLabel::currentLayer()
122123
{
123-
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID ) );
124+
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID ) );
124125
return vlayer;
125126
}
126127

@@ -130,17 +131,16 @@ QgsPalLayerSettings& QgsMapToolLabel::currentLabelSettings( bool* ok )
130131
QgsVectorLayer* vlayer = currentLayer();
131132
if ( vlayer )
132133
{
133-
//QgsDebugMsg( "has vlayer" );
134-
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
135-
if ( labelEngine )
136-
{
137-
//QgsDebugMsg( "has labelEngine" );
138-
if ( ok )
139-
{
134+
if ( vlayer == mCurrentLayer )
135+
return mCurrentSettings;
136+
137+
mCurrentLayer = vlayer;
138+
mCurrentSettings = QgsPalLayerSettings::fromLayer( vlayer );
139+
140+
if ( ok )
140141
*ok = true;
141-
}
142-
return labelEngine->layer( mCurrentLabelPos.layerID );
143-
}
142+
143+
return mCurrentSettings;
144144
}
145145

146146
if ( ok )
@@ -430,16 +430,10 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool
430430
return true;
431431
}
432432

433-
int QgsMapToolLabel::dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, const QgsVectorLayer* vlayer ) const
433+
int QgsMapToolLabel::dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, QgsVectorLayer* vlayer ) const
434434
{
435-
436-
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
437-
if ( !labelEngine )
438-
{
439-
return -1;
440-
}
441435
QgsDebugMsg( QString( "dataDefinedProperties layer id:%1" ).arg( vlayer->id() ) );
442-
QgsPalLayerSettings& labelSettings = labelEngine->layer( vlayer->id() );
436+
QgsPalLayerSettings labelSettings( QgsPalLayerSettings::fromLayer( vlayer ) );
443437

444438
QgsDebugMsg( QString( "dataDefinedProperties count:%1" ).arg( labelSettings.dataDefinedProperties.size() ) );
445439

@@ -498,9 +492,9 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, int featureId
498492
return true;
499493
}
500494

501-
bool QgsMapToolLabel::layerIsRotatable( const QgsMapLayer* layer, int& rotationCol ) const
495+
bool QgsMapToolLabel::layerIsRotatable( QgsMapLayer* layer, int& rotationCol ) const
502496
{
503-
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( layer );
497+
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( layer );
504498
if ( !vlayer || !vlayer->isEditable() )
505499
{
506500
return false;
@@ -575,9 +569,9 @@ bool QgsMapToolLabel::dataDefinedShowHide( QgsVectorLayer* vlayer, int featureId
575569
return true;
576570
}
577571

578-
bool QgsMapToolLabel::diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
572+
bool QgsMapToolLabel::diagramMoveable( QgsMapLayer* ml, int& xCol, int& yCol ) const
579573
{
580-
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
574+
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
581575
if ( vlayer && vlayer->diagramRenderer() )
582576
{
583577
const QgsDiagramLayerSettings *dls = vlayer->diagramLayerSettings();
@@ -591,9 +585,9 @@ bool QgsMapToolLabel::diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yC
591585
return false;
592586
}
593587

594-
bool QgsMapToolLabel::labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
588+
bool QgsMapToolLabel::labelMoveable( QgsMapLayer *ml, int& xCol, int& yCol ) const
595589
{
596-
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
590+
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
597591
if ( !vlayer || !vlayer->isEditable() )
598592
{
599593
return false;
@@ -624,17 +618,17 @@ bool QgsMapToolLabel::labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol
624618
return false;
625619
}
626620

627-
bool QgsMapToolLabel::layerCanPin( const QgsMapLayer* ml, int& xCol, int& yCol ) const
621+
bool QgsMapToolLabel::layerCanPin( QgsMapLayer* ml, int& xCol, int& yCol ) const
628622
{
629623
// currently same as QgsMapToolLabel::labelMoveable, but may change
630624
bool canPin = labelMoveable( ml, xCol, yCol );
631625
return canPin;
632626
}
633627

634-
bool QgsMapToolLabel::layerCanShowHide( const QgsMapLayer* ml, int& showCol ) const
628+
bool QgsMapToolLabel::layerCanShowHide( QgsMapLayer* ml, int& showCol ) const
635629
{
636630
//QgsDebugMsg( "entered" );
637-
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
631+
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
638632
if ( !vlayer || !vlayer->isEditable() )
639633
{
640634
return false;

‎src/app/qgsmaptoollabel.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
3535
@param xCol out: index of the attribute for data defined x coordinate
3636
@param yCol out: index of the attribute for data defined y coordinate
3737
@return true if labels of layer can be moved*/
38-
bool labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
38+
bool labelMoveable( QgsMapLayer* ml, int& xCol, int& yCol ) const;
3939
/**Returns true if diagram move can be applied to a layer
4040
@param xCol out: index of the attribute for data defined x coordinate
4141
@param yCol out: index of the attribute for data defined y coordinate
4242
@return true if labels of layer can be moved*/
43-
bool diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
43+
bool diagramMoveable( QgsMapLayer* ml, int& xCol, int& yCol ) const;
4444
/**Returns true if layer has attribute fields set up
4545
@param xCol out: index of the attribute for data defined x coordinate
4646
@param yCol out: index of the attribute for data defined y coordinate
4747
@return true if layer fields set up and exist*/
48-
bool layerCanPin( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
48+
bool layerCanPin( QgsMapLayer* ml, int& xCol, int& yCol ) const;
4949
/**Returns true if layer has attribute field set up
5050
@param showCol out: attribute column for data defined label showing*/
51-
bool layerCanShowHide( const QgsMapLayer* layer, int& showCol ) const;
51+
bool layerCanShowHide( QgsMapLayer* layer, int& showCol ) const;
5252
/**Checks if labels in a layer can be rotated
5353
@param rotationCol out: attribute column for data defined label rotation*/
54-
bool layerIsRotatable( const QgsMapLayer* layer, int& rotationCol ) const;
54+
bool layerIsRotatable( QgsMapLayer *layer, int& rotationCol ) const;
5555

5656
protected:
5757
QgsRubberBand* mLabelRubberBand;
@@ -111,7 +111,7 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
111111
@return -1 if column does not exist or an expression is used instead */
112112
// int dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, const QgsPalLayerSettings& labelSettings, const QgsVectorLayer* vlayer ) const;
113113

114-
int dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, const QgsVectorLayer* vlayer ) const;
114+
int dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, QgsVectorLayer* vlayer ) const;
115115

116116
/**Returns whether to preserve predefined rotation data during label pin/unpin operations*/
117117
bool preserveRotation();
@@ -150,6 +150,9 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
150150

151151
private:
152152
QgsPalLayerSettings mInvalidLabelSettings;
153+
154+
QgsVectorLayer* mCurrentLayer;
155+
QgsPalLayerSettings mCurrentSettings;
153156
};
154157

155158
#endif // QGSMAPTOOLLABEL_H

‎src/app/qgsmaptoolpinlabels.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
181181
QgsDebugMsg( QString( "Highlighting pinned labels" ) );
182182

183183
// get list of all drawn labels from all layers within given extent
184-
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
185-
if ( !labelEngine )
184+
const QgsLabelingResults* labelingResults = mCanvas->labelingResults();
185+
if ( !labelingResults )
186186
{
187187
QgsDebugMsg( QString( "No labeling engine" ) );
188188
return;
@@ -191,7 +191,7 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
191191
QgsRectangle ext = mCanvas->extent();
192192
QgsDebugMsg( QString( "Getting labels from canvas extent" ) );
193193

194-
QList<QgsLabelPosition> labelPosList = labelEngine->labelsWithinRect( ext );
194+
QList<QgsLabelPosition> labelPosList = labelingResults->labelsWithinRect( ext );
195195

196196
QApplication::setOverrideCursor( Qt::WaitCursor );
197197
QList<QgsLabelPosition>::const_iterator it;
@@ -252,14 +252,14 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle& ext, QMouseEvent *
252252
// get list of all drawn labels from all layers within, or touching, chosen extent
253253
bool labelChanged = false;
254254

255-
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
256-
if ( !labelEngine )
255+
const QgsLabelingResults* labelingResults = mCanvas->labelingResults();
256+
if ( !labelingResults )
257257
{
258258
QgsDebugMsg( QString( "No labeling engine" ) );
259259
return;
260260
}
261261

262-
QList<QgsLabelPosition> labelPosList = labelEngine->labelsWithinRect( ext );
262+
QList<QgsLabelPosition> labelPosList = labelingResults->labelsWithinRect( ext );
263263

264264
QList<QgsLabelPosition>::const_iterator it;
265265
for ( it = labelPosList.constBegin() ; it != labelPosList.constEnd(); ++it )

‎src/app/qgsmaptoolshowhidelabels.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ bool QgsMapToolShowHideLabels::selectedLabelFeatures( QgsVectorLayer* vlayer,
234234
{
235235
// get list of all drawn labels from current layer that intersect rubberband
236236

237-
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
238-
if ( !labelEngine )
237+
const QgsLabelingResults* labelingResults = mCanvas->labelingResults();
238+
if ( !labelingResults )
239239
{
240240
QgsDebugMsg( "No labeling engine" );
241241
return false;
@@ -245,7 +245,7 @@ bool QgsMapToolShowHideLabels::selectedLabelFeatures( QgsVectorLayer* vlayer,
245245

246246
QgsRectangle ext = mRubberBand->asGeometry()->boundingBox();
247247

248-
QList<QgsLabelPosition> labelPosList = labelEngine->labelsWithinRect( ext );
248+
QList<QgsLabelPosition> labelPosList = labelingResults->labelsWithinRect( ext );
249249

250250
QList<QgsLabelPosition>::const_iterator it;
251251
for ( it = labelPosList.constBegin() ; it != labelPosList.constEnd(); ++it )

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
106106
// Create the Labeling dialog tab
107107
layout = new QVBoxLayout( labelingFrame );
108108
layout->setMargin( 0 );
109-
labelingDialog = new QgsLabelingGui( QgisApp::instance()->mapCanvas()->labelingEngine(), layer, QgisApp::instance()->mapCanvas(), labelingFrame );
109+
labelingDialog = new QgsLabelingGui( layer, QgisApp::instance()->mapCanvas(), labelingFrame );
110110
labelingDialog->layout()->setContentsMargins( -1, 0, -1, 0 );
111111
layout->addWidget( labelingDialog );
112112
labelingFrame->setLayout( layout );

‎src/core/composer/qgscomposermap.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const
161161
jobMapSettings.setOutputSize( size.toSize() );
162162
jobMapSettings.setOutputDpi( dpi );
163163

164-
QgsPalLabeling labeling;
165-
labeling.loadEngineSettings();
166-
167164
//use stored layer set or read current set from main canvas
168165
jobMapSettings.setLayers( mKeepLayerSet ? mLayerSet : ms.layers() );
169166
jobMapSettings.setDestinationCrs( ms.destinationCrs() );
@@ -177,7 +174,6 @@ void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const
177174

178175
// render
179176
QgsMapRendererCustomPainterJob job( jobMapSettings, painter );
180-
job.setLabelingEngine( &labeling );
181177
job.start();
182178
job.waitForFinished();
183179

@@ -639,9 +635,6 @@ bool QgsComposerMap::containsAdvancedEffects() const
639635

640636
QStringList layers = mComposition->mapSettings().layers();
641637

642-
//Also need to check PAL labeling for blend modes
643-
QgsPalLabeling* lbl = 0; // TODO: dynamic_cast<QgsPalLabeling*>( mMapRenderer->labelingEngine() );
644-
645638
QStringList::const_iterator layer_it = layers.constBegin();
646639
QgsMapLayer* currentLayer = 0;
647640

@@ -667,10 +660,10 @@ bool QgsComposerMap::containsAdvancedEffects() const
667660
return true;
668661
}
669662
// check label blend modes
670-
if ( lbl && lbl->willUseLayer( currentVectorLayer ) )
663+
if ( QgsPalLabeling::staticWillUseLayer( currentVectorLayer ) )
671664
{
672665
// Check all label blending properties
673-
QgsPalLayerSettings& layerSettings = lbl->layer( currentVectorLayer->id() );
666+
QgsPalLayerSettings layerSettings = QgsPalLayerSettings::fromLayer( currentVectorLayer );
674667
if (( layerSettings.blendMode != QPainter::CompositionMode_SourceOver ) ||
675668
( layerSettings.bufferSize != 0 && layerSettings.bufferBlendMode != QPainter::CompositionMode_SourceOver ) ||
676669
( layerSettings.shadowDraw && layerSettings.shadowBlendMode != QPainter::CompositionMode_SourceOver ) ||

‎src/core/qgslabelsearchtree.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ QgsLabelSearchTree::~QgsLabelSearchTree()
3131
clear();
3232
}
3333

34-
void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& posList )
34+
void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& posList ) const
3535
{
3636
double c_min[2]; c_min[0] = p.x() - 0.1; c_min[1] = p.y() - 0.1;
3737
double c_max[2]; c_max[0] = p.x() + 0.1; c_max[1] = p.y() + 0.1;
@@ -51,7 +51,7 @@ void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& pos
5151
}
5252
}
5353

54-
void QgsLabelSearchTree::labelsInRect( const QgsRectangle& r, QList<QgsLabelPosition*>& posList )
54+
void QgsLabelSearchTree::labelsInRect( const QgsRectangle& r, QList<QgsLabelPosition*>& posList ) const
5555
{
5656
double c_min[2]; c_min[0] = r.xMinimum(); c_min[1] = r.yMinimum();
5757
double c_max[2]; c_max[0] = r.xMaximum(); c_max[1] = r.yMaximum();

‎src/core/qgslabelsearchtree.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ class CORE_EXPORT QgsLabelSearchTree
4343
* @note not available in python bindings
4444
* TODO: why does this break bindings with QList<QgsLabelPosition>?
4545
*/
46-
void label( const QgsPoint& p, QList<QgsLabelPosition*>& posList );
46+
void label( const QgsPoint& p, QList<QgsLabelPosition*>& posList ) const;
4747

4848
/**Returns label position(s) in given rectangle. QgsLabelSearchTree keeps ownership, don't delete the LabelPositions
4949
* @note not available in python bindings
5050
* TODO: why does this break bindings with QList<QgsLabelPosition>?
5151
*/
52-
void labelsInRect( const QgsRectangle& r, QList<QgsLabelPosition*>& posList );
52+
void labelsInRect( const QgsRectangle& r, QList<QgsLabelPosition*>& posList ) const;
5353

5454
/**Inserts label position. Does not take ownership of labelPos
5555
* @return true in case of success
@@ -58,7 +58,8 @@ class CORE_EXPORT QgsLabelSearchTree
5858
bool insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram = false, bool pinned = false );
5959

6060
private:
61-
RTree<QgsLabelPosition*, double, 2, double> mSpatialIndex;
61+
// set as mutable because RTree template is not const-correct
62+
mutable RTree<QgsLabelPosition*, double, 2, double> mSpatialIndex;
6263
};
6364

6465
#endif // QGSLABELTREE_H

‎src/core/qgsmaprenderer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ class CORE_EXPORT QgsLabelingEngineInterface
105105
virtual void exit() = 0;
106106
//! return infos about labels at a given (map) position
107107
//! @note: this method was added in version 1.7
108-
virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
108+
//! @deprecated since 2.1 - use takeResults() and methods of QgsLabelingResults
109+
Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
109110
//! return infos about labels within a given (map) rectangle
110111
//! @note: this method was added in version 1.9
111-
virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
112+
//! @deprecated since 2.1 - use takeResults() and methods of QgsLabelingResults
113+
Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
112114

113115
//! called when passing engine among map renderers
114116
virtual QgsLabelingEngineInterface* clone() = 0;

‎src/core/qgsmaprendererjob.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ QgsMapRendererQImageJob::QgsMapRendererQImageJob(QgsMapRendererJob::Type type, c
2020
QgsMapRendererSequentialJob::QgsMapRendererSequentialJob(const QgsMapSettings& settings)
2121
: QgsMapRendererQImageJob(SequentialJob, settings)
2222
, mInternalJob(0)
23+
, mLabelingResults( 0 )
2324
{
2425
qDebug("SEQUENTIAL construct");
2526

@@ -37,6 +38,9 @@ QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob()
3738
qDebug("SEQUENTIAL destruct");
3839
//delete mInternalJob;
3940
Q_ASSERT(mInternalJob == 0);
41+
42+
delete mLabelingResults;
43+
mLabelingResults = 0;
4044
}
4145

4246

@@ -65,9 +69,11 @@ void QgsMapRendererSequentialJob::waitForFinished()
6569
mInternalJob->cancel();
6670
}
6771

68-
void QgsMapRendererSequentialJob::setLabelingEngine( QgsPalLabeling *labeling )
72+
QgsLabelingResults* QgsMapRendererSequentialJob::takeLabelingResults()
6973
{
70-
mInternalJob->setLabelingEngine( labeling );
74+
QgsLabelingResults* tmp = mLabelingResults;
75+
mLabelingResults = 0;
76+
return tmp;
7177
}
7278

7379

@@ -85,6 +91,8 @@ void QgsMapRendererSequentialJob::internalFinished()
8591
delete mPainter;
8692
mPainter = 0;
8793

94+
mLabelingResults = mInternalJob->takeLabelingResults();
95+
8896
delete mInternalJob;
8997
mInternalJob = 0;
9098

@@ -109,6 +117,9 @@ QgsMapRendererCustomPainterJob::~QgsMapRendererCustomPainterJob()
109117
qDebug("QPAINTER destruct");
110118
Q_ASSERT(!mFutureWatcher.isRunning());
111119
//cancel();
120+
121+
delete mLabelingEngine;
122+
mLabelingEngine = 0;
112123
}
113124

114125
void QgsMapRendererCustomPainterJob::start()
@@ -163,9 +174,9 @@ void QgsMapRendererCustomPainterJob::waitForFinished()
163174
}
164175

165176

166-
void QgsMapRendererCustomPainterJob::setLabelingEngine( QgsPalLabeling *labeling )
177+
QgsLabelingResults* QgsMapRendererCustomPainterJob::takeLabelingResults()
167178
{
168-
mLabelingEngine = labeling;
179+
return mLabelingEngine ? mLabelingEngine->takeResults() : 0;
169180
}
170181

171182

@@ -178,12 +189,12 @@ void QgsMapRendererCustomPainterJob::futureFinished()
178189

179190
void QgsMapRendererCustomPainterJob::staticRender(QgsMapRendererCustomPainterJob* self)
180191
{
181-
self->startRender();
192+
self->doRender();
182193
}
183194

184-
void QgsMapRendererCustomPainterJob::startRender()
195+
void QgsMapRendererCustomPainterJob::doRender()
185196
{
186-
qDebug("QPAINTER startRender");
197+
qDebug("QPAINTER doRender");
187198

188199
// clear the background
189200
mPainter->fillRect( 0, 0, mSettings.outputSize().width(), mSettings.outputSize().height(), mSettings.backgroundColor() );
@@ -200,12 +211,19 @@ void QgsMapRendererCustomPainterJob::startRender()
200211
renderTime.start();
201212
#endif
202213

214+
delete mLabelingEngine;
215+
mLabelingEngine = 0;
216+
217+
if ( mSettings.testFlag( QgsMapSettings::DrawLabeling ) )
218+
{
219+
mLabelingEngine = new QgsPalLabeling;
220+
mLabelingEngine->loadEngineSettings();
221+
mLabelingEngine->init( mSettings );
222+
}
223+
203224
mRenderContext = QgsRenderContext::fromMapSettings( mSettings );
204225
mRenderContext.setPainter( mPainter );
205-
206226
mRenderContext.setLabelingEngine( mLabelingEngine );
207-
if ( mLabelingEngine )
208-
mLabelingEngine->init( mSettings );
209227

210228
// render all layers in the stack, starting at the base
211229
QListIterator<QString> li( mSettings.layers() );
@@ -434,7 +452,7 @@ void QgsMapRendererCustomPainterJob::startRender()
434452
}
435453
}
436454

437-
if ( mSettings.testFlag( QgsMapSettings::DrawLabeling ) && mLabelingEngine )
455+
if ( mLabelingEngine )
438456
{
439457
// set correct extent
440458
mRenderContext.setExtent( mSettings.visibleExtent() );

‎src/core/qgsmaprendererjob.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "qgsmapsettings.h"
1010

11-
class QgsPalLabeling;
11+
class QgsLabelingResults;
1212

1313

1414
/** abstract base class renderer jobs that asynchronously start map rendering */
@@ -42,9 +42,8 @@ class QgsMapRendererJob : public QObject
4242

4343
// TODO: isActive() ?
4444

45-
//! Assign an existing labeling engine with the rendering job
46-
//! TODO: handle concurrency - one labeling instance cannot be shared by multiple active rendering jobs!
47-
virtual void setLabelingEngine( QgsPalLabeling* labeling ) = 0;
45+
//! Get pointer to internal labeling engine (in order to get access to the results)
46+
virtual QgsLabelingResults* takeLabelingResults() = 0;
4847

4948
signals:
5049

@@ -94,7 +93,7 @@ class QgsMapRendererSequentialJob : public QgsMapRendererQImageJob
9493
virtual void cancel();
9594
virtual void waitForFinished();
9695

97-
virtual void setLabelingEngine( QgsPalLabeling* labeling );
96+
virtual QgsLabelingResults* takeLabelingResults();
9897

9998
// from QgsMapRendererJobWithPreview
10099
virtual QImage renderedImage();
@@ -108,6 +107,7 @@ public slots:
108107
QgsMapRendererCustomPainterJob* mInternalJob;
109108
QImage mImage;
110109
QPainter* mPainter;
110+
QgsLabelingResults* mLabelingResults;
111111
};
112112

113113

@@ -121,6 +121,8 @@ public slots:
121121
#include <QtConcurrentRun>
122122
#include <QFutureWatcher>
123123

124+
class QgsPalLabeling;
125+
124126
/** job implementation that renders everything sequentially using a custom painter.
125127
* The returned image is always invalid (because there is none available).
126128
*/
@@ -134,16 +136,15 @@ class QgsMapRendererCustomPainterJob : public QgsMapRendererJob
134136
virtual void start();
135137
virtual void cancel();
136138
virtual void waitForFinished();
137-
138-
virtual void setLabelingEngine( QgsPalLabeling* labeling );
139+
virtual QgsLabelingResults* takeLabelingResults();
139140

140141
protected slots:
141142
void futureFinished();
142143

143144
protected:
144145
static void staticRender(QgsMapRendererCustomPainterJob* self); // function to be used within the thread
145146

146-
void startRender();
147+
void doRender();
147148

148149
private:
149150
QPainter* mPainter;

‎src/core/qgspallabeling.cpp

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ QgsPalLayerSettings::~QgsPalLayerSettings()
578578
dataDefinedProperties.clear();
579579
}
580580

581+
582+
QgsPalLayerSettings QgsPalLayerSettings::fromLayer( QgsVectorLayer* layer )
583+
{
584+
QgsPalLayerSettings settings;
585+
settings.readFromLayer( layer );
586+
return settings;
587+
}
588+
589+
581590
QgsExpression* QgsPalLayerSettings::getLabelExpression()
582591
{
583592
if ( expression == NULL )
@@ -3044,6 +3053,7 @@ double QgsPalLayerSettings::scaleToPixelContext( double size, const QgsRenderCon
30443053

30453054
QgsPalLabeling::QgsPalLabeling()
30463055
: mMapSettings( NULL ), mPal( NULL )
3056+
, mResults( 0 )
30473057
{
30483058

30493059
// find out engine defaults
@@ -3065,8 +3075,6 @@ QgsPalLabeling::QgsPalLabeling()
30653075
mShowingShadowRects = false;
30663076
mShowingAllLabels = false;
30673077
mShowingPartialsLabels = p.getShowPartial();
3068-
3069-
mLabelSearchTree = new QgsLabelSearchTree();
30703078
}
30713079

30723080
QgsPalLabeling::~QgsPalLabeling()
@@ -3076,11 +3084,17 @@ QgsPalLabeling::~QgsPalLabeling()
30763084

30773085
clearActiveLayers();
30783086

3079-
delete mLabelSearchTree;
3080-
mLabelSearchTree = NULL;
3087+
delete mResults;
3088+
mResults = 0;
30813089
}
30823090

30833091
bool QgsPalLabeling::willUseLayer( QgsVectorLayer* layer )
3092+
{
3093+
return staticWillUseLayer( layer );
3094+
}
3095+
3096+
3097+
bool QgsPalLabeling::staticWillUseLayer( QgsVectorLayer* layer )
30843098
{
30853099
// don't do QgsPalLayerSettings::readFromLayer( layer ) if not needed
30863100
bool enabled = false;
@@ -3090,6 +3104,7 @@ bool QgsPalLabeling::willUseLayer( QgsVectorLayer* layer )
30903104
return enabled;
30913105
}
30923106

3107+
30933108
void QgsPalLabeling::clearActiveLayers()
30943109
{
30953110
QHash<QgsVectorLayer*, QgsPalLayerSettings>::iterator lit;
@@ -3809,10 +3824,8 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
38093824
QPainter* painter = context.painter();
38103825
QgsRectangle extent = context.extent();
38113826

3812-
if ( mLabelSearchTree )
3813-
{
3814-
mLabelSearchTree->clear();
3815-
}
3827+
delete mResults;
3828+
mResults = new QgsLabelingResults;
38163829

38173830
QTime t;
38183831
t.start();
@@ -3891,12 +3904,12 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
38913904
}
38923905

38933906
//insert into label search tree to manipulate position interactively
3894-
if ( mLabelSearchTree )
3907+
if ( mResults->mLabelSearchTree )
38953908
{
38963909
//for diagrams, remove the additional 'd' at the end of the layer id
38973910
QString layerId = layerName;
38983911
layerId.chop( 1 );
3899-
mLabelSearchTree->insertLabel( *it, QString( palGeometry->strId() ).toInt(), QString( "" ), layerId, QFont(), true, false );
3912+
mResults->mLabelSearchTree->insertLabel( *it, QString( palGeometry->strId() ).toInt(), QString( "" ), layerId, QFont(), true, false );
39003913
}
39013914
continue;
39023915
}
@@ -3969,10 +3982,10 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
39693982

39703983
drawLabel( *it, context, tmpLyr, LabelText );
39713984

3972-
if ( mLabelSearchTree )
3985+
if ( mResults->mLabelSearchTree )
39733986
{
39743987
QString labeltext = (( QgsPalGeometry* )( *it )->getFeaturePart()->getUserGeometry() )->text();
3975-
mLabelSearchTree->insertLabel( *it, QString( palGeometry->strId() ).toInt(), layerName, labeltext, dFont, false, palGeometry->isPinned() );
3988+
mResults->mLabelSearchTree->insertLabel( *it, QString( palGeometry->strId() ).toInt(), layerName, labeltext, dFont, false, palGeometry->isPinned() );
39763989
}
39773990
}
39783991

@@ -4016,38 +4029,24 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
40164029

40174030
QList<QgsLabelPosition> QgsPalLabeling::labelsAtPosition( const QgsPoint& p )
40184031
{
4019-
QList<QgsLabelPosition> positions;
4020-
4021-
QList<QgsLabelPosition*> positionPointers;
4022-
if ( mLabelSearchTree )
4023-
{
4024-
mLabelSearchTree->label( p, positionPointers );
4025-
QList<QgsLabelPosition*>::const_iterator pointerIt = positionPointers.constBegin();
4026-
for ( ; pointerIt != positionPointers.constEnd(); ++pointerIt )
4027-
{
4028-
positions.push_back( QgsLabelPosition( **pointerIt ) );
4029-
}
4030-
}
4031-
4032-
return positions;
4032+
return mResults ? mResults->labelsAtPosition( p ) : QList<QgsLabelPosition>();
40334033
}
40344034

40354035
QList<QgsLabelPosition> QgsPalLabeling::labelsWithinRect( const QgsRectangle& r )
40364036
{
4037-
QList<QgsLabelPosition> positions;
4037+
return mResults ? mResults->labelsWithinRect( r ) : QList<QgsLabelPosition>();
4038+
}
40384039

4039-
QList<QgsLabelPosition*> positionPointers;
4040-
if ( mLabelSearchTree )
4040+
QgsLabelingResults *QgsPalLabeling::takeResults()
4041+
{
4042+
if ( mResults )
40414043
{
4042-
mLabelSearchTree->labelsInRect( r, positionPointers );
4043-
QList<QgsLabelPosition*>::const_iterator pointerIt = positionPointers.constBegin();
4044-
for ( ; pointerIt != positionPointers.constEnd(); ++pointerIt )
4045-
{
4046-
positions.push_back( QgsLabelPosition( **pointerIt ) );
4047-
}
4044+
QgsLabelingResults* tmp = mResults;
4045+
mResults = 0;
4046+
return tmp; // ownership passed to the caller
40484047
}
4049-
4050-
return positions;
4048+
else
4049+
return 0;
40514050
}
40524051

40534052
void QgsPalLabeling::numCandidatePositions( int& candPoint, int& candLine, int& candPolygon )
@@ -4875,3 +4874,51 @@ QgsLabelingEngineInterface* QgsPalLabeling::clone()
48754874
lbl->mShowingPartialsLabels = mShowingPartialsLabels;
48764875
return lbl;
48774876
}
4877+
4878+
4879+
QgsLabelingResults::QgsLabelingResults()
4880+
{
4881+
mLabelSearchTree = new QgsLabelSearchTree();
4882+
}
4883+
4884+
QgsLabelingResults::~QgsLabelingResults()
4885+
{
4886+
delete mLabelSearchTree;
4887+
mLabelSearchTree = NULL;
4888+
}
4889+
4890+
QList<QgsLabelPosition> QgsLabelingResults::labelsAtPosition( const QgsPoint& p ) const
4891+
{
4892+
QList<QgsLabelPosition> positions;
4893+
4894+
QList<QgsLabelPosition*> positionPointers;
4895+
if ( mLabelSearchTree )
4896+
{
4897+
mLabelSearchTree->label( p, positionPointers );
4898+
QList<QgsLabelPosition*>::const_iterator pointerIt = positionPointers.constBegin();
4899+
for ( ; pointerIt != positionPointers.constEnd(); ++pointerIt )
4900+
{
4901+
positions.push_back( QgsLabelPosition( **pointerIt ) );
4902+
}
4903+
}
4904+
4905+
return positions;
4906+
}
4907+
4908+
QList<QgsLabelPosition> QgsLabelingResults::labelsWithinRect( const QgsRectangle& r ) const
4909+
{
4910+
QList<QgsLabelPosition> positions;
4911+
4912+
QList<QgsLabelPosition*> positionPointers;
4913+
if ( mLabelSearchTree )
4914+
{
4915+
mLabelSearchTree->labelsInRect( r, positionPointers );
4916+
QList<QgsLabelPosition*>::const_iterator pointerIt = positionPointers.constBegin();
4917+
for ( ; pointerIt != positionPointers.constEnd(); ++pointerIt )
4918+
{
4919+
positions.push_back( QgsLabelPosition( **pointerIt ) );
4920+
}
4921+
}
4922+
4923+
return positions;
4924+
}

‎src/core/qgspallabeling.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class CORE_EXPORT QgsPalLayerSettings
6767
QgsPalLayerSettings( const QgsPalLayerSettings& s );
6868
~QgsPalLayerSettings();
6969

70+
//! @note added in 2.1
71+
static QgsPalLayerSettings fromLayer( QgsVectorLayer* layer );
72+
7073
enum Placement
7174
{
7275
AroundPoint, // Point / Polygon
@@ -652,6 +655,32 @@ class CORE_EXPORT QgsLabelComponent
652655
double mDpiRatio;
653656
};
654657

658+
659+
660+
/**
661+
* Class that stores computed placement from labeling engine.
662+
* @note added in 2.1
663+
*/
664+
class QgsLabelingResults
665+
{
666+
public:
667+
QgsLabelingResults();
668+
~QgsLabelingResults();
669+
670+
//! return infos about labels at a given (map) position
671+
QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) const;
672+
//! return infos about labels within a given (map) rectangle
673+
QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) const;
674+
675+
protected:
676+
QgsLabelingResults( const QgsLabelingResults& ) {} // no copying allowed
677+
678+
QgsLabelSearchTree* mLabelSearchTree;
679+
680+
friend class QgsPalLabeling;
681+
};
682+
683+
655684
class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
656685
{
657686
public:
@@ -699,6 +728,11 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
699728
virtual void init( const QgsMapSettings& mapSettings );
700729
//! called to find out whether the layer is used for labeling
701730
virtual bool willUseLayer( QgsVectorLayer* layer );
731+
732+
//! called to find out whether the layer is used for labeling
733+
//! @note added in 2.1
734+
static bool staticWillUseLayer( QgsVectorLayer* layer );
735+
702736
//! clears all PAL layer settings for registered layers
703737
//! @note: this method was added in version 1.9
704738
virtual void clearActiveLayers();
@@ -717,9 +751,15 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
717751
//! called when we're done with rendering
718752
virtual void exit();
719753
//! return infos about labels at a given (map) position
720-
virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p );
754+
//! @deprecated since 2.1 - use takeResults() and methods of QgsLabelingResults
755+
Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p );
721756
//! return infos about labels within a given (map) rectangle
722-
virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r );
757+
//! @deprecated since 2.1 - use takeResults() and methods of QgsLabelingResults
758+
Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r );
759+
760+
//! Return pointer to recently computed results (in drawLabeling()) and pass the ownership of results to the caller
761+
//! @note added in 2.1
762+
QgsLabelingResults* takeResults();
723763

724764
//! called when passing engine among map renderers
725765
virtual QgsLabelingEngineInterface* clone();
@@ -792,7 +832,7 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
792832
bool mShowingShadowRects; // whether to show debugging rectangles for drop shadows
793833
bool mShowingPartialsLabels; // whether to avoid partials labels or not
794834

795-
QgsLabelSearchTree* mLabelSearchTree;
835+
QgsLabelingResults* mResults;
796836
};
797837

798838
#endif // QGSPALLABELING_H

‎src/gui/qgsmapcanvas.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class QgsMapCanvas::CanvasProperties
8282
QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
8383
: QGraphicsView( parent )
8484
, mCanvasProperties( new CanvasProperties )
85-
, mLabeling( new QgsPalLabeling )
85+
, mLabelingResults( 0 )
8686
//, mNewSize( QSize() )
8787
//, mPainting( false )
8888
{
@@ -165,14 +165,14 @@ QgsMapCanvas::~QgsMapCanvas()
165165
it++;
166166
}
167167

168-
delete mLabeling;
169-
170168
mScene->deleteLater(); // crashes in python tests on windows
171169

172170
delete mMapRenderer;
173171
// mCanvasProperties auto-deleted via std::auto_ptr
174172
// CanvasProperties struct has its own dtor for freeing resources
175173

174+
delete mLabelingResults;
175+
176176
} // dtor
177177

178178
void QgsMapCanvas::enableAntiAliasing( bool theFlag )
@@ -394,9 +394,15 @@ void QgsMapCanvas::setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
394394
emit destinationSrsChanged();
395395
}
396396

397-
QgsPalLabeling *QgsMapCanvas::labelingEngine()
397+
const QgsLabelingResults *QgsMapCanvas::labelingResults() const
398+
{
399+
return mLabelingResults;
400+
}
401+
402+
void QgsMapCanvas::assignLabelingResults( QgsLabelingResults* results )
398403
{
399-
return mLabeling;
404+
delete mLabelingResults;
405+
mLabelingResults = results;
400406
}
401407

402408

‎src/gui/qgsmapcanvas.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class QgsLegendView;
5858
class QgsHighlight;
5959
class QgsVectorLayer;
6060

61+
class QgsLabelingResults;
6162
class QgsMapRenderer;
6263
class QgsMapSettings;
6364
class QgsMapCanvasMap;
6465
class QgsMapOverviewCanvas;
6566
class QgsMapTool;
66-
class QgsPalLabeling;
6767

6868
/** \ingroup gui
6969
* A class that stores visibility and presence in overview flags together
@@ -132,9 +132,14 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
132132
//! @note added in 2.1
133133
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
134134

135-
//! Get access to the labeling engine
135+
//! Get access to the labeling results (may be null)
136136
//! @note added in 2.1
137-
QgsPalLabeling* labelingEngine();
137+
const QgsLabelingResults* labelingResults() const;
138+
139+
//! Store newly generated labeling results (may be null) and take ownership of the object.
140+
//! Only meant for communication with QgsMapCanvasMap
141+
//! @note added in 2.1
142+
void assignLabelingResults( QgsLabelingResults* results );
138143

139144
//! @deprecated since 2.1 - there could be more than just one "map" items
140145
QgsMapCanvasMap* map();
@@ -523,7 +528,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
523528
//! Mouse wheel action
524529
WheelAction mWheelAction;
525530

526-
QgsPalLabeling* mLabeling;
531+
QgsLabelingResults* mLabelingResults;
527532

528533
//! resize canvas size
529534
//QSize mNewSize;

‎src/gui/qgsmapcanvasmap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ void QgsMapCanvasMap::paint( QPainter* p, const QStyleOptionGraphicsItem*, QWidg
7979
// create the renderer job
8080
Q_ASSERT(mJob == 0);
8181
mJob = new QgsMapRendererSequentialJob( mCanvas->mapSettings() );
82-
mJob->setLabelingEngine( mCanvas->labelingEngine() );
8382
connect(mJob, SIGNAL(finished()), SLOT(finish()));
8483
mJob->start();
8584

@@ -138,6 +137,8 @@ void QgsMapCanvasMap::finish()
138137

139138
mImage = mJob->renderedImage();
140139

140+
mCanvas->assignLabelingResults( mJob->takeLabelingResults() );
141+
141142
update();
142143
}
143144

0 commit comments

Comments
 (0)
Please sign in to comment.