Skip to content

Commit 3a92b17

Browse files
committedJan 26, 2017
Reading/saving labeling engine uses a particular QgsProject
... rather than using QgsProject::instance() internally These are small cleanups to dig out some instance() uses and move them one level up... At some point we should maybe make labeling engine configuration a part of QgsMapSettings and have default project labeling engine config accessible from QgsProject in a way similar to e.g. snapping configuration.
1 parent 958dff9 commit 3a92b17

10 files changed

+61
-65
lines changed
 

‎doc/api_break.dox

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,8 @@ QgsPalLabeling {#qgis_api_break_3_0_QgsPalLabeling}
14481448
- labelsAtPosition() was removed. Use takeResults() and methods of QgsLabelingResults instead.
14491449
- labelsWithinRect() was removed. Use takeResults() and methods of QgsLabelingResults instead.
14501450
- isStoredWithProject() and setStoredWithProject() had no effect and were removed.
1451+
- staticWillUseLayer(QString) was removed. Use the variant with QgsVectorLayer argument.
1452+
- clearEngineSettings() was replaced by QgsLabelingEngine::clearSettingsInProject().
14511453

14521454

14531455
QgsPalLayerSettings {#qgis_api_break_3_0_QgsPalLayerSettings}

‎python/core/qgspallabeling.sip

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,13 @@ class QgsPalLabeling
552552
//! called to find out whether the layer is used for labeling
553553
//! @note added in 2.4
554554
static bool staticWillUseLayer( QgsVectorLayer* layer );
555-
static bool staticWillUseLayer( const QString& layerID );
556555

557556
//! @note not available in python bindings
558557
// void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
559558

560559
//! load/save engine settings to project file
561560
void loadEngineSettings();
562561
void saveEngineSettings();
563-
void clearEngineSettings();
564562

565563
/** Prepares a geometry for registration with PAL. Handles reprojection, rotation, clipping, etc.
566564
* @param geometry geometry to prepare

‎src/app/qgslabelengineconfigdialog.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "qgslabelengineconfigdialog.h"
1616

1717
#include "qgspallabeling.h"
18+
#include "qgslabelingengine.h"
19+
#include "qgsproject.h"
1820
#include <pal/pal.h>
1921

2022
#include <QPushButton>
@@ -28,44 +30,44 @@ QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QWidget* parent )
2830
connect( buttonBox->button( QDialogButtonBox::RestoreDefaults ), SIGNAL( clicked() ),
2931
this, SLOT( setDefaults() ) );
3032

31-
QgsPalLabeling lbl;
32-
lbl.loadEngineSettings();
33+
QgsLabelingEngine engine;
34+
engine.readSettingsFromProject( QgsProject::instance() );
3335

3436
// search method
35-
cboSearchMethod->setCurrentIndex( lbl.searchMethod() );
37+
cboSearchMethod->setCurrentIndex( engine.searchMethod() );
3638

3739
// candidate numbers
3840
int candPoint, candLine, candPolygon;
39-
lbl.numCandidatePositions( candPoint, candLine, candPolygon );
41+
engine.numCandidatePositions( candPoint, candLine, candPolygon );
4042
spinCandPoint->setValue( candPoint );
4143
spinCandLine->setValue( candLine );
4244
spinCandPolygon->setValue( candPolygon );
4345

44-
chkShowCandidates->setChecked( lbl.isShowingCandidates() );
45-
chkShowAllLabels->setChecked( lbl.isShowingAllLabels() );
46+
chkShowCandidates->setChecked( engine.testFlag( QgsLabelingEngine::DrawCandidates ) );
47+
chkShowAllLabels->setChecked( engine.testFlag( QgsLabelingEngine::UseAllLabels ) );
4648

47-
chkShowPartialsLabels->setChecked( lbl.isShowingPartialsLabels() );
48-
mDrawOutlinesChkBox->setChecked( lbl.isDrawingOutlineLabels() );
49+
chkShowPartialsLabels->setChecked( engine.testFlag( QgsLabelingEngine::UsePartialCandidates ) );
50+
mDrawOutlinesChkBox->setChecked( engine.testFlag( QgsLabelingEngine::RenderOutlineLabels ) );
4951
}
5052

5153

5254
void QgsLabelEngineConfigDialog::onOK()
5355
{
54-
QgsPalLabeling lbl;
56+
QgsLabelingEngine engine;
5557

5658
// save
57-
lbl.setSearchMethod(( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );
59+
engine.setSearchMethod(( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );
5860

59-
lbl.setNumCandidatePositions( spinCandPoint->value(),
60-
spinCandLine->value(),
61-
spinCandPolygon->value() );
61+
engine.setNumCandidatePositions( spinCandPoint->value(),
62+
spinCandLine->value(),
63+
spinCandPolygon->value() );
6264

63-
lbl.setShowingCandidates( chkShowCandidates->isChecked() );
64-
lbl.setShowingAllLabels( chkShowAllLabels->isChecked() );
65-
lbl.setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );
66-
lbl.setDrawingOutlineLabels( mDrawOutlinesChkBox->isChecked() );
65+
engine.setFlag( QgsLabelingEngine::DrawCandidates, chkShowCandidates->isChecked() );
66+
engine.setFlag( QgsLabelingEngine::UseAllLabels, chkShowAllLabels->isChecked() );
67+
engine.setFlag( QgsLabelingEngine::UsePartialCandidates, chkShowPartialsLabels->isChecked() );
68+
engine.setFlag( QgsLabelingEngine::RenderOutlineLabels, mDrawOutlinesChkBox->isChecked() );
6769

68-
lbl.saveEngineSettings();
70+
engine.writeSettingsToProject( QgsProject::instance() );
6971

7072
accept();
7173
}

‎src/core/dxf/qgsdxfexport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgsdxfpallabeling.h"
3131
#include "qgsvectordataprovider.h"
3232
#include "qgspoint.h"
33+
#include "qgsproject.h"
3334
#include "qgsrenderer.h"
3435
#include "qgssymbollayer.h"
3536
#include "qgsfillsymbollayer.h"
@@ -941,7 +942,7 @@ void QgsDxfExport::writeEntities()
941942

942943
// label engine
943944
QgsLabelingEngine engine;
944-
engine.readSettingsFromProject();
945+
engine.readSettingsFromProject( QgsProject::instance() );
945946
engine.setMapSettings( mMapSettings );
946947

947948
// iterate through the maplayers

‎src/core/qgslabelingengine.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,9 @@ QgsLabelingResults* QgsLabelingEngine::takeResults()
347347
}
348348

349349

350-
void QgsLabelingEngine::readSettingsFromProject()
350+
void QgsLabelingEngine::readSettingsFromProject( QgsProject* prj )
351351
{
352352
bool saved = false;
353-
QgsProject* prj = QgsProject::instance();
354353
mSearchMethod = static_cast< QgsPalLabeling::Search >( prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( QgsPalLabeling::Chain ), &saved ) );
355354
mCandPoint = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ), 16, &saved );
356355
mCandLine = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ), 50, &saved );
@@ -364,18 +363,30 @@ void QgsLabelingEngine::readSettingsFromProject()
364363
if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), true, &saved ) ) mFlags |= RenderOutlineLabels;
365364
}
366365

367-
void QgsLabelingEngine::writeSettingsToProject()
366+
void QgsLabelingEngine::writeSettingsToProject( QgsProject* project )
368367
{
369-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( mSearchMethod ) );
370-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ), mCandPoint );
371-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ), mCandLine );
372-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygon" ), mCandPolygon );
373-
374-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), mFlags.testFlag( DrawCandidates ) );
375-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), mFlags.testFlag( DrawLabelRectOnly ) );
376-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), mFlags.testFlag( UseAllLabels ) );
377-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), mFlags.testFlag( UsePartialCandidates ) );
378-
QgsProject::instance()->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), mFlags.testFlag( RenderOutlineLabels ) );
368+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( mSearchMethod ) );
369+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ), mCandPoint );
370+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ), mCandLine );
371+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygon" ), mCandPolygon );
372+
373+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), mFlags.testFlag( DrawCandidates ) );
374+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), mFlags.testFlag( DrawLabelRectOnly ) );
375+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), mFlags.testFlag( UseAllLabels ) );
376+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), mFlags.testFlag( UsePartialCandidates ) );
377+
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), mFlags.testFlag( RenderOutlineLabels ) );
378+
}
379+
380+
void QgsLabelingEngine::clearSettingsInProject( QgsProject* project )
381+
{
382+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ) );
383+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ) );
384+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ) );
385+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygon" ) );
386+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ) );
387+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ) );
388+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ) );
389+
project->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ) );
379390
}
380391

381392

‎src/core/qgslabelingengine.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ class CORE_EXPORT QgsLabelingEngine
220220
//! Which search method to use for removal collisions between labels
221221
QgsPalLabeling::Search searchMethod() const { return mSearchMethod; }
222222

223-
//! Read configuration of the labeling engine from the current project file
224-
void readSettingsFromProject();
225-
//! Write configuration of the labeling engine to the current project file
226-
void writeSettingsToProject();
223+
//! Read configuration of the labeling engine from a project
224+
void readSettingsFromProject( QgsProject* project );
225+
//! Write configuration of the labeling engine to a project
226+
void writeSettingsToProject( QgsProject* project );
227+
//! Clear configuration of the labeling engine in a project
228+
static void clearSettingsInProject( QgsProject* project );
227229

228230
protected:
229231
void processProvider( QgsAbstractLabelProvider* provider, QgsRenderContext& context, pal::Pal& p ); //#spellok

‎src/core/qgsmaprenderercustompainterjob.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void QgsMapRendererCustomPainterJob::start()
7979
if ( mSettings.testFlag( QgsMapSettings::DrawLabeling ) )
8080
{
8181
mLabelingEngineV2 = new QgsLabelingEngine();
82-
mLabelingEngineV2->readSettingsFromProject();
82+
mLabelingEngineV2->readSettingsFromProject( QgsProject::instance() );
8383
mLabelingEngineV2->setMapSettings( mSettings );
8484
}
8585

‎src/core/qgsmaprendererparalleljob.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgslogger.h"
2121
#include "qgsmaplayerrenderer.h"
2222
#include "qgspallabeling.h"
23+
#include "qgsproject.h"
2324

2425
#include <QtConcurrentMap>
2526

@@ -57,7 +58,7 @@ void QgsMapRendererParallelJob::start()
5758
if ( mSettings.testFlag( QgsMapSettings::DrawLabeling ) )
5859
{
5960
mLabelingEngineV2 = new QgsLabelingEngine();
60-
mLabelingEngineV2->readSettingsFromProject();
61+
mLabelingEngineV2->readSettingsFromProject( QgsProject::instance() );
6162
mLabelingEngineV2->setMapSettings( mSettings );
6263
}
6364

‎src/core/qgspallabeling.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,15 +2740,6 @@ QgsPalLabeling::~QgsPalLabeling()
27402740
mEngine = nullptr;
27412741
}
27422742

2743-
bool QgsPalLabeling::staticWillUseLayer( const QString& layerID )
2744-
{
2745-
QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsProject::instance()->mapLayer( layerID ) );
2746-
if ( !layer )
2747-
return false;
2748-
return staticWillUseLayer( layer );
2749-
}
2750-
2751-
27522743
bool QgsPalLabeling::staticWillUseLayer( QgsVectorLayer* layer )
27532744
{
27542745
// don't do QgsPalLayerSettings::readFromLayer( layer ) if not needed
@@ -3485,26 +3476,16 @@ void QgsPalLabeling::drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* p
34853476
drawLabelCandidateRect( lp->getNextPart(), painter, xform, candidates );
34863477
}
34873478

3479+
// TODO: remove once not used in labeling tests
34883480
void QgsPalLabeling::loadEngineSettings()
34893481
{
3490-
mEngine->readSettingsFromProject();
3482+
mEngine->readSettingsFromProject( QgsProject::instance() );
34913483
}
34923484

3485+
// TODO: remove once not used in labeling tests
34933486
void QgsPalLabeling::saveEngineSettings()
34943487
{
3495-
mEngine->writeSettingsToProject();
3496-
}
3497-
3498-
void QgsPalLabeling::clearEngineSettings()
3499-
{
3500-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ) );
3501-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ) );
3502-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ) );
3503-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygon" ) );
3504-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ) );
3505-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ) );
3506-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ) );
3507-
QgsProject::instance()->removeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ) );
3488+
mEngine->writeSettingsToProject( QgsProject::instance() );
35083489
}
35093490

35103491
QgsLabelingResults::QgsLabelingResults()

‎src/core/qgspallabeling.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,13 @@ class CORE_EXPORT QgsPalLabeling
737737
//! called to find out whether the layer is used for labeling
738738
//! @note added in 2.4
739739
static bool staticWillUseLayer( QgsVectorLayer* layer );
740-
static bool staticWillUseLayer( const QString& layerID );
741740

742741
//! @note not available in python bindings
743742
static void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform, QList<QgsLabelCandidate>* candidates = nullptr );
744743

745744
//! load/save engine settings to project file
746745
void loadEngineSettings();
747746
void saveEngineSettings();
748-
void clearEngineSettings();
749747

750748
/** Prepares a geometry for registration with PAL. Handles reprojection, rotation, clipping, etc.
751749
* @param geometry geometry to prepare

0 commit comments

Comments
 (0)
Please sign in to comment.