QGIS API Documentation 4.3.0-Master (767b36bf018)
Loading...
Searching...
No Matches
pal.h
Go to the documentation of this file.
1/*
2 * libpal - Automated Placement of Labels Library
3 *
4 * Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD
5 * University of Applied Sciences, Western Switzerland
6 * http://www.hes-so.ch
7 *
8 * Contact:
9 * maxence.laurent <at> heig-vd <dot> ch
10 * or
11 * eric.taillard <at> heig-vd <dot> ch
12 *
13 * This file is part of libpal.
14 *
15 * libpal is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * libpal is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with libpal. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30#ifndef PAL_H
31#define PAL_H
32
33
34#include <ctime>
35#include <iostream>
36#include <unordered_map>
37
38#include "qgis_core.h"
39#include "qgsgeometry.h"
40#include "qgsgeos.h"
41#include "qgssettingstree.h"
42
43#include <QList>
44#include <QMutex>
45#include <QString>
46#include <QStringList>
47
48#define SIP_NO_FILE
49
50using namespace Qt::StringLiterals;
51
53
54// TODO ${MAJOR} ${MINOR} etc instead of 0.2
55
59
60namespace pal
61{
62 class Layer;
63 class LabelPosition;
64 class Problem;
65 class PointSet;
66
76
86 class CORE_EXPORT Pal
87 {
88 friend class Problem;
89 friend class FeaturePart;
90 friend class Layer;
91
92 public:
93 static inline QgsSettingsTreeNode *sTreePal = QgsSettingsTree::sTreeRendering->createChildNode( u"pal"_s );
94
98
104
105 Pal( const Pal &other ) = delete;
106 Pal &operator=( const Pal &other ) = delete;
107
111 Qgis::LabelingFlags flags() const { return mFlags; }
112
125 Layer *addLayer( QgsAbstractLabelProvider *provider, const QString &layerName, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel );
126
132 void removeLayer( Layer *layer );
133
135 typedef bool ( *FnIsCanceled )( void *ctx );
136
138 void registerCancellationCallback( FnIsCanceled fnCanceled, void *context );
139
141 inline bool isCanceled() { return fnIsCanceled ? fnIsCanceled( fnIsCanceledContext ) : false; }
142
161 std::unique_ptr< Problem > extractProblem( const QgsRectangle &extent, const QgsGeometry &mapBoundary, QgsRenderContext &context );
162
175 QList<LabelPosition *> solveProblem( Problem *prob, QgsRenderContext &context, bool displayAll, QList<pal::LabelPosition *> *unlabeled = nullptr );
176
182 void setShowPartialLabels( bool show );
183
184 static constexpr bool DEFAULT_SHOW_PARTIAL_LABELS = true;
185
191 bool showPartialLabels() const;
192
198 double maximumLineCandidatesPerMapUnit() const { return mMaxLineCandidatesPerMapUnit; }
199
205 void setMaximumLineCandidatesPerMapUnit( double candidates ) { mMaxLineCandidatesPerMapUnit = candidates; }
206
212 double maximumPolygonCandidatesPerMapUnitSquared() const { return mMaxPolygonCandidatesPerMapUnitSquared; }
213
219 void setMaximumPolygonCandidatesPerMapUnitSquared( double candidates ) { mMaxPolygonCandidatesPerMapUnitSquared = candidates; }
220
226 Qgis::LabelPlacementEngineVersion placementVersion() const;
227
233 void setPlacementVersion( Qgis::LabelPlacementEngineVersion placementVersion );
234
245 int globalCandidatesLimitPoint() const { return mGlobalCandidatesLimitPoint; }
246
257 int globalCandidatesLimitLine() const { return mGlobalCandidatesLimitLine; }
258
269 int globalCandidatesLimitPolygon() const { return mGlobalCandidatesLimitPolygon; }
270
274 bool candidatesAreConflicting( const LabelPosition *lp1, const LabelPosition *lp2 ) const;
275
285 void setRules( const QList< QgsAbstractLabelingEngineRule * > &rules );
286
293 QList< QgsAbstractLabelingEngineRule * > rules() const { return mRules; }
294
295 private:
296 Qgis::LabelingFlags mFlags;
297
298 std::vector< std::pair< QgsAbstractLabelProvider *, std::unique_ptr< Layer > > > mLayers;
299
300 QList< QgsAbstractLabelingEngineRule * > mRules;
301
302 QMutex mMutex;
303
304 /*
305 * POPMUSIC Tuning
306 */
307 int mPopmusicR = 30;
308
309 int mTabuMaxIt = 4;
310 int mTabuMinIt = 2;
311
312 int mEjChainDeg = 50;
313 int mTenure = 10;
314 double mCandListSize = 0.2;
315
316 unsigned int mNextCandidateId = 1;
317 mutable QHash< QPair< unsigned int, unsigned int >, bool > mCandidateConflicts;
318
322 bool mShowPartialLabels = DEFAULT_SHOW_PARTIAL_LABELS;
323
324 double mMaxLineCandidatesPerMapUnit = 0;
325 double mMaxPolygonCandidatesPerMapUnitSquared = 0;
326
327 int mGlobalCandidatesLimitPoint = 0;
328 int mGlobalCandidatesLimitLine = 0;
329 int mGlobalCandidatesLimitPolygon = 0;
330
332
334 FnIsCanceled fnIsCanceled = nullptr;
336 void *fnIsCanceledContext = nullptr;
337
342 void setPopmusicR( int r );
343
348 void setMinIt( int min_it );
349
354 void setMaxIt( int max_it );
355
360 void setTenure( int tenure );
361
366 void setEjChainDeg( int degree );
367
372 void setCandListSize( double fact );
373
374
379 int getMinIt() const;
380
385 int getMaxIt() const;
386 };
387
388} // end namespace pal
389
390#endif
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1287
QFlags< LabelingFlag > LabelingFlags
Flags that affect drawing and placement of labels.
Definition qgis.h:3064
LabelPlacementEngineVersion
Labeling placement engine version.
Definition qgis.h:3075
@ Version2
Version 2 (default for new projects since QGIS 3.12).
Definition qgis.h:3077
An abstract interface class for label providers.
Abstract base class for labeling engine rules.
A geometry is the spatial representation of a feature.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
An integer settings entry.
A tree node for the settings tree to help organizing and introspecting the tree.
static QgsSettingsTreeNode * sTreeRendering
LabelPosition is a candidate feature label position.
A set of features which influence the labeling process.
Definition layer.h:63
double maximumLineCandidatesPerMapUnit() const
Returns the maximum number of line label candidate positions per map unit.
Definition pal.h:198
Qgis::LabelingFlags flags() const
Returns labeling flags.
Definition pal.h:111
int globalCandidatesLimitPoint() const
Returns the global candidates limit for point features, or 0 if no global limit is in effect.
Definition pal.h:245
static QgsSettingsTreeNode * sTreePal
Definition pal.h:93
double maximumPolygonCandidatesPerMapUnitSquared() const
Returns the maximum number of polygon label candidate positions per map unit squared.
Definition pal.h:212
void setMaximumPolygonCandidatesPerMapUnitSquared(double candidates)
Sets the maximum number of polygon label candidates per map unit squared.
Definition pal.h:219
friend class Layer
Definition pal.h:90
int globalCandidatesLimitLine() const
Returns the global candidates limit for line features, or 0 if no global limit is in effect.
Definition pal.h:257
bool(* FnIsCanceled)(void *ctx)
Cancellation check callback function.
Definition pal.h:135
bool showPartialLabels() const
Returns whether partial labels should be allowed.
Definition pal.cpp:858
static const QgsSettingsEntryInteger * settingsRenderingLabelCandidatesLimitLines
Definition pal.h:96
static const QgsSettingsEntryInteger * settingsRenderingLabelCandidatesLimitPoints
Definition pal.h:95
static const QgsSettingsEntryInteger * settingsRenderingLabelCandidatesLimitPolygons
Definition pal.h:97
void setMaximumLineCandidatesPerMapUnit(double candidates)
Sets the maximum number of line label candidates per map unit.
Definition pal.h:205
friend class Problem
Definition pal.h:88
bool isCanceled()
Check whether the job has been canceled.
Definition pal.h:141
Pal(const Pal &other)=delete
friend class FeaturePart
Definition pal.h:89
static constexpr bool DEFAULT_SHOW_PARTIAL_LABELS
Definition pal.h:184
Pal(Qgis::LabelingFlags flags)
Constructor for pal labeling engine.
Definition pal.cpp:67
void registerCancellationCallback(FnIsCanceled fnCanceled, void *context)
Register a function that returns whether this job has been canceled - PAL calls it during the computa...
Definition pal.cpp:678
int globalCandidatesLimitPolygon() const
Returns the global candidates limit for polygon features, or 0 if no global limit is in effect.
Definition pal.h:269
Pal & operator=(const Pal &other)=delete
QList< QgsAbstractLabelingEngineRule * > rules() const
Returns the rules which the labeling solution must satisfy.
Definition pal.h:293
Representation of a labeling problem.
Definition problem.h:75
SearchMethod
Search method to use.
Definition pal.h:69
@ FALP
Only initial solution.
Definition pal.h:74
@ POPMUSIC_TABU
Is a little bit better than CHAIN but slower.
Definition pal.h:72
@ POPMUSIC_CHAIN
Is slower and best than TABU, worse and faster than TABU_CHAIN.
Definition pal.h:73
@ CHAIN
Is the worst but fastest method.
Definition pal.h:70
@ POPMUSIC_TABU_CHAIN
Is the best but slowest.
Definition pal.h:71