QGIS API Documentation  2.99.0-Master (d55fa22)
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 #include "qgis_core.h"
34 #include "pal.h" // for LineArrangementFlags enum
35 #include "rtree.hpp"
36 #include <QMutex>
37 #include <QLinkedList>
38 #include <QHash>
39 #include <fstream>
40 
41 class QgsLabelFeature;
42 
43 namespace pal
44 {
45 
47  template<class DATATYPE, class ELEMTYPE, int NUMDIMS, class ELEMTYPEREAL, int TMAXNODES, int TMINNODES> class RTree;
49 
50  class FeaturePart;
51  class Pal;
52  class LabelInfo;
53 
60  class CORE_EXPORT Layer
61  {
62  friend class Pal;
63  friend class FeaturePart;
64 
65  friend class Problem;
66 
67  friend class LabelPosition;
68 
69  public:
70  enum LabelMode { LabelPerFeature, LabelPerFeaturePart };
72  {
73  Upright, // upside-down labels (90 <= angle < 270) are shown upright
74  ShowDefined, // show upside down when rotation is layer- or data-defined
75  ShowAll // show upside down for all labels, including dynamic ones
76  };
77 
78  virtual ~Layer();
79 
80  bool displayAll() const { return mDisplayAll; }
81 
84  int featureCount() { return mHashtable.size(); }
85 
87  QgsAbstractLabelProvider *provider() const { return mProvider; }
88 
91  QString name() const { return mName; }
92 
96  QgsPalLayerSettings::Placement arrangement() const { return mArrangement; }
97 
100  bool isCurved() const { return mArrangement == QgsPalLayerSettings::Curved || mArrangement == QgsPalLayerSettings::PerimeterCurved; }
101 
106  void setArrangement( QgsPalLayerSettings::Placement arrangement ) { mArrangement = arrangement; }
107 
111  LineArrangementFlags arrangementFlags() const { return mArrangementFlags; }
112 
117  void setArrangementFlags( LineArrangementFlags flags ) { mArrangementFlags = flags; }
118 
129  void setActive( bool active ) { mActive = active; }
130 
134  bool active() const { return mActive; }
135 
142  void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
143 
147  bool labelLayer() const { return mLabelLayer; }
148 
153  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
154 
160  void setObstacleType( QgsPalLayerSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
161 
167  void setPriority( double priority );
168 
173  double priority() const { return mDefaultPriority; }
174 
179  void setLabelMode( LabelMode mode ) { mMode = mode; }
180 
184  LabelMode labelMode() const { return mMode; }
185 
190  void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
191 
195  bool mergeConnectedLines() const { return mMergeLines; }
196 
201  void setUpsidedownLabels( UpsideDownLabels ud ) { mUpsidedownLabels = ud; }
202 
206  UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
207 
214  void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
215 
220  bool centroidInside() const { return mCentroidInside; }
221 
230  bool registerFeature( QgsLabelFeature *label );
231 
233  void joinConnectedFeatures();
234 
239  int connectedFeatureId( QgsFeatureId featureId ) const;
240 
242  void chopFeaturesAtRepeatDistance();
243 
244  protected:
246  QString mName;
247 
249  QLinkedList<FeaturePart *> mFeatureParts;
250 
252  QList<FeaturePart *> mObstacleParts;
253 
254  Pal *pal = nullptr;
255 
257 
259  bool mActive;
263 
266  LineArrangementFlags mArrangementFlags;
269 
271 
272  // indexes (spatial and id)
273  RTree<FeaturePart *, double, 2, double, 8, 4> *mFeatureIndex;
275  QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
276 
277  //obstacle r-tree
278  RTree<FeaturePart *, double, 2, double, 8, 4> *mObstacleIndex;
279 
280  QHash< QString, QLinkedList<FeaturePart *>* > mConnectedHashtable;
281  QStringList mConnectedTexts;
282  QHash< QgsFeatureId, int > mConnectedFeaturesIds;
283 
284  QMutex mMutex;
285 
299  Layer( QgsAbstractLabelProvider *provider, const QString &name, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );
300 
302  void addFeaturePart( FeaturePart *fpart, const QString &labelText = QString() );
303 
305  void addObstaclePart( FeaturePart *fpart );
306 
307  };
308 
309 } // end namespace pal
310 
311 #endif
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition: layer.h:147
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition: layer.h:87
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition: layer.h:220
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition: layer.h:252
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition: layer.h:282
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition: layer.h:195
bool mMergeLines
Definition: layer.h:268
bool mActive
Definition: layer.h:259
bool mLabelLayer
Definition: layer.h:260
QStringList mConnectedTexts
Definition: layer.h:281
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them) ...
Definition: layer.h:275
A set of features which influence the labeling process.
Definition: layer.h:60
double mDefaultPriority
Definition: layer.h:256
Main Pal labeling class.
Definition: pal.h:85
UpsideDownLabels
Definition: layer.h:71
void setUpsidedownLabels(UpsideDownLabels ud)
Sets how upside down labels will be handled within the layer.
Definition: layer.h:201
UpsideDownLabels upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition: layer.h:206
void setObstacleType(QgsPalLayerSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels...
Definition: layer.h:160
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:214
QgsPalLayerSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels...
Definition: layer.h:153
QString mName
Definition: layer.h:246
bool displayAll() const
Definition: layer.h:80
QHash< QString, QLinkedList< FeaturePart * > *> mConnectedHashtable
Definition: layer.h:280
double priority() const
Returns the layer&#39;s priority, between 0 and 1.
Definition: layer.h:173
RTree< FeaturePart *, double, 2, double, 8, 4 > * mFeatureIndex
Definition: layer.h:273
void setArrangement(QgsPalLayerSettings::Placement arrangement)
Sets the layer&#39;s arrangement policy.
Definition: layer.h:106
QgsPalLayerSettings::Placement arrangement() const
Returns the layer&#39;s arrangement policy.
Definition: layer.h:96
int featureCount()
Returns the number of features in layer.
Definition: layer.h:84
bool mCentroidInside
Definition: layer.h:262
QgsPalLayerSettings::ObstacleType mObstacleType
Definition: layer.h:258
Main class to handle feature.
Definition: feature.h:92
The QgsAbstractLabelProvider class is an interface class.
LineArrangementFlags mArrangementFlags
Definition: layer.h:266
Placement
Placement modes which determine how label candidates are generated for a feature. ...
LabelMode
Definition: layer.h:70
bool mDisplayAll
Definition: layer.h:261
void setActive(bool active)
Sets whether the layer is currently active.
Definition: layer.h:129
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition: layer.h:142
QgsAbstractLabelProvider * mProvider
Definition: layer.h:245
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
QLinkedList< FeaturePart * > mFeatureParts
List of feature parts.
Definition: layer.h:249
RTree< FeaturePart *, double, 2, double, 8, 4 > * mObstacleIndex
Definition: layer.h:278
void setLabelMode(LabelMode mode)
Sets the layer&#39;s labeling mode.
Definition: layer.h:179
QMutex mMutex
Definition: layer.h:284
QString name() const
Returns the layer&#39;s name.
Definition: layer.h:91
bool active() const
Returns whether the layer is currently active.
Definition: layer.h:134
LabelPosition is a candidate feature label position.
Definition: labelposition.h:52
LabelMode mMode
Definition: layer.h:267
qint64 QgsFeatureId
Definition: qgsfeature.h:37
Representation of a labeling problem.
Definition: problem.h:104
void setArrangementFlags(LineArrangementFlags flags)
Sets the layer&#39;s arrangement flags.
Definition: layer.h:117
LineArrangementFlags arrangementFlags() const
Returns the layer&#39;s arrangement flags.
Definition: layer.h:111
QgsPalLayerSettings::Placement mArrangement
Optional flags used for some placement methods.
Definition: layer.h:265
LabelMode labelMode() const
Returns the layer&#39;s labeling mode.
Definition: layer.h:184
bool isCurved() const
Returns true if the layer has curved labels.
Definition: layer.h:100
UpsideDownLabels mUpsidedownLabels
Definition: layer.h:270
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition: layer.h:190
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels...