Skip to content

Commit

Permalink
Create QgsLabeling class for labeling related enums and constants,
Browse files Browse the repository at this point in the history
and avoid inclusion of pal header in a public QGIS header

pal is an internal implementation detail -- it should not be exposed
publicly
  • Loading branch information
nyalldawson committed Jan 15, 2020
1 parent 15a6363 commit 0cd9557
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 78 deletions.
8 changes: 8 additions & 0 deletions python/core/auto_additions/qgslabeling.py
@@ -0,0 +1,8 @@
# The following has been generated automatically from src/core/labeling/qgslabeling.h
# monkey patching scoped based enum
QgsLabeling.LinePlacementFlag.OnLine.__doc__ = "Labels can be placed directly over a line feature."
QgsLabeling.LinePlacementFlag.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.LinePlacementFlag.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.LinePlacementFlag.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."
QgsLabeling.LinePlacementFlag.__doc__ = 'Line placement flags, which control how candidates are generated for a linear feature.\n\n' + '* ``OnLine``: ' + QgsLabeling.LinePlacementFlag.OnLine.__doc__ + '\n' + '* ``AboveLine``: ' + QgsLabeling.LinePlacementFlag.AboveLine.__doc__ + '\n' + '* ``BelowLine``: ' + QgsLabeling.LinePlacementFlag.BelowLine.__doc__ + '\n' + '* ``MapOrientation``: ' + QgsLabeling.LinePlacementFlag.MapOrientation.__doc__
# --
46 changes: 46 additions & 0 deletions python/core/auto_generated/labeling/qgslabeling.sip.in
@@ -0,0 +1,46 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/labeling/qgslabeling.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsLabeling
{
%Docstring

Contains constants and enums relating to labeling.

.. versionadded:: 3.12
%End

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

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


};

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


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/labeling/qgslabeling.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -312,6 +312,7 @@
%Include auto_generated/gps/qgsgpsdconnection.sip
%Include auto_generated/gps/qgsgpsdetector.sip
%Include auto_generated/gps/qgsnmeaconnection.sip
%Include auto_generated/labeling/qgslabeling.sip
%Include auto_generated/labeling/qgslabelingenginesettings.sip
%Include auto_generated/labeling/qgslabelobstaclesettings.sip
%Include auto_generated/labeling/qgslabelsearchtree.sip
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -1040,6 +1040,7 @@ SET(QGIS_CORE_HDRS
gps/qgsnmeaconnection.h

labeling/qgslabelfeature.h
labeling/qgslabeling.h
labeling/qgslabelingengine.h
labeling/qgslabelingenginesettings.h
labeling/qgslabelobstaclesettings.h
Expand Down
10 changes: 0 additions & 10 deletions src/core/labeling/qgslabelfeature.cpp
Expand Up @@ -21,16 +21,6 @@ QgsLabelFeature::QgsLabelFeature( QgsFeatureId id, geos::unique_ptr geometry, QS
: mId( id )
, mGeometry( std::move( geometry ) )
, mSize( size )
, mPriority( -1 )
, mZIndex( 0 )
, mHasFixedPosition( false )
, mHasFixedAngle( false )
, mFixedAngle( 0 )
, mHasFixedQuadrant( false )
, mDistLabel( 0 )
, mOffsetType( QgsPalLayerSettings::FromPoint )
, mRepeatDistance( 0 )
, mAlwaysShow( false )
{
}

Expand Down
29 changes: 15 additions & 14 deletions src/core/labeling/qgslabelfeature.h
Expand Up @@ -23,11 +23,12 @@
#include "qgsgeos.h"
#include "qgsmargins.h"
#include "qgslabelobstaclesettings.h"
#include "pal.h"
#include "qgslabeling.h"

namespace pal
{
class LabelInfo;
class Layer;
}

class QgsAbstractLabelProvider;
Expand Down Expand Up @@ -308,14 +309,14 @@ class CORE_EXPORT QgsLabelFeature
* Returns the feature's arrangement flags.
* \see setArrangementFlags
*/
pal::LineArrangementFlags arrangementFlags() const { return mArrangementFlags; }
QgsLabeling::LinePlacementFlags arrangementFlags() const { return mArrangementFlags; }

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


/**
Expand Down Expand Up @@ -470,33 +471,33 @@ class CORE_EXPORT QgsLabelFeature
//! Size of associated rendered symbol, if applicable
QSizeF mSymbolSize;
//! Priority of the label
double mPriority;
double mPriority = -1;
//! Z-index of label (higher z-index labels are rendered on top of lower z-index labels)
double mZIndex;
double mZIndex = 0;
//! whether mFixedPosition should be respected
bool mHasFixedPosition;
bool mHasFixedPosition = false;
//! fixed position for the label (instead of automatic placement)
QgsPointXY mFixedPosition;
//! whether mFixedAngle should be respected
bool mHasFixedAngle;
bool mHasFixedAngle = false;
//! fixed rotation for the label (instead of automatic choice)
double mFixedAngle;
double mFixedAngle = 0;
//! whether mQuadOffset should be respected (only for "around point" placement)
bool mHasFixedQuadrant;
bool mHasFixedQuadrant = false;
//! whether the side of the label is fixed (only for "around point" placement)
QPointF mQuadOffset;
//! offset of label from the feature (only for "offset from point" placement)
QgsPointXY mPositionOffset;
//! distance of label from the feature (only for "around point" placement or linestrings)
double mDistLabel;
double mDistLabel = 0;
//! Offset type for certain placement modes
QgsPalLayerSettings::OffsetType mOffsetType;
QgsPalLayerSettings::OffsetType mOffsetType = QgsPalLayerSettings::FromPoint;
//! Ordered list of predefined positions for label (only for OrderedPositionsAroundPoint placement)
QVector< QgsPalLayerSettings::PredefinedPointPosition > mPredefinedPositionOrder;
//! distance after which label should be repeated (only for linestrings)
double mRepeatDistance;
double mRepeatDistance = 0;
//! whether to always show label - even in case of collisions
bool mAlwaysShow;
bool mAlwaysShow = false;
//! text of the label
QString mLabelText;
//! extra information for curved labels (may be NULLPTR)
Expand All @@ -507,7 +508,7 @@ class CORE_EXPORT QgsLabelFeature
//! Distance to smooth angle of line start and end when calculating overruns
double mOverrunSmoothDistance = 0;

pal::LineArrangementFlags mArrangementFlags = nullptr;
QgsLabeling::LinePlacementFlags mArrangementFlags = nullptr;

private:

Expand Down
51 changes: 51 additions & 0 deletions src/core/labeling/qgslabeling.h
@@ -0,0 +1,51 @@
/***************************************************************************
qgslabeling.h
--------------------------
Date : January 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 QGSLABELING_H
#define QGSLABELING_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include <QFlags>

/**
* \ingroup core
* \class QgsLabeling
*
* Contains constants and enums relating to labeling.
*
* \since QGIS 3.12
*/
class CORE_EXPORT QgsLabeling
{
public:

/**
* Line placement flags, which control how candidates are generated for a linear feature.
*/
enum class LinePlacementFlag : int
{
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 )

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabeling::LinePlacementFlags )

#endif // QGSLABELING_H
22 changes: 11 additions & 11 deletions src/core/labeling/qgslabelingengine.cpp
Expand Up @@ -737,39 +737,39 @@ QVector<QgsPalLayerSettings::PredefinedPointPosition> QgsLabelingUtils::decodePr
return result;
}

QString QgsLabelingUtils::encodeLinePlacementFlags( pal::LineArrangementFlags flags )
QString QgsLabelingUtils::encodeLinePlacementFlags( QgsLabeling::LinePlacementFlags flags )
{
QStringList parts;
if ( flags & pal::FLAG_ON_LINE )
if ( flags & QgsLabeling::LinePlacementFlag::OnLine )
parts << QStringLiteral( "OL" );
if ( flags & pal::FLAG_ABOVE_LINE )
if ( flags & QgsLabeling::LinePlacementFlag::AboveLine )
parts << QStringLiteral( "AL" );
if ( flags & pal::FLAG_BELOW_LINE )
if ( flags & QgsLabeling::LinePlacementFlag::BelowLine )
parts << QStringLiteral( "BL" );
if ( !( flags & pal::FLAG_MAP_ORIENTATION ) )
if ( !( flags & QgsLabeling::LinePlacementFlag::MapOrientation ) )
parts << QStringLiteral( "LO" );
return parts.join( ',' );
}

pal::LineArrangementFlags QgsLabelingUtils::decodeLinePlacementFlags( const QString &string )
QgsLabeling::LinePlacementFlags QgsLabelingUtils::decodeLinePlacementFlags( const QString &string )
{
pal::LineArrangementFlags flags = nullptr;
QgsLabeling::LinePlacementFlags flags = nullptr;
const QStringList flagList = string.split( ',' );
bool foundLineOrientationFlag = false;
for ( const QString &flag : flagList )
{
QString cleaned = flag.trimmed().toUpper();
if ( cleaned == QLatin1String( "OL" ) )
flags |= pal::FLAG_ON_LINE;
flags |= QgsLabeling::LinePlacementFlag::OnLine;
else if ( cleaned == QLatin1String( "AL" ) )
flags |= pal::FLAG_ABOVE_LINE;
flags |= QgsLabeling::LinePlacementFlag::AboveLine;
else if ( cleaned == QLatin1String( "BL" ) )
flags |= pal::FLAG_BELOW_LINE;
flags |= QgsLabeling::LinePlacementFlag::BelowLine;
else if ( cleaned == QLatin1String( "LO" ) )
foundLineOrientationFlag = true;
}
if ( !foundLineOrientationFlag )
flags |= pal::FLAG_MAP_ORIENTATION;
flags |= QgsLabeling::LinePlacementFlag::MapOrientation;
return flags;
}

10 changes: 7 additions & 3 deletions src/core/labeling/qgslabelingengine.h
Expand Up @@ -23,10 +23,14 @@

#include "qgspallabeling.h"
#include "qgslabelingenginesettings.h"
#include "pal.h"
#include "qgslabeling.h"

class QgsLabelingEngine;

namespace pal
{
class Problem;
}

/**
* \ingroup core
Expand Down Expand Up @@ -414,13 +418,13 @@ class CORE_EXPORT QgsLabelingUtils
* Encodes line placement \a flags to a string.
* \see decodeLinePlacementFlags()
*/
static QString encodeLinePlacementFlags( pal::LineArrangementFlags flags );
static QString encodeLinePlacementFlags( QgsLabeling::LinePlacementFlags flags );

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

};

