1 /***************************************************************************
2                           qgsmaplayer.h  -  description
3                              -------------------
4     begin                : Fri Jun 28 2002
5     copyright            : (C) 2002 by Gary E.Sherman
6     email                : sherman at mrcc.com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef QGSMAPLAYER_H
19 #define QGSMAPLAYER_H
20 
21 #include "qgis_core.h"
22 #include <QDateTime>
23 #include <QDomNode>
24 #include <QImage>
25 #include <QObject>
26 #include <QPainter>
27 #include <QUndoStack>
28 #include <QVariant>
29 #include <QIcon>
30 #include <QSet>
31 
32 #include "qgis_sip.h"
33 #include "qgserror.h"
34 #include "qgsobjectcustomproperties.h"
35 #include "qgsrectangle.h"
36 #include "qgscoordinatereferencesystem.h"
37 #include "qgsmaplayerdependency.h"
38 #include "qgslayermetadata.h"
39 #include "qgsmaplayerserverproperties.h"
40 #include "qgsmaplayerstyle.h"
41 #include "qgsreadwritecontext.h"
42 #include "qgsdataprovider.h"
43 #include "qgis.h"
44 #include "qgslogger.h"
45 
46 class QgsAbstract3DRenderer;
47 class QgsDataProvider;
48 class QgsMapLayerLegend;
49 class QgsMapLayerRenderer;
50 class QgsMapLayerStyleManager;
51 class QgsProject;
52 class QgsStyleEntityVisitorInterface;
53 class QgsMapLayerTemporalProperties;
54 class QgsMapLayerElevationProperties;
55 
56 class QDomDocument;
57 class QKeyEvent;
58 class QPainter;
59 class QgsRenderContext;
60 
61 /*
62  * Constants used to describe copy-paste MIME types
63  */
64 #define QGSCLIPBOARD_MAPLAYER_MIME "application/qgis.maplayer"
65 
66 
67 /**
68  * \ingroup core
69  * \brief Base class for all map layer types.
70  * This is the base class for all map layer types (vector, raster).
71  */
72 class CORE_EXPORT QgsMapLayer : public QObject
73 {
74     Q_OBJECT
75 
76     Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
77     Q_PROPERTY( int autoRefreshInterval READ autoRefreshInterval WRITE setAutoRefreshInterval NOTIFY autoRefreshIntervalChanged )
78     Q_PROPERTY( QgsLayerMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )
79     Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
80     Q_PROPERTY( QgsMapLayerType type READ type CONSTANT )
81     Q_PROPERTY( bool isValid READ isValid NOTIFY isValidChanged )
82     Q_PROPERTY( double opacity READ opacity WRITE setOpacity NOTIFY opacityChanged )
83 
84 #ifdef SIP_RUN
85     SIP_CONVERT_TO_SUBCLASS_CODE
86     QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sipCpp );
87 
88     sipType = 0;
89 
90     if ( layer )
91     {
92       switch ( layer->type() )
93       {
94         case QgsMapLayerType::VectorLayer:
95           sipType = sipType_QgsVectorLayer;
96           break;
97         case QgsMapLayerType::RasterLayer:
98           sipType = sipType_QgsRasterLayer;
99           break;
100         case QgsMapLayerType::PluginLayer:
101           sipType = sipType_QgsPluginLayer;
102           break;
103         case QgsMapLayerType::MeshLayer:
104           sipType = sipType_QgsMeshLayer;
105           break;
106         case QgsMapLayerType::VectorTileLayer:
107           sipType = sipType_QgsVectorTileLayer;
108           break;
109         case QgsMapLayerType::AnnotationLayer:
110           sipType = sipType_QgsAnnotationLayer;
111           break;
112         case QgsMapLayerType::PointCloudLayer:
113           sipType = sipType_QgsPointCloudLayer;
114           break;
115         default:
116           sipType = nullptr;
117           break;
118       }
119     }
120     SIP_END
121 #endif
122 
123   public:
124 
125     /**
126      * Maplayer has a style and a metadata property
127      * \since QGIS 3.0
128      */
129     enum PropertyType
130     {
131       Style = 0,
132       Metadata,
133     };
134 
135     /**
136      * Flags for the map layer
137      * \note Flags are options specified by the user used for the UI but are not preventing any API call.
138      * \since QGIS 3.4
139      */
140     enum LayerFlag
141     {
142       Identifiable = 1 << 0, //!< If the layer is identifiable using the identify map tool and as a WMS layer.
143       Removable = 1 << 1,    //!< If the layer can be removed from the project. The layer will not be removable from the legend menu entry but can still be removed with an API call.
144       Searchable = 1 << 2,   //!< Only for vector-layer, determines if the layer is used in the 'search all layers' locator.
145       Private = 1 << 3,       //!< Determines if the layer is meant to be exposed to the GUI, i.e. visible in the layer legend tree.
146     };
147     Q_ENUM( LayerFlag )
148     Q_DECLARE_FLAGS( LayerFlags, LayerFlag )
149     Q_FLAG( LayerFlags )
150 
151     /**
152      * Categories of style to distinguish appropriate sections for import/export
153      * \since QGIS 3.4
154      */
155     enum StyleCategory
156     {
157       LayerConfiguration = 1 << 0,  //!< General configuration: identifiable, removable, searchable, display expression, read-only
158       Symbology          = 1 << 1,  //!< Symbology
159       Symbology3D        = 1 << 2,  //!< 3D symbology
160       Labeling           = 1 << 3,  //!< Labeling
161       Fields             = 1 << 4,  //!< Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields
162       Forms              = 1 << 5,  //!< Feature form
163       Actions            = 1 << 6,  //!< Actions
164       MapTips            = 1 << 7,  //!< Map tips
165       Diagrams           = 1 << 8,  //!< Diagrams
166       AttributeTable     = 1 << 9,  //!< Attribute table settings: choice and order of columns, conditional styling
167       Rendering          = 1 << 10, //!< Rendering: scale visibility, simplify method, opacity
168       CustomProperties   = 1 << 11, //!< Custom properties (by plugins for instance)
169       GeometryOptions    = 1 << 12, //!< Geometry validation configuration
170       Relations          = 1 << 13, //!< Relations
171       Temporal           = 1 << 14, //!< Temporal properties (since QGIS 3.14)
172       Legend             = 1 << 15, //!< Legend settings (since QGIS 3.16)
173       Elevation          = 1 << 16, //!< Elevation settings (since QGIS 3.18)
174       Notes              = 1 << 17, //!< Layer user notes (since QGIS 3.20)
175       AllStyleCategories = LayerConfiguration | Symbology | Symbology3D | Labeling | Fields | Forms | Actions |
176                            MapTips | Diagrams | AttributeTable | Rendering | CustomProperties | GeometryOptions | Relations | Temporal | Legend | Elevation | Notes,
177     };
178     Q_ENUM( StyleCategory )
179     Q_DECLARE_FLAGS( StyleCategories, StyleCategory )
180     Q_FLAG( StyleCategories )
181 
182     /**
183      * Constructor for QgsMapLayer
184      * \param type layer type
185      * \param name display name for the layer
186      * \param source datasource of layer
187      */
188     QgsMapLayer( QgsMapLayerType type = QgsMapLayerType::VectorLayer, const QString &name = QString(), const QString &source = QString() );
189 
190     ~QgsMapLayer() override;
191 
192     //! QgsMapLayer cannot be copied
193     QgsMapLayer( QgsMapLayer const & ) = delete;
194     //! QgsMapLayer cannot be copied
195     QgsMapLayer &operator=( QgsMapLayer const & ) = delete;
196 
197     /**
198      * Returns a new instance equivalent to this one except for the id which
199      *  is still unique.
200      * \returns a new layer instance
201      * \since QGIS 3.0
202      */
203     virtual QgsMapLayer *clone() const = 0;
204 
205     /**
206      * Returns the type of the layer.
207      */
208     QgsMapLayerType type() const;
209 
210     /**
211      * Returns the flags for this layer.
212      * \note Flags are options specified by the user used for the UI but are not preventing any API call.
213      * For instance, even if the Removable flag is not set, the layer can still be removed with the API
214      * but the action will not be listed in the legend menu.
215      *
216      * \see properties()
217      *
218      * \since QGIS 3.4
219      */
220     QgsMapLayer::LayerFlags flags() const;
221 
222     /**
223      * Returns the flags for this layer.
224      * \note Flags are options specified by the user used for the UI but are not preventing any API call.
225      * For instance, even if the Removable flag is not set, the layer can still be removed with the API
226      * but the action will not be listed in the legend menu.
227      *
228      * \see properties()
229      *
230      * \since QGIS 3.4
231      */
232     void setFlags( QgsMapLayer::LayerFlags flags );
233 
234     /**
235      * Returns the map layer properties of this layer.
236      *
237      * \note properties() differ from flags() in that flags() are user settable, and reflect options that
238      * users can enable for map layers. In contrast properties() are reflections of inherent capabilities
239      * for the layer, which cannot be directly changed by users.
240      *
241      * \since QGIS 3.22
242      */
243     virtual Qgis::MapLayerProperties properties() const;
244 
245     /**
246      * Returns the extension of a Property.
247      * \returns The extension
248      * \since QGIS 3.0
249      */
250     static QString extensionPropertyType( PropertyType type );
251 
252     //! Returns the layer's unique ID, which is used to access this layer from QgsProject.
253     QString id() const;
254 
255     /**
256      * Set the display \a name of the layer.
257      * \see name()
258      * \since QGIS 2.16
259      */
260     void setName( const QString &name );
261 
262     /**
263      * Returns the display name of the layer.
264      * \see setName()
265      */
266     QString name() const;
267 
268     /**
269      * Returns the layer's data provider, it may be NULLPTR.
270      */
271     Q_INVOKABLE virtual QgsDataProvider *dataProvider();
272 
273     /**
274      * Returns the layer's data provider in a const-correct manner, it may be NULLPTR.
275      * \note not available in Python bindings
276      */
277     virtual const QgsDataProvider *dataProvider() const SIP_SKIP;
278 
279     /**
280      * Sets the short name of the layer
281      *  used by QGIS Server to identify the layer.
282      * \returns the layer short name
283      * \see shortName()
284      */
setShortName(const QString & shortName)285     void setShortName( const QString &shortName ) { mShortName = shortName; }
286 
287     /**
288      * Returns the short name of the layer
289      *  used by QGIS Server to identify the layer.
290      * \see setShortName()
291      */
292     QString shortName() const;
293 
294     /**
295      * Sets the title of the layer
296      *  used by QGIS Server in GetCapabilities request.
297      * \see title()
298      */
setTitle(const QString & title)299     void setTitle( const QString &title ) { mTitle = title; }
300 
301     /**
302      * Returns the title of the layer
303      *  used by QGIS Server in GetCapabilities request.
304      * \returns the layer title
305      * \see setTitle()
306      */
title()307     QString title() const { return mTitle; }
308 
309     /**
310      * Sets the abstract of the layer
311      *  used by QGIS Server in GetCapabilities request.
312      * \returns the layer abstract
313      * \see abstract()
314      */
setAbstract(const QString & abstract)315     void setAbstract( const QString &abstract ) { mAbstract = abstract; }
316 
317     /**
318      * Returns the abstract of the layer
319      *  used by QGIS Server in GetCapabilities request.
320      * \returns the layer abstract
321      * \see setAbstract()
322      */
abstract()323     QString abstract() const { return mAbstract; }
324 
325     /**
326      * Sets the keyword list of the layer
327      *  used by QGIS Server in GetCapabilities request.
328      * \returns the layer keyword list
329      * \see keywordList()
330      */
setKeywordList(const QString & keywords)331     void setKeywordList( const QString &keywords ) { mKeywordList = keywords; }
332 
333     /**
334      * Returns the keyword list of the layer
335      *  used by QGIS Server in GetCapabilities request.
336      * \returns the layer keyword list
337      * \see setKeywordList()
338      */
keywordList()339     QString keywordList() const { return mKeywordList; }
340 
341     /* Layer dataUrl information */
342 
343     /**
344      * Sets the DataUrl of the layer
345      *  used by QGIS Server in GetCapabilities request.
346      *  DataUrl is a a link to the underlying data represented by a particular layer.
347      * \returns the layer DataUrl
348      * \see dataUrl()
349      */
setDataUrl(const QString & dataUrl)350     void setDataUrl( const QString &dataUrl ) { mDataUrl = dataUrl; }
351 
352     /**
353      * Returns the DataUrl of the layer
354      *  used by QGIS Server in GetCapabilities request.
355      *  DataUrl is a a link to the underlying data represented by a particular layer.
356      * \returns the layer DataUrl
357      * \see setDataUrl()
358      */
dataUrl()359     QString dataUrl() const { return mDataUrl; }
360 
361     /**
362      * Sets the DataUrl format of the layer
363      *  used by QGIS Server in GetCapabilities request.
364      *  DataUrl is a a link to the underlying data represented by a particular layer.
365      * \returns the layer DataUrl format
366      * \see dataUrlFormat()
367      */
setDataUrlFormat(const QString & dataUrlFormat)368     void setDataUrlFormat( const QString &dataUrlFormat ) { mDataUrlFormat = dataUrlFormat; }
369 
370     /**
371      * Returns the DataUrl format of the layer
372      *  used by QGIS Server in GetCapabilities request.
373      *  DataUrl is a a link to the underlying data represented by a particular layer.
374      * \returns the layer DataUrl format
375      * \see setDataUrlFormat()
376      */
dataUrlFormat()377     QString dataUrlFormat() const { return mDataUrlFormat; }
378 
379     /* Layer attribution information */
380 
381     /**
382      * Sets the attribution of the layer
383      *  used by QGIS Server in GetCapabilities request.
384      *  Attribution indicates the provider of a layer or collection of layers.
385      * \returns the layer attribution
386      * \see attribution()
387      */
setAttribution(const QString & attrib)388     void setAttribution( const QString &attrib ) { mAttribution = attrib; }
389 
390     /**
391      * Returns the attribution of the layer
392      *  used by QGIS Server in GetCapabilities request.
393      *  Attribution indicates the provider of a layer or collection of layers.
394      * \returns the layer attribution
395      * \see setAttribution()
396      */
attribution()397     QString attribution() const { return mAttribution; }
398 
399     /**
400      * Sets the attribution URL of the layer
401      *  used by QGIS Server in GetCapabilities request.
402      *  Attribution indicates the provider of a layer or collection of layers.
403      * \returns the layer attribution URL
404      * \see attributionUrl()
405      */
setAttributionUrl(const QString & attribUrl)406     void setAttributionUrl( const QString &attribUrl ) { mAttributionUrl = attribUrl; }
407 
408     /**
409      * Returns the attribution URL of the layer
410      *  used by QGIS Server in GetCapabilities request.
411      *  Attribution indicates the provider of a layer or collection of layers.
412      * \returns the layer attribution URL
413      * \see setAttributionUrl()
414      */
attributionUrl()415     QString attributionUrl() const { return mAttributionUrl; }
416 
417     /* Layer metadataUrl information */
418 
419     /**
420      * Returns QGIS Server Properties for the map layer
421      * \since QGIS 3.22
422      */
serverProperties()423     QgsMapLayerServerProperties *serverProperties() { return mServerProperties.get(); };
424 
425     /**
426      * Returns QGIS Server Properties const for the map layer
427      * \since QGIS 3.22
428      */
serverProperties()429     const QgsMapLayerServerProperties *serverProperties() const { return mServerProperties.get(); } SIP_SKIP;
430 
431     /**
432      * Sets the metadata URL of the layer
433      *  used by QGIS Server in GetCapabilities request.
434      *  MetadataUrl is a a link to the detailed, standardized metadata about the data.
435      *  Since QGIS 3.22, it edits the first metadata URL link.
436      * \see serverProperties()
437      * \deprecated since QGIS 3.22
438      */
439     Q_DECL_DEPRECATED void setMetadataUrl( const QString &metaUrl ) SIP_DEPRECATED;
440 
441     /**
442      * Returns the metadata URL of the layer
443      *  used by QGIS Server in GetCapabilities request.
444      *  MetadataUrl is a a link to the detailed, standardized metadata about the data.
445      * Since QGIS 3.22, it returns the first metadata URL link.
446      * \returns the layer metadata URL
447      * \see serverProperties()
448      * \deprecated since QGIS 3.22
449      */
450     Q_DECL_DEPRECATED QString metadataUrl() const SIP_DEPRECATED;
451 
452     /**
453      * Set the metadata type of the layer
454      *  used by QGIS Server in GetCapabilities request
455      *  MetadataUrlType indicates the standard to which the metadata complies.
456      *  Since QGIS 3.22, it edits the first metadata URL type.
457      * \returns the layer metadata type
458      * \see serverProperties()
459      * \deprecated since QGIS 3.22
460      */
461     Q_DECL_DEPRECATED void setMetadataUrlType( const QString &metaUrlType ) SIP_DEPRECATED;
462 
463     /**
464      * Returns the metadata type of the layer
465      *  used by QGIS Server in GetCapabilities request.
466      *  MetadataUrlType indicates the standard to which the metadata complies.
467      * Since QGIS 3.22, it returns the first metadata URL type.
468      * \returns the layer metadata type
469      * \see serverProperties()
470      * \deprecated since QGIS 3.22
471      */
472     Q_DECL_DEPRECATED QString metadataUrlType() const SIP_DEPRECATED;
473 
474     /**
475      * Sets the metadata format of the layer
476      *  used by QGIS Server in GetCapabilities request.
477      *  MetadataUrlType indicates how the metadata is structured.
478      *  Since QGIS 3.22, it edits the first metadata URL format.
479      * \see serverProperties()
480      * \deprecated since QGIS 3.22
481      */
482     Q_DECL_DEPRECATED void setMetadataUrlFormat( const QString &metaUrlFormat ) SIP_DEPRECATED;
483 
484     /**
485      * Returns the metadata format of the layer
486      *  used by QGIS Server in GetCapabilities request.
487      *  MetadataUrlType indicates how the metadata is structured.
488      * Since QGIS 3.22, it returns the first metadata URL format.
489      * \returns the layer metadata format
490      * \see serverProperties()
491      * \deprecated since QGIS 3.22
492      */
493     Q_DECL_DEPRECATED QString metadataUrlFormat() const SIP_DEPRECATED;
494 
495     /**
496      * Set the blending mode used for rendering a layer.
497      * \param blendMode new blending mode
498      * \see blendMode()
499     */
500     void setBlendMode( QPainter::CompositionMode blendMode );
501 
502     /**
503      * Returns the current blending mode for a layer.
504      * \see setBlendMode()
505     */
506     QPainter::CompositionMode blendMode() const;
507 
508     /**
509      * Sets the \a opacity for the layer, where \a opacity is a value between 0 (totally transparent)
510      * and 1.0 (fully opaque).
511      * \see opacity()
512      * \see opacityChanged()
513      * \note Prior to QGIS 3.18, this method was available for vector layers only
514      * \since QGIS 3.18
515      */
516     virtual void setOpacity( double opacity );
517 
518     /**
519      * Returns the opacity for the layer, where opacity is a value between 0 (totally transparent)
520      * and 1.0 (fully opaque).
521      * \see setOpacity()
522      * \see opacityChanged()
523      * \note Prior to QGIS 3.18, this method was available for vector layers only
524      * \since QGIS 3.18
525      */
526     virtual double opacity() const;
527 
528     //! Returns if this layer is read only.
readOnly()529     bool readOnly() const { return isReadOnly(); }
530 
531     /**
532      * Synchronises with changes in the datasource
533      */
reload()534     Q_INVOKABLE virtual void reload() {}
535 
536     /**
537      * Returns new instance of QgsMapLayerRenderer that will be used for rendering of given context
538      * \since QGIS 2.4
539      */
540     virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) = 0 SIP_FACTORY;
541 
542     //! Returns the extent of the layer.
543     virtual QgsRectangle extent() const;
544 
545     /**
546      * Returns the WGS84 extent (EPSG:4326) of the layer according to
547      * ReadFlag::FlagTrustLayerMetadata. If that flag is activated, then the
548      * WGS84 extent read in the qgs project is returned. Otherwise, the actual
549      * WGS84 extent is returned.
550      * \param forceRecalculate True to return the current WGS84 extent whatever the read flags
551      * \since QGIS 3.20
552      */
553     QgsRectangle wgs84Extent( bool forceRecalculate = false ) const;
554 
555     /**
556      * Returns the status of the layer. An invalid layer is one which has a bad datasource
557      * or other problem. Child classes set this flag when initialized.
558      * \returns TRUE if the layer is valid and can be accessed
559      */
560     bool isValid() const;
561 
562     /**
563      * Gets a version of the internal layer definition that has sensitive
564       *  bits removed (for example, the password). This function should
565       * be used when displaying the source name for general viewing.
566       * \see source()
567      */
568     QString publicSource() const;
569 
570     /**
571      * Returns the source for the layer. This source may contain usernames, passwords
572      * and other sensitive information.
573      * \see publicSource()
574      */
575     QString source() const;
576 
577     /**
578      * Returns the sublayers of this layer.
579      * (Useful for providers that manage their own layers, such as WMS).
580      */
581     virtual QStringList subLayers() const;
582 
583     /**
584      * Reorders the *previously selected* sublayers of this layer from bottom to top.
585      * (Useful for providers that manage their own layers, such as WMS).
586      */
587     virtual void setLayerOrder( const QStringList &layers );
588 
589     /**
590      * Set the visibility of the given sublayer name.
591      * \param name sublayer name
592      * \param visible sublayer visibility
593     */
594     virtual void setSubLayerVisibility( const QString &name, bool visible );
595 
596     /**
597      * Returns whether the layer supports editing or not.
598      * \returns FALSE if the layer is read only or the data provider has no editing capabilities.
599      * \note default implementation returns FALSE.
600      * \since QGIS 3.22 in the base class QgsMapLayer.
601      */
602     virtual bool supportsEditing() const;
603 
604     //! Returns TRUE if the layer can be edited.
605     virtual bool isEditable() const;
606 
607     /**
608      * Returns TRUE if the layer has been modified since last commit/save.
609      * \note default implementation returns FALSE.
610      * \since QGIS 3.22 in the base class QgsMapLayer.
611      */
612     virtual bool isModified() const;
613 
614     /**
615      * Returns TRUE if the layer is considered a spatial layer, ie it has some form of geometry associated with it.
616      * \since QGIS 2.16
617      */
618     virtual bool isSpatial() const;
619 
620     /**
621      * Returns TRUE if the layer is considered a temporary layer.
622      *
623      * These include memory-only layers such as those created by the "memory" data provider, layers
624      * stored inside a local temporary folder (such as the "/tmp" folder on Linux)
625      * or layer with temporary data (as temporary mesh layer dataset)
626      *
627      * \since QGIS 3.10.1
628      */
629     virtual bool isTemporary() const;
630 
631     /**
632      * Flags which control project read behavior.
633      * \since QGIS 3.10
634      */
635     enum ReadFlag
636     {
637       FlagDontResolveLayers = 1 << 0, //!< Don't resolve layer paths or create data providers for layers.
638       FlagTrustLayerMetadata = 1 << 1, //!< Trust layer metadata. Improves layer load time by skipping expensive checks like primary key unicity, geometry type and srid and by using estimated metadata on layer load. Since QGIS 3.16
639       FlagReadExtentFromXml = 1 << 2, //!< Read extent from xml and skip get extent from provider.
640     };
641     Q_DECLARE_FLAGS( ReadFlags, ReadFlag )
642 
643     /**
644      * Sets state from DOM document
645      * \param layerElement The DOM element corresponding to ``maplayer'' tag
646      * \param context writing context (e.g. for conversion between relative and absolute paths)
647      * \param flags optional argument which can be used to control layer reading behavior.
648      * \note
649      *
650      * The DOM node corresponds to a DOM document project file XML element read
651      * by QgsProject.
652      *
653      * This, in turn, calls readXml() (and then readSymbology()), which is overridable
654      * by sub-classes so that they can read their own specific state from the given DOM node.
655      *
656      * Invoked by QgsProject::read().
657      *
658      * \returns TRUE if successful
659      */
660     bool readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags = QgsMapLayer::ReadFlags() );
661 
662     /**
663      * Stores state in DOM node
664      * \param layerElement is a DOM element corresponding to ``maplayer'' tag
665      * \param document is a the DOM document being written
666      * \param context reading context (e.g. for conversion between relative and absolute paths)
667      * \note
668      *
669      * The DOM node corresponds to a DOM document project file XML element to be
670      * written by QgsProject.
671      *
672      * This, in turn, calls writeXml() (and then writeSymbology), which is over-rideable
673      * by sub-classes so that they can write their own specific state to the given DOM node.
674      *
675      * Invoked by QgsProject::write().
676      *
677      * \returns TRUE if successful
678      */
679     bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
680 
681     /**
682      * Resolve references to other layers (kept as layer IDs after reading XML) into layer objects.
683      * \since QGIS 3.0
684      */
685     virtual void resolveReferences( QgsProject *project );
686 
687     /**
688      * Returns list of all keys within custom properties. Properties are stored in a map and saved in project file.
689      * \see customProperty()
690      * \since QGIS 3.0
691      */
692     Q_INVOKABLE QStringList customPropertyKeys() const;
693 
694     /**
695      * Set a custom property for layer. Properties are stored in a map and saved in project file.
696      * \see customProperty()
697      * \see removeCustomProperty()
698      */
699     Q_INVOKABLE void setCustomProperty( const QString &key, const QVariant &value );
700 
701     /**
702      * Read a custom property from layer. Properties are stored in a map and saved in project file.
703      * \see setCustomProperty()
704     */
705     Q_INVOKABLE QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;
706 
707     /**
708      * Set custom properties for layer. Current properties are dropped.
709      * \since QGIS 3.0
710      */
711     void setCustomProperties( const QgsObjectCustomProperties &properties );
712 
713     /**
714      * Read all custom properties from layer. Properties are stored in a map and saved in project file.
715      * \see setCustomProperties
716      * \since QGIS 3.14
717      */
718     const QgsObjectCustomProperties &customProperties() const;
719 
720 #ifndef SIP_RUN
721 
722     /**
723      * Returns the property value for a property based on an enum.
724      * This forces the output to be a valid and existing entry of the enum.
725      * Hence if the property value is incorrect, the given default value is returned.
726      * This tries first with property as a string (as the enum) and then as an integer value.
727      * \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS).
728      * \see setCustomEnumProperty
729      * \see customFlagProperty
730      * \since QGIS 3.22
731      */
732     template <class T>
customEnumProperty(const QString & key,const T & defaultValue)733     T customEnumProperty( const QString &key, const T &defaultValue )
734     {
735       const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
736       Q_ASSERT( metaEnum.isValid() );
737       if ( !metaEnum.isValid() )
738       {
739         QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
740       }
741 
742       T v;
743       bool ok = false;
744 
745       if ( metaEnum.isValid() )
746       {
747         // read as string
748         QByteArray ba = customProperty( key, metaEnum.valueToKey( static_cast<int>( defaultValue ) ) ).toString().toUtf8();
749         const char *vs = ba.data();
750         v = static_cast<T>( metaEnum.keyToValue( vs, &ok ) );
751         if ( ok )
752           return v;
753       }
754 
755       // if failed, try to read as int (old behavior)
756       // this code shall be removed later
757       // then the method could be marked as const
758       v = static_cast<T>( customProperty( key, static_cast<int>( defaultValue ) ).toInt( &ok ) );
759       if ( metaEnum.isValid() )
760       {
761         if ( !ok || !metaEnum.valueToKey( static_cast<int>( v ) ) )
762         {
763           v = defaultValue;
764         }
765         else
766         {
767           // found property as an integer
768           // convert the property to the new form (string)
769           setCustomEnumProperty( key, v );
770         }
771       }
772 
773       return v;
774     }
775 
776     /**
777      * Set the value of a property based on an enum.
778      * The property will be saved as string.
779      * \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS).
780      * \see customEnumProperty
781      * \see setCustomFlagProperty
782      * \since QGIS 3.22
783      */
784     template <class T>
setCustomEnumProperty(const QString & key,const T & value)785     void setCustomEnumProperty( const QString &key, const T &value )
786     {
787       const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
788       Q_ASSERT( metaEnum.isValid() );
789       if ( metaEnum.isValid() )
790       {
791         setCustomProperty( key, metaEnum.valueToKey( static_cast<int>( value ) ) );
792       }
793       else
794       {
795         QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
796       }
797     }
798 
799     /**
800      * Returns the property value for a property based on a flag.
801      * This forces the output to be a valid and existing entry of the flag.
802      * Hence if the property value is incorrect, the given default value is returned.
803      * This tries first with property as a string (using a byte array) and then as an integer value.
804      * \note The flag needs to be declared with Q_FLAG (not Q_FLAGS).
805      * \note for Python bindings, a custom implementation is achieved in Python directly.
806      * \see setCustomFlagProperty
807      * \see customEnumProperty
808      * \since QGIS 3.22
809      */
810     template <class T>
customFlagProperty(const QString & key,const T & defaultValue)811     T customFlagProperty( const QString &key, const T &defaultValue )
812     {
813       const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
814       Q_ASSERT( metaEnum.isValid() );
815       if ( !metaEnum.isValid() )
816       {
817         QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
818       }
819 
820       T v = defaultValue;
821       bool ok = false;
822 
823       if ( metaEnum.isValid() )
824       {
825         // read as string
826         QByteArray ba = customProperty( key, metaEnum.valueToKeys( defaultValue ) ).toString().toUtf8();
827         const char *vs = ba.data();
828         v = static_cast<T>( metaEnum.keysToValue( vs, &ok ) );
829       }
830       if ( !ok )
831       {
832         // if failed, try to read as int
833         const int intValue = customProperty( key, static_cast<int>( defaultValue ) ).toInt( &ok );
834         if ( metaEnum.isValid() )
835         {
836           if ( ok )
837           {
838             // check that the int value does correspond to a flag
839             // see https://stackoverflow.com/a/68495949/1548052
840             const QByteArray keys = metaEnum.valueToKeys( intValue );
841             const int intValueCheck = metaEnum.keysToValue( keys );
842             if ( intValue != intValueCheck )
843             {
844               v = defaultValue;
845             }
846             else
847             {
848               // found property as an integer
849               v = T( intValue );
850               // convert the property to the new form (string)
851               // this code could be removed
852               // then the method could be marked as const
853               setCustomFlagProperty( key, v );
854             }
855           }
856           else
857           {
858             v = defaultValue;
859           }
860         }
861       }
862 
863       return v;
864     }
865 
866     /**
867      * Set the value of a property based on a flag.
868      * The property will be saved as string.
869      * \note The flag needs to be declared with Q_FLAG (not Q_FLAGS).
870      * \see customFlagProperty
871      * \see customEnumProperty
872      * \since QGIS 3.22
873      */
874     template <class T>
setCustomFlagProperty(const QString & key,const T & value)875     void setCustomFlagProperty( const QString &key, const T &value )
876     {
877       const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
878       Q_ASSERT( metaEnum.isValid() );
879       if ( metaEnum.isValid() )
880       {
881         setCustomProperty( key, metaEnum.valueToKeys( value ) );
882       }
883       else
884       {
885         QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) );
886       }
887     }
888 #endif
889 
890 
891     /**
892      * Remove a custom property from layer. Properties are stored in a map and saved in project file.
893      * \see setCustomProperty()
894      */
895     void removeCustomProperty( const QString &key );
896 
897     /**
898      * Gets current status error. This error describes some principal problem
899      *  for which layer cannot work and thus is not valid. It is not last error
900      *  after accessing data by draw() etc.
901      */
902     virtual QgsError error() const;
903 
904     /**
905      * Returns the layer's spatial reference system.
906      * \since QGIS 1.4
907      */
908     QgsCoordinateReferenceSystem crs() const;
909 
910     //! Sets layer's spatial reference system
911     void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );
912 
913     /**
914      * Returns the layer data provider coordinate transform context
915      * or a default transform context if the layer does not have a valid data provider.
916     * \since QGIS 3.8
917      */
918     QgsCoordinateTransformContext transformContext( ) const;
919 
920 
921     /**
922      * A convenience function to capitalize and format a layer \a name.
923      *
924      * \since QGIS 3.0
925      */
926     static QString formatLayerName( const QString &name );
927 
928     /**
929      * Retrieve the metadata URI for this layer
930      * (either as a .qmd file on disk or as a
931      * record in the users style table in their personal qgis.db)
932      * \returns a QString with the metadata file name
933      * \since QGIS 3.0
934      */
935     virtual QString metadataUri() const;
936 
937     /**
938      * Export the current metadata of this layer as named metadata in a QDomDocument
939      * \param doc the target QDomDocument
940      * \param errorMsg this QString will be initialized on error
941      * \since QGIS 3.0
942      */
943     void exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const;
944 
945     /**
946      * Save the current metadata of this layer as the default metadata
947      * (either as a .qmd file on disk or as a
948      * record in the users style table in their personal qgis.db)
949      * \param resultFlag a reference to a flag that will be set to FALSE if
950      * we did not manage to save the default metadata.
951      * \returns a QString with any status messages
952      * \since QGIS 3.0
953      */
954     virtual QString saveDefaultMetadata( bool &resultFlag SIP_OUT );
955 
956     /**
957      * Save the current metadata of this layer as a named metadata
958      * (either as a .qmd file on disk or as a
959      * record in the users style table in their personal qgis.db)
960      * \param uri the file name or other URI for the
961      * metadata file. First an attempt will be made to see if this
962      * is a file and save to that, if that fails the qgis.db metadata
963      * table will be used to create a metadata entry who's
964      * key matches the URI.
965      * \param resultFlag a reference to a flag that will be set to FALSE if
966      * we did not manage to save the default metadata.
967      * \returns a QString with any status messages
968      * \since QGIS 3.0
969      */
970     QString saveNamedMetadata( const QString &uri, bool &resultFlag );
971 
972     /**
973      * Retrieve a named metadata for this layer if one
974      * exists (either as a .qmd file on disk or as a
975      * record in the users style table in their personal qgis.db)
976      * \param uri - the file name or other URI for the
977      * metadata file. First an attempt will be made to see if this
978      * is a file and load that, if that fails the qgis.db metadata
979      * table will be consulted to see if there is a metadata who's
980      * key matches the URI.
981      * \param resultFlag a reference to a flag that will be set to FALSE if
982      * we did not manage to load the default metadata.
983      * \returns a QString with any status messages
984      * \since QGIS 3.0
985      */
986     virtual QString loadNamedMetadata( const QString &uri, bool &resultFlag SIP_OUT );
987 
988     /**
989      * Retrieve the default metadata for this layer if one
990      * exists (either as a .qmd file on disk or as a
991      * record in the users metadata table in their personal qgis.db)
992      * \param resultFlag a reference to a flag that will be set to FALSE if
993      * we did not manage to load the default metadata.
994      * \returns a QString with any status messages
995      * \since QGIS 3.0
996      */
997     virtual QString loadDefaultMetadata( bool &resultFlag );
998 
999     /**
1000      * Retrieve a named metadata for this layer from a sqlite database.
1001      * \param db path to sqlite database
1002      * \param uri uri for table
1003      * \param qmd will be set to QMD xml metadata content from database
1004      * \returns TRUE if style was successfully loaded
1005      * \since QGIS 3.0
1006      */
1007     bool loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd );
1008 
1009     /**
1010      * Import the metadata of this layer from a QDomDocument
1011      * \param document source QDomDocument
1012      * \param errorMessage this QString will be initialized on error
1013      * \returns TRUE on success
1014      * \since QGIS 3.0
1015      */
1016     bool importNamedMetadata( QDomDocument &document, QString &errorMessage );
1017 
1018     /**
1019      * Retrieve the style URI for this layer
1020      * (either as a .qml file on disk or as a
1021      * record in the users style table in their personal qgis.db)
1022      * \returns a QString with the style file name
1023      * \see also loadNamedStyle() and saveNamedStyle();
1024      */
1025     virtual QString styleURI() const;
1026 
1027     /**
1028      * Retrieve the default style for this layer if one
1029      * exists (either as a .qml file on disk or as a
1030      * record in the users style table in their personal qgis.db)
1031      * \param resultFlag a reference to a flag that will be set to FALSE if
1032      * we did not manage to load the default style.
1033      * \returns a QString with any status messages
1034      * \see also loadNamedStyle();
1035      */
1036     virtual QString loadDefaultStyle( bool &resultFlag SIP_OUT );
1037 
1038     /**
1039      * Retrieve a named style for this layer if one
1040      * exists (either as a .qml file on disk or as a
1041      * record in the users style table in their personal qgis.db)
1042      * \param uri - the file name or other URI for the
1043      * style file. First an attempt will be made to see if this
1044      * is a file and load that, if that fails the qgis.db styles
1045      * table will be consulted to see if there is a style who's
1046      * key matches the URI.
1047      * \param resultFlag a reference to a flag that will be set to FALSE if
1048      * we did not manage to load the default style.
1049      * \param categories the style categories to be loaded.
1050      * \returns a QString with any status messages
1051      * \see also loadDefaultStyle ();
1052      */
1053     virtual QString loadNamedStyle( const QString &uri, bool &resultFlag SIP_OUT, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
1054 
1055     /**
1056      * Retrieve a named style for this layer from a sqlite database.
1057      * \param db path to sqlite database
1058      * \param uri uri for table
1059      * \param qml will be set to QML style content from database
1060      * \returns TRUE if style was successfully loaded
1061      */
1062     virtual bool loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml SIP_OUT );
1063 
1064     /**
1065      * Import the properties of this layer from a QDomDocument
1066      * \param doc source QDomDocument
1067      * \param errorMsg this QString will be initialized on error
1068      * during the execution of readSymbology
1069      * \param categories the style categories to import
1070      * \returns TRUE on success
1071      * \since QGIS 2.8
1072      */
1073     virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT,
1074                                    QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
1075 
1076     /**
1077      * Export the properties of this layer as named style in a QDomDocument
1078      * \param doc the target QDomDocument
1079      * \param errorMsg this QString will be initialized on error
1080      * \param context read write context
1081      * \param categories the style categories to export
1082      * during the execution of writeSymbology
1083      */
1084     virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT, const QgsReadWriteContext &context = QgsReadWriteContext(),
1085                                    QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const;
1086 
1087 
1088     /**
1089      * Export the properties of this layer as SLD style in a QDomDocument
1090      * \param doc the target QDomDocument
1091      * \param errorMsg this QString will be initialized on error
1092      * during the execution of writeSymbology
1093      */
1094     virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;
1095 
1096     /**
1097      * Save the properties of this layer as the default style
1098      * (either as a .qml file on disk or as a
1099      * record in the users style table in their personal qgis.db)
1100      * \param resultFlag a reference to a flag that will be set to FALSE if
1101      * we did not manage to save the default style.
1102      * \returns a QString with any status messages
1103      * \see loadNamedStyle() and \see saveNamedStyle()
1104      */
1105     virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT );
1106 
1107     /**
1108      * Save the properties of this layer as a named style
1109      * (either as a .qml file on disk or as a
1110      * record in the users style table in their personal qgis.db)
1111      * \param uri the file name or other URI for the
1112      * style file. First an attempt will be made to see if this
1113      * is a file and save to that, if that fails the qgis.db styles
1114      * table will be used to create a style entry who's
1115      * key matches the URI.
1116      * \param resultFlag a reference to a flag that will be set to FALSE if
1117      * we did not manage to save the default style.
1118      * \param categories the style categories to be saved.
1119      * \returns a QString with any status messages
1120      * \see saveDefaultStyle()
1121      */
1122     virtual QString saveNamedStyle( const QString &uri, bool &resultFlag SIP_OUT, StyleCategories categories = AllStyleCategories );
1123 
1124     /**
1125      * Saves the properties of this layer to an SLD format file.
1126      * \param uri uri of destination for exported SLD file.
1127      * \param resultFlag a reference to a flag that will be set to FALSE if
1128      * the SLD file could not be generated
1129      * \returns a string with any status or error messages
1130      * \see loadSldStyle()
1131      */
1132     virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;
1133 
1134     /**
1135      * Attempts to style the layer using the formatting from an SLD type file.
1136      * \param uri uri of source SLD file
1137      * \param resultFlag a reference to a flag that will be set to FALSE if
1138      * the SLD file could not be loaded
1139      * \returns a string with any status or error messages
1140      * \see saveSldStyle()
1141      */
1142     virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
1143 
readSld(const QDomNode & node,QString & errorMessage)1144     virtual bool readSld( const QDomNode &node, QString &errorMessage )
1145     { Q_UNUSED( node ) errorMessage = QStringLiteral( "Layer type %1 not supported" ).arg( static_cast<int>( type() ) ); return false; }
1146 
1147 
1148 
1149     /**
1150      * Read the symbology for the current layer from the DOM node supplied.
1151      * \param node node that will contain the symbology definition for this layer.
1152      * \param errorMessage reference to string that will be updated with any error messages
1153      * \param context reading context (used for transform from relative to absolute paths)
1154      * \param categories the style categories to be read
1155      * \returns TRUE in case of success.
1156      */
1157     virtual bool readSymbology( const QDomNode &node, QString &errorMessage,
1158                                 QgsReadWriteContext &context, StyleCategories categories = AllStyleCategories ) = 0;
1159 
1160     /**
1161      * Read the style for the current layer from the DOM node supplied.
1162      * \param node node that will contain the style definition for this layer.
1163      * \param errorMessage reference to string that will be updated with any error messages
1164      * \param context reading context (used for transform from relative to absolute paths)
1165      * \param categories the style categories to be read
1166      * \returns TRUE in case of success.
1167      * \note To be implemented in subclasses. Default implementation does nothing and returns FALSE.
1168      * \since QGIS 2.16
1169      */
1170     virtual bool readStyle( const QDomNode &node, QString &errorMessage,
1171                             QgsReadWriteContext &context, StyleCategories categories = AllStyleCategories );
1172 
1173     /**
1174      * Write the style for the layer into the document provided.
1175      *  \param node the node that will have the style element added to it.
1176      *  \param doc the document that will have the QDomNode added.
1177      *  \param errorMessage reference to string that will be updated with any error messages
1178      *  \param context writing context (used for transform from absolute to relative paths)
1179      *  \param categories the style categories to be written
1180      *  \note There is a confusion of terms with the GUI. This method actually writes what is called a style in the application.
1181      *  \returns TRUE in case of success.
1182      */
1183     virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context,
1184                                  StyleCategories categories = AllStyleCategories ) const = 0;
1185 
1186     /**
1187      * Write just the symbology information for the layer into the document
1188      *  \param node the node that will have the style element added to it.
1189      *  \param doc the document that will have the QDomNode added.
1190      *  \param errorMessage reference to string that will be updated with any error messages
1191      *  \param context writing context (used for transform from absolute to relative paths)
1192      *  \param categories the style categories to be written
1193      *  \returns TRUE in case of success.
1194      *  \note To be implemented in subclasses. Default implementation does nothing and returns FALSE.
1195      *  \note There is a confusion of terms with the GUI. This method actually writes what is known as the symbology in the application.
1196      *  \since QGIS 2.16
1197      */
1198     virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context,
1199                              StyleCategories categories = AllStyleCategories ) const;
1200 
1201 
1202     /**
1203      * Updates the data source of the layer. The layer's renderer and legend will be preserved only
1204      * if the geometry type of the new data source matches the current geometry type of the layer.
1205      *
1206      * This method was defined in QgsVectorLayer since 2.10 and was marked as deprecated since 3.2
1207      *
1208      * \param dataSource new layer data source
1209      * \param baseName base name of the layer
1210      * \param provider provider string
1211      * \param loadDefaultStyleFlag set to TRUE to reset the layer's style to the default for the
1212      * data source
1213      * \see dataSourceChanged()
1214      * \since QGIS 3.20
1215      */
1216     void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, bool loadDefaultStyleFlag = false );
1217 
1218     /**
1219      * Updates the data source of the layer. The layer's renderer and legend will be preserved only
1220      * if the geometry type of the new data source matches the current geometry type of the layer.
1221      *
1222      * \param dataSource new layer data source
1223      * \param baseName base name of the layer
1224      * \param provider provider string
1225      * \param options provider options
1226      * \param loadDefaultStyleFlag set to TRUE to reset the layer's style to the default for the
1227      * data source
1228      * \see dataSourceChanged()
1229      * \since QGIS 3.6
1230      */
1231     void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false );
1232 
1233     /**
1234      * Updates the data source of the layer. The layer's renderer and legend will be preserved only
1235      * if the geometry type of the new data source matches the current geometry type of the layer.
1236      *
1237      * Subclasses should override setDataSourcePrivate: default implementation does nothing.
1238      *
1239      * \param dataSource new layer data source
1240      * \param baseName base name of the layer
1241      * \param provider provider string
1242      * \param options provider options
1243      * \param flags provider read flags which control dataprovider construction like FlagTrustDataSource, FlagLoadDefaultStyle, etc
1244      * \see dataSourceChanged()
1245      * \since QGIS 3.20
1246      */
1247     void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags );
1248 
1249     /**
1250      * Returns the provider type (provider key) for this layer
1251      */
1252     QString providerType() const;
1253 
1254     //! Returns pointer to layer's undo stack
1255     QUndoStack *undoStack();
1256 
1257     /**
1258      * Returns pointer to layer's style undo stack
1259      *  \since QGIS 2.16
1260      */
1261     QUndoStack *undoStackStyles();
1262 
1263     /**
1264      * Sets the URL for the layer's legend.
1265     */
setLegendUrl(const QString & legendUrl)1266     void setLegendUrl( const QString &legendUrl ) { mLegendUrl = legendUrl; }
1267 
1268     /**
1269      * Returns the URL for the layer's legend.
1270      */
legendUrl()1271     QString legendUrl() const { return mLegendUrl; }
1272 
1273     /**
1274      * Sets the format for a URL based layer legend.
1275      */
setLegendUrlFormat(const QString & legendUrlFormat)1276     void setLegendUrlFormat( const QString &legendUrlFormat ) { mLegendUrlFormat = legendUrlFormat; }
1277 
1278     /**
1279      * Returns the format for a URL based layer legend.
1280      */
legendUrlFormat()1281     QString legendUrlFormat() const { return mLegendUrlFormat; }
1282 
1283     /**
1284      * Assign a legend controller to the map layer. The object will be responsible for providing legend items.
1285      * \param legend Takes ownership of the object. Can be NULLPTR.
1286      * \since QGIS 2.6
1287      */
1288     void setLegend( QgsMapLayerLegend *legend SIP_TRANSFER );
1289 
1290     /**
1291      * Can be NULLPTR.
1292      * \since QGIS 2.6
1293      */
1294     QgsMapLayerLegend *legend() const;
1295 
1296     /**
1297      * Gets access to the layer's style manager. Style manager allows switching between multiple styles.
1298      * \since QGIS 2.8
1299      */
1300     QgsMapLayerStyleManager *styleManager() const;
1301 
1302     /**
1303      * Sets 3D renderer for the layer. Takes ownership of the renderer.
1304      * \since QGIS 3.0
1305      */
1306     void setRenderer3D( QgsAbstract3DRenderer *renderer SIP_TRANSFER );
1307 
1308     /**
1309      * Returns 3D renderer associated with the layer. May be NULLPTR.
1310      * \since QGIS 3.0
1311      */
1312     QgsAbstract3DRenderer *renderer3D() const;
1313 
1314     /**
1315      * Tests whether the layer should be visible at the specified \a scale.
1316      *  The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
1317      * \returns TRUE if the layer is visible at the given scale.
1318      * \see minimumScale()
1319      * \see maximumScale()
1320      * \see hasScaleBasedVisibility()
1321      * \since QGIS 2.16
1322      */
1323     bool isInScaleRange( double scale ) const;
1324 
1325     /**
1326      * Returns the minimum map scale (i.e. most "zoomed out" scale) at which the layer will be visible.
1327      * The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
1328      * A scale of 0 indicates no minimum scale visibility.
1329      * \note Scale based visibility is only used if setScaleBasedVisibility() is set to TRUE.
1330      * \see setMinimumScale()
1331      * \see maximumScale()
1332      * \see hasScaleBasedVisibility()
1333      * \see isInScaleRange()
1334      */
1335     double minimumScale() const;
1336 
1337     /**
1338      * Returns the maximum map scale (i.e. most "zoomed in" scale) at which the layer will be visible.
1339      * The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
1340      * A scale of 0 indicates no maximum scale visibility.
1341      * \note Scale based visibility is only used if setScaleBasedVisibility() is set to TRUE.
1342      * \see setMaximumScale()
1343      * \see minimumScale()
1344      * \see hasScaleBasedVisibility()
1345      * \see isInScaleRange()
1346      */
1347     double maximumScale() const;
1348 
1349     /**
1350      * Returns whether scale based visibility is enabled for the layer.
1351      * \returns TRUE if scale based visibility is enabled
1352      * \see minimumScale()
1353      * \see maximumScale()
1354      * \see setScaleBasedVisibility()
1355      * \see isInScaleRange()
1356      */
1357     bool hasScaleBasedVisibility() const;
1358 
1359     /**
1360      * Returns TRUE if auto refresh is enabled for the layer.
1361      * \see autoRefreshInterval()
1362      * \see setAutoRefreshEnabled()
1363      * \since QGIS 3.0
1364      */
1365     bool hasAutoRefreshEnabled() const;
1366 
1367     /**
1368      * Returns the auto refresh interval (in milliseconds). Note that
1369      * auto refresh is only active when hasAutoRefreshEnabled() is TRUE.
1370      * \see hasAutoRefreshEnabled()
1371      * \see setAutoRefreshInterval()
1372      * \since QGIS 3.0
1373      */
1374     int autoRefreshInterval() const;
1375 
1376     /**
1377      * Sets the auto refresh interval (in milliseconds) for the layer. This
1378      * will cause the layer to be automatically redrawn on a matching interval.
1379      * Note that auto refresh must be enabled by calling setAutoRefreshEnabled().
1380      *
1381      * Note that auto refresh triggers deferred repaints of the layer. Any map
1382      * canvas must be refreshed separately in order to view the refreshed layer.
1383      * \see autoRefreshInterval()
1384      * \see setAutoRefreshEnabled()
1385      * \since QGIS 3.0
1386      */
1387     void setAutoRefreshInterval( int interval );
1388 
1389     /**
1390      * Sets whether auto refresh is enabled for the layer.
1391      * \see hasAutoRefreshEnabled()
1392      * \see setAutoRefreshInterval()
1393      * \since QGIS 3.0
1394      */
1395     void setAutoRefreshEnabled( bool enabled );
1396 
1397     /**
1398      * Returns a reference to the layer's metadata store.
1399      * \see setMetadata()
1400      * \see metadataChanged()
1401      * \since QGIS 3.0
1402      */
1403     virtual const QgsLayerMetadata &metadata() const;
1404 
1405     /**
1406      * Sets the layer's \a metadata store.
1407      * \see metadata()
1408      * \see metadataChanged()
1409      * \since QGIS 3.0
1410      */
1411     virtual void setMetadata( const QgsLayerMetadata &metadata );
1412 
1413     /**
1414      * Obtain a formatted HTML string containing assorted metadata for this layer.
1415      * \since QGIS 3.0
1416      */
1417     virtual QString htmlMetadata() const;
1418 
1419     //! Time stamp of data source in the moment when data/metadata were loaded by provider
1420     virtual QDateTime timestamp() const;
1421 
1422     /**
1423      * Gets the list of dependencies. This includes data dependencies set by the user (\see setDataDependencies)
1424      * as well as dependencies given by the provider
1425      *
1426      * \returns a set of QgsMapLayerDependency
1427      * \since QGIS 3.0
1428      */
1429     virtual QSet<QgsMapLayerDependency> dependencies() const;
1430 
1431     /**
1432      * Returns the message that should be notified by the provider to triggerRepaint
1433      *
1434      * \since QGIS 3.0
1435      */
refreshOnNotifyMessage()1436     QString refreshOnNotifyMessage() const { return mRefreshOnNofifyMessage; }
1437 
1438     /**
1439      * Returns TRUE if the refresh on provider nofification is enabled
1440      *
1441      * \since QGIS 3.0
1442      */
isRefreshOnNotifyEnabled()1443     bool isRefreshOnNotifyEnabled() const { return mIsRefreshOnNofifyEnabled; }
1444 
1445     /**
1446      * Returns the XML properties of the original layer as they were when the layer
1447      * was first read from the project file. In case of new layers this is normally empty.
1448      *
1449      * The storage format for the XML is qlr
1450      *
1451      * \since QGIS 3.6
1452      */
1453     QString originalXmlProperties() const;
1454 
1455     /**
1456      * Sets the original XML properties for the layer to  \a originalXmlProperties
1457      *
1458      * The storage format for the XML is qlr
1459      *
1460      * \since QGIS 3.6
1461      */
1462     void setOriginalXmlProperties( const QString &originalXmlProperties );
1463 
1464     /**
1465      * Generates an unique identifier for this layer, the generate ID is prefixed by \a layerName
1466      * \since QGIS 3.8
1467      */
1468     static QString generateId( const QString &layerName );
1469 
1470     /**
1471      * Accepts the specified symbology \a visitor, causing it to visit all symbols associated
1472      * with the layer.
1473      *
1474      * Returns TRUE if the visitor should continue visiting other objects, or FALSE if visiting
1475      * should be canceled.
1476      *
1477      * \since QGIS 3.10
1478      */
1479     virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
1480 
1481     /**
1482      * Returns the layer's temporal properties. This may be NULLPTR, depending on the layer type.
1483      *
1484      * \since QGIS 3.14
1485      */
temporalProperties()1486     virtual QgsMapLayerTemporalProperties *temporalProperties() { return nullptr; }
1487 
1488     /**
1489      * Returns the layer's elevation properties. This may be NULLPTR, depending on the layer type.
1490      *
1491      * \since QGIS 3.18
1492      */
elevationProperties()1493     virtual QgsMapLayerElevationProperties *elevationProperties() { return nullptr; }
1494 
1495     /**
1496      * Returns path to the placeholder image or an empty string if a generated legend is shown
1497      * \return placholder image path
1498      * \since QGIS 3.22
1499      */
legendPlaceholderImage()1500     QString legendPlaceholderImage() const { return mLegendPlaceholderImage;}
1501 
1502     /**
1503      * Set placeholder image for legend. If the string is empty, a generated legend will be shown.
1504      * \param imgPath file path to the placeholder image
1505      * \since QGIS 3.22
1506      */
setLegendPlaceholderImage(const QString & imgPath)1507     void setLegendPlaceholderImage( const QString &imgPath ) { mLegendPlaceholderImage = imgPath; }
1508 
1509   public slots:
1510 
1511     /**
1512      * Sets the minimum map \a scale (i.e. most "zoomed out" scale) at which the layer will be visible.
1513      * The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
1514      * A \a scale of 0 indicates no minimum scale visibility.
1515      * \note Scale based visibility is only used if setScaleBasedVisibility() is set to TRUE.
1516      * \see minimumScale()
1517      * \see setMaximumScale()
1518      * \see setScaleBasedVisibility()
1519      */
1520     void setMinimumScale( double scale );
1521 
1522     /**
1523      * Sets the maximum map \a scale (i.e. most "zoomed in" scale) at which the layer will be visible.
1524      * The \a scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
1525      * A \a scale of 0 indicates no maximum scale visibility.
1526      * \note Scale based visibility is only used if setScaleBasedVisibility() is set to TRUE.
1527      * \see maximumScale()
1528      * \see setMinimumScale()
1529      * \see setScaleBasedVisibility()
1530      */
1531     void setMaximumScale( double scale );
1532 
1533     /**
1534      * Sets whether scale based visibility is enabled for the layer.
1535      * \param enabled set to TRUE to enable scale based visibility
1536      * \see setMinimumScale
1537      * \see setMaximumScale
1538      * \see hasScaleBasedVisibility
1539      */
1540     void setScaleBasedVisibility( bool enabled );
1541 
1542     /**
1543      * Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
1544      * Will emit a repaintRequested() signal.
1545      * If \a deferredUpdate is TRUE then the layer will only be repainted when the canvas is next
1546      * re-rendered, and will not trigger any canvas redraws itself.
1547      *
1548      * \note in 2.6 function moved from vector/raster subclasses to QgsMapLayer
1549      */
1550     void triggerRepaint( bool deferredUpdate = false );
1551 
1552     /**
1553      * Will advise any 3D maps that this layer requires to be updated in the scene.
1554      * Will emit a request3DUpdate() signal.
1555      *
1556      * \since QGIS 3.18
1557      */
1558     void trigger3DUpdate();
1559 
1560     /**
1561      * Triggers an emission of the styleChanged() signal.
1562      * \since QGIS 2.16
1563      */
1564     void emitStyleChanged();
1565 
1566     /**
1567      * Sets the list of dependencies.
1568      * \see dependencies()
1569      *
1570      * \param layers set of QgsMapLayerDependency. Only user-defined dependencies will be added
1571      * \returns FALSE if a dependency cycle has been detected
1572      * \since QGIS 3.0
1573      */
1574     virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
1575 
1576     /**
1577      * Set whether provider notification is connected to triggerRepaint
1578      *
1579      * \since QGIS 3.0
1580      */
1581     void setRefreshOnNotifyEnabled( bool enabled );
1582 
1583     /**
1584      * Set the notification message that triggers repaint
1585      * If refresh on notification is enabled, the notification will triggerRepaint only
1586      * if the notification message is equal to \param message
1587      *
1588      * \since QGIS 3.0
1589      */
setRefreshOnNofifyMessage(const QString & message)1590     void setRefreshOnNofifyMessage( const QString &message ) { mRefreshOnNofifyMessage = message; }
1591 
1592     /**
1593      * Sets the coordinate transform context to \a transformContext
1594      *
1595      * \since QGIS 3.8
1596      */
1597     virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) = 0;
1598 
1599 #ifdef SIP_RUN
1600     SIP_PYOBJECT __repr__();
1601     % MethodCode
1602     QString str = QStringLiteral( "<QgsMapLayer: '%1' (%2)>" ).arg( sipCpp->name(), sipCpp->dataProvider() ? sipCpp->dataProvider()->name() : QStringLiteral( "Invalid" ) );
1603     sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1604     % End
1605 #endif
1606 
1607     /**
1608      * Returns the parent project if this map layer is added to a project.
1609      * Otherwise returns NULLPTR
1610      *
1611      * \since QGIS 3.18
1612      */
1613     QgsProject *project() const;
1614 
1615   signals:
1616 
1617     /**
1618      * Emitted when all layers are loaded and references can be resolved,
1619      * just before the references of this layer are resolved.
1620      *
1621      * \since QGIS 3.10
1622      */
1623     void beforeResolveReferences( QgsProject *project );
1624 
1625     //! Emit a signal with status (e.g. to be caught by QgisApp and display a msg on status bar)
1626     void statusChanged( const QString &status );
1627 
1628     /**
1629      * Emitted when the name has been changed
1630      *
1631      * \since QGIS 2.16
1632      */
1633     void nameChanged();
1634 
1635     //! Emit a signal that layer's CRS has been reset
1636     void crsChanged();
1637 
1638     /**
1639      * By emitting this signal the layer tells that either appearance or content have been changed
1640      * and any view showing the rendered layer should refresh itself.
1641      * If \a deferredUpdate is TRUE then the layer will only be repainted when the canvas is next
1642      * re-rendered, and will not trigger any canvas redraws itself.
1643      */
1644     void repaintRequested( bool deferredUpdate = false );
1645 
1646     //! This is used to send a request that any mapcanvas using this layer update its extents
1647     void recalculateExtents() const;
1648 
1649     //! Data of layer changed
1650     void dataChanged();
1651 
1652     //! Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode()
1653     void blendModeChanged( QPainter::CompositionMode blendMode );
1654 
1655     /**
1656      * Emitted when the layer's opacity is changed, where \a opacity is a value between 0 (transparent)
1657      * and 1 (opaque).
1658      * \see setOpacity()
1659      * \see opacity()
1660      * \note Prior to QGIS 3.18, this signal was available for vector layers only
1661      * \since QGIS 3.18
1662      */
1663     void opacityChanged( double opacity );
1664 
1665     /**
1666      * Signal emitted when renderer is changed.
1667      * \see styleChanged()
1668     */
1669     void rendererChanged();
1670 
1671     /**
1672      * Signal emitted whenever a change affects the layer's style. Ie this may be triggered
1673      * by renderer changes, label style changes, or other style changes such as blend
1674      * mode or layer opacity changes.
1675      *
1676      * \warning This signal should never be manually emitted. Instead call the emitStyleChanged() method
1677      * to ensure that the signal is only emitted when appropriate.
1678      *
1679      * \see rendererChanged()
1680      * \since QGIS 2.16
1681     */
1682     void styleChanged();
1683 
1684     /**
1685      * Signal emitted when legend of the layer has changed
1686      * \since QGIS 2.6
1687      */
1688     void legendChanged();
1689 
1690     /**
1691      * Signal emitted when 3D renderer associated with the layer has changed.
1692      * \since QGIS 3.0
1693      */
1694     void renderer3DChanged();
1695 
1696     /**
1697      * Signal emitted when a layer requires an update in any 3D maps.
1698      *
1699      * \since QGIS 3.18
1700      */
1701     void request3DUpdate();
1702 
1703     /**
1704      * Emitted whenever the configuration is changed. The project listens to this signal
1705      * to be marked as dirty.
1706      */
1707     void configChanged();
1708 
1709     /**
1710      * Emitted when dependencies are changed.
1711      */
1712     void dependenciesChanged();
1713 
1714     /**
1715      * Emitted in the destructor when the layer is about to be deleted,
1716      * but it is still in a perfectly valid state: the last chance for
1717      * other pieces of code for some cleanup if they use the layer.
1718      * \since QGIS 3.0
1719      */
1720     void willBeDeleted();
1721 
1722     /**
1723      * Emitted when the auto refresh interval changes.
1724      * \see setAutoRefreshInterval()
1725      * \since QGIS 3.0
1726      */
1727     void autoRefreshIntervalChanged( int interval );
1728 
1729     /**
1730      * Emitted when the layer's metadata is changed.
1731      * \see setMetadata()
1732      * \see metadata()
1733      * \since QGIS 3.0
1734      */
1735     void metadataChanged();
1736 
1737     /**
1738      * Emitted when layer's flags have been modified.
1739      * \see setFlags()
1740      * \see flags()
1741      * \since QGIS 3.4
1742      */
1743     void flagsChanged();
1744 
1745     /**
1746      * Emitted whenever the layer's data source has been changed.
1747      *
1748      * \see setDataSource()
1749      *
1750      * \since QGIS 3.5
1751      */
1752     void dataSourceChanged();
1753 
1754     /**
1755      * Emitted when a style has been loaded
1756      * \param categories style categories
1757      * \since QGIS 3.12
1758      */
1759     void styleLoaded( QgsMapLayer::StyleCategories categories );
1760 
1761     /**
1762      * Emitted when the validity of this layer changed.
1763      *
1764      * \since QGIS 3.16
1765      */
1766     void isValidChanged();
1767 
1768     /**
1769      * Emitted when a custom property of the layer has been changed or removed.
1770      *
1771      * \since QGIS 3.18
1772      */
1773     void customPropertyChanged( const QString &key );
1774 
1775     /**
1776      * Emitted when editing on this layer has started.
1777      * \since QGIS 3.22 in the QgsMapLayer base class
1778      */
1779     void editingStarted();
1780 
1781     /**
1782      * Emitted when edited changes have been successfully written to the data provider.
1783      * \since QGIS 3.22 in the QgsMapLayer base class
1784      */
1785     void editingStopped();
1786 
1787     /**
1788      * Emitted when modifications has been done on layer
1789      * \since QGIS 3.22 in the QgsMapLayer base class
1790      */
1791     void layerModified();
1792 
1793   private slots:
1794 
1795     void onNotified( const QString &message );
1796 
1797     /**
1798      * Updates the data source of the layer. The layer's renderer and legend will be preserved only
1799      * if the geometry type of the new data source matches the current geometry type of the layer.
1800      *
1801      * Called by setDataSource()
1802      * Subclasses should override this method: default implementation does nothing.
1803      *
1804      * \param dataSource new layer data source
1805      * \param baseName base name of the layer
1806      * \param provider provider string
1807      * \param options provider options
1808      * \param flags provider read flags
1809      * \see dataSourceChanged()
1810      * \since QGIS 3.20
1811      */
1812     virtual void setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags );
1813 
1814   protected:
1815 
1816     /**
1817      * Copies attributes like name, short name, ... into another layer.
1818      * \param layer The copy recipient
1819      * \since QGIS 3.0
1820      */
1821     void clone( QgsMapLayer *layer ) const;
1822 
1823     //! Sets the extent
1824     virtual void setExtent( const QgsRectangle &rect );
1825 
1826     //! Sets whether layer is valid or not
1827     void setValid( bool valid );
1828 
1829     /**
1830      * Called by readLayerXML(), used by children to read state specific to them from
1831      *  project files.
1832      */
1833     virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );
1834 
1835     /**
1836      * Called by writeLayerXML(), used by children to write state specific to them to
1837      *  project files.
1838      */
1839     virtual bool writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const;
1840 
1841     /**
1842      * Called by writeLayerXML(), used by derived classes to encode provider's specific data
1843      * source to project files. Typically resolving absolute or relative paths, usernames and
1844      * passwords or drivers prefixes ("HDF5:")
1845      *
1846      * \param source data source to encode, typically QgsMapLayer::source()
1847      * \param context writing context (e.g. for conversion between relative and absolute paths)
1848      * \return encoded source, typically to be written in the DOM element "datasource"
1849      *
1850      * \since QGIS 3.2
1851      */
1852     virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;
1853 
1854     /**
1855      * Called by readLayerXML(), used by derived classes to decode provider's specific data
1856      * source from project files. Typically resolving absolute or relative paths, usernames and
1857      * passwords or drivers prefixes ("HDF5:")
1858      *
1859      * \param source data source to decode, typically read from layer's DOM element "datasource"
1860      * \param dataProvider string identification of data provider (e.g. "ogr"), typically read from layer's DOM element
1861      * \param context reading context (e.g. for conversion between relative and absolute paths)
1862      * \return decoded source, typically to be used as the layer's datasource
1863      *
1864      * \since QGIS 3.2
1865      */
1866     virtual QString decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const;
1867 
1868     /**
1869      * Read custom properties from project file.
1870      * \param layerNode note to read from
1871      * \param keyStartsWith reads only properties starting with the specified string (or all if the string is empty)
1872     */
1873     void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
1874 
1875     //! Write custom properties to project file.
1876     void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
1877 
1878     //! Read style manager's configuration (if any). To be called by subclasses.
1879     void readStyleManager( const QDomNode &layerNode );
1880     //! Write style manager's configuration (if exists). To be called by subclasses.
1881     void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
1882 
1883     /**
1884      * Write style data common to all layer types
1885      * \since QGIS 3.0
1886      */
1887     void writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
1888                            const QgsReadWriteContext &context,
1889                            StyleCategories categories = AllStyleCategories ) const;
1890 
1891     /**
1892      * Read style data common to all layer types
1893      * \since QGIS 3.0
1894      */
1895     void readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
1896                           StyleCategories categories = AllStyleCategories );
1897 
1898     //! Sets the \a providerType (provider key)
1899     void setProviderType( const QString &providerType );
1900 
1901 #ifndef SIP_RUN
1902 #if 0
1903     //! Debugging member - invoked when a connect() is made to this object
1904     void connectNotify( const char *signal ) override;
1905 #endif
1906 #endif
1907 
1908     //! Add error message
appendError(const QgsErrorMessage & error)1909     void appendError( const QgsErrorMessage &error ) { mError.append( error );}
1910     //! Sets error message
setError(const QgsError & error)1911     void setError( const QgsError &error ) { mError = error;}
1912 
1913     /**
1914      * Invalidates the WGS84 extent. If FlagTrustLayerMetadata is enabled,
1915      * the extent is not invalidated because we want to trust metadata whatever
1916      * happens.
1917      * \since QGIS 3.20
1918      */
1919     void invalidateWgs84Extent();
1920 
1921     //! Indicates if the layer is valid and can be drawn
1922     bool mValid = false;
1923 
1924     //! Data source description string, varies by layer type
1925     QString mDataSource;
1926 
1927     //! Name of the layer - used for display
1928     QString mLayerName;
1929 
1930     QString mShortName;
1931     QString mTitle;
1932 
1933     //! Description of the layer
1934     QString mAbstract;
1935     QString mKeywordList;
1936 
1937     //! DataUrl of the layer
1938     QString mDataUrl;
1939     QString mDataUrlFormat;
1940 
1941     //! Attribution of the layer
1942     QString mAttribution;
1943     QString mAttributionUrl;
1944 
1945     //! WMS legend
1946     QString mLegendUrl;
1947     QString mLegendUrlFormat;
1948 
1949     //! \brief Error
1950     QgsError mError;
1951 
1952     //! List of layers that may modify this layer on modification
1953     QSet<QgsMapLayerDependency> mDependencies;
1954 
1955     /**
1956      * Checks whether a new set of dependencies will introduce a cycle
1957      * this method is now deprecated and always return FALSE, because circular dependencies are now correctly managed.
1958      * \deprecated since QGIS 3.10
1959      */
hasDependencyCycle(const QSet<QgsMapLayerDependency> &)1960     Q_DECL_DEPRECATED bool hasDependencyCycle( const QSet<QgsMapLayerDependency> & ) const {return false;}
1961 
1962     bool mIsRefreshOnNofifyEnabled = false;
1963     QString mRefreshOnNofifyMessage;
1964 
1965     //! Data provider key (name of the data provider)
1966     QString mProviderKey;
1967 
1968     //TODO QGIS 4 - move to readXml as a new argument (breaks API)
1969 
1970     //! Read flags. It's up to the subclass to respect these when restoring state from XML
1971     QgsMapLayer::ReadFlags mReadFlags = QgsMapLayer::ReadFlags();
1972 
1973     /**
1974      * TRUE if the layer's CRS should be validated and invalid CRSes are not permitted.
1975      *
1976      * \since QGIS 3.10
1977      */
1978     bool mShouldValidateCrs = true;
1979 
1980     /**
1981      * Layer opacity.
1982      *
1983      * \since QGIS 3.18
1984      */
1985     double mLayerOpacity = 1.0;
1986 
1987     /**
1988      * If non-zero, the styleChanged signal should not be emitted.
1989      *
1990      * \since QGIS 3.20
1991      */
1992     int mBlockStyleChangedSignal = 0;
1993 
1994 #ifndef SIP_RUN
1995 
1996     /**
1997      * Returns a HTML fragment containing the layer's CRS metadata, for use
1998      * in the htmlMetadata() method.
1999      *
2000      * \note Not available in Python bindings.
2001      *
2002      * \since QGIS 3.20
2003      */
2004     QString crsHtmlMetadata() const;
2005 #endif
2006 
2007 #ifndef SIP_RUN
2008 
2009     /**
2010      * Returns an HTML fragment containing general  metadata information, for use
2011      * in the htmlMetadata() method.
2012      *
2013      * \note Not available in Python bindings.
2014      *
2015      * \since QGIS 3.22
2016      */
2017     QString generalHtmlMetadata() const;
2018 #endif
2019 
2020   private:
2021 
2022     virtual QString baseURI( PropertyType type ) const;
2023     QString saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type,
2024                                bool &resultFlag, StyleCategories categories = AllStyleCategories );
2025     QString loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type,
2026                                bool &resultFlag, StyleCategories categories = AllStyleCategories );
2027     bool loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type );
2028 
2029     // const method because extents are mutable
2030     void updateExtent( const QgsRectangle &extent ) const;
2031 
2032     /**
2033      * This method returns TRUE by default but can be overwritten to specify
2034      * that a certain layer is writable.
2035      */
2036     virtual bool isReadOnly() const;
2037 
2038     /**
2039      * Layer's spatial reference system.
2040      * private to make sure setCrs must be used and crsChanged() is emitted.
2041     */
2042     QgsCoordinateReferenceSystem mCRS;
2043 
2044     //! Unique ID of this layer - used to refer to this layer in map layer registry
2045     QString mID;
2046 
2047     //! Type of the layer (e.g., vector, raster)
2048     QgsMapLayerType mLayerType;
2049 
2050     LayerFlags mFlags = LayerFlags( Identifiable | Removable | Searchable );
2051 
2052     //! Blend mode for the layer
2053     QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
2054 
2055     //! Tag for embedding additional information
2056     QString mTag;
2057 
2058     //set some generous  defaults for scale based visibility
2059 
2060     //! Minimum scale denominator at which this layer should be displayed
2061     double mMinScale = 0;
2062     //! Maximum scale denominator at which this layer should be displayed
2063     double mMaxScale = 100000000;
2064     //! A flag that tells us whether to use the above vars to restrict layer visibility
2065     bool mScaleBasedVisibility = false;
2066 
2067     /**
2068      * Stores information about server properties
2069      */
2070     std::unique_ptr< QgsMapLayerServerProperties > mServerProperties;
2071 
2072     //! Collection of undoable operations for this layer.
2073     QUndoStack *mUndoStack = nullptr;
2074 
2075     QUndoStack *mUndoStackStyles = nullptr;
2076 
2077     //! Layer's persistent storage of additional properties (may be used by plugins)
2078     QgsObjectCustomProperties mCustomProperties;
2079 
2080     //! Controller of legend items of this layer
2081     QgsMapLayerLegend *mLegend = nullptr;
2082 
2083     //! Manager of multiple styles available for a layer (may be NULLPTR)
2084     QgsMapLayerStyleManager *mStyleManager = nullptr;
2085 
2086     //! Timer for triggering automatic refreshes of the layer
2087     QTimer *mRefreshTimer = nullptr;
2088 
2089     QgsLayerMetadata mMetadata;
2090 
2091     //! Renderer for 3D views
2092     QgsAbstract3DRenderer *m3DRenderer = nullptr;
2093 
2094     //! Extent of the layer
2095     mutable QgsRectangle mExtent;
2096 
2097     //! Extent of the layer in EPSG:4326
2098     mutable QgsRectangle mWgs84Extent;
2099 
2100     /**
2101      * Stores the original XML properties of the layer when loaded from the project
2102      *
2103      * This information can be used to pass through the bad layers or to reset changes on a good layer
2104      */
2105     QString mOriginalXmlProperties;
2106 
2107     //! To avoid firing multiple time repaintRequested signal on circular layer circular dependencies
2108     bool mRepaintRequestedFired = false;
2109 
2110     //! Path to placeholder image for layer legend. If the string is empty, a generated legend is shown
2111     QString mLegendPlaceholderImage;
2112 
2113     friend class QgsVectorLayer;
2114     friend class TestQgsMapLayer;
2115 };
2116 
2117 Q_DECLARE_METATYPE( QgsMapLayer * )
2118 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayer::LayerFlags )
2119 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayer::StyleCategories )
2120 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayer::ReadFlags )
2121 
2122 
2123 #ifndef SIP_RUN
2124 
2125 /**
2126  * Weak pointer for QgsMapLayer
2127  * \note not available in Python bindings
2128  * \since QGIS 3.0
2129  */
2130 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
2131 
2132 /**
2133  * A list of weak pointers to QgsMapLayers.
2134  * \note not available in Python bindings
2135  * \since QGIS 3.0
2136  */
2137 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
2138 #endif
2139 
2140 #endif
2141