Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Start cleaning up label line settings, by moving them from the
QgsPalLayerSettings mega class to a dedicated QgsLabelLineSettings
class
  • Loading branch information
nyalldawson committed Aug 17, 2020
1 parent 250ce76 commit 23848e0
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 48 deletions.
1 change: 0 additions & 1 deletion python/3d/3d_auto.sip
Expand Up @@ -19,5 +19,4 @@
%Include auto_generated/symbols/qgsline3dsymbol.sip
%Include auto_generated/symbols/qgspoint3dsymbol.sip
%Include auto_generated/symbols/qgspolygon3dsymbol.sip
%Include auto_generated/qgs3dexportobject.sip
%Include auto_generated/qgs3dmapexportsettings.sip
58 changes: 58 additions & 0 deletions python/core/auto_generated/labeling/qgslabellinesettings.sip.in
@@ -0,0 +1,58 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/labeling/qgslabellinesettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLabelLineSettings
{
%Docstring

Contains settings related to how the label engine places and formats
labels for line features (or polygon features which are labeled in
a "perimeter" style mode).

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgslabellinesettings.h"
%End
public:

QgsLabeling::LinePlacementFlags placementFlags() const;
%Docstring
Returns the line placement flags, which dictate how line labels can be placed
above or below the lines.

.. seealso:: :py:func:`setPlacementFlags`
%End

void setPlacementFlags( QgsLabeling::LinePlacementFlags flags );
%Docstring
Returns the line placement ``flags``, which dictate how line labels can be placed
above or below the lines.

.. seealso:: :py:func:`placementFlags`
%End

void updateDataDefinedProperties( const QgsPropertyCollection &properties, QgsExpressionContext &context );
%Docstring
Updates the thinning settings to respect any data defined properties
set within the specified ``properties`` collection.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/labeling/qgslabellinesettings.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
33 changes: 28 additions & 5 deletions python/core/auto_generated/labeling/qgspallabeling.sip.in
Expand Up @@ -133,9 +133,9 @@ class QgsPalLayerSettings
AboveLine,
BelowLine,
MapOrientation,
};
} /Deprecated/;

