Skip to content

Commit

Permalink
Move labeling engine config to a new class + QgsPalLabeling cleanup
Browse files Browse the repository at this point in the history
Another bit to make core library independent from QgsProject::instance().
Until now labeling engine configuration was taken from project singleton.

Removed legacy methods from QgsPalLabeling - the class now does not have
any more member variables/methods, it is just a bunch of helper functions.

(PR #4384)
  • Loading branch information
wonder-sk authored Apr 21, 2017
1 parent bcf1ef5 commit 25ed76b
Show file tree
Hide file tree
Showing 40 changed files with 476 additions and 396 deletions.
9 changes: 8 additions & 1 deletion doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,14 @@ QgsPalLabeling {#qgis_api_break_3_0_QgsPalLabeling}
--------------

- all methods implemented from QgsLabelingEngineInterface have been removed: init(), willUseLayer(), clearActiveLayers(), clearActiveLayer(), prepareLayer(), prepareDiagramLayer(), registerFeature(), registerDiagramFeature(), drawLabeling(), exit(), clone(). Labeling is now driven by QgsLabelingEngine.
- all methods for global labeling engine configuration have been moved to QgsLabelingEngineSettings:
- searchMethod(), setSearchMethod(), numCandidatePositions(), setNumCandidatePositions() - methods with the same name in QgsLabelingEngineSettings
- isShowingCandidates(), setShowingCandidates() - replaced by DrawCandidates flag
- isShowingAllLabels(), setShowingAllLabels() - replaced by UseAllLabels flag
- isShowingPartialLabels(), setShowingPartialLabels() - replaced by UsePartialCandidates flag
- drawLabelRectOnly(), setDrawLabelRectOnly() - replaced by DrawLabelRectOnly flag
- loadEngineSettings(), saveEngineSettings() - replaced by readSettingsFromProject() and writeSettingsToProject()
- clearEngineSettings() - replaced by clear() that resets the settings instance (rather than immediately writing to project)
- init(QgsMapRenderer*) has been removed. Use init(const QgsMapSettings&) instead.
- prepareGeometry and geometryRequiresPreparation now take geometry references, not pointers.
- layer() was removed. If direct access to QgsPalLayerSettings is necessary, use QgsPalLayerSettings::fromLayer()
Expand All @@ -1586,7 +1594,6 @@ QgsPalLabeling {#qgis_api_break_3_0_QgsPalLabeling}
- labelsWithinRect() was removed. Use takeResults() and methods of QgsLabelingResults instead.
- isStoredWithProject() and setStoredWithProject() had no effect and were removed.
- staticWillUseLayer(QString) was removed. Use the variant with QgsVectorLayer argument.
- clearEngineSettings() was replaced by QgsLabelingEngine::clearSettingsInProject().


QgsPalLayerSettings {#qgis_api_break_3_0_QgsPalLayerSettings}
Expand Down
1 change: 1 addition & 0 deletions python/core/core.sip
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
%Include qgsfieldmodel.sip
%Include qgsfieldproxymodel.sip
%Include qgsfontutils.sip
%Include qgslabelingenginesettings.sip
%Include qgslabelsearchtree.sip
%Include qgslegendrenderer.sip
%Include qgslegendsettings.sip
Expand Down
109 changes: 109 additions & 0 deletions python/core/qgslabelingenginesettings.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslabelingenginesettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLabelingEngineSettings
{
%Docstring
Stores global configuration for labeling engine
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslabelingenginesettings.h"
%End
public:
enum Flag
{
UseAllLabels,
UsePartialCandidates,
RenderOutlineLabels,
DrawLabelRectOnly,
DrawCandidates,
};
typedef QFlags<QgsLabelingEngineSettings::Flag> Flags;


enum Search
{
Chain,
Popmusic_Tabu,
Popmusic_Chain,
Popmusic_Tabu_Chain,
Falp
};

QgsLabelingEngineSettings();

void clear();
%Docstring
Return the configuration to the defaults
%End

void setFlags( Flags flags );
%Docstring
Set flags of the labeling engine
%End
Flags flags() const;
%Docstring
Get flags of the labeling engine
:rtype: Flags
%End
bool testFlag( Flag f ) const;
%Docstring
Test whether a particular flag is enabled
:rtype: bool
%End
void setFlag( Flag f, bool enabled = true );
%Docstring
Set whether a particual flag is enabled
%End

void numCandidatePositions( int &candPoint, int &candLine, int &candPolygon ) const;
%Docstring
Get number of candidate positions that will be generated for each label feature (default to 8)
%End
void setNumCandidatePositions( int candPoint, int candLine, int candPolygon );
%Docstring
Set number of candidate positions that will be generated for each label feature
%End

void setSearchMethod( Search s );
%Docstring
Set which search method to use for removal collisions between labels
%End
Search searchMethod() const;
%Docstring
Which search method to use for removal collisions between labels
:rtype: Search
%End

void readSettingsFromProject( QgsProject *project );
%Docstring
Read configuration of the labeling engine from a project
%End
void writeSettingsToProject( QgsProject *project );
%Docstring
Write configuration of the labeling engine to a project
%End

};

QFlags<QgsLabelingEngineSettings::Flag> operator|(QgsLabelingEngineSettings::Flag f1, QFlags<QgsLabelingEngineSettings::Flag> f2);


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslabelingenginesettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

11 changes: 11 additions & 0 deletions python/core/qgsmapsettings.sip
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ class QgsMapSettings
/** Gets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)*/
QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const;

/**
* Sets global configuration of the labeling engine
* \since QGIS 3.0
*/
void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );

/**
* Returns global configuration of the labeling engine
* \since QGIS 3.0
*/
const QgsLabelingEngineSettings &labelingEngineSettings() const;

protected:

Expand Down
72 changes: 12 additions & 60 deletions python/core/qgspallabeling.sip
Original file line number Diff line number Diff line change
Expand Up @@ -514,55 +514,9 @@ class QgsPalLabeling

public:

QgsPalLabeling();
~QgsPalLabeling();

void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon );
void setNumCandidatePositions( int candPoint, int candLine, int candPolygon );

enum Search { Chain, Popmusic_Tabu, Popmusic_Chain, Popmusic_Tabu_Chain, Falp };

void setSearchMethod( Search s );
Search searchMethod() const;

bool isShowingCandidates() const;
void setShowingCandidates( bool showing );

bool isShowingAllLabels() const;
void setShowingAllLabels( bool showing );

bool isShowingPartialsLabels() const;
void setShowingPartialsLabels( bool showing );

//! @note added in 2.4
bool isDrawingOutlineLabels() const;
void setDrawingOutlineLabels( bool outline );

/** Returns whether the engine will only draw the outline rectangles of labels,
* not the label contents themselves. Used for debugging and testing purposes.
* @see setDrawLabelRectOnly
* @note added in QGIS 2.12
*/
bool drawLabelRectOnly() const;

/** Sets whether the engine should only draw the outline rectangles of labels,
* not the label contents themselves. Used for debugging and testing purposes.
* @param drawRect set to true to enable rect drawing only
* @see drawLabelRectOnly
* @note added in QGIS 2.12
*/
void setDrawLabelRectOnly( bool drawRect );

//! called to find out whether the layer is used for labeling
//! @note added in 2.4
static bool staticWillUseLayer( QgsVectorLayer* layer );

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

//! load/save engine settings to project file
void loadEngineSettings();
void saveEngineSettings();
//! \since QGIS 2.4
static bool staticWillUseLayer( QgsVectorLayer *layer );

/** Prepares a geometry for registration with PAL. Handles reprojection, rotation, clipping, etc.
* @param geometry geometry to prepare
Expand Down Expand Up @@ -604,26 +558,24 @@ class QgsPalLabeling

protected:
// update temporary QgsPalLayerSettings with any data defined text style values
void dataDefinedTextStyle( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );
static void dataDefinedTextStyle( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );

// update temporary QgsPalLayerSettings with any data defined text formatting values
void dataDefinedTextFormatting( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );
static void dataDefinedTextFormatting( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );

// update temporary QgsPalLayerSettings with any data defined text buffer values
void dataDefinedTextBuffer( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );
static void dataDefinedTextBuffer( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );

// update temporary QgsPalLayerSettings with any data defined shape background values
void dataDefinedShapeBackground( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );
static void dataDefinedShapeBackground( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );

// update temporary QgsPalLayerSettings with any data defined drop shadow values
void dataDefinedDropShadow( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );

void deleteTemporaryData();
static void dataDefinedDropShadow( QgsPalLayerSettings& tmpLyr,
const QMap< QgsPalLayerSettings::Property, QVariant >& ddValues );

/** Checks whether a geometry exceeds the minimum required size for a geometry to be labeled.
* @param context render context
Expand Down
19 changes: 19 additions & 0 deletions python/core/qgsproject.sip
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ class QgsProject : QObject, QgsExpressionContextGenerator
void setAvoidIntersectionsLayers( const QList<QgsVectorLayer*>& layers );
QVariantMap customVariables() const;
void setCustomVariables( const QVariantMap& customVariables );

/**
* Sets project's global labeling engine settings
* \since QGIS 3.0
*/
void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );

/**
* Returns project's global labeling engine settings
* \since QGIS 3.0
*/
const QgsLabelingEngineSettings &labelingEngineSettings() const;

int count() const;

QgsMapLayer* mapLayer( const QString& layerId ) const;
Expand Down Expand Up @@ -583,6 +596,12 @@ class QgsProject : QObject, QgsExpressionContextGenerator
*/
void mapThemeCollectionChanged();

/**
* Emitted when global configuration of the labeling engine changes.
* \since QGIS 3.0
*/
void labelingEngineSettingsChanged();

//
// signals from QgsMapLayerRegistry
//
Expand Down
2 changes: 2 additions & 0 deletions python/gui/qgsmapcanvas.sip
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class QgsMapCanvas : QGraphicsView
QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
bool annotationsVisible() const;
void setAnnotationsVisible( bool visible );
void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
const QgsLabelingEngineSettings &labelingEngineSettings() const;

public slots:

Expand Down
6 changes: 6 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,12 @@ void QgisApp::setupConnections()
mMapCanvas->setDestinationCrs( QgsProject::instance()->crs() );
} );

connect( QgsProject::instance(), &QgsProject::labelingEngineSettingsChanged,
this, [ = ]
{
mMapCanvas->setLabelingEngineSettings( QgsProject::instance()->labelingEngineSettings() );
} );

// connect legend signals
connect( mLayerTreeView, &QgsLayerTreeView::currentLayerChanged,
this, &QgisApp::activateDeactivateLayerRelatedActions );
Expand Down
36 changes: 16 additions & 20 deletions src/app/qgslabelengineconfigdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
***************************************************************************/
#include "qgslabelengineconfigdialog.h"

#include "qgspallabeling.h"
#include "qgslabelingengine.h"
#include "qgslabelingenginesettings.h"
#include "qgsproject.h"
#include <pal/pal.h>

Expand All @@ -30,44 +29,41 @@ QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QWidget *parent )
connect( buttonBox->button( QDialogButtonBox::RestoreDefaults ), &QAbstractButton::clicked,
this, &QgsLabelEngineConfigDialog::setDefaults );

QgsLabelingEngine engine;
engine.readSettingsFromProject( QgsProject::instance() );
QgsLabelingEngineSettings engineSettings = QgsProject::instance()->labelingEngineSettings();

// search method
cboSearchMethod->setCurrentIndex( engine.searchMethod() );
cboSearchMethod->setCurrentIndex( engineSettings.searchMethod() );

