QGIS API Documentation  2.99.0-Master (b95d432)
layer.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_LAYER_H_
31 #define PAL_LAYER_H_
32 
33 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "pal.h" // for LineArrangementFlags enum
38 #include "rtree.hpp"
39 #include <QMutex>
40 #include <QLinkedList>
41 #include <QHash>
42 #include <fstream>
43 
44 class QgsLabelFeature;
45 
46 namespace pal
47 {
48 
50  template<class DATATYPE, class ELEMTYPE, int NUMDIMS, class ELEMTYPEREAL, int TMAXNODES, int TMINNODES> class RTree;
52 
53  class FeaturePart;
54  class Pal;
55  class LabelInfo;
56 
63  class CORE_EXPORT Layer
64  {
65  friend class Pal;
66  friend class FeaturePart;
67 
68  friend class Problem;
69 
70  friend class LabelPosition;
71 
72  public:
73  enum LabelMode { LabelPerFeature, LabelPerFeaturePart };
75  {
76  Upright, // upside-down labels (90 <= angle < 270) are shown upright
77  ShowDefined, // show upside down when rotation is layer- or data-defined
78  ShowAll // show upside down for all labels, including dynamic ones
79  };
80 
81  virtual ~Layer();
82 
83  bool displayAll() const { return mDisplayAll; }
84 
87  int featureCount() { return mHashtable.size(); }
88 
90  QgsAbstractLabelProvider *provider() const { return mProvider; }
91 
94  QString name() const { return mName; }
95 
99  QgsPalLayerSettings::Placement arrangement() const { return mArrangement; }
100 
103  bool isCurved() const { return mArrangement == QgsPalLayerSettings::Curved || mArrangement == QgsPalLayerSettings::PerimeterCurved; }
104 
109  void setArrangement( QgsPalLayerSettings::Placement arrangement ) { mArrangement = arrangement; }
110 
114  LineArrangementFlags arrangementFlags() const { return mArrangementFlags; }
115 
120  void setArrangementFlags( LineArrangementFlags flags ) { mArrangementFlags = flags; }
121 
132  void setActive( bool active ) { mActive = active; }
133 
137  bool active() const { return mActive; }
138 
145  void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
146 
150  bool labelLayer() const { return mLabelLayer; }
151 
156  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
157 
163  void setObstacleType( QgsPalLayerSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
164 
170  void setPriority( double priority );
171 
176  double priority() const { return mDefaultPriority; }
177 
182  void setLabelMode( LabelMode mode ) { mMode = mode; }
183 
187  LabelMode labelMode() const { return mMode; }
188 
193  void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
194 
198  bool mergeConnectedLines() const { return mMergeLines; }
199 
204  void setUpsidedownLabels( UpsideDownLabels ud ) { mUpsidedownLabels = ud; }
205 
209  UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
210 
217  void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
218 
223  bool centroidInside() const { return mCentroidInside; }
224 
233  bool registerFeature( QgsLabelFeature *label );
234 
236  void joinConnectedFeatures();
237 
242  int connectedFeatureId( QgsFeatureId featureId ) const;
243 
245  void chopFeaturesAtRepeatDistance();
246 
247  protected:
249  QString mName;
250 
252  QLinkedList<FeaturePart *> mFeatureParts;
253 
255  QList<FeaturePart *> mObstacleParts;
256 
257  Pal *pal = nullptr;
258 
260 
262  bool mActive;
266 
269  LineArrangementFlags mArrangementFlags;
272 
274 
275  // indexes (spatial and id)
276  RTree<FeaturePart *, double, 2, double, 8, 4> *mFeatureIndex;
278  QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
279 
280  //obstacle r-tree
281  RTree<FeaturePart *, double, 2, double, 8, 4> *mObstacleIndex;
282 
283  QHash< QString, QLinkedList<FeaturePart *>* > mConnectedHashtable;
284  QStringList mConnectedTexts;
285  QHash< QgsFeatureId, int > mConnectedFeaturesIds;
286 
287  QMutex mMutex;
288 
302  Layer( QgsAbstractLabelProvider *provider, const QString &name, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );
303 
305  void addFeaturePart( FeaturePart *fpart, const QString &labelText = QString() );
306 
308  void addObstaclePart( FeaturePart *fpart );
309 
310  };
311 
312 } // end namespace pal
313 
314 #endif
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition: layer.h:150
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition: layer.h:90
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition: layer.h:223
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition: layer.h:255
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition: layer.h:285
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition: layer.h:198
bool mMergeLines
Definition: layer.h:271
bool mActive
Definition: layer.h:262
bool mLabelLayer
Definition: layer.h:263
QStringList mConnectedTexts
Definition: layer.h:284
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them) ...
Definition: layer.h:278
A set of features which influence the labeling process.
Definition: layer.h:63
double mDefaultPriority
Definition: layer.h:259
Main Pal labeling class.
Definition: pal.h:88
UpsideDownLabels
Definition: layer.h:74
void setUpsidedownLabels(UpsideDownLabels ud)
Sets how upside down labels will be handled within the layer.
Definition: layer.h:204
UpsideDownLabels upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition: layer.h:209
void setObstacleType(QgsPalLayerSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels...
Definition: layer.h:163
void setCentroidInside(bool forceInside)
Sets whether labels placed at the centroid of features within the layer are forced to be placed insid...
Definition: layer.h:217
QgsPalLayerSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels...
Definition: layer.h:156
QString mName
Definition: layer.h:249
bool displayAll() const
Definition: layer.h:83
QHash< QString, QLinkedList< FeaturePart * > *> mConnectedHashtable
Definition: layer.h:283
double priority() const
Returns the layer&#39;s priority, between 0 and 1.
Definition: layer.h:176
RTree< FeaturePart *, double, 2, double, 8, 4 > * mFeatureIndex
Definition: layer.h:276
void setArrangement(QgsPalLayerSettings::Placement arrangement)
Sets the layer&#39;s arrangement policy.
Definition: layer.h:109
QgsPalLayerSettings::Placement arrangement() const
Returns the layer&#39;s arrangement policy.
Definition: layer.h:99
int featureCount()
Returns the number of features in layer.
Definition: layer.h:87
bool mCentroidInside
Definition: layer.h:265
QgsPalLayerSettings::ObstacleType mObstacleType
Definition: layer.h:261
Main class to handle feature.
Definition: feature.h:95
The QgsAbstractLabelProvider class is an interface class.
LineArrangementFlags mArrangementFlags
Definition: layer.h:269
Placement
Placement modes which determine how label candidates are generated for a feature. ...
LabelMode
Definition: layer.h:73
bool mDisplayAll
Definition: layer.h:264
void setActive(bool active)
Sets whether the layer is currently active.
Definition: layer.h:132
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition: layer.h:145
QgsAbstractLabelProvider * mProvider
Definition: layer.h:248
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
QLinkedList< FeaturePart * > mFeatureParts
List of feature parts.
Definition: layer.h:252
RTree< FeaturePart *, double, 2, double, 8, 4 > * mObstacleIndex
Definition: layer.h:281
void setLabelMode(LabelMode mode)
Sets the layer&#39;s labeling mode.
Definition: layer.h:182
QMutex mMutex
Definition: layer.h:287
QString name() const
Returns the layer&#39;s name.
Definition: layer.h:94
bool active() const
Returns whether the layer is currently active.
Definition: layer.h:137
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
LabelMode mMode
Definition: layer.h:270
qint64 QgsFeatureId
Definition: qgsfeature.h:37
Representation of a labeling problem.
Definition: problem.h:107
void setArrangementFlags(LineArrangementFlags flags)
Sets the layer&#39;s arrangement flags.
Definition: layer.h:120
LineArrangementFlags arrangementFlags() const
Returns the layer&#39;s arrangement flags.
Definition: layer.h:114
QgsPalLayerSettings::Placement mArrangement
Optional flags used for some placement methods.
Definition: layer.h:268
LabelMode labelMode() const
Returns the layer&#39;s labeling mode.
Definition: layer.h:187
bool isCurved() const
Returns true if the layer has curved labels.
Definition: layer.h:103
UpsideDownLabels mUpsidedownLabels
Definition: layer.h:273
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition: layer.h:193
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels...