QGIS API Documentation  2.99.0-Master (b058df7)
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 
40 class QgsDataProvider;
41 class QgsMapLayerLegend;
45 class QgsProject;
46 
47 class QDomDocument;
48 class QKeyEvent;
49 class QPainter;
50 
56 class CORE_EXPORT QgsMapLayer : public QObject
57 {
58  Q_OBJECT
59 
60  Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
61  Q_PROPERTY( int autoRefreshInterval READ autoRefreshInterval WRITE setAutoRefreshInterval NOTIFY autoRefreshIntervalChanged )
62  Q_PROPERTY( QgsLayerMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )
63 
64 #ifdef SIP_RUN
66  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sipCpp );
67 
68  sipType = 0;
69 
70  if ( layer )
71  {
72  switch ( layer->type() )
73  {
75  sipType = sipType_QgsVectorLayer;
76  break;
78  sipType = sipType_QgsRasterLayer;
79  break;
81  sipType = sipType_QgsPluginLayer;
82  break;
83  default:
84  sipType = nullptr;
85  break;
86  }
87  }
88  SIP_END
89 #endif
90 
91  public:
92 
94  enum LayerType
95  {
98  PluginLayer
99  };
100 
107  QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, const QString &name = QString(), const QString &source = QString() );
108 
109  virtual ~QgsMapLayer();
110 
112  QgsMapLayer( QgsMapLayer const & ) = delete;
114  QgsMapLayer &operator=( QgsMapLayer const & ) = delete;
115 
122  virtual QgsMapLayer *clone() const = 0;
123 
127  QgsMapLayer::LayerType type() const;
128 
130  QString id() const;
131 
137  void setName( const QString &name );
138 
143  QString name() const;
144 
148  virtual QgsDataProvider *dataProvider();
149 
154  virtual const QgsDataProvider *dataProvider() const SIP_SKIP;
155 
162  void setShortName( const QString &shortName ) { mShortName = shortName; }
163 
169  QString shortName() const { return mShortName; }
170 
176  void setTitle( const QString &title ) { mTitle = title; }
177 
184  QString title() const { return mTitle; }
185 
192  void setAbstract( const QString &abstract ) { mAbstract = abstract; }
193 
200  QString abstract() const { return mAbstract; }
201 
208  void setKeywordList( const QString &keywords ) { mKeywordList = keywords; }
209 
216  QString keywordList() const { return mKeywordList; }
217 
218  /* Layer dataUrl information */
219 
227  void setDataUrl( const QString &dataUrl ) { mDataUrl = dataUrl; }
228 
236  QString dataUrl() const { return mDataUrl; }
237 
245  void setDataUrlFormat( const QString &dataUrlFormat ) { mDataUrlFormat = dataUrlFormat; }
246 
254  QString dataUrlFormat() const { return mDataUrlFormat; }
255 
256  /* Layer attribution information */
257 
265  void setAttribution( const QString &attrib ) { mAttribution = attrib; }
266 
274  QString attribution() const { return mAttribution; }
275 
283  void setAttributionUrl( const QString &attribUrl ) { mAttributionUrl = attribUrl; }
284 
292  QString attributionUrl() const { return mAttributionUrl; }
293 
294  /* Layer metadataUrl information */
295 
303  void setMetadataUrl( const QString &metaUrl ) { mMetadataUrl = metaUrl; }
304 
312  QString metadataUrl() const { return mMetadataUrl; }
313 
321  void setMetadataUrlType( const QString &metaUrlType ) { mMetadataUrlType = metaUrlType; }
322 
330  QString metadataUrlType() const { return mMetadataUrlType; }
331 
339  void setMetadataUrlFormat( const QString &metaUrlFormat ) { mMetadataUrlFormat = metaUrlFormat; }
340 
348  QString metadataUrlFormat() const { return mMetadataUrlFormat; }
349 
355  void setBlendMode( QPainter::CompositionMode blendMode );
356 
361  QPainter::CompositionMode blendMode() const;
362 
364  bool readOnly() const { return isReadOnly(); }
365 
369  virtual void reload() {}
370 
375  virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) = 0 SIP_FACTORY;
376 
378  virtual QgsRectangle extent() const;
379 
385  bool isValid() const;
386 
393  QString publicSource() const;
394 
400  QString source() const;
401 
406  virtual QStringList subLayers() const;
407 
412  virtual void setLayerOrder( const QStringList &layers );
413 
419  virtual void setSubLayerVisibility( const QString &name, bool visible );
420 
422  virtual bool isEditable() const;
423 
428  virtual bool isSpatial() const;
429 
446  bool readLayerXml( const QDomElement &layerElement, const QgsReadWriteContext &context );
447 
465  bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
466 
471  virtual void resolveReferences( QgsProject *project );
472 
478  QStringList customPropertyKeys() const;
479 
485  void setCustomProperty( const QString &key, const QVariant &value );
486 
491  QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;
492 
497  void setCustomProperties( const QgsObjectCustomProperties &properties );
498 
503  void removeCustomProperty( const QString &key );
504 
510  virtual QgsError error() const;
511 
516  QgsCoordinateReferenceSystem crs() const;
517 
519  void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );
520 
526  static QString formatLayerName( const QString &name );
527 
535  virtual QString styleURI() const;
536 
546  virtual QString loadDefaultStyle( bool &resultFlag SIP_OUT );
547 
562  virtual QString loadNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
563 
571  virtual bool loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml SIP_OUT );
572 
581  virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT );
582 
589  virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg ) const;
590 
591 
598  virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;
599 
609  virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT );
610 
625  virtual QString saveNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
626 
635  virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;
636 
645  virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
646 
647  virtual bool readSld( const QDomNode &node, QString &errorMessage )
648  { Q_UNUSED( node ); errorMessage = QStringLiteral( "Layer type %1 not supported" ).arg( type() ); return false; }
649 
650 
651 
659  virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context ) = 0;
660 
670  virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
671 
680  virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const = 0;
681 
692  virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;
693 
695  QUndoStack *undoStack();
696 
701  QUndoStack *undoStackStyles();
702 
706  void setLegendUrl( const QString &legendUrl ) { mLegendUrl = legendUrl; }
707 
711  QString legendUrl() const { return mLegendUrl; }
712 
716  void setLegendUrlFormat( const QString &legendUrlFormat ) { mLegendUrlFormat = legendUrlFormat; }
717 
721  QString legendUrlFormat() const { return mLegendUrlFormat; }
722 
728  void setLegend( QgsMapLayerLegend *legend SIP_TRANSFER );
729 
734  QgsMapLayerLegend *legend() const;
735 
740  QgsMapLayerStyleManager *styleManager() const;
741 
746  void setRenderer3D( QgsAbstract3DRenderer *renderer SIP_TRANSFER );
747 
752  QgsAbstract3DRenderer *renderer3D() const;
753 
763  bool isInScaleRange( double scale ) const;
764 
775  double minimumScale() const;
776 
787  double maximumScale() const;
788 
797  bool hasScaleBasedVisibility() const;
798 
805  bool hasAutoRefreshEnabled() const;
806 
814  int autoRefreshInterval() const;
815 
827  void setAutoRefreshInterval( int interval );
828 
835  void setAutoRefreshEnabled( bool enabled );
836 
843  virtual const QgsLayerMetadata &metadata() const;
844 
851  virtual void setMetadata( const QgsLayerMetadata &metadata );
852 
857  virtual QString htmlMetadata() const;
858 
860  virtual QDateTime timestamp() const;
861 
869  virtual QSet<QgsMapLayerDependency> dependencies() const;
870 
876  QString refreshOnNotifyMessage() const { return mRefreshOnNofifyMessage; }
877 
883  bool isRefreshOnNotifyEnabled() const { return mIsRefreshOnNofifyEnabled; }
884 
885  public slots:
886 
896  void setMinimumScale( double scale );
897 
907  void setMaximumScale( double scale );
908 
916  void setScaleBasedVisibility( const bool enabled );
917 
926  void triggerRepaint( bool deferredUpdate = false );
927 
932  void emitStyleChanged();
933 
942  virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
943 
949  void setRefreshOnNotifyEnabled( bool enabled );
950 
958  void setRefreshOnNofifyMessage( const QString &message ) { mRefreshOnNofifyMessage = message; }
959 
960  signals:
961 
963  void statusChanged( const QString &status );
964 
970  void nameChanged();
971 
973  void crsChanged();
974 
981  void repaintRequested( bool deferredUpdate = false );
982 
984  void recalculateExtents() const;
985 
987  void dataChanged();
988 
990  void blendModeChanged( QPainter::CompositionMode blendMode );
991 
996  void rendererChanged();
997 
1005  void styleChanged();
1006 
1011  void legendChanged();
1012 
1017  void renderer3DChanged();
1018 
1023  void configChanged();
1024 
1028  void dependenciesChanged();
1029 
1036  void willBeDeleted();
1037 
1043  void autoRefreshIntervalChanged( int interval );
1044 
1051  void metadataChanged();
1052 
1053  private slots:
1054 
1055  void onNotifiedTriggerRepaint( const QString &message );
1056 
1057  protected:
1058 
1064  void clone( QgsMapLayer *layer ) const;
1065 
1067  virtual void setExtent( const QgsRectangle &rect );
1068 
1070  void setValid( bool valid );
1071 
1076  virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
1077 
1082  virtual bool writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const;
1083 
1088  void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
1089 
1091  void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
1092 
1094  void readStyleManager( const QDomNode &layerNode );
1096  void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
1097 
1098 #ifndef SIP_RUN
1099 #if 0
1100  void connectNotify( const char *signal ) override;
1102 #endif
1103 #endif
1104 
1106  void appendError( const QgsErrorMessage &error ) { mError.append( error );}
1108  void setError( const QgsError &error ) { mError = error;}
1109 
1112 
1114  bool mValid = false;
1115 
1117  QString mDataSource;
1118 
1120  QString mLayerName;
1121 
1122  QString mShortName;
1123  QString mTitle;
1124 
1126  QString mAbstract;
1127  QString mKeywordList;
1128 
1130  QString mDataUrl;
1132 
1134  QString mAttribution;
1136 
1138  QString mMetadataUrl;
1141 
1143  QString mLegendUrl;
1145 
1148 
1150  QSet<QgsMapLayerDependency> mDependencies;
1151 
1153  bool hasDependencyCycle( const QSet<QgsMapLayerDependency> &layers ) const;
1154 
1155  bool mIsRefreshOnNofifyEnabled = false;
1157 
1158  private:
1159 
1164  virtual bool isReadOnly() const;
1165 
1170 
1172  QString mID;
1173 
1175  QgsMapLayer::LayerType mLayerType;
1176 
1178  QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
1179 
1181  QString mTag;
1182 
1184  double mMinScale;
1186  double mMaxScale;
1188  bool mScaleBasedVisibility;
1189 
1191  QUndoStack mUndoStack;
1192 
1193  QUndoStack mUndoStackStyles;
1194 
1196  QgsObjectCustomProperties mCustomProperties;
1197 
1199  QgsMapLayerLegend *mLegend = nullptr;
1200 
1202  QgsMapLayerStyleManager *mStyleManager = nullptr;
1203 
1205  QTimer mRefreshTimer;
1206 
1207  QgsLayerMetadata mMetadata;
1208 
1210  QgsAbstract3DRenderer *m3DRenderer = nullptr;
1211 
1212 };
1213 
1215 
1216 #ifndef SIP_RUN
1217 
1223 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
1224 
1230 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
1231 #endif
1232 
1233 #endif
QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:292
QString mShortName
Definition: qgsmaplayer.h:1122
void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:303
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:254
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
Base class for all renderers that may to participate in 3D view.
QString mAttributionUrl
Definition: qgsmaplayer.h:1135
QString mKeywordList
Definition: qgsmaplayer.h:1127
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
Definition: qgsmaplayer.h:169
QString mDataUrlFormat
Definition: qgsmaplayer.h:1131
QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:236
QString mLegendUrlFormat
Definition: qgsmaplayer.h:1144
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:369
void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
Definition: qgsmaplayer.h:321
Abstract base class for spatial data provider implementations.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1223
Q_DECLARE_METATYPE(QModelIndex)
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:1120
bool isRefreshOnNotifyEnabled() const
Returns true if the refresh on provider nofification is enabled.
Definition: qgsmaplayer.h:883
QgsError mError
Error.
Definition: qgsmaplayer.h:1147
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:1111
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:1138
void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
Definition: qgsmaplayer.h:716
QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:348
void setKeywordList(const QString &keywords)
Sets the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:208
void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:176
#define SIP_SKIP
Definition: qgis_sip.h:119
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:94
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
Definition: qgsmaplayer.h:1150
QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:216
QString mDataUrl
DataUrl of the layer.
Definition: qgsmaplayer.h:1130
#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:711
Reads and writes project states.
Definition: qgsproject.h:81
QgsErrorMessage represents single error message.
Definition: qgserror.h:32
#define SIP_FACTORY
Definition: qgis_sip.h:69
void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:245
QString mTitle
Definition: qgsmaplayer.h:1123
virtual bool readSld(const QDomNode &node, QString &errorMessage)
Definition: qgsmaplayer.h:647
QString legendUrlFormat() const
Returns the format for a URL based layer legend.
Definition: qgsmaplayer.h:721
QString mMetadataUrlFormat
Definition: qgsmaplayer.h:1140
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:706
void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:339
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:184
QString mAttribution
Attribution of the layer.
Definition: qgsmaplayer.h:1134
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:1126
A structured metadata store for a map layer.
QString mRefreshOnNofifyMessage
Definition: qgsmaplayer.h:1156
Contains information about the context of a rendering operation.
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:1117
QgsError is container for error messages (report).
Definition: qgserror.h:83
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1230
#define SIP_OUT
Definition: qgis_sip.h:51
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:1143
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:283
void appendError(const QgsErrorMessage &error)
Add error message.
Definition: qgsmaplayer.h:1106
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:364
QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:312
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request. ...
Definition: qgsmaplayer.h:265
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:958
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:876
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:227
QString mMetadataUrlType
Definition: qgsmaplayer.h:1139
void setError(const QgsError &error)
Set error message.
Definition: qgsmaplayer.h:1108
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:192
QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:274
QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:330