@@ -51,6 +51,7 @@ class CORE_EXPORT QgsLabelFeature
51
51
public:
52
52
// ! Create label feature, takes ownership of the geometry instance
53
53
QgsLabelFeature ( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size );
54
+ // ! Clean up geometry and curved label info (if present)
54
55
virtual ~QgsLabelFeature ();
55
56
56
57
// ! Identifier of the label (unique within the parent label provider)
@@ -78,16 +79,20 @@ class CORE_EXPORT QgsLabelFeature
78
79
79
80
// ! Whether the label should use a fixed position instead of being automatically placed
80
81
bool hasFixedPosition () const { return mHasFixedPosition ; }
82
+ // ! Set whether the label should use a fixed position instead of being automatically placed
81
83
void setHasFixedPosition ( bool enabled ) { mHasFixedPosition = enabled; }
82
84
// ! Coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
83
85
QgsPoint fixedPosition () const { return mFixedPosition ; }
86
+ // ! Set coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
84
87
void setFixedPosition ( const QgsPoint& point ) { mFixedPosition = point; }
85
88
86
89
// ! Whether the label should use a fixed angle instead of using angle from automatic placement
87
90
bool hasFixedAngle () const { return mHasFixedAngle ; }
91
+ // ! Set whether the label should use a fixed angle instead of using angle from automatic placement
88
92
void setHasFixedAngle ( bool enabled ) { mHasFixedAngle = enabled; }
89
93
// ! Angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
90
94
double fixedAngle () const { return mFixedAngle ; }
95
+ // ! Set angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
91
96
void setFixedAngle ( double angle ) { mFixedAngle = angle; }
92
97
93
98
/* * Returns whether the quadrant for the label is fixed.
@@ -107,22 +112,32 @@ class CORE_EXPORT QgsLabelFeature
107
112
// ! For X coordinate, values -1, 0, 1 mean left, center, right.
108
113
// ! For Y coordinate, values -1, 0, 1 mean above, center, below.
109
114
QPointF quadOffset () const { return mQuadOffset ; }
115
+ // ! Set which side of the point to use
116
+ // ! @see quadOffset
110
117
void setQuadOffset ( const QPointF& quadOffset ) { mQuadOffset = quadOffset; }
111
- // ! Applies only to "offset from point" placement strategy - what offset to use from the point
118
+ // ! Applies only to "offset from point" placement strategy.
119
+ // ! What offset (in map units) to use from the point
112
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
113
123
void setPositionOffset ( const QgsPoint& offset ) { mPositionOffset = offset; }
114
124
// ! Applies to "around point" placement strategy or linestring features.
115
125
// ! Distance of the label from the feature (in map units)
116
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)
117
129
void setDistLabel ( double dist ) { mDistLabel = dist; }
118
130
119
131
// ! Applies only to linestring features - after what distance (in map units)
120
132
// ! the labels should be repeated (0 = no repetitions)
121
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)
122
136
void setRepeatDistance ( double dist ) { mRepeatDistance = dist; }
123
137
124
138
// ! Whether label should be always shown (sets very high label priority)
125
139
bool alwaysShow () const { return mAlwaysShow ; }
140
+ // ! Set whether label should be always shown (sets very high label priority)
126
141
void setAlwaysShow ( bool enabled ) { mAlwaysShow = enabled; }
127
142
128
143
/* * Returns whether the feature will act as an obstacle for labels.
@@ -131,7 +146,7 @@ class CORE_EXPORT QgsLabelFeature
131
146
*/
132
147
bool isObstacle () const { return mIsObstacle ; }
133
148
/* * Sets whether the feature will act as an obstacle for labels.
134
- * @param obstacle whether feature will act as an obstacle
149
+ * @param enabled whether feature will act as an obstacle
135
150
* @see isObstacle
136
151
*/
137
152
void setIsObstacle ( bool enabled ) { mIsObstacle = enabled; }
@@ -149,11 +164,13 @@ class CORE_EXPORT QgsLabelFeature
149
164
*/
150
165
void setObstacleFactor ( double factor ) { mObstacleFactor = factor; }
151
166
152
- // ! Text of the label
153
- // !
154
- // ! Used if "merge connected lines to avoid duplicate labels" is enabled
155
- // ! to identify which features may be merged
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
+ */
156
172
QString labelText () const { return mLabelText ; }
173
+ // ! Set text of the label
157
174
void setLabelText ( const QString& text ) { mLabelText = text; }
158
175
159
176
// ! Get additional infor required for curved label placement. Returns null if not set
@@ -164,9 +181,10 @@ class CORE_EXPORT QgsLabelFeature
164
181
// ! Get PAL layer of the label feature. Should be only used internally in PAL
165
182
pal::Layer* layer () const { return mLayer ; }
166
183
// ! Assign PAL layer to the label feature. Should be only used internally in PAL
167
- void setLayer (pal::Layer* layer) { mLayer = layer; }
184
+ void setLayer ( pal::Layer* layer ) { mLayer = layer; }
168
185
169
186
protected:
187
+ // ! Pointer to PAL layer (assigned when registered to PAL)
170
188
pal::Layer* mLayer ;
171
189
172
190
// ! Associated ID unique within the parent label provider
@@ -175,20 +193,35 @@ class CORE_EXPORT QgsLabelFeature
175
193
GEOSGeometry* mGeometry ;
176
194
// ! Width and height of the label
177
195
QSizeF mSize ;
196
+ // ! Priority of the label
178
197
double mPriority ;
198
+ // ! whether mFixedPosition should be respected
179
199
bool mHasFixedPosition ;
200
+ // ! fixed position for the label (instead of automatic placement)
180
201
QgsPoint mFixedPosition ;
202
+ // ! whether mFixedAngle should be respected
181
203
bool mHasFixedAngle ;
204
+ // ! fixed rotation for the label (instead of automatic choice)
182
205
double mFixedAngle ;
206
+ // ! whether mQuadOffset should be respected (only for "around point" placement)
183
207
bool mHasFixedQuadrant ;
208
+ // ! whether the side of the label is fixed (only for "around point" placement)
184
209
QPointF mQuadOffset ;
210
+ // ! offset of label from the feature (only for "offset from point" placement)
185
211
QgsPoint mPositionOffset ;
212
+ // ! distance of label from the feature (only for "around point" placement or linestrings)
186
213
double mDistLabel ;
214
+ // ! distance after which label should be repeated (only for linestrings)
187
215
double mRepeatDistance ;
216
+ // ! whether to always show label - even in case of collisions
188
217
bool mAlwaysShow ;
218
+ // ! whether the feature geometry acts as an obstacle for labels
189
219
bool mIsObstacle ;
220
+ // ! how strong is the geometry acting as obstacle
190
221
double mObstacleFactor ;
222
+ // ! text of the label
191
223
QString mLabelText ;
224
+ // ! extra information for curved labels (may be null)
192
225
pal::LabelInfo* mInfo ;
193
226
};
194
227
@@ -202,15 +235,20 @@ class QgsLabelingEngineV2;
202
235
* return list of labels and their associated geometries - these are used by
203
236
* QgsLabelingEngineV2 to compute the final layout of labels.
204
237
*
238
+ * Implementations also take care of drawing the returned final label positions.
239
+ *
205
240
* @note added in QGIS 2.12
206
241
*/
207
242
class CORE_EXPORT QgsAbstractLabelProvider
208
243
{
209
244
210
245
public:
246
+ // ! Construct the provider with default values
211
247
QgsAbstractLabelProvider ();
248
+ // ! Vritual destructor
212
249
virtual ~QgsAbstractLabelProvider () {}
213
250
251
+ // ! Associate provider with a labeling engine (should be only called internally from QgsLabelingEngineV2)
214
252
void setEngine ( const QgsLabelingEngineV2* engine ) { mEngine = engine; }
215
253
216
254
enum Flag
@@ -227,35 +265,47 @@ class CORE_EXPORT QgsAbstractLabelProvider
227
265
// ! Return unique identifier of the provider
228
266
virtual QString id () const = 0;
229
267
230
- // ! Return list of labels
268
+ // ! Return list of label features (they are owned by the provider and thus deleted on its destruction)
231
269
virtual QList<QgsLabelFeature*> labelFeatures ( const QgsRenderContext& context ) = 0;
232
270
233
271
// ! draw this label at the position determined by the labeling engine
234
272
virtual void drawLabel ( QgsRenderContext& context, pal::LabelPosition* label ) const = 0;
235
273
274
+ // ! Flags associated with the provider
236
275
Flags flags () const { return mFlags ; }
237
276
238
277
// ! What placement strategy to use for the labels
239
278
QgsPalLayerSettings::Placement placement () const { return mPlacement ; }
240
279
280
+ // ! For layers with linestring geometries - extra placement flags (or-ed combination of QgsPalLayerSettings::LinePlacementFlags)
241
281
unsigned int linePlacementFlags () const { return mLinePlacementFlags ; }
242
282
283
+ // ! Default priority of labels (may be overridden by individual labels)
243
284
double priority () const { return mPriority ; }
244
285
286
+ // ! How the feature geometries will work as obstacles
245
287
QgsPalLayerSettings::ObstacleType obstacleType () const { return mObstacleType ; }
246
288
247
- unsigned int upsidedownLabels () const { return mUpsidedownLabels ; }
289
+ // ! How to handle labels that would be upside down
290
+ QgsPalLayerSettings::UpsideDownLabels upsidedownLabels () const { return mUpsidedownLabels ; }
248
291
249
292
250
293
protected:
294
+ // ! Associated labeling engine
251
295
const QgsLabelingEngineV2* mEngine ;
252
296
297
+ // ! Flags altering drawing and registration of features
253
298
Flags mFlags ;
299
+ // ! Placement strategy
254
300
QgsPalLayerSettings::Placement mPlacement ;
301
+ // ! Extra placement flags for linestring geometries
255
302
unsigned int mLinePlacementFlags ;
303
+ // ! Default priority of labels
256
304
double mPriority ;
305
+ // ! Type of the obstacle of feature geometries
257
306
QgsPalLayerSettings::ObstacleType mObstacleType ;
258
- unsigned int mUpsidedownLabels ;
307
+ // ! How to handle labels that would be upside down
308
+ QgsPalLayerSettings::UpsideDownLabels mUpsidedownLabels ;
259
309
};
260
310
261
311
Q_DECLARE_OPERATORS_FOR_FLAGS ( QgsAbstractLabelProvider::Flags )
@@ -269,12 +319,29 @@ Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
269
319
* with no collisions between the labels. Drawing of resulting labels is done
270
320
* again by label providers.
271
321
*
322
+ * The labeling engine is used for the map rendering in QgsMapRendererJob instances,
323
+ * individual map layer renderers may add label providers - for example,
324
+ * QgsVectorLayerRenderer may add text label provider and diagram provider
325
+ * (if labeling / diagrams were configured for such vector layer).
326
+ *
327
+ * The labeling engine may also be used independently from map rendering loop:
328
+ * \code
329
+ * QgsLabelingEngineV2 engine;
330
+ * engine.setMapSettings( mapSettings );
331
+ * // add one or more providers
332
+ * engine.addProvider( ... );
333
+ * // compute the labeling and draw labels (using painter from the context)
334
+ * engine.run( context );
335
+ * \endcode
336
+ *
272
337
* @note added in QGIS 2.12
273
338
*/
274
339
class CORE_EXPORT QgsLabelingEngineV2
275
340
{
276
341
public:
342
+ // ! Construct the labeling engine with default settings
277
343
QgsLabelingEngineV2 ();
344
+ // ! Clean up everything (especially the registered providers)
278
345
~QgsLabelingEngineV2 ();
279
346
280
347
enum Flag
@@ -288,7 +355,9 @@ class CORE_EXPORT QgsLabelingEngineV2
288
355
};
289
356
Q_DECLARE_FLAGS ( Flags, Flag )
290
357
358
+ // ! Associate map settings instance
291
359
void setMapSettings ( const QgsMapSettings& mapSettings ) { mMapSettings = mapSettings; }
360
+ // ! Get associated map settings
292
361
const QgsMapSettings& mapSettings () const { return mMapSettings ; }
293
362
294
363
// ! Add provider of label features. Takes ownership of the provider
@@ -309,27 +378,43 @@ class CORE_EXPORT QgsLabelingEngineV2
309
378
// ! For internal use by the providers
310
379
QgsLabelingResults* results () const { return mResults ; }
311
380
381
+ // ! Set flags of the labeling engine
312
382
void setFlags ( Flags flags ) { mFlags = flags; }
383
+ // ! Get flags of the labeling engine
313
384
Flags flags () const { return mFlags ; }
385
+ // ! Test whether a particular flag is enabled
314
386
bool testFlag ( Flag f ) const { return mFlags .testFlag ( f ); }
315
- void setFlag ( Flag f, bool enabled ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
387
+ // ! Set whether a particual flag is enabled
388
+ void setFlag ( Flag f, bool enabled = true ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
316
389
390
+ // ! Get number of candidate positions that will be generated for each label feature (default to 8)
317
391
void numCandidatePositions ( int & candPoint, int & candLine, int & candPolygon ) { candPoint = mCandPoint ; candLine = mCandLine ; candPolygon = mCandPolygon ; }
392
+ // ! Set number of candidate positions that will be generated for each label feature
318
393
void setNumCandidatePositions ( int candPoint, int candLine, int candPolygon ) { mCandPoint = candPoint; mCandLine = candLine; mCandPolygon = candPolygon; }
319
394
395
+ // ! Set which search method to use for removal collisions between labels
320
396
void setSearchMethod ( QgsPalLabeling::Search s ) { mSearchMethod = s; }
397
+ // ! Which search method to use for removal collisions between labels
321
398
QgsPalLabeling::Search searchMethod () const { return mSearchMethod ; }
322
399
400
+ // ! Read configuration of the labeling engine from the current project file
323
401
void readSettingsFromProject ();
402
+ // ! Write configuration of the labeling engine to the current project file
324
403
void writeSettingsToProject ();
325
404
326
405
protected:
406
+ // ! Associated map settings instance
327
407
QgsMapSettings mMapSettings ;
408
+ // ! List of providers (the are owned by the labeling engine)
328
409
QList<QgsAbstractLabelProvider*> mProviders ;
410
+ // ! Flags
329
411
Flags mFlags ;
412
+ // ! search method to use for removal collisions between labels
330
413
QgsPalLabeling::Search mSearchMethod ;
414
+ // ! Number of candedate positions that will be generated for features
331
415
int mCandPoint , mCandLine , mCandPolygon ;
332
416
417
+ // ! Resulting labeling layout
333
418
QgsLabelingResults* mResults ;
334
419
};
335
420
0 commit comments