Skip to content

Commit

Permalink
Promote some label flags to enum class, move to Qgis
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 31, 2023
1 parent fd7b2fb commit 2b29758
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 194 deletions.
33 changes: 33 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -904,6 +904,39 @@
Qgis.LabelQuadrantPosition.__doc__ = 'Label quadrant positions\n\n.. note::\n\n Prior to QGIS 3.26 this was available as :py:class:`QgsPalLayerSettings`.QuadrantPosition\n\n.. versionadded:: 3.26\n\n' + '* ``QuadrantAboveLeft``: ' + Qgis.LabelQuadrantPosition.AboveLeft.__doc__ + '\n' + '* ``QuadrantAbove``: ' + Qgis.LabelQuadrantPosition.Above.__doc__ + '\n' + '* ``QuadrantAboveRight``: ' + Qgis.LabelQuadrantPosition.AboveRight.__doc__ + '\n' + '* ``QuadrantLeft``: ' + Qgis.LabelQuadrantPosition.Left.__doc__ + '\n' + '* ``QuadrantOver``: ' + Qgis.LabelQuadrantPosition.Over.__doc__ + '\n' + '* ``QuadrantRight``: ' + Qgis.LabelQuadrantPosition.Right.__doc__ + '\n' + '* ``QuadrantBelowLeft``: ' + Qgis.LabelQuadrantPosition.BelowLeft.__doc__ + '\n' + '* ``QuadrantBelow``: ' + Qgis.LabelQuadrantPosition.Below.__doc__ + '\n' + '* ``QuadrantBelowRight``: ' + Qgis.LabelQuadrantPosition.BelowRight.__doc__
# --
Qgis.LabelQuadrantPosition.baseClass = Qgis
QgsLabeling.LinePlacementFlag = Qgis.LabelLinePlacementFlag
# monkey patching scoped based enum
QgsLabeling.OnLine = Qgis.LabelLinePlacementFlag.OnLine
QgsLabeling.OnLine.is_monkey_patched = True
QgsLabeling.OnLine.__doc__ = "Labels can be placed directly over a line feature."
QgsLabeling.AboveLine = Qgis.LabelLinePlacementFlag.AboveLine
QgsLabeling.AboveLine.is_monkey_patched = True
QgsLabeling.AboveLine.__doc__ = "Labels can be placed above a line feature. Unless MapOrientation is also specified this mode respects the direction of the line feature, so a line from right to left labels will have labels placed placed below the line feature."
QgsLabeling.BelowLine = Qgis.LabelLinePlacementFlag.BelowLine
QgsLabeling.BelowLine.is_monkey_patched = True
QgsLabeling.BelowLine.__doc__ = "Labels can be placed below a line feature. Unless MapOrientation is also specified this mode respects the direction of the line feature, so a line from right to left labels will have labels placed placed above the line feature."
QgsLabeling.MapOrientation = Qgis.LabelLinePlacementFlag.MapOrientation
QgsLabeling.MapOrientation.is_monkey_patched = True
QgsLabeling.MapOrientation.__doc__ = "Signifies that the AboveLine and BelowLine flags should respect the map's orientation rather than the feature's orientation. For example, AboveLine will always result in label's being placed above a line, regardless of the line's direction."
Qgis.LabelLinePlacementFlag.__doc__ = 'Line placement flags, which control how candidates are generated for a linear feature.\n\n.. note::\n\n Prior to QGIS 3.32 this was available as :py:class:`QgsLabeling`.LinePlacementFlag\n\n.. versionadded:: 3.32\n\n' + '* ``OnLine``: ' + Qgis.LabelLinePlacementFlag.OnLine.__doc__ + '\n' + '* ``AboveLine``: ' + Qgis.LabelLinePlacementFlag.AboveLine.__doc__ + '\n' + '* ``BelowLine``: ' + Qgis.LabelLinePlacementFlag.BelowLine.__doc__ + '\n' + '* ``MapOrientation``: ' + Qgis.LabelLinePlacementFlag.MapOrientation.__doc__
# --
Qgis.LabelLinePlacementFlag.baseClass = Qgis
QgsLabeling.LinePlacementFlags = Qgis.LabelLinePlacementFlags
Qgis.LabelLinePlacementFlags.baseClass = Qgis
LabelLinePlacementFlags = Qgis # dirty hack since SIP seems to introduce the flags in module
QgsLabeling.PolygonPlacementFlag = Qgis.LabelPolygonPlacementFlag
# monkey patching scoped based enum
QgsLabeling.AllowPlacementOutsideOfPolygon = Qgis.LabelPolygonPlacementFlag.AllowPlacementOutsideOfPolygon
QgsLabeling.AllowPlacementOutsideOfPolygon.is_monkey_patched = True
QgsLabeling.AllowPlacementOutsideOfPolygon.__doc__ = "Labels can be placed outside of a polygon feature"
QgsLabeling.AllowPlacementInsideOfPolygon = Qgis.LabelPolygonPlacementFlag.AllowPlacementInsideOfPolygon
QgsLabeling.AllowPlacementInsideOfPolygon.is_monkey_patched = True
QgsLabeling.AllowPlacementInsideOfPolygon.__doc__ = "Labels can be placed inside a polygon feature"
Qgis.LabelPolygonPlacementFlag.__doc__ = 'Polygon placement flags, which control how candidates are generated for a polygon feature.\n\n.. note::\n\n Prior to QGIS 3.32 this was available as :py:class:`QgsLabeling`.PolygonPlacementFlag\n\n.. versionadded:: 3.32\n\n' + '* ``AllowPlacementOutsideOfPolygon``: ' + Qgis.LabelPolygonPlacementFlag.AllowPlacementOutsideOfPolygon.__doc__ + '\n' + '* ``AllowPlacementInsideOfPolygon``: ' + Qgis.LabelPolygonPlacementFlag.AllowPlacementInsideOfPolygon.__doc__
# --
Qgis.LabelPolygonPlacementFlag.baseClass = Qgis
Qgis.LabelPolygonPlacementFlags.baseClass = Qgis
LabelPolygonPlacementFlags = Qgis # dirty hack since SIP seems to introduce the flags in module
QgsPalLayerSettings.UpsideDownLabels = Qgis.UpsideDownLabelHandling
# monkey patching scoped based enum
QgsPalLayerSettings.Upright = Qgis.UpsideDownLabelHandling.FlipUpsideDownLabels
Expand Down
21 changes: 0 additions & 21 deletions python/core/auto_generated/labeling/qgslabeling.sip.in
Expand Up @@ -22,30 +22,9 @@ Contains constants and enums relating to labeling.
%End
public:

