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