enum QuadrantPosition
enum
{
QuadrantAboveLeft,
QuadrantAbove,
Expand Down Expand Up @@ -403,8 +403,6 @@ Returns the QgsExpression for this label settings. May be ``None`` if isExpressi

Placement placement;

unsigned int placementFlags;

QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const;
%Docstring
Returns the polygon placement flags, which dictate how polygon labels can be placed.
Expand Down Expand Up @@ -498,6 +496,7 @@ Sets the polygon placement ``flags``, which dictate how polygon labels can be pl
%Property( name = obstacle, get = _getIsObstacle, set = _setIsObstacle )
%Property( name = obstacleFactor, get = _getObstacleFactor, set = _setObstacleFactor )
%Property( name = obstacleType, get = _getObstacleType, set = _setObstacleType )
%Property( name = placementFlags, get = _getLinePlacementFlags, set = _setLinePlacementFlags )

bool _limitNumLabels() const;
void _setLimitNumLabels( bool limit );
Expand All @@ -511,7 +510,8 @@ Sets the polygon placement ``flags``, which dictate how polygon labels can be pl
void _setObstacleFactor( double factor );
ObstacleType _getObstacleType() const;
void _setObstacleType( ObstacleType type );

unsigned int _getLinePlacementFlags() const;
void _setLinePlacementFlags( unsigned int flags );

double zIndex;

Expand Down Expand Up @@ -624,6 +624,29 @@ Ownership of ``callout`` is transferred to the settings.
%End


QgsLabelLineSettings &lineSettings();
%Docstring
Returns the label line settings, which contain settings related to how the label
engine places and formats labels for line features (or polygon features which are labeled in
a "perimeter" style mode).

.. seealso:: :py:func:`setLineSettings`

.. versionadded:: 3.16
%End

void setLineSettings( const QgsLabelLineSettings &settings );
%Docstring
Sets the label line ``settings``, which contain settings related to how the label
engine places and formats labels for line features (or polygon features which are labeled in
a "perimeter" style mode).

.. seealso:: :py:func:`lineSettings`

.. versionadded:: 3.16
%End


QgsLabelObstacleSettings &obstacleSettings();
%Docstring
Returns the label obstacle settings.
Expand Down
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -347,6 +347,7 @@
%Include auto_generated/gps/qgsnmeaconnection.sip
%Include auto_generated/labeling/qgslabeling.sip
%Include auto_generated/labeling/qgslabelingenginesettings.sip
%Include auto_generated/labeling/qgslabellinesettings.sip
%Include auto_generated/labeling/qgslabelobstaclesettings.sip
%Include auto_generated/labeling/qgslabelsearchtree.sip
%Include auto_generated/labeling/qgslabelthinningsettings.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -620,6 +620,7 @@ SET(QGIS_CORE_SRCS
labeling/qgslabelfeature.cpp
labeling/qgslabelingengine.cpp
labeling/qgslabelingenginesettings.cpp
labeling/qgslabellinesettings.cpp
labeling/qgslabelobstaclesettings.cpp
labeling/qgslabelsearchtree.cpp
labeling/qgslabelsink.cpp
Expand Down Expand Up @@ -1167,6 +1168,7 @@ SET(QGIS_CORE_HDRS
labeling/qgslabeling.h
labeling/qgslabelingengine.h
labeling/qgslabelingenginesettings.h
labeling/qgslabellinesettings.h
labeling/qgslabelobstaclesettings.h
labeling/qgslabelsearchtree.h
labeling/qgslabelthinningsettings.h
Expand Down
34 changes: 34 additions & 0 deletions src/core/labeling/qgslabellinesettings.cpp
@@ -0,0 +1,34 @@
/***************************************************************************
qgslabellinesettings.cpp
----------------------------
Date : August 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslabellinesettings.h"
#include "qgspropertycollection.h"
#include "qgsexpressioncontext.h"
#include "qgslabelingengine.h"
#include "qgspallabeling.h"


void QgsLabelLineSettings::updateDataDefinedProperties( const QgsPropertyCollection &properties, QgsExpressionContext &context )
{
if ( properties.isActive( QgsPalLayerSettings::LinePlacementOptions ) )
{
context.setOriginalValueVariable( QgsLabelingUtils::encodeLinePlacementFlags( mPlacementFlags ) );
const QString dataDefinedLineArrangement = properties.valueAsString( QgsPalLayerSettings::LinePlacementOptions, context );
if ( !dataDefinedLineArrangement.isEmpty() )
{
mPlacementFlags = QgsLabelingUtils::decodeLinePlacementFlags( dataDefinedLineArrangement );
}
}
}
67 changes: 67 additions & 0 deletions src/core/labeling/qgslabellinesettings.h
@@ -0,0 +1,67 @@
/***************************************************************************
qgslabellinesettings.h
--------------------------
Date : August 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLABELLINESETTINGS_H
#define QGSLABELLINESETTINGS_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgslabeling.h"

class QgsPropertyCollection;
class QgsExpressionContext;

/**
* \ingroup core
* \class QgsLabelLineSettings
*
* Contains settings related to how the label engine places and formats
* labels for line features (or polygon features which are labeled in
* a "perimeter" style mode).
*
* \since QGIS 3.16
*/
class CORE_EXPORT QgsLabelLineSettings
{
public:

/**
* Returns the line placement flags, which dictate how line labels can be placed
* above or below the lines.
*
* \see setPlacementFlags()
*/
QgsLabeling::LinePlacementFlags placementFlags() const { return mPlacementFlags; }

/**
* Returns the line placement \a flags, which dictate how line labels can be placed
* above or below the lines.
*
* \see placementFlags()
*/
void setPlacementFlags( QgsLabeling::LinePlacementFlags flags ) { mPlacementFlags = flags; }

/**
* Updates the thinning settings to respect any data defined properties
* set within the specified \a properties collection.
*/
void updateDataDefinedProperties( const QgsPropertyCollection &properties, QgsExpressionContext &context );

private:
QgsLabeling::LinePlacementFlags mPlacementFlags = QgsLabeling::LinePlacementFlag::AboveLine | QgsLabeling::LinePlacementFlag::MapOrientation;

};

#endif // QGSLABELLINESETTINGS_H
22 changes: 7 additions & 15 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -309,7 +309,6 @@ QgsPalLayerSettings &QgsPalLayerSettings::operator=( const QgsPalLayerSettings &

// placement
placement = s.placement;
placementFlags = s.placementFlags;
mPolygonPlacementFlags = s.mPolygonPlacementFlags;
centroidWhole = s.centroidWhole;
centroidInside = s.centroidInside;
Expand Down Expand Up @@ -355,6 +354,7 @@ QgsPalLayerSettings &QgsPalLayerSettings::operator=( const QgsPalLayerSettings &

mCallout.reset( s.mCallout ? s.mCallout->clone() : nullptr );

mLineSettings = s.mLineSettings;
mObstacleSettings = s.mObstacleSettings;
mThinningSettings = s.mThinningSettings;

Expand Down Expand Up @@ -727,7 +727,7 @@ void QgsPalLayerSettings::readFromLayerCustomProperties( QgsVectorLayer *layer )

// placement
placement = static_cast< Placement >( layer->customProperty( QStringLiteral( "labeling/placement" ) ).toInt() );
placementFlags = layer->customProperty( QStringLiteral( "labeling/placementFlags" ) ).toUInt();
mLineSettings.setPlacementFlags( static_cast< QgsLabeling::LinePlacementFlags >( layer->customProperty( QStringLiteral( "labeling/placementFlags" ) ).toUInt() ) );
centroidWhole = layer->customProperty( QStringLiteral( "labeling/centroidWhole" ), QVariant( false ) ).toBool();
centroidInside = layer->customProperty( QStringLiteral( "labeling/centroidInside" ), QVariant( false ) ).toBool();
predefinedPositionOrder = QgsLabelingUtils::decodePredefinedPositionOrder( layer->customProperty( QStringLiteral( "labeling/predefinedPositionOrder" ) ).toString() );
Expand Down Expand Up @@ -939,7 +939,7 @@ void QgsPalLayerSettings::readXml( const QDomElement &elem, const QgsReadWriteCo
// placement
QDomElement placementElem = elem.firstChildElement( QStringLiteral( "placement" ) );
placement = static_cast< Placement >( placementElem.attribute( QStringLiteral( "placement" ) ).toInt() );
placementFlags = placementElem.attribute( QStringLiteral( "placementFlags" ) ).toUInt();
mLineSettings.setPlacementFlags( static_cast< QgsLabeling::LinePlacementFlags >( placementElem.attribute( QStringLiteral( "placementFlags" ) ).toUInt() ) );
mPolygonPlacementFlags = static_cast< QgsLabeling::PolygonPlacementFlags >( placementElem.attribute( QStringLiteral( "polygonPlacementFlags" ), QString::number( static_cast< int >( QgsLabeling::PolygonPlacementFlag::AllowPlacementInsideOfPolygon ) ) ).toInt() );

centroidWhole = placementElem.attribute( QStringLiteral( "centroidWhole" ), QStringLiteral( "0" ) ).toInt();
Expand Down Expand Up @@ -1176,7 +1176,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc, const QgsReadWrite
QDomElement placementElem = doc.createElement( QStringLiteral( "placement" ) );
placementElem.setAttribute( QStringLiteral( "placement" ), placement );
placementElem.setAttribute( QStringLiteral( "polygonPlacementFlags" ), static_cast< int >( mPolygonPlacementFlags ) );
placementElem.setAttribute( QStringLiteral( "placementFlags" ), static_cast< unsigned int >( placementFlags ) );
placementElem.setAttribute( QStringLiteral( "placementFlags" ), static_cast< unsigned int >( mLineSettings.placementFlags() ) );
placementElem.setAttribute( QStringLiteral( "centroidWhole" ), centroidWhole );
placementElem.setAttribute( QStringLiteral( "centroidInside" ), centroidInside );
placementElem.setAttribute( QStringLiteral( "predefinedPositionOrder" ), QgsLabelingUtils::encodePredefinedPositionOrder( predefinedPositionOrder ) );
Expand Down Expand Up @@ -2561,17 +2561,9 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
( *labelFeature )->setHasFixedQuadrant( true );
}

QgsLabeling::LinePlacementFlags featureArrangementFlags = static_cast< QgsLabeling::LinePlacementFlags >( placementFlags );
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::LinePlacementOptions ) )
{
context.expressionContext().setOriginalValueVariable( QgsLabelingUtils::encodeLinePlacementFlags( featureArrangementFlags ) );
const QString dataDefinedLineArrangement = mDataDefinedProperties.valueAsString( QgsPalLayerSettings::LinePlacementOptions, context.expressionContext() );
if ( !dataDefinedLineArrangement.isEmpty() )
{
featureArrangementFlags = QgsLabelingUtils::decodeLinePlacementFlags( dataDefinedLineArrangement );
}
}
( *labelFeature )->setArrangementFlags( featureArrangementFlags );
QgsLabelLineSettings lineSettings = mLineSettings;
lineSettings.updateDataDefinedProperties( mDataDefinedProperties, context.expressionContext() );
( *labelFeature )->setArrangementFlags( lineSettings.placementFlags() );

( *labelFeature )->setPolygonPlacementFlags( polygonPlacement );

Expand Down

0 comments on commit 23848e0

Please sign in to comment.