Expand Down
2 changes: 1 addition & 1 deletion src/core/labeling/qgspallabeling.cpp
Expand Up @@ -2434,7 +2434,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
( *labelFeature )->setHasFixedQuadrant( true );
}

pal::LineArrangementFlags featureArrangementFlags = static_cast< pal::LineArrangementFlags >( placementFlags );
QgsLabeling::LinePlacementFlags featureArrangementFlags = static_cast< QgsLabeling::LinePlacementFlags >( placementFlags );
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::LinePlacementOptions ) )
{
context.expressionContext().setOriginalValueVariable( QgsLabelingUtils::encodeLinePlacementFlags( featureArrangementFlags ) );
Expand Down
3 changes: 1 addition & 2 deletions src/core/labeling/qgspallabeling.h
Expand Up @@ -256,8 +256,7 @@ class CORE_EXPORT QgsPalLayerSettings
FromSymbolBounds, //!< Offset distance applies from rendered symbol bounds
};

//TODO QGIS 4.0 - move to QgsLabelingEngine, rename to LinePlacementFlag, use Q_DECLARE_FLAGS to make
//LinePlacementFlags type, and replace use of pal::LineArrangementFlag
//TODO QGIS 4.0 - remove, replaced by QgsLabeling::LinePlacementFlags

/**
* Line placement flags, which control how candidates are generated for a linear feature.
Expand Down

0 comments on commit 0cd9557

Please sign in to comment.