Skip to content

Commit 4cc590c

Browse files
committed
Cleanup labeling, avoid some duplicate enums, add docs
1 parent fe9a098 commit 4cc590c

File tree

10 files changed

+76
-109
lines changed

10 files changed

+76
-109
lines changed

python/core/qgspallabeling.sip

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,32 @@ class QgsPalLayerSettings
9191
//! @note added in 2.4
9292
static QgsPalLayerSettings fromLayer( QgsVectorLayer* layer );
9393

94+
/** Placement modes which determine how label candidates are generated for a feature.
95+
*/
9496
enum Placement
9597
{
96-
AroundPoint, // Point / Polygon
97-
OverPoint, // Point / Polygon
98-
Line, // Line / Polygon
99-
Curved, // Line
100-
Horizontal, // Polygon
101-
Free // Polygon
98+
AroundPoint, /**< Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygon layers only.*/
99+
OverPoint, /** Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point. Applies to point or polygon layers only.*/
100+
Line, /**< Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon's perimeter. Applies to line or polygon layers only. */
101+
Curved, /** Arranges candidates following the curvature of a line feature. Applies to line layers only.*/
102+
Horizontal, /**< Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only.*/
103+
Free /**< Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the polygon's orientation. Applies to polygon layers only.*/
102104
};
103105

106+
/** Line placement flags, which control how candidates are generated for a linear feature.
107+
*/
104108
enum LinePlacementFlags
105109
{
106-
OnLine,
107-
AboveLine,
108-
BelowLine,
109-
MapOrientation,
110+
OnLine, /**< Labels can be placed directly over a line feature.*/
111+
AboveLine, /**< Labels can be placed above a line feature. Unless MapOrientation is also specified this mode
112+
respects the direction of the line feature, so a line from right to left labels will have labels
113+
placed placed below the line feature. */
114+
BelowLine, /**< Labels can be placed below a line feature. Unless MapOrientation is also specified this mode
115+
respects the direction of the line feature, so a line from right to left labels will have labels
116+
placed placed above the line feature. */
117+
MapOrientation, /**< Signifies that the AboveLine and BelowLine flags should respect the map's orientation rather
118+
than the feature's orientation. Eg, AboveLine will always result in label's being placed
119+
above a line, regardless of the line's direction. */
110120
};
111121

112122
enum QuadrantPosition

src/core/pal/costcalculator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ namespace pal
6969
// behaviour depends on obstacle avoid type
7070
switch ( obstacle->layer()->obstacleType() )
7171
{
72-
case PolygonInterior:
72+
case QgsPalLayerSettings::PolygonInterior:
7373
// n ranges from 0 -> 12
7474
n = lp->polygonIntersectionCost( obstacle );
7575
break;
76-
case PolygonBoundary:
76+
case QgsPalLayerSettings::PolygonBoundary:
7777
// penalty may need tweaking, given that interior mode ranges up to 12
7878
n = ( lp->crossesBoundary( obstacle ) ? 6 : 0 );
7979
break;
80-
case PolygonWhole:
80+
case QgsPalLayerSettings::PolygonWhole:
8181
// n is either 0 or 12
8282
n = ( lp->intersectsWithPolygon( obstacle ) ? 12 : 0 );
8383
break;
@@ -232,7 +232,7 @@ namespace pal
232232
if ( feat->feature->getGeosType() == GEOS_POLYGON )
233233
{
234234
int arrangement = feat->feature->layer()->arrangement();
235-
if ( arrangement == P_FREE || arrangement == P_HORIZ )
235+
if ( arrangement == QgsPalLayerSettings::Free || arrangement == QgsPalLayerSettings::Horizontal )
236236
setPolygonCandidatesCost( stop, feat->lPos, obstacles, bbx, bby );
237237
}
238238

src/core/pal/feature.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ namespace pal
267267
}
268268
}
269269