enum LinePlacementFlag
{
OnLine,
AboveLine,
BelowLine,
MapOrientation,
};
typedef QFlags<QgsLabeling::LinePlacementFlag> LinePlacementFlags;


enum PolygonPlacementFlag
{
AllowPlacementOutsideOfPolygon,
AllowPlacementInsideOfPolygon,
};
typedef QFlags<QgsLabeling::PolygonPlacementFlag> PolygonPlacementFlags;


};

QFlags<QgsLabeling::LinePlacementFlag> operator|(QgsLabeling::LinePlacementFlag f1, QFlags<QgsLabeling::LinePlacementFlag> f2);

QFlags<QgsLabeling::PolygonPlacementFlag> operator|(QgsLabeling::PolygonPlacementFlag f1, QFlags<QgsLabeling::PolygonPlacementFlag> f2);


/************************************************************************
* This file has been generated automatically from *
Expand Down
Expand Up @@ -55,15 +55,15 @@ a "perimeter" style mode).
FollowPlacement,
};

QgsLabeling::LinePlacementFlags placementFlags() const;
Qgis::LabelLinePlacementFlags 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 );
void setPlacementFlags( Qgis::LabelLinePlacementFlags flags );
%Docstring
Returns the line placement ``flags``, which dictate how line labels can be placed
above or below the lines.
Expand Down
4 changes: 2 additions & 2 deletions python/core/auto_generated/labeling/qgspallabeling.sip.in
Expand Up @@ -281,7 +281,7 @@ Returns the :py:class:`QgsExpression` for this label settings. May be ``None`` i

Qgis::LabelPlacement placement;

QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const;
Qgis::LabelPolygonPlacementFlags polygonPlacementFlags() const;
%Docstring
Returns the polygon placement flags, which dictate how polygon labels can be placed.

Expand All @@ -290,7 +290,7 @@ Returns the polygon placement flags, which dictate how polygon labels can be pla
.. versionadded:: 3.14
%End

void setPolygonPlacementFlags( QgsLabeling::PolygonPlacementFlags flags );
void setPolygonPlacementFlags( Qgis::LabelPolygonPlacementFlags flags );
%Docstring
Sets the polygon placement ``flags``, which dictate how polygon labels can be placed.

Expand Down
24 changes: 23 additions & 1 deletion python/core/auto_generated/qgis.sip.in
Expand Up @@ -471,7 +471,6 @@ The development version
OutsidePolygons,
};


enum class LabelPredefinedPointPosition
{
TopLeft,
Expand Down Expand Up @@ -507,6 +506,26 @@ The development version
BelowRight,
};

enum class LabelLinePlacementFlag
{
OnLine,
AboveLine,
BelowLine,
MapOrientation,
};

typedef QFlags<Qgis::LabelLinePlacementFlag> LabelLinePlacementFlags;


enum class LabelPolygonPlacementFlag
{
AllowPlacementOutsideOfPolygon,
AllowPlacementInsideOfPolygon,
};

typedef QFlags<Qgis::LabelPolygonPlacementFlag> LabelPolygonPlacementFlags;


enum class UpsideDownLabelHandling
{
FlipUpsideDownLabels,
Expand Down Expand Up @@ -2041,6 +2060,9 @@ QFlags<Qgis::ScriptLanguageCapability> operator|(Qgis::ScriptLanguageCapability

QFlags<Qgis::LayerTreeFilterFlag> operator|(Qgis::LayerTreeFilterFlag f1, QFlags<Qgis::LayerTreeFilterFlag> f2);

QFlags<Qgis::LabelLinePlacementFlag> operator|(Qgis::LabelLinePlacementFlag f1, QFlags<Qgis::LabelLinePlacementFlag> f2);

QFlags<Qgis::LabelPolygonPlacementFlag> operator|(Qgis::LabelPolygonPlacementFlag f1, QFlags<Qgis::LabelPolygonPlacementFlag> f2);



Expand Down
13 changes: 6 additions & 7 deletions src/core/labeling/qgslabelfeature.h
Expand Up @@ -23,7 +23,6 @@
#include "qgsmargins.h"
#include "qgslabelobstaclesettings.h"
#include "qgslabellinesettings.h"
#include "qgslabeling.h"
#include "qgsfeature.h"
#include "qgscoordinatereferencesystem.h"

Expand Down Expand Up @@ -338,30 +337,30 @@ class CORE_EXPORT QgsLabelFeature
* Returns the feature's arrangement flags.
* \see setArrangementFlags
*/
QgsLabeling::LinePlacementFlags arrangementFlags() const { return mArrangementFlags; }
Qgis::LabelLinePlacementFlags arrangementFlags() const { return mArrangementFlags; }

