Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsvectorlayerlabelprovider.h 3 : : -------------------------------------- 4 : : Date : September 2015 5 : : Copyright : (C) 2015 by Martin Dobias 6 : : Email : wonder dot sk at gmail dot com 7 : : *************************************************************************** 8 : : * * 9 : : * This program is free software; you can redistribute it and/or modify * 10 : : * it under the terms of the GNU General Public License as published by * 11 : : * the Free Software Foundation; either version 2 of the License, or * 12 : : * (at your option) any later version. * 13 : : * * 14 : : ***************************************************************************/ 15 : : 16 : : #ifndef QGSVECTORLAYERLABELPROVIDER_H 17 : : #define QGSVECTORLAYERLABELPROVIDER_H 18 : : 19 : : #define SIP_NO_FILE 20 : : 21 : : #include "qgis_core.h" 22 : : #include "qgslabelingengine.h" 23 : : #include "qgsrenderer.h" 24 : : #include "qgstextrenderer.h" 25 : : 26 : : class QgsAbstractFeatureSource; 27 : : class QgsFeatureRenderer; 28 : : class QgsSymbol; 29 : : 30 : : /** 31 : : * \ingroup core 32 : : * \brief The QgsVectorLayerLabelProvider class implements a label provider 33 : : * for vector layers. Parameters for the labeling are taken from the layer's 34 : : * custom properties or from the given settings. 35 : : * 36 : : * \note this class is not a part of public API yet. See notes in QgsLabelingEngine 37 : : * \note not available in Python bindings 38 : : * \since QGIS 2.12 39 : : */ 40 : : class CORE_EXPORT QgsVectorLayerLabelProvider : public QgsAbstractLabelProvider 41 : : { 42 : : public: 43 : : 44 : : //! Convenience constructor to initialize the provider from given vector layer 45 : : explicit QgsVectorLayerLabelProvider( QgsVectorLayer *layer, 46 : : const QString &providerId, 47 : : bool withFeatureLoop, 48 : : const QgsPalLayerSettings *settings, 49 : : const QString &layerName = QString() ); 50 : : 51 : : //! Constructor to initialize the provider from any map layer (e.g. vector tile layer) 52 : : explicit QgsVectorLayerLabelProvider( QgsWkbTypes::GeometryType geometryType, 53 : : const QgsFields &fields, 54 : : const QgsCoordinateReferenceSystem &crs, 55 : : const QString &providerId, 56 : : const QgsPalLayerSettings *settings, 57 : : QgsMapLayer *layer, 58 : : const QString &layerName = QString() ); 59 : : 60 : : ~QgsVectorLayerLabelProvider() override; 61 : : 62 : : QList<QgsLabelFeature *> labelFeatures( QgsRenderContext &context ) override; 63 : : 64 : : void drawLabelBackground( QgsRenderContext &context, pal::LabelPosition *label ) const override; 65 : : void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const override; 66 : : void drawUnplacedLabel( QgsRenderContext &context, pal::LabelPosition *label ) const override; 67 : : void startRender( QgsRenderContext &context ) override; 68 : : void stopRender( QgsRenderContext &context ) override; 69 : : 70 : : // new virtual methods 71 : : 72 : : /** 73 : : * Prepare for registration of features. Must be called after provider has been added to engine (uses its map settings) 74 : : * \param context render context. 75 : : * \param attributeNames list of attribute names to which additional required attributes shall be added 76 : : * \returns Whether the preparation was successful - if not, the provider shall not be used 77 : : */ 78 : : virtual bool prepare( QgsRenderContext &context, QSet<QString> &attributeNames ); 79 : : 80 : : /** 81 : : * Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels 82 : : * 83 : : * \param feature feature to label 84 : : * \param context render context. The QgsExpressionContext contained within the render context 85 : : * must have already had the feature and fields sets prior to calling this method. 86 : : * \param obstacleGeometry optional obstacle geometry, if a different geometry to the feature's geometry 87 : : * should be used as an obstacle for labels (e.g., if the feature has been rendered with an offset point 88 : : * symbol, the obstacle geometry should represent the bounds of the offset symbol). If not set, 89 : : * the feature's original geometry will be used as an obstacle for labels. 90 : : * \param symbol feature symbol to label (ownership is not transferred - the symbol must exist until after labeling is complete) 91 : : */ 92 : : virtual void registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry = QgsGeometry(), const QgsSymbol *symbol = nullptr ); 93 : : 94 : : /** 95 : : * Returns the geometry for a point feature which should be used as an obstacle for labels. This 96 : : * obstacle geometry will respect the dimensions and offsets of the symbol used to render the 97 : : * point, and ensures that labels will not overlap large or offset points. 98 : : * \param fet point feature 99 : : * \param context render context 100 : : * \param symbols symbols rendered for point feature 101 : : * \since QGIS 2.14 102 : : */ 103 : : static QgsGeometry getPointObstacleGeometry( QgsFeature &fet, QgsRenderContext &context, const QgsSymbolList &symbols ); 104 : : 105 : : /** 106 : : * Returns the layer's settings. 107 : : * \since QGIS 3.10 108 : : */ 109 : : const QgsPalLayerSettings &settings() const; 110 : : 111 : : /** 112 : : * Sets fields of this label provider. Normally this is not needed, but when used for vector tiles, 113 : : * fields are not known at the time of creation of label providers. It should be called before 114 : : * a call to prepare() which uses the list of fields. 115 : : * \since QGIS 3.14 116 : : */ 117 : 0 : void setFields( const QgsFields &fields ) { mFields = fields; } 118 : : 119 : : protected: 120 : : //! initialization method - called from constructors 121 : : void init(); 122 : : //! Internal label drawing method 123 : : void drawLabelPrivate( pal::LabelPosition *label, QgsRenderContext &context, QgsPalLayerSettings &tmpLyr, QgsTextRenderer::TextPart drawType, double dpiRatio = 1.0 ) const; 124 : : 125 : : protected: 126 : : //! Layer's labeling configuration 127 : : QgsPalLayerSettings mSettings; 128 : : //! Geometry type of layer 129 : : QgsWkbTypes::GeometryType mLayerGeometryType; 130 : : 131 : : QgsFeatureRenderer *mRenderer = nullptr; 132 : : 133 : : // these are needed only if using own renderer loop 134 : : 135 : : //! Layer's fields 136 : : QgsFields mFields; 137 : : //! Layer's CRS 138 : : QgsCoordinateReferenceSystem mCrs; 139 : : //! Layer's feature source 140 : : std::unique_ptr<QgsAbstractFeatureSource> mSource; 141 : : 142 : : //! List of generated 143 : : QList<QgsLabelFeature *> mLabels; 144 : : 145 : : private: 146 : : 147 : : friend class TestQgsLabelingEngine; 148 : : void drawCallout( QgsRenderContext &context, pal::LabelPosition *label ) const; 149 : : }; 150 : : 151 : : #endif // QGSVECTORLAYERLABELPROVIDER_H