270-
if ( mLF->layer()->arrangement() == P_POINT )
270+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::AroundPoint )
271271
{
272272
//if in "around point" placement mode, then we use the label distance to determine
273273
//the label's offset
@@ -604,7 +604,7 @@ namespace pal
604604
#ifdef _DEBUG_FULL_
605605
std::cout << " Create new label" << std::endl;
606606
#endif
607-
if ( mLF->layer()->arrangement() == P_LINE )
607+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Line )
608608
{
609609
// find out whether the line direction for this candidate is from right to left
610610
bool isRightToLeft = ( alpha > M_PI / 2 || alpha <= -M_PI / 2 );
@@ -630,7 +630,7 @@ namespace pal
630630
positions.append( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
631631
}
632632
}
633-
else if ( mLF->layer()->arrangement() == P_HORIZ )
633+
else if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Horizontal )
634634
{
635635
positions.append( new LabelPosition( i, bx - xrm / 2, by - yrm / 2, xrm, yrm, 0, cost, this ) ); // Line
636636
}
@@ -1087,7 +1087,7 @@ namespace pal
10871087
continue;
10881088
}
10891089

1090-
if ( mLF->layer()->arrangement() == P_HORIZ && mLF->layer()->fitInPolygonOnly() )
1090+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Horizontal && mLF->layer()->fitInPolygonOnly() )
10911091
{
10921092
//check width/height of bbox is sufficient for label
10931093
if ( box->length < labelWidth || box->width < labelHeight )
@@ -1106,7 +1106,7 @@ namespace pal
11061106
#endif
11071107

11081108
bool enoughPlace = false;
1109-
if ( mLF->layer()->arrangement() == P_FREE )
1109+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Free )
11101110
{
11111111
enoughPlace = true;
11121112
px = ( box->x[0] + box->x[2] ) / 2 - labelWidth;
@@ -1134,7 +1134,7 @@ namespace pal
11341134

11351135
} // arrangement== FREE ?
11361136