/**
* Sets the feature's arrangement flags.
* \param flags arrangement flags
* \see arrangementFlags
*/
void setArrangementFlags( QgsLabeling::LinePlacementFlags flags ) { mArrangementFlags = flags; }
void setArrangementFlags( Qgis::LabelLinePlacementFlags flags ) { mArrangementFlags = flags; }

/**
* Returns the polygon placement flags, which dictate how polygon labels can be placed.
*
* \see setPolygonPlacementFlags()
* \since QGIS 3.14
*/
QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const { return mPolygonPlacementFlags; }
Qgis::LabelPolygonPlacementFlags polygonPlacementFlags() const { return mPolygonPlacementFlags; }

/**
* Sets the polygon placement \a flags, which dictate how polygon labels can be placed.
*
* \see polygonPlacementFlags()
* \since QGIS 3.14
*/
void setPolygonPlacementFlags( QgsLabeling::PolygonPlacementFlags flags ) { mPolygonPlacementFlags = flags; }
void setPolygonPlacementFlags( Qgis::LabelPolygonPlacementFlags flags ) { mPolygonPlacementFlags = flags; }

/**
* Text of the label
Expand Down Expand Up @@ -687,8 +686,8 @@ class CORE_EXPORT QgsLabelFeature
//! Distance to smooth angle of line start and end when calculating overruns
double mOverrunSmoothDistance = 0;

QgsLabeling::LinePlacementFlags mArrangementFlags = QgsLabeling::LinePlacementFlags();
QgsLabeling::PolygonPlacementFlags mPolygonPlacementFlags = QgsLabeling::PolygonPlacementFlag::AllowPlacementInsideOfPolygon;
Qgis::LabelLinePlacementFlags mArrangementFlags = Qgis::LabelLinePlacementFlags();
Qgis::LabelPolygonPlacementFlags mPolygonPlacementFlags = Qgis::LabelPolygonPlacementFlag::AllowPlacementInsideOfPolygon;

private:

Expand Down
25 changes: 0 additions & 25 deletions src/core/labeling/qgslabeling.h
Expand Up @@ -32,33 +32,8 @@ class CORE_EXPORT QgsLabeling
{
public:

/**
* Line placement flags, which control how candidates are generated for a linear feature.
*/
enum LinePlacementFlag
{
OnLine = 1, //!< Labels can be placed directly over a line feature.
AboveLine = 2, //!< Labels can be placed above a line feature. Unless MapOrientation is also specified this mode respects the direction of the line feature, so a line from right to left labels will have labels placed placed below the line feature.
BelowLine = 4, //!< Labels can be placed below a line feature. Unless MapOrientation is also specified this mode respects the direction of the line feature, so a line from right to left labels will have labels placed placed above the line feature.
MapOrientation = 8, //!< Signifies that the AboveLine and BelowLine flags should respect the map's orientation rather than the feature's orientation. For example, AboveLine will always result in label's being placed above a line, regardless of the line's direction.
};
Q_DECLARE_FLAGS( LinePlacementFlags, LinePlacementFlag )

