Skip to content

Commit 5ac6170

Browse files
committedJan 4, 2016
Move QgsLabelFeature and QgsTextLabelFeature class to separate files
No actual code changes This code has been funded by Tuscany Region (Italy) - SITA (CIG: 63526840AE) and commissioned to Gis3W s.a.s.
1 parent 215ba22 commit 5ac6170

13 files changed

+437
-272
lines changed
 

‎src/core/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ SET(QGIS_CORE_SRCS
120120
qgslayerdefinition.cpp
121121
qgslabel.cpp
122122
qgslabelattributes.cpp
123+
qgslabelfeature.cpp
123124
qgslabelingenginev2.cpp
124125
qgslabelsearchtree.cpp
125126
qgslegacyhelpers.cpp
@@ -184,6 +185,7 @@ SET(QGIS_CORE_SRCS
184185
qgsstatisticalsummary.cpp
185186
qgsstringutils.cpp
186187
qgstransaction.cpp
188+
qgstextlabelfeature.cpp
187189
qgstolerance.cpp
188190
qgsvectordataprovider.cpp
189191
qgsvectorfilewriter.cpp
@@ -608,6 +610,7 @@ SET(QGIS_CORE_HDRS
608610
qgslayerdefinition.h
609611
qgslabel.h
610612
qgslabelattributes.h
613+
qgslabelfeature.h
611614
qgslabelingenginev2.h
612615
qgslabelsearchtree.h
613616
qgslegacyhelpers.h
@@ -627,7 +630,6 @@ SET(QGIS_CORE_HDRS
627630
qgsogcutils.h
628631
qgsowsconnection.h
629632
qgspaintenginehack.h
630-
qgspalgeometry.h
631633
qgspallabeling.h
632634
qgspluginlayerregistry.h
633635
qgspoint.h
@@ -655,6 +657,7 @@ SET(QGIS_CORE_HDRS
655657
qgssqlexpressioncompiler.h
656658
qgsstatisticalsummary.h
657659
qgsstringutils.h
660+
qgstextlabelfeature.h
658661
qgstolerance.h
659662

660663
qgsvectordataprovider.h

‎src/core/dxf/qgsdxfpallabeling.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
#include "qgsdxfpallabeling.h"
1919
#include "qgsdxfexport.h"
20-
#include "qgspalgeometry.h"
20+
#include "qgstextlabelfeature.h"
2121
#include "qgspallabeling.h"
2222
#include "qgsmapsettings.h"
2323

24+
#include "pal/feature.h"
2425
#include "pal/pointset.h"
2526
#include "pal/labelposition.h"
2627

‎src/core/pal/feature.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include <cmath>
4040
#include <QString>
4141

42-
#include "qgslabelingenginev2.h"
42+
#include "qgslabelfeature.h"
4343

4444
/**
4545
* \class pal::LabelInfo

‎src/core/pal/problem.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <cfloat>
4242
#include <limits.h> //for INT_MAX
4343

44+
#include "qgslabelingenginev2.h"
45+
4446
using namespace pal;
4547

4648
inline void delete_chain( Chain *chain )

‎src/core/qgslabelfeature.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "qgslabelfeature.h"
Code has comments. Press enter to view.
2+
3+
#include "feature.h"
4+
5+
6+
QgsLabelFeature::QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size )
7+
: mLayer( nullptr )
8+
, mId( id )
9+
, mGeometry( geometry )
10+
, mObstacleGeometry( nullptr )
11+
, mSize( size )
12+
, mPriority( -1 )
13+
, mHasFixedPosition( false )
14+
, mHasFixedAngle( false )
15+
, mFixedAngle( 0 )
16+
, mHasFixedQuadrant( false )
17+
, mDistLabel( 0 )
18+
, mRepeatDistance( 0 )
19+
, mAlwaysShow( false )
20+
, mIsObstacle( false )
21+
, mObstacleFactor( 1 )
22+
, mInfo( nullptr )
23+
{
24+
}
25+
26+
QgsLabelFeature::~QgsLabelFeature()
27+
{
28+
if ( mGeometry )
29+
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mGeometry );
30+
31+
if ( mObstacleGeometry )
32+
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mObstacleGeometry );
33+
34+
delete mInfo;
35+
}
36+
37+
void QgsLabelFeature::setObstacleGeometry( GEOSGeometry* obstacleGeom )
38+
{
39+
if ( mObstacleGeometry )
40+
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mObstacleGeometry );
41+
42+
mObstacleGeometry = obstacleGeom;
43+
}

‎src/core/qgslabelfeature.h

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
#ifndef QGSLABELFEATURE_H
2+
#define QGSLABELFEATURE_H
3+
4+
#include "qgsgeometry.h"
5+
6+
#include "qgspallabeling.h"
7+
8+
namespace pal
9+
{
10+
class LabelInfo;
11+
}
12+
13+
class QgsAbstractLabelProvider;
14+
class QgsRenderContext;
15+
class QgsGeometry;
16+
17+
18+
/**
19+
* @brief The QgsLabelFeature class describes a feature that
20+
* should be used within the labeling engine. Those may be the usual textual labels,
21+
* diagrams, or any other custom type of map annotations (generated by custom
22+
* label providers).
23+
*
24+
* Instances only contain data relevant to the labeling engine (geometry, label size etc.)
25+
* necessary for the layout. Rendering of labels is done by label providers.
26+
*
27+
* Individual label providers may create subclasses of QgsLabelFeature in order to add
28+
* more data to the instances that will be later used for drawing of labels.
29+
*
30+
* @note this class is not a part of public API yet. See notes in QgsLabelingEngineV2
31+
* @note added in QGIS 2.12
32+
*/
33+
class CORE_EXPORT QgsLabelFeature
34+
{
35+
public:
36+
//! Create label feature, takes ownership of the geometry instance
37+
QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size );
38+
//! Clean up geometry and curved label info (if present)
39+
virtual ~QgsLabelFeature();
40+
41+
//! Identifier of the label (unique within the parent label provider)
42+
QgsFeatureId id() const { return mId; }
43+
44+
//! Get access to the associated geometry
45+
GEOSGeometry* geometry() const { return mGeometry; }
46+
47+
/** Sets the label's obstacle geometry, if different to the feature geometry.
48+
* This can be used to override the shape of the feature for obstacle detection, eg to
49+
* buffer around a point geometry to prevent labels being placed too close to the
50+
* point itself. It not set, the feature's geometry is used for obstacle detection.
51+
* Ownership of obstacle geometry is transferred.
52+
* @note added in QGIS 2.14
53+
* @see obstacleGeometry()
54+
*/
55+
void setObstacleGeometry( GEOSGeometry* obstacleGeom );
56+
57+
/** Returns the label's obstacle geometry, if different to the feature geometry.
58+
* @note added in QGIS 2.14
59+
* @see setObstacleGeometry()
60+
*/
61+
GEOSGeometry* obstacleGeometry() const { return mObstacleGeometry; }
62+
63+
//! Size of the label (in map units)
64+
QSizeF size() const { return mSize; }
65+
66+
/** Returns the feature's labeling priority.
67+
* @returns feature's priority, as a value between 0 (highest priority)
68+
* and 1 (lowest priority). Returns -1.0 if feature will use the layer's default priority.
69+
* @see setPriority
70+
*/
71+
double priority() const { return mPriority; }
72+
/** Sets the priority for labeling the feature.
73+
* @param priority feature's priority, as a value between 0 (highest priority)
74+
* and 1 (lowest priority). Set to -1.0 to use the layer's default priority
75+
* for this feature.
76+
* @see priority
77+
*/
78+
void setPriority( double priority ) { mPriority = priority; }
79+
80+
//! Whether the label should use a fixed position instead of being automatically placed
81+
bool hasFixedPosition() const { return mHasFixedPosition; }
82+
//! Set whether the label should use a fixed position instead of being automatically placed
83+
void setHasFixedPosition( bool enabled ) { mHasFixedPosition = enabled; }
84+
//! Coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
85+
QgsPoint fixedPosition() const { return mFixedPosition; }
86+
//! Set coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
87+
void setFixedPosition( const QgsPoint& point ) { mFixedPosition = point; }
88+
89+
//! Whether the label should use a fixed angle instead of using angle from automatic placement
90+
bool hasFixedAngle() const { return mHasFixedAngle; }
91+
//! Set whether the label should use a fixed angle instead of using angle from automatic placement
92+
void setHasFixedAngle( bool enabled ) { mHasFixedAngle = enabled; }
93+
//! Angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
94+
double fixedAngle() const { return mFixedAngle; }
95+
//! Set angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
96+
void setFixedAngle( double angle ) { mFixedAngle = angle; }
97+
98+
/** Returns whether the quadrant for the label is fixed.
99+
* Applies to "around point" placement strategy.
100+
* @see setFixedQuadrant
101+
* @see quadOffset
102+
*/
103+
bool hasFixedQuadrant() const { return mHasFixedQuadrant; }
104+
/** Sets whether the quadrant for the label must be respected. This can be used
105+
* to fix the quadrant for specific features when using an "around point" placement.
106+
* @see fixedQuadrant
107+
* @see quadOffset
108+
*/
109+
void setHasFixedQuadrant( bool enabled ) { mHasFixedQuadrant = enabled; }
110+
//! Applies to "offset from point" placement strategy and "around point" (in case hasFixedQuadrant() returns true).
111+
//! Determines which side of the point to use.
112+
//! For X coordinate, values -1, 0, 1 mean left, center, right.
113+
//! For Y coordinate, values -1, 0, 1 mean above, center, below.
114+
QPointF quadOffset() const { return mQuadOffset; }
115+
//! Set which side of the point to use
116+
//! @see quadOffset
117+
void setQuadOffset( const QPointF& quadOffset ) { mQuadOffset = quadOffset; }
118+
//! Applies only to "offset from point" placement strategy.
119+
//! What offset (in map units) to use from the point
120+
QgsPoint positionOffset() const { return mPositionOffset; }
121+
//! Applies only to "offset from point" placement strategy.
122+
//! Set what offset (in map units) to use from the point
123+
void setPositionOffset( const QgsPoint& offset ) { mPositionOffset = offset; }
124+
//! Applies to "around point" placement strategy or linestring features.
125+
//! Distance of the label from the feature (in map units)
126+
double distLabel() const { return mDistLabel; }
127+
//! Applies to "around point" placement strategy or linestring features.
128+
//! Set distance of the label from the feature (in map units)
129+
void setDistLabel( double dist ) { mDistLabel = dist; }
130+
131+
//! Applies only to linestring features - after what distance (in map units)
132+
//! the labels should be repeated (0 = no repetitions)
133+
double repeatDistance() const { return mRepeatDistance; }
134+
//! Applies only to linestring features - set after what distance (in map units)
135+
//! the labels should be repeated (0 = no repetitions)
136+
void setRepeatDistance( double dist ) { mRepeatDistance = dist; }
137+
138+
//! Whether label should be always shown (sets very high label priority)
139+
bool alwaysShow() const { return mAlwaysShow; }
140+
//! Set whether label should be always shown (sets very high label priority)
141+
void setAlwaysShow( bool enabled ) { mAlwaysShow = enabled; }
142+
143+
/** Returns whether the feature will act as an obstacle for labels.
144+
* @returns true if feature is an obstacle
145+
* @see setIsObstacle
146+
*/
147+
bool isObstacle() const { return mIsObstacle; }
148+
/** Sets whether the feature will act as an obstacle for labels.
149+
* @param enabled whether feature will act as an obstacle
150+
* @see isObstacle
151+
*/
152+
void setIsObstacle( bool enabled ) { mIsObstacle = enabled; }
153+
/** Returns the obstacle factor for the feature. The factor controls the penalty
154+
* for labels overlapping this feature.
155+
* @see setObstacleFactor
156+
*/
157+
double obstacleFactor() const { return mObstacleFactor; }
158+
/** Sets the obstacle factor for the feature. The factor controls the penalty
159+
* for labels overlapping this feature.
160+
* @param factor larger factors ( > 1.0 ) will result in labels
161+
* which are less likely to cover this feature, smaller factors ( < 1.0 ) mean labels
162+
* are more likely to cover this feature (where required)
163+
* @see obstacleFactor
164+
*/
165+
void setObstacleFactor( double factor ) { mObstacleFactor = factor; }
166+
167+
/** Text of the label
168+
*
169+
* Used also if "merge connected lines to avoid duplicate labels" is enabled
170+
* to identify which features may be merged.
171+
*/
172+
QString labelText() const { return mLabelText; }
173+
//! Set text of the label
174+
void setLabelText( const QString& text ) { mLabelText = text; }
175+
176+
//! Get additional infor required for curved label placement. Returns null if not set
177+
pal::LabelInfo* curvedLabelInfo() const { return mInfo; }
178+
//! takes ownership of the instance
179+
void setCurvedLabelInfo( pal::LabelInfo* info ) { mInfo = info; }
180+
181+
//! Get PAL layer of the label feature. Should be only used internally in PAL
182+
pal::Layer* layer() const { return mLayer; }
183+
//! Assign PAL layer to the label feature. Should be only used internally in PAL
184+
void setLayer( pal::Layer* layer ) { mLayer = layer; }
185+
186+
//! Return provider of this instance
187+
QgsAbstractLabelProvider* provider() const;
188+
189+
protected:
190+
//! Pointer to PAL layer (assigned when registered to PAL)
191+
pal::Layer* mLayer;
192+
193+
//! Associated ID unique within the parent label provider
194+
QgsFeatureId mId;
195+
//! Geometry of the feature to be labelled
196+
GEOSGeometry* mGeometry;
197+
//! Optional geometry to use for label obstacles, if different to mGeometry
198+
GEOSGeometry* mObstacleGeometry;
199+
//! Width and height of the label
200+
QSizeF mSize;
201+
//! Priority of the label
202+
double mPriority;
203+
//! whether mFixedPosition should be respected
204+
bool mHasFixedPosition;
205+
//! fixed position for the label (instead of automatic placement)
206+
QgsPoint mFixedPosition;
207+
//! whether mFixedAngle should be respected
208+
bool mHasFixedAngle;
209+
//! fixed rotation for the label (instead of automatic choice)
210+
double mFixedAngle;
211+
//! whether mQuadOffset should be respected (only for "around point" placement)
212+
bool mHasFixedQuadrant;
213+
//! whether the side of the label is fixed (only for "around point" placement)
214+
QPointF mQuadOffset;
215+
//! offset of label from the feature (only for "offset from point" placement)
216+
QgsPoint mPositionOffset;
217+
//! distance of label from the feature (only for "around point" placement or linestrings)
218+
double mDistLabel;
219+
//! distance after which label should be repeated (only for linestrings)
220+
double mRepeatDistance;
221+
//! whether to always show label - even in case of collisions
222+
bool mAlwaysShow;
223+
//! whether the feature geometry acts as an obstacle for labels
224+
bool mIsObstacle;
225+
//! how strong is the geometry acting as obstacle
226+
double mObstacleFactor;
227+
//! text of the label
228+
QString mLabelText;
229+
//! extra information for curved labels (may be null)
230+
pal::LabelInfo* mInfo;
231+
};
232+
233+
#endif // QGSLABELFEATURE_H

‎src/core/qgslabelingenginev2.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -340,47 +340,6 @@ void QgsLabelingEngineV2::writeSettingsToProject()
340340

341341
////
342342

343-
344-
345-
QgsLabelFeature::QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size )
346-
: mLayer( nullptr )
347-
, mId( id )
348-
, mGeometry( geometry )
349-
, mObstacleGeometry( nullptr )
350-
, mSize( size )
351-
, mPriority( -1 )
352-
, mHasFixedPosition( false )
353-
, mHasFixedAngle( false )
354-
, mFixedAngle( 0 )
355-
, mHasFixedQuadrant( false )
356-
, mDistLabel( 0 )
357-
, mRepeatDistance( 0 )
358-
, mAlwaysShow( false )
359-
, mIsObstacle( false )
360-
, mObstacleFactor( 1 )
361-
, mInfo( nullptr )
362-
{
363-
}
364-
365-
QgsLabelFeature::~QgsLabelFeature()
366-
{
367-
if ( mGeometry )
368-
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mGeometry );
369-
370-
if ( mObstacleGeometry )
371-
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mObstacleGeometry );
372-
373-
delete mInfo;
374-
}
375-
376-
void QgsLabelFeature::setObstacleGeometry( GEOSGeometry* obstacleGeom )
377-
{
378-
if ( mObstacleGeometry )
379-
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mObstacleGeometry );
380-
381-
mObstacleGeometry = obstacleGeom;
382-
}
383-
384343
QgsAbstractLabelProvider*QgsLabelFeature::provider() const
385344
{
386345
return mLayer ? mLayer->provider() : nullptr;

‎src/core/qgslabelingenginev2.h

Lines changed: 0 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -24,231 +24,6 @@
2424

2525
#include <QFlags>
2626

27-
class QgsAbstractLabelProvider;
28-
class QgsRenderContext;
29-
class QgsGeometry;
30-
31-
namespace pal
32-
{
33-
class LabelInfo;
34-
}
35-
36-
/**
37-
* @brief The QgsLabelFeature class describes a feature that
38-
* should be used within the labeling engine. Those may be the usual textual labels,
39-
* diagrams, or any other custom type of map annotations (generated by custom
40-
* label providers).
41-
*
42-
* Instances only contain data relevant to the labeling engine (geometry, label size etc.)
43-
* necessary for the layout. Rendering of labels is done by label providers.
44-
*
45-
* Individual label providers may create subclasses of QgsLabelFeature in order to add
46-
* more data to the instances that will be later used for drawing of labels.
47-
*
48-
* @note this class is not a part of public API yet. See notes in QgsLabelingEngineV2
49-
* @note added in QGIS 2.12
50-
*/
51-
class CORE_EXPORT QgsLabelFeature
52-
{
53-
public:
54-
//! Create label feature, takes ownership of the geometry instance
55-
QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size );
56-
//! Clean up geometry and curved label info (if present)
57-
virtual ~QgsLabelFeature();
58-
59-
//! Identifier of the label (unique within the parent label provider)
60-
QgsFeatureId id() const { return mId; }
61-
62-
//! Get access to the associated geometry
63-
GEOSGeometry* geometry() const { return mGeometry; }
64-
65-
/** Sets the label's obstacle geometry, if different to the feature geometry.
66-
* This can be used to override the shape of the feature for obstacle detection, eg to
67-
* buffer around a point geometry to prevent labels being placed too close to the
68-
* point itself. It not set, the feature's geometry is used for obstacle detection.
69-
* Ownership of obstacle geometry is transferred.
70-
* @note added in QGIS 2.14
71-
* @see obstacleGeometry()
72-
*/
73-
void setObstacleGeometry( GEOSGeometry* obstacleGeom );
74-
75-
/** Returns the label's obstacle geometry, if different to the feature geometry.
76-
* @note added in QGIS 2.14
77-
* @see setObstacleGeometry()
78-
*/
79-
GEOSGeometry* obstacleGeometry() const { return mObstacleGeometry; }
80-
81-
//! Size of the label (in map units)
82-
QSizeF size() const { return mSize; }
83-
84-
/** Returns the feature's labeling priority.
85-
* @returns feature's priority, as a value between 0 (highest priority)
86-
* and 1 (lowest priority). Returns -1.0 if feature will use the layer's default priority.
87-
* @see setPriority
88-
*/
89-
double priority() const { return mPriority; }
90-
/** Sets the priority for labeling the feature.
91-
* @param priority feature's priority, as a value between 0 (highest priority)
92-
* and 1 (lowest priority). Set to -1.0 to use the layer's default priority
93-
* for this feature.
94-
* @see priority
95-
*/
96-
void setPriority( double priority ) { mPriority = priority; }
97-
98-
//! Whether the label should use a fixed position instead of being automatically placed
99-
bool hasFixedPosition() const { return mHasFixedPosition; }
100-
//! Set whether the label should use a fixed position instead of being automatically placed
101-
void setHasFixedPosition( bool enabled ) { mHasFixedPosition = enabled; }
102-
//! Coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
103-
QgsPoint fixedPosition() const { return mFixedPosition; }
104-
//! Set coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
105-
void setFixedPosition( const QgsPoint& point ) { mFixedPosition = point; }
106-
107-
//! Whether the label should use a fixed angle instead of using angle from automatic placement
108-
bool hasFixedAngle() const { return mHasFixedAngle; }
109-
//! Set whether the label should use a fixed angle instead of using angle from automatic placement
110-
void setHasFixedAngle( bool enabled ) { mHasFixedAngle = enabled; }
111-
//! Angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
112-
double fixedAngle() const { return mFixedAngle; }
113-
//! Set angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
114-
void setFixedAngle( double angle ) { mFixedAngle = angle; }
115-
116-
/** Returns whether the quadrant for the label is fixed.
117-
* Applies to "around point" placement strategy.
118-
* @see setFixedQuadrant
119-
* @see quadOffset
120-
*/
121-
bool hasFixedQuadrant() const { return mHasFixedQuadrant; }
122-
/** Sets whether the quadrant for the label must be respected. This can be used
123-
* to fix the quadrant for specific features when using an "around point" placement.
124-
* @see fixedQuadrant
125-
* @see quadOffset
126-
*/
127-
void setHasFixedQuadrant( bool enabled ) { mHasFixedQuadrant = enabled; }
128-
//! Applies to "offset from point" placement strategy and "around point" (in case hasFixedQuadrant() returns true).
129-
//! Determines which side of the point to use.
130-
//! For X coordinate, values -1, 0, 1 mean left, center, right.
131-
//! For Y coordinate, values -1, 0, 1 mean above, center, below.
132-
QPointF quadOffset() const { return mQuadOffset; }
133-
//! Set which side of the point to use
134-
//! @see quadOffset
135-
void setQuadOffset( const QPointF& quadOffset ) { mQuadOffset = quadOffset; }
136-
//! Applies only to "offset from point" placement strategy.
137-
//! What offset (in map units) to use from the point
138-
QgsPoint positionOffset() const { return mPositionOffset; }
139-
//! Applies only to "offset from point" placement strategy.
140-
//! Set what offset (in map units) to use from the point
141-
void setPositionOffset( const QgsPoint& offset ) { mPositionOffset = offset; }
142-
//! Applies to "around point" placement strategy or linestring features.
143-
//! Distance of the label from the feature (in map units)
144-
double distLabel() const { return mDistLabel; }
145-
//! Applies to "around point" placement strategy or linestring features.
146-
//! Set distance of the label from the feature (in map units)
147-
void setDistLabel( double dist ) { mDistLabel = dist; }
148-
149-
//! Applies only to linestring features - after what distance (in map units)
150-
//! the labels should be repeated (0 = no repetitions)
151-
double repeatDistance() const { return mRepeatDistance; }
152-
//! Applies only to linestring features - set after what distance (in map units)
153-
//! the labels should be repeated (0 = no repetitions)
154-
void setRepeatDistance( double dist ) { mRepeatDistance = dist; }
155-
156-
//! Whether label should be always shown (sets very high label priority)
157-
bool alwaysShow() const { return mAlwaysShow; }
158-
//! Set whether label should be always shown (sets very high label priority)
159-
void setAlwaysShow( bool enabled ) { mAlwaysShow = enabled; }
160-
161-
/** Returns whether the feature will act as an obstacle for labels.
162-
* @returns true if feature is an obstacle
163-
* @see setIsObstacle
164-
*/
165-
bool isObstacle() const { return mIsObstacle; }
166-
/** Sets whether the feature will act as an obstacle for labels.
167-
* @param enabled whether feature will act as an obstacle
168-
* @see isObstacle
169-
*/
170-
void setIsObstacle( bool enabled ) { mIsObstacle = enabled; }
171-
/** Returns the obstacle factor for the feature. The factor controls the penalty
172-
* for labels overlapping this feature.
173-
* @see setObstacleFactor
174-
*/
175-
double obstacleFactor() const { return mObstacleFactor; }
176-
/** Sets the obstacle factor for the feature. The factor controls the penalty
177-
* for labels overlapping this feature.
178-
* @param factor larger factors ( > 1.0 ) will result in labels
179-
* which are less likely to cover this feature, smaller factors ( < 1.0 ) mean labels
180-
* are more likely to cover this feature (where required)
181-
* @see obstacleFactor
182-
*/
183-
void setObstacleFactor( double factor ) { mObstacleFactor = factor; }
184-
185-
/** Text of the label
186-
*
187-
* Used also if "merge connected lines to avoid duplicate labels" is enabled
188-
* to identify which features may be merged.
189-
*/
190-
QString labelText() const { return mLabelText; }
191-
//! Set text of the label
192-
void setLabelText( const QString& text ) { mLabelText = text; }
193-
194-
//! Get additional infor required for curved label placement. Returns null if not set
195-
pal::LabelInfo* curvedLabelInfo() const { return mInfo; }
196-
//! takes ownership of the instance
197-
void setCurvedLabelInfo( pal::LabelInfo* info ) { mInfo = info; }
198-
199-
//! Get PAL layer of the label feature. Should be only used internally in PAL
200-
pal::Layer* layer() const { return mLayer; }
201-
//! Assign PAL layer to the label feature. Should be only used internally in PAL
202-
void setLayer( pal::Layer* layer ) { mLayer = layer; }
203-
204-
//! Return provider of this instance
205-
QgsAbstractLabelProvider* provider() const;
206-
207-
protected:
208-
//! Pointer to PAL layer (assigned when registered to PAL)
209-
pal::Layer* mLayer;
210-
211-
//! Associated ID unique within the parent label provider
212-
QgsFeatureId mId;
213-
//! Geometry of the feature to be labelled
214-
GEOSGeometry* mGeometry;
215-
//! Optional geometry to use for label obstacles, if different to mGeometry
216-
GEOSGeometry* mObstacleGeometry;
217-
//! Width and height of the label
218-
QSizeF mSize;
219-
//! Priority of the label
220-
double mPriority;
221-
//! whether mFixedPosition should be respected
222-
bool mHasFixedPosition;
223-
//! fixed position for the label (instead of automatic placement)
224-
QgsPoint mFixedPosition;
225-
//! whether mFixedAngle should be respected
226-
bool mHasFixedAngle;
227-
//! fixed rotation for the label (instead of automatic choice)
228-
double mFixedAngle;
229-
//! whether mQuadOffset should be respected (only for "around point" placement)
230-
bool mHasFixedQuadrant;
231-
//! whether the side of the label is fixed (only for "around point" placement)
232-
QPointF mQuadOffset;
233-
//! offset of label from the feature (only for "offset from point" placement)
234-
QgsPoint mPositionOffset;
235-
//! distance of label from the feature (only for "around point" placement or linestrings)
236-
double mDistLabel;
237-
//! distance after which label should be repeated (only for linestrings)
238-
double mRepeatDistance;
239-
//! whether to always show label - even in case of collisions
240-
bool mAlwaysShow;
241-
//! whether the feature geometry acts as an obstacle for labels
242-
bool mIsObstacle;
243-
//! how strong is the geometry acting as obstacle
244-
double mObstacleFactor;
245-
//! text of the label
246-
QString mLabelText;
247-
//! extra information for curved labels (may be null)
248-
pal::LabelInfo* mInfo;
249-
};
250-
251-
25227

