QGIS API Documentation  2.99.0-Master (53aba61)
qgsmaplayer.h
Go to the documentation of this file.
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 
30 #include "qgis.h"
31 #include "qgserror.h"
33 #include "qgsrectangle.h"
35 #include "qgsrendercontext.h"
36 #include "qgsmaplayerdependency.h"
37 #include "qgslayermetadata.h"
38 
39 class QgsDataProvider;
40 class QgsMapLayerLegend;
44 class QgsProject;
45 
46 class QDomDocument;
47 class QKeyEvent;
48 class QPainter;
49 
54 class CORE_EXPORT QgsMapLayer : public QObject
55 {
56  Q_OBJECT
57 
58  Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
59  Q_PROPERTY( int autoRefreshInterval READ autoRefreshInterval WRITE setAutoRefreshInterval NOTIFY autoRefreshIntervalChanged )
60  Q_PROPERTY( QgsLayerMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )
61 
62 #ifdef SIP_RUN
64  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sipCpp );
65 
66  sipType = 0;
67 
68  if ( layer )
69  {
70  switch ( layer->type() )
71  {
73  sipType = sipType_QgsVectorLayer;
74  break;
76  sipType = sipType_QgsRasterLayer;
77  break;
79  sipType = sipType_QgsPluginLayer;
80  break;
81  default:
82  sipType = nullptr;
83  break;
84  }
85  }
86  SIP_END
87 #endif
88 
89  public:
90 
92  enum LayerType
93  {
96  PluginLayer
97  };
98 
104  QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, const QString &name = QString(), const QString &source = QString() );
105 
106  virtual ~QgsMapLayer();
107 
109  QgsMapLayer( QgsMapLayer const & ) = delete;
111  QgsMapLayer &operator=( QgsMapLayer const & ) = delete;
112 
118  virtual QgsMapLayer *clone() const = 0;
119 
122  QgsMapLayer::LayerType type() const;
123 
125  QString id() const;
126 
132  void setName( const QString &name );
133 
138  QString name() const;
139 
143  virtual QgsDataProvider *dataProvider();
144 
149  virtual const QgsDataProvider *dataProvider() const SIP_SKIP;
150 
153  QString originalName() const;
154 
160  void setShortName( const QString &shortName ) { mShortName = shortName; }
161 
166  QString shortName() const { return mShortName; }
167 
172  void setTitle( const QString &title ) { mTitle = title; }
173 
179  QString title() const { return mTitle; }
180 
186  void setAbstract( const QString &abstract ) { mAbstract = abstract; }
187 
193  QString abstract() const { return mAbstract; }
194 
200  void setKeywordList( const QString &keywords ) { mKeywordList = keywords; }
201 
207  QString keywordList() const { return mKeywordList; }
208 
209  /* Layer dataUrl information */
210 
217  void setDataUrl( const QString &dataUrl ) { mDataUrl = dataUrl; }
218 
225  QString dataUrl() const { return mDataUrl; }
226 
233  void setDataUrlFormat( const QString &dataUrlFormat ) { mDataUrlFormat = dataUrlFormat; }
234 
241  QString dataUrlFormat() const { return mDataUrlFormat; }
242 
243  /* Layer attribution information */
244 
251  void setAttribution( const QString &attrib ) { mAttribution = attrib; }
252 
259  QString attribution() const { return mAttribution; }
260 
267  void setAttributionUrl( const QString &attribUrl ) { mAttributionUrl = attribUrl; }
268 
275  QString attributionUrl() const { return mAttributionUrl; }
276 
277  /* Layer metadataUrl information */
278 
285  void setMetadataUrl( const QString &metaUrl ) { mMetadataUrl = metaUrl; }
286 
293  QString metadataUrl() const { return mMetadataUrl; }
294 
301  void setMetadataUrlType( const QString &metaUrlType ) { mMetadataUrlType = metaUrlType; }
302 
309  QString metadataUrlType() const { return mMetadataUrlType; }
310 
317  void setMetadataUrlFormat( const QString &metaUrlFormat ) { mMetadataUrlFormat = metaUrlFormat; }
318 
325  QString metadataUrlFormat() const { return mMetadataUrlFormat; }
326 
331  void setBlendMode( QPainter::CompositionMode blendMode );
332 
336  QPainter::CompositionMode blendMode() const;
337 
339  bool readOnly() const { return isReadOnly(); }
340 
343  virtual void reload() {}
344 
348  virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) = 0 SIP_FACTORY;
349 
351  virtual QgsRectangle extent() const;
352 
357  bool isValid() const;
358 
364  QString publicSource() const;
365 
370  QString source() const;
371 
376  virtual QStringList subLayers() const;
377 
382  virtual void setLayerOrder( const QStringList &layers );
383 
388  virtual void setSubLayerVisibility( const QString &name, bool visible );
389 
391  virtual bool isEditable() const;
392 
396  virtual bool isSpatial() const;
397 
413  bool readLayerXml( const QDomElement &layerElement, const QgsReadWriteContext &context );
414 
431  bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
432 
437  QStringList customPropertyKeys() const;
438 
443  void setCustomProperty( const QString &key, const QVariant &value );
444 
448  QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;
449 
453  void setCustomProperties( const QgsObjectCustomProperties &properties );
454 
458  void removeCustomProperty( const QString &key );
459 
464  virtual QgsError error() const;
465 
469  QgsCoordinateReferenceSystem crs() const;
470 
472  void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );
473 
475  static QString capitalizeLayerName( const QString &name );
476 
483  virtual QString styleURI() const;
484 
493  virtual QString loadDefaultStyle( bool &resultFlag SIP_OUT );
494 
508  virtual QString loadNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
509 
516  virtual bool loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml SIP_OUT );
517 
526  virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT );
527 
534  virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg ) const;
535 
536 
543  virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;
544 
553  virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT );
554 
568  virtual QString saveNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
569 
577  virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;
578 
586  virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
587 
588  virtual bool readSld( const QDomNode &node, QString &errorMessage )
589  { Q_UNUSED( node ); errorMessage = QStringLiteral( "Layer type %1 not supported" ).arg( type() ); return false; }
590 
591 
592 
599  virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context ) = 0;
600 
609  virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
610 
618  virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const = 0;
619 
629  virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;
630 
632  QUndoStack *undoStack();
633 
637  QUndoStack *undoStackStyles();
638 
642  void setLegendUrl( const QString &legendUrl ) { mLegendUrl = legendUrl; }
643 
647  QString legendUrl() const { return mLegendUrl; }
648 
652  void setLegendUrlFormat( const QString &legendUrlFormat ) { mLegendUrlFormat = legendUrlFormat; }
653 
657  QString legendUrlFormat() const { return mLegendUrlFormat; }
658 
664  void setLegend( QgsMapLayerLegend *legend SIP_TRANSFER );
665 
670  QgsMapLayerLegend *legend() const;
671 
676  QgsMapLayerStyleManager *styleManager() const;
677 
686  bool isInScaleRange( double scale ) const;
687 
698  double minimumScale() const;
699 
710  double maximumScale() const;
711 
719  bool hasScaleBasedVisibility() const;
720 
727  bool hasAutoRefreshEnabled() const;
728 
736  int autoRefreshInterval() const;
737 
749  void setAutoRefreshInterval( int interval );
750 
757  void setAutoRefreshEnabled( bool enabled );
758 
765  virtual const QgsLayerMetadata &metadata() const;
766 
773  virtual void setMetadata( const QgsLayerMetadata &metadata );
774 
779  virtual QString htmlMetadata() const;
780 
782  virtual QDateTime timestamp() const;
783 
791  virtual QSet<QgsMapLayerDependency> dependencies() const;
792 
793  public slots:
794 
804  void setMinimumScale( double scale );
805 
815  void setMaximumScale( double scale );
816 
823  void setScaleBasedVisibility( const bool enabled );
824 
833  void triggerRepaint( bool deferredUpdate = false );
834 
838  void emitStyleChanged();
839 
848  virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
849 
855  void setRefreshOnNotifyEnabled( bool enabled );
856 
864  void setRefreshOnNofifyMessage( const QString &message ) { mRefreshOnNofifyMessage = message; }
865 
871  QString refreshOnNotifyMessage() const { return mRefreshOnNofifyMessage; }
872 
878  bool isRefreshOnNotifyEnabled() const { return mIsRefreshOnNofifyEnabled; }
879 
880 
881  signals:
882 
884  void statusChanged( const QString &status );
885 
891  void nameChanged();
892 
894  void crsChanged();
895 
901  void repaintRequested( bool deferredUpdate = false );
902 
904  void recalculateExtents() const;
905 
907  void dataChanged();
908 
910  void blendModeChanged( QPainter::CompositionMode blendMode );
911 
915  void rendererChanged();
916 
923  void styleChanged();
924 
929  void legendChanged();
930 
935  void configChanged();
936 
940  void dependenciesChanged();
941 
948  void willBeDeleted();
949 
955  void autoRefreshIntervalChanged( int interval );
956 
963  void metadataChanged();
964 
965  private slots:
966 
967  void onNotifiedTriggerRepaint( const QString &message );
968 
969  protected:
970 
975  void clone( QgsMapLayer *layer ) const;
976 
978  virtual void setExtent( const QgsRectangle &rect );
979 
981  void setValid( bool valid );
982 
986  virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
987 
991  virtual bool writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const;
992 
993 
997  void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
998 
1000  void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
1001 
1003  void readStyleManager( const QDomNode &layerNode );
1005  void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
1006 
1007 #ifndef SIP_RUN
1008 #if 0
1009  void connectNotify( const char *signal ) override;
1011 #endif
1012 #endif
1013 
1015  void appendError( const QgsErrorMessage &error ) { mError.append( error );}
1017  void setError( const QgsError &error ) { mError = error;}
1018 
1021 
1023  bool mValid;
1024 
1026  QString mDataSource;
1027 
1029  QString mLayerName;
1030 
1034 
1035  QString mShortName;
1036  QString mTitle;
1037 
1039  QString mAbstract;
1040  QString mKeywordList;
1041 
1043  QString mDataUrl;
1045 
1047  QString mAttribution;
1049 
1051  QString mMetadataUrl;
1054 
1056  QString mLegendUrl;
1058 
1061 
1063  QSet<QgsMapLayerDependency> mDependencies;
1064 
1066  bool hasDependencyCycle( const QSet<QgsMapLayerDependency> &layers ) const;
1067 
1068  bool mIsRefreshOnNofifyEnabled = false;
1070 
1071  private:
1072 
1077  virtual bool isReadOnly() const;
1078 
1082 
1084  QString mID;
1085 
1087  QgsMapLayer::LayerType mLayerType;
1088 
1090  QPainter::CompositionMode mBlendMode;
1091 
1093  QString mTag;
1094 
1096  double mMinScale;
1098  double mMaxScale;
1100  bool mScaleBasedVisibility;
1101 
1103  QUndoStack mUndoStack;
1104 
1105  QUndoStack mUndoStackStyles;
1106 
1108  QgsObjectCustomProperties mCustomProperties;
1109 
1111  QgsMapLayerLegend *mLegend = nullptr;
1112 
1114  QgsMapLayerStyleManager *mStyleManager = nullptr;
1115 
1117  QTimer mRefreshTimer;
1118 
1119  QgsLayerMetadata mMetadata;
1120 
1121 };
1122 
1124 
1125 #ifndef SIP_RUN
1126 
1132 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
1133 
1139 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
1140 #endif
1141 
1142 #endif
QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:275
QString mShortName
Definition: qgsmaplayer.h:1035
void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:285
The class is used as a container of context for various read/write operations on other objects...
QString dataUrlFormat() const
Returns the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:241
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:54
QString mAttributionUrl
Definition: qgsmaplayer.h:1048
QString mKeywordList
Definition: qgsmaplayer.h:1040
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
Definition: qgsmaplayer.h:166
QString mDataUrlFormat
Definition: qgsmaplayer.h:1044
QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:225
QString mLegendUrlFormat
Definition: qgsmaplayer.h:1057
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:343
void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
Definition: qgsmaplayer.h:301
Abstract base class for spatial data provider implementations.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1132
Q_DECLARE_METATYPE(QModelIndex)
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:1029
bool isRefreshOnNotifyEnabled() const
Returns true if the refresh on provider nofification is enabled.
Definition: qgsmaplayer.h:878
QgsError mError
Error.
Definition: qgsmaplayer.h:1060
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:1020
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:1051
void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
Definition: qgsmaplayer.h:652
QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:325
void setKeywordList(const QString &keywords)
Sets the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:200
void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:172
#define SIP_SKIP
Definition: qgis_sip.h:119
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:92
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
Definition: qgsmaplayer.h:1063
QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:207
QString mDataUrl
DataUrl of the layer.
Definition: qgsmaplayer.h:1043
#define SIP_TRANSFER
Definition: qgis_sip.h:36
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
#define SIP_END
Definition: qgis_sip.h:175
QString legendUrl() const
Returns the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:647
Reads and writes project states.
Definition: qgsproject.h:79
QgsErrorMessage represents single error message.
Definition: qgserror.h:31
#define SIP_FACTORY
Definition: qgis_sip.h:69
bool mValid
Indicates if the layer is valid and can be drawn.
Definition: qgsmaplayer.h:1023
void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:233
QString mTitle
Definition: qgsmaplayer.h:1036
virtual bool readSld(const QDomNode &node, QString &errorMessage)
Definition: qgsmaplayer.h:588
QString legendUrlFormat() const
Returns the format for a URL based layer legend.
Definition: qgsmaplayer.h:657
QString mMetadataUrlFormat
Definition: qgsmaplayer.h:1053
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:642
void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:317
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:179
QString mAttribution
Attribution of the layer.
Definition: qgsmaplayer.h:1047
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:1039
A structured metadata store for a map layer.
QString mRefreshOnNofifyMessage
Definition: qgsmaplayer.h:1069
Contains information about the context of a rendering operation.
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:1026
QgsError is container for error messages (report).
Definition: qgserror.h:82
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1139
#define SIP_OUT
Definition: qgis_sip.h:51
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:1056
This class represents a coordinate reference system (CRS).
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
void setAttributionUrl(const QString &attribUrl)
Sets the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:267
void appendError(const QgsErrorMessage &error)
Add error message.
Definition: qgsmaplayer.h:1015
Base class for utility classes that encapsulate information necessary for rendering of map layers...
bool readOnly() const
Returns if this layer is read only.
Definition: qgsmaplayer.h:339
QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:293
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request. ...
Definition: qgsmaplayer.h:251
void setRefreshOnNofifyMessage(const QString &message)
Set the notification message that triggers repaine If refresh on notification is enabled, the notification will triggerRepaint only if the notification message is equal to.
Definition: qgsmaplayer.h:864
Management of styles for use with one map layer.
QString refreshOnNotifyMessage() const
Returns the message that should be notified by the provider to triggerRepaint.
Definition: qgsmaplayer.h:871
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:217
QString mLayerOrigName
Original name of the layer.
Definition: qgsmaplayer.h:1033
QString mMetadataUrlType
Definition: qgsmaplayer.h:1052
void setError(const QgsError &error)
Set error message.
Definition: qgsmaplayer.h:1017
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:186
QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:259
QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:309