1137-
if ( mLF->layer()->arrangement() == P_HORIZ || enoughPlace )
1137+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Horizontal || enoughPlace )
11381138
{
11391139
alpha = 0.0; // HORIZ
11401140
}
@@ -1280,13 +1280,13 @@ namespace pal
12801280
switch ( type )
12811281
{
12821282
case GEOS_POINT:
1283-
if ( mLF->layer()->arrangement() == P_POINT_OVER || mLF->hasFixedQuadrant() )
1283+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::OverPoint || mLF->hasFixedQuadrant() )
12841284
setPositionOverPoint( x[0], y[0], lPos, angle );
12851285
else
12861286
setPositionForPoint( x[0], y[0], lPos, angle );
12871287
break;
12881288
case GEOS_LINESTRING:
1289-
if ( mLF->layer()->arrangement() == P_CURVED )
1289+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::Curved )
12901290
setPositionForLineCurved( lPos, mapShape );
12911291
else
12921292
setPositionForLine( lPos, mapShape );
@@ -1295,16 +1295,16 @@ namespace pal
12951295
case GEOS_POLYGON:
12961296
switch ( mLF->layer()->arrangement() )
12971297
{
1298-
case P_POINT:
1299-
case P_POINT_OVER:
1298+
case QgsPalLayerSettings::AroundPoint:
1299+
case QgsPalLayerSettings::OverPoint:
13001300
double cx, cy;
13011301
mapShape->getCentroid( cx, cy, mLF->layer()->centroidInside() );
1302-
if ( mLF->layer()->arrangement() == P_POINT_OVER )
1302+
if ( mLF->layer()->arrangement() == QgsPalLayerSettings::OverPoint )
13031303
setPositionOverPoint( cx, cy, lPos, angle, mapShape );
13041304
else
13051305
setPositionForPoint( cx, cy, lPos, angle, mapShape );
13061306
break;
1307-
case P_LINE:
1307+
case QgsPalLayerSettings::Line:
13081308
setPositionForLine( lPos, mapShape );
13091309
break;
13101310
default:

src/core/pal/labelposition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace pal
101101
y[3] = y1 + dy2;
102102

103103
// upside down ? (curved labels are always correct)
104-
if ( feature->layer()->arrangement() != P_CURVED &&
104+
if ( feature->layer()->arrangement() != QgsPalLayerSettings::Curved &&
105105
this->alpha > M_PI / 2 && this->alpha <= 3*M_PI / 2 )
106106
{
107107
bool uprightLabel = false;

src/core/pal/layer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
namespace pal
4444
{
4545

46-
Layer::Layer( QgsAbstractLabelProvider* provider, const QString& name, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll )
46+
Layer::Layer( QgsAbstractLabelProvider* provider, const QString& name, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll )
4747
: mProvider( provider )
4848
, mName( name )
4949
, pal( pal )
50-
, mObstacleType( PolygonInterior )
50+
, mObstacleType( QgsPalLayerSettings::PolygonInterior )
5151
, mActive( active )
5252
, mLabelLayer( toLabel )
5353
, mDisplayAll( displayAll )

src/core/pal/layer.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ namespace pal
8989
/** Returns the layer's arrangement policy.
9090
* @see setArrangement
9191
*/
92-
Arrangement arrangement() const { return mArrangement; }
92+
QgsPalLayerSettings::Placement arrangement() const { return mArrangement; }
9393

9494
/** Sets the layer's arrangement policy.
9595
* @param arrangement arrangement policy
9696
* @see arrangement
9797
*/
98-
void setArrangement( Arrangement arrangement ) { mArrangement = arrangement; }
98+
void setArrangement( QgsPalLayerSettings::Placement arrangement ) { mArrangement = arrangement; }
9999

100100
/** Returns the layer's arrangement flags.
101101
* @see setArrangementFlags
@@ -142,14 +142,14 @@ namespace pal
142142
* act as obstacles for labels.
143143
* @see setObstacleType
144144
*/
145-
ObstacleType obstacleType() const { return mObstacleType; }
145+
QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
146146

147147
/** Sets the obstacle type, which controls how features within the layer
148148
* act as obstacles for labels.
149149
* @param obstacleType new obstacle type
150150
* @see obstacleType
151151
*/
152-
void setObstacleType( ObstacleType obstacleType ) { mObstacleType = obstacleType; }
152+
void setObstacleType( QgsPalLayerSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
153153

154154
/** Sets the layer's priority.
155155
* @param priority layer priority, between 0 and 1. 0 corresponds to highest priority,
@@ -262,15 +262,15 @@ namespace pal
262262

263263
double mDefaultPriority;
264264

265-
ObstacleType mObstacleType;
265+
QgsPalLayerSettings::ObstacleType mObstacleType;
266266
bool mActive;
267267
bool mLabelLayer;
268268
bool mDisplayAll;
269269
bool mCentroidInside;
270270
bool mFitInPolygon;
271271

272272
/** Optional flags used for some placement methods */
273-
Arrangement mArrangement;
273+
QgsPalLayerSettings::Placement mArrangement;
274274
LineArrangementFlags mArrangementFlags;
275275
LabelMode mMode;
276276
bool mMergeLines;
@@ -304,7 +304,7 @@ namespace pal
304304
* @param displayAll if true, all features will be labelled even though overlaps occur
305305
*
306306
*/
307-
Layer( QgsAbstractLabelProvider* provider, const QString& name, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );
307+
Layer( QgsAbstractLabelProvider* provider, const QString& name, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );
308308

309309
/** Add newly created feature part into r tree and to the list */
310310
void addFeaturePart( FeaturePart* fpart, const QString &labelText = QString() );

src/core/pal/pal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace pal
116116
//finishGEOS();
117117
}
118118

119-
Layer* Pal::addLayer( QgsAbstractLabelProvider* provider, const QString& layerName, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll )
119+
Layer* Pal::addLayer( QgsAbstractLabelProvider* provider, const QString& layerName, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll )
120120
{
121121
mMutex.lock();
122122

src/core/pal/pal.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define PAL_H
3232

3333
#include "qgsgeometry.h"
34+
#include "qgspallabeling.h"
3435
#include <QList>
3536
#include <iostream>
3637
#include <ctime>
@@ -62,20 +63,6 @@ namespace pal
6263
FALP = 4 /** only initial solution */
6364
};
6465

65-
/** The way to arrange labels against spatial entities
66-
*
67-
* image html arrangement.png "Arrangement modes" width=7cm
68-
* */
69-
enum Arrangement
70-
{
71-
P_POINT = 0, /**< arranges candidates around a point (centroid for polygon)*/
72-
P_POINT_OVER, /** arranges candidates over a point (centroid for polygon)*/
73-
P_LINE, /**< Only for lines and polygons, arranges candidates over the line or the polygon perimeter */
74-
P_CURVED, /** Only for lines, labels along the line */
75-
P_HORIZ, /**< Only for polygon, arranges candidates horizontaly */
76-
P_FREE /**< Only for polygon, arranges candidates with respect of polygon orientation */
77-
};
78-
7966
/** Enumeration line arrangement flags. Flags can be combined. */
8067
enum LineArrangementFlag
8168
{
@@ -86,13 +73,6 @@ namespace pal
8673
};
8774
Q_DECLARE_FLAGS( LineArrangementFlags, LineArrangementFlag )
8875

89-
enum ObstacleType
90-
{
91-
PolygonInterior,
92-
PolygonBoundary,
93-
PolygonWhole
94-
};
95-
9676
/**
9777
* \brief Main Pal labelling class
9878
*
@@ -134,7 +114,7 @@ namespace pal
134114
*
135115
* @todo add symbolUnit
136116
*/
137-
Layer* addLayer( QgsAbstractLabelProvider* provider, const QString& layerName, Arrangement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll = false );
117+
Layer* addLayer( QgsAbstractLabelProvider* provider, const QString& layerName, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll = false );
138118

139119
/**
140120
* \brief remove a layer

src/core/qgslabelingenginev2.cpp

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,12 @@ void QgsLabelingEngineV2::removeProvider( QgsAbstractLabelProvider* provider )
6868

6969
void QgsLabelingEngineV2::processProvider( QgsAbstractLabelProvider* provider, QgsRenderContext& context, pal::Pal& p )
7070
{
71-
// how to place the labels
72-
pal::Arrangement arrangement;
73-
switch ( provider->placement() )
74-
{
75-
case QgsPalLayerSettings::AroundPoint:
76-
arrangement = pal::P_POINT;
77-
break;
78-
case QgsPalLayerSettings::OverPoint:
79-
arrangement = pal::P_POINT_OVER;
80-
break;
81-
case QgsPalLayerSettings::Line:
82-
arrangement = pal::P_LINE;
83-
break;
84-
case QgsPalLayerSettings::Curved:
85-
arrangement = pal::P_CURVED;
86-
break;
87-
case QgsPalLayerSettings::Horizontal:
88-
arrangement = pal::P_HORIZ;
89-
break;
90-
case QgsPalLayerSettings::Free:
91-
arrangement = pal::P_FREE;
92-
break;
93-
default:
94-
Q_ASSERT( "unsupported placement" && 0 );
95-
return;
96-
}
97-
9871
QgsAbstractLabelProvider::Flags flags = provider->flags();
9972

10073
// create the pal layer
10174
pal::Layer* l = p.addLayer( provider,
10275
provider->name(),
103-
arrangement,
76+
provider->placement(),
10477
provider->priority(),
10578
true,
10679
flags.testFlag( QgsAbstractLabelProvider::DrawLabels ),
@@ -116,18 +89,7 @@ void QgsLabelingEngineV2::processProvider( QgsAbstractLabelProvider* provider, Q
11689
l->setMergeConnectedLines( flags.testFlag( QgsAbstractLabelProvider::MergeConnectedLines ) );
11790

11891
// set obstacle type
119-
switch ( provider->obstacleType() )
120-
{
121-
case QgsPalLayerSettings::PolygonInterior:
122-
l->setObstacleType( pal::PolygonInterior );
123-
break;
124-
case QgsPalLayerSettings::PolygonBoundary:
125-
l->setObstacleType( pal::PolygonBoundary );
126-
break;
127-
case QgsPalLayerSettings::PolygonWhole:
128-
l->setObstacleType( pal::PolygonWhole );
129-
break;
130-
}
92+
l->setObstacleType( provider->obstacleType() );
13193

13294
// set whether location of centroid must be inside of polygons
13395
l->setCentroidInside( flags.testFlag( QgsAbstractLabelProvider::CentroidMustBeInside ) );

0 commit comments

Comments
 (0)