25328
class QgsLabelingEngineV2;
25429

‎src/core/qgspallabeling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgspallabeling.h"
19-
#include "qgspalgeometry.h"
19+
#include "qgstextlabelfeature.h"
2020

2121
#include <list>
2222

‎src/core/qgstextlabelfeature.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
#include "qgstextlabelfeature.h"
3+
4+
#include "qgsgeometry.h"
5+
#include "qgspallabeling.h"
6+
#include <pal/feature.h>
7+
8+
9+
QgsTextLabelFeature::QgsTextLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size )
10+
: QgsLabelFeature( id, geometry, size )
11+
, mFontMetrics( NULL )
12+
{
13+
mDefinedFont = QFont();
14+
}
15+
16+
17+
QgsTextLabelFeature::~QgsTextLabelFeature()
18+
{
19+
delete mFontMetrics;
20+
}
21+
22+
23+
QString QgsTextLabelFeature::text( int partId ) const
24+
{
25+
if ( partId == -1 )
26+
return mLabelText;
27+
else
28+
return mClusters.at( partId );
29+
}
30+
31+
32+
void QgsTextLabelFeature::calculateInfo( bool curvedLabeling, QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale, double maxinangle, double maxoutangle )
33+
{
34+
if ( mInfo )
35+
return;
36+
37+
mFontMetrics = new QFontMetricsF( *fm ); // duplicate metrics for when drawing label
38+
39+
qreal letterSpacing = mDefinedFont.letterSpacing();
40+
qreal wordSpacing = mDefinedFont.wordSpacing();
41+
42+
// max angle between curved label characters (20.0/-20.0 was default in QGIS <= 1.8)
43+
if ( maxinangle < 20.0 )
44+
maxinangle = 20.0;
45+
if ( 60.0 < maxinangle )
46+
maxinangle = 60.0;
47+
if ( maxoutangle > -20.0 )
48+
maxoutangle = -20.0;
49+
if ( -95.0 > maxoutangle )
50+
maxoutangle = -95.0;
51+
52+
// create label info!
53+
double mapScale = xform->mapUnitsPerPixel();
54+
double labelHeight = mapScale * fm->height() / fontScale;
55+
56+
// mLetterSpacing/mWordSpacing = 0.0 is default for non-curved labels
57+
// (non-curved spacings handled by Qt in QgsPalLayerSettings/QgsPalLabeling)
58+
qreal charWidth;
59+
qreal wordSpaceFix;
60+
61+
//split string by valid grapheme boundaries - required for certain scripts (see #6883)
62+
mClusters = QgsPalLabeling::splitToGraphemes( mLabelText );
63+
64+
mInfo = new pal::LabelInfo( mClusters.count(), labelHeight, maxinangle, maxoutangle );
65+
for ( int i = 0; i < mClusters.count(); i++ )
66+
{
67+
// reconstruct how Qt creates word spacing, then adjust per individual stored character
68+
// this will allow PAL to create each candidate width = character width + correct spacing
69+
charWidth = fm->width( mClusters[i] );
70+
if ( curvedLabeling )
71+
{
72+
wordSpaceFix = qreal( 0.0 );
73+
if ( mClusters[i] == QLatin1String( " " ) )
74+
{
75+
// word spacing only gets added once at end of consecutive run of spaces, see QTextEngine::shapeText()
76+
int nxt = i + 1;
77+
wordSpaceFix = ( nxt < mClusters.count() && mClusters[nxt] != QLatin1String( " " ) ) ? wordSpacing : qreal( 0.0 );
78+
}
79+
// this workaround only works for clusters with a single character. Not sure how it should be handled
80+
// with multi-character clusters.
81+
if ( mClusters[i].length() == 1 &&
82+
!qgsDoubleNear( fm->width( QString( mClusters[i].at( 0 ) ) ), fm->width( mClusters[i].at( 0 ) ) + letterSpacing ) )
83+
{
84+
// word spacing applied when it shouldn't be
85+
wordSpaceFix -= wordSpacing;
86+
}
87+
88+
charWidth = fm->width( QString( mClusters[i] ) ) + wordSpaceFix;
89+
}
90+
91+
double labelWidth = mapScale * charWidth / fontScale;
92+
mInfo->char_info[i].width = labelWidth;
93+
}
94+
}