/**
* Polygon placement flags, which control how candidates are generated for a polygon feature.
*
* \since QGIS 3.14
*/
enum PolygonPlacementFlag
{
AllowPlacementOutsideOfPolygon = 1 << 0, //!< Labels can be placed outside of a polygon feature
AllowPlacementInsideOfPolygon = 1 << 1, //!< Labels can be placed inside a polygon feature
};
Q_DECLARE_FLAGS( PolygonPlacementFlags, PolygonPlacementFlag )

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabeling::LinePlacementFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabeling::PolygonPlacementFlags )

#endif // QGSLABELING_H
22 changes: 11 additions & 11 deletions src/core/labeling/qgslabelingengine.cpp
Expand Up @@ -768,38 +768,38 @@ QVector<Qgis::LabelPredefinedPointPosition> QgsLabelingUtils::decodePredefinedPo
return result;
}

QString QgsLabelingUtils::encodeLinePlacementFlags( QgsLabeling::LinePlacementFlags flags )
QString QgsLabelingUtils::encodeLinePlacementFlags( Qgis::LabelLinePlacementFlags flags )
{
QStringList parts;
if ( flags & QgsLabeling::LinePlacementFlag::OnLine )
if ( flags & Qgis::LabelLinePlacementFlag::OnLine )
parts << QStringLiteral( "OL" );
if ( flags & QgsLabeling::LinePlacementFlag::AboveLine )
if ( flags & Qgis::LabelLinePlacementFlag::AboveLine )
parts << QStringLiteral( "AL" );
if ( flags & QgsLabeling::LinePlacementFlag::BelowLine )
if ( flags & Qgis::LabelLinePlacementFlag::BelowLine )
parts << QStringLiteral( "BL" );
if ( !( flags & QgsLabeling::LinePlacementFlag::MapOrientation ) )
if ( !( flags & Qgis::LabelLinePlacementFlag::MapOrientation ) )
parts << QStringLiteral( "LO" );
return parts.join( ',' );
}