// candidate numbers
int candPoint, candLine, candPolygon;
engine.numCandidatePositions( candPoint, candLine, candPolygon );
engineSettings.numCandidatePositions( candPoint, candLine, candPolygon );
spinCandPoint->setValue( candPoint );
spinCandLine->setValue( candLine );
spinCandPolygon->setValue( candPolygon );

chkShowCandidates->setChecked( engine.testFlag( QgsLabelingEngine::DrawCandidates ) );
chkShowAllLabels->setChecked( engine.testFlag( QgsLabelingEngine::UseAllLabels ) );
chkShowCandidates->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::DrawCandidates ) );
chkShowAllLabels->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::UseAllLabels ) );

chkShowPartialsLabels->setChecked( engine.testFlag( QgsLabelingEngine::UsePartialCandidates ) );
mDrawOutlinesChkBox->setChecked( engine.testFlag( QgsLabelingEngine::RenderOutlineLabels ) );
chkShowPartialsLabels->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::UsePartialCandidates ) );
mDrawOutlinesChkBox->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::RenderOutlineLabels ) );
}


void QgsLabelEngineConfigDialog::onOK()
{
QgsLabelingEngine engine;
QgsLabelingEngineSettings engineSettings;

// save
engine.setSearchMethod( ( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );
engineSettings.setSearchMethod( ( QgsLabelingEngineSettings::Search ) cboSearchMethod->currentIndex() );

engine.setNumCandidatePositions( spinCandPoint->value(),
spinCandLine->value(),
spinCandPolygon->value() );
engineSettings.setNumCandidatePositions( spinCandPoint->value(), spinCandLine->value(), spinCandPolygon->value() );

engine.setFlag( QgsLabelingEngine::DrawCandidates, chkShowCandidates->isChecked() );
engine.setFlag( QgsLabelingEngine::UseAllLabels, chkShowAllLabels->isChecked() );
engine.setFlag( QgsLabelingEngine::UsePartialCandidates, chkShowPartialsLabels->isChecked() );
engine.setFlag( QgsLabelingEngine::RenderOutlineLabels, mDrawOutlinesChkBox->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, chkShowCandidates->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::UseAllLabels, chkShowAllLabels->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, chkShowPartialsLabels->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::RenderOutlineLabels, mDrawOutlinesChkBox->isChecked() );

engine.writeSettingsToProject( QgsProject::instance() );
QgsProject::instance()->setLabelingEngineSettings( engineSettings );

accept();
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ SET(QGIS_CORE_SRCS
qgsjsonutils.cpp
qgslabelfeature.cpp
qgslabelingengine.cpp
qgslabelingenginesettings.cpp
qgslabelsearchtree.cpp
qgslayerdefinition.cpp
qgslegendrenderer.cpp
Expand Down Expand Up @@ -729,6 +730,7 @@ SET(QGIS_CORE_HDRS
qgslayerdefinition.h
qgslabelfeature.h
qgslabelingengine.h
qgslabelingenginesettings.h
qgslabelsearchtree.h
qgslegendrenderer.h
qgslegendsettings.h
Expand Down
Loading

0 comments on commit 25ed76b

Please sign in to comment.