‎src/core/qgstextlabelfeature.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef QGSTEXTLABELFEATURE_H
2+
#define QGSTEXTLABELFEATURE_H
3+
4+
#include "qgslabelfeature.h"
5+
6+
/**
7+
* Class that adds extra information to QgsLabelFeature for text labels
8+
*
9+
* @note not part of public API
10+
*/
11+
class QgsTextLabelFeature : public QgsLabelFeature
12+
{
13+
public:
14+
//! Construct text label feature
15+
QgsTextLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size );
16+
17+
//! Clean up
18+
~QgsTextLabelFeature();
19+
20+
/** Returns the text component corresponding to a specified label part
21+
* @param partId Set to -1 for labels which are not broken into parts (eg, non-curved labels), or the required
22+
* part index for labels which are broken into parts (curved labels)
23+
* @note added in QGIS 2.10
24+
*/
25+
QString text( int partId ) const;
26+
27+
//! calculate data for info(). setDefinedFont() must have been called already.
28+
void calculateInfo( bool curvedLabeling, QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale, double maxinangle, double maxoutangle );
29+
30+
//! Get data-defined values
31+
const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant >& dataDefinedValues() const { return mDataDefinedValues; }
32+
//! Set data-defined values
33+
void setDataDefinedValues( const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant >& values ) { mDataDefinedValues = values; }
34+
35+
//! Set font to be used for rendering
36+
void setDefinedFont( const QFont& f ) { mDefinedFont = f; }
37+
//! Font to be used for rendering
38+
QFont definedFont() { return mDefinedFont; }
39+
40+
//! Metrics of the font for rendering
41+
QFontMetricsF* labelFontMetrics() { return mFontMetrics; }
42+
43+
protected:
44+
//! List of graphemes (used for curved labels)
45+
QStringList mClusters;
46+
//! Font for rendering
47+
QFont mDefinedFont;
48+
//! Metrics of the font for rendering
49+
QFontMetricsF* mFontMetrics;
50+
/** Stores attribute values for data defined properties*/
51+
QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues;
52+
53+
};
54+
55+
#endif //QGSTEXTLABELFEATURE_H

‎src/core/qgsvectorlayerdiagramprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define QGSVECTORLAYERDIAGRAMPROVIDER_H
1818

1919
#include "qgslabelingenginev2.h"
20-
20+
#include "qgslabelfeature.h"
2121

2222
/**
2323
* Class that adds extra information to QgsLabelFeature for labeling of diagrams

‎src/core/qgsvectorlayerlabelprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "qgsdatadefined.h"
1919
#include "qgsgeometry.h"
2020
#include "qgslabelsearchtree.h"
21-
#include "qgspalgeometry.h"
2221
#include "qgspallabeling.h"
22+
#include "qgstextlabelfeature.h"
2323
#include "qgsvectorlayer.h"
2424
#include "qgsvectorlayerfeatureiterator.h"
2525
#include "qgsrendererv2.h"

0 commit comments

Comments
 (0)
Please sign in to comment.