QgsLabeling::LinePlacementFlags QgsLabelingUtils::decodeLinePlacementFlags( const QString &string )
Qgis::LabelLinePlacementFlags QgsLabelingUtils::decodeLinePlacementFlags( const QString &string )
{
QgsLabeling::LinePlacementFlags flags = QgsLabeling::LinePlacementFlags();
Qgis::LabelLinePlacementFlags flags = Qgis::LabelLinePlacementFlags();
const QStringList flagList = string.split( ',' );
bool foundLineOrientationFlag = false;
for ( const QString &flag : flagList )
{
QString cleaned = flag.trimmed().toUpper();
if ( cleaned == QLatin1String( "OL" ) )
flags |= QgsLabeling::LinePlacementFlag::OnLine;
flags |= Qgis::LabelLinePlacementFlag::OnLine;
else if ( cleaned == QLatin1String( "AL" ) )
flags |= QgsLabeling::LinePlacementFlag::AboveLine;
flags |= Qgis::LabelLinePlacementFlag::AboveLine;
else if ( cleaned == QLatin1String( "BL" ) )
flags |= QgsLabeling::LinePlacementFlag::BelowLine;
flags |= Qgis::LabelLinePlacementFlag::BelowLine;
else if ( cleaned == QLatin1String( "LO" ) )
foundLineOrientationFlag = true;
}
if ( !foundLineOrientationFlag )
flags |= QgsLabeling::LinePlacementFlag::MapOrientation;
flags |= Qgis::LabelLinePlacementFlag::MapOrientation;
return flags;
}
5 changes: 2 additions & 3 deletions src/core/labeling/qgslabelingengine.h
Expand Up @@ -21,7 +21,6 @@
#include "qgis_core.h"
#include "qgsmapsettings.h"

#include "qgslabeling.h"
#include "qgsfeedback.h"
#include "qgslabelobstaclesettings.h"

Expand Down Expand Up @@ -550,13 +549,13 @@ class CORE_EXPORT QgsLabelingUtils
* Encodes line placement \a flags to a string.
* \see decodeLinePlacementFlags()
*/
static QString encodeLinePlacementFlags( QgsLabeling::LinePlacementFlags flags );
static QString encodeLinePlacementFlags( Qgis::LabelLinePlacementFlags flags );

/**
* Decodes a \a string to set of line placement flags.
* \see encodeLinePlacementFlags()
*/
static QgsLabeling::LinePlacementFlags decodeLinePlacementFlags( const QString &string );
static Qgis::LabelLinePlacementFlags decodeLinePlacementFlags( const QString &string );

};

Expand Down
7 changes: 3 additions & 4 deletions src/core/labeling/qgslabellinesettings.h
Expand Up @@ -18,7 +18,6 @@

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgslabeling.h"
#include "qgsmapunitscale.h"
#include <QString>

Expand Down Expand Up @@ -94,15 +93,15 @@ class CORE_EXPORT QgsLabelLineSettings
*
* \see setPlacementFlags()
*/
QgsLabeling::LinePlacementFlags placementFlags() const { return mPlacementFlags; }
Qgis::LabelLinePlacementFlags 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; }
void setPlacementFlags( Qgis::LabelLinePlacementFlags flags ) { mPlacementFlags = flags; }

/**
* Returns TRUE if connected line features with identical label text should be merged
Expand Down Expand Up @@ -365,7 +364,7 @@ class CORE_EXPORT QgsLabelLineSettings
void setAnchorTextPoint( AnchorTextPoint point ) { mAnchorTextPoint = point; }

private:
QgsLabeling::LinePlacementFlags mPlacementFlags = QgsLabeling::LinePlacementFlag::AboveLine | QgsLabeling::LinePlacementFlag::MapOrientation;
Qgis::LabelLinePlacementFlags mPlacementFlags = Qgis::LabelLinePlacementFlag::AboveLine | Qgis::LabelLinePlacementFlag::MapOrientation;
bool mMergeLines = false;
bool mAddDirectionSymbol = false;
QString mLeftDirectionSymbol = QString( '<' );
Expand Down

0 comments on commit 2b29758

Please sign in to comment.