QGIS API Documentation  2.99.0-Master (6c64c5a)
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 
106  {
107  Style = 0,
109  };
110 
117  QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, const QString &name = QString(), const QString &source = QString() );
118 
119  ~QgsMapLayer() override;
120 
122  QgsMapLayer( QgsMapLayer const & ) = delete;
124  QgsMapLayer &operator=( QgsMapLayer const & ) = delete;
125 
132  virtual QgsMapLayer *clone() const = 0;
133 
137  QgsMapLayer::LayerType type() const;
138 
144  static QString extensionPropertyType( PropertyType type );
145 
147  QString id() const;
148 
154  void setName( const QString &name );
155 
160  QString name() const;
161 
165  virtual QgsDataProvider *dataProvider();
166 
171  virtual const QgsDataProvider *dataProvider() const SIP_SKIP;
172 
179  void setShortName( const QString &shortName ) { mShortName = shortName; }
180 
186  QString shortName() const { return mShortName; }
187 
193  void setTitle( const QString &title ) { mTitle = title; }
194 
201  QString title() const { return mTitle; }
202 
209  void setAbstract( const QString &abstract ) { mAbstract = abstract; }
210 
217  QString abstract() const { return mAbstract; }
218 
225  void setKeywordList( const QString &keywords ) { mKeywordList = keywords; }
226 
233  QString keywordList() const { return mKeywordList; }
234 
235  /* Layer dataUrl information */
236 
244  void setDataUrl( const QString &dataUrl ) { mDataUrl = dataUrl; }
245 
253  QString dataUrl() const { return mDataUrl; }
254 
262  void setDataUrlFormat( const QString &dataUrlFormat ) { mDataUrlFormat = dataUrlFormat; }
263 
271  QString dataUrlFormat() const { return mDataUrlFormat; }
272 
273  /* Layer attribution information */
274 
282  void setAttribution( const QString &attrib ) { mAttribution = attrib; }
283 
291  QString attribution() const { return mAttribution; }
292 
300  void setAttributionUrl( const QString &attribUrl ) { mAttributionUrl = attribUrl; }
301 
309  QString attributionUrl() const { return mAttributionUrl; }
310 
311  /* Layer metadataUrl information */
312 
320  void setMetadataUrl( const QString &metaUrl ) { mMetadataUrl = metaUrl; }
321 
329  QString metadataUrl() const { return mMetadataUrl; }
330 
338  void setMetadataUrlType( const QString &metaUrlType ) { mMetadataUrlType = metaUrlType; }
339 
347  QString metadataUrlType() const { return mMetadataUrlType; }
348 
356  void setMetadataUrlFormat( const QString &metaUrlFormat ) { mMetadataUrlFormat = metaUrlFormat; }
357 
365  QString metadataUrlFormat() const { return mMetadataUrlFormat; }
366 
372  void setBlendMode( QPainter::CompositionMode blendMode );
373 
378  QPainter::CompositionMode blendMode() const;
379 
381  bool readOnly() const { return isReadOnly(); }
382 
386  virtual void reload() {}
387 
392  virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) = 0 SIP_FACTORY;
393 
395  virtual QgsRectangle extent() const;
396 
402  bool isValid() const;
403 
410  QString publicSource() const;
411 
417  QString source() const;
418 
423  virtual QStringList subLayers() const;
424 
429  virtual void setLayerOrder( const QStringList &layers );
430 
436  virtual void setSubLayerVisibility( const QString &name, bool visible );
437 
439  virtual bool isEditable() const;
440 
445  virtual bool isSpatial() const;
446 
463  bool readLayerXml( const QDomElement &layerElement, const QgsReadWriteContext &context );
464 
482  bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
483 
488  virtual void resolveReferences( QgsProject *project );
489 
495  QStringList customPropertyKeys() const;
496 
502  void setCustomProperty( const QString &key, const QVariant &value );
503 
508  QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;
509 
514  void setCustomProperties( const QgsObjectCustomProperties &properties );
515 
520  void removeCustomProperty( const QString &key );
521 
527  virtual QgsError error() const;
528 
533  QgsCoordinateReferenceSystem crs() const;
534 
536  void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );
537 
543  static QString formatLayerName( const QString &name );
544 
552  virtual QString metadataUri() const;
553 
560  void exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const;
561 
571  virtual QString saveDefaultMetadata( bool &resultFlag SIP_OUT );
572 
587  QString saveNamedMetadata( const QString &uri, bool &resultFlag );
588 
603  virtual QString loadNamedMetadata( const QString &uri, bool &resultFlag SIP_OUT );
604 
614  QString loadDefaultMetadata( bool &resultFlag );
615 
624  bool loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd );
625 
633  bool importNamedMetadata( QDomDocument &document, QString &errorMessage );
634 
642  virtual QString styleURI() const;
643 
653  virtual QString loadDefaultStyle( bool &resultFlag SIP_OUT );
654 
669  virtual QString loadNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
670 
678  virtual bool loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml SIP_OUT );
679 
688  virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT );
689 
696  virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg ) const;
697 
698 
705  virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;
706 
716  virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT );
717 
732  virtual QString saveNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
733 
742  virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;
743 
752  virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
753 
754  virtual bool readSld( const QDomNode &node, QString &errorMessage )
755  { Q_UNUSED( node ); errorMessage = QStringLiteral( "Layer type %1 not supported" ).arg( type() ); return false; }
756 
757 
758 
766  virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context ) = 0;
767 
777  virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
778 
787  virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const = 0;
788 
799  virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;
800 
802  QUndoStack *undoStack();
803 
808  QUndoStack *undoStackStyles();
809 
813  void setLegendUrl( const QString &legendUrl ) { mLegendUrl = legendUrl; }
814 
818  QString legendUrl() const { return mLegendUrl; }
819 
823  void setLegendUrlFormat( const QString &legendUrlFormat ) { mLegendUrlFormat = legendUrlFormat; }
824 
828  QString legendUrlFormat() const { return mLegendUrlFormat; }
829 
835  void setLegend( QgsMapLayerLegend *legend SIP_TRANSFER );
836 
841  QgsMapLayerLegend *legend() const;
842 
847  QgsMapLayerStyleManager *styleManager() const;
848 
853  void setRenderer3D( QgsAbstract3DRenderer *renderer SIP_TRANSFER );
854 
859  QgsAbstract3DRenderer *renderer3D() const;
860 
870  bool isInScaleRange( double scale ) const;
871 
882  double minimumScale() const;
883 
894  double maximumScale() const;
895 
904  bool hasScaleBasedVisibility() const;
905 
912  bool hasAutoRefreshEnabled() const;
913 
921  int autoRefreshInterval() const;
922 
934  void setAutoRefreshInterval( int interval );
935 
942  void setAutoRefreshEnabled( bool enabled );
943 
950  virtual const QgsLayerMetadata &metadata() const;
951 
958  virtual void setMetadata( const QgsLayerMetadata &metadata );
959 
964  virtual QString htmlMetadata() const;
965 
967  virtual QDateTime timestamp() const;
968 
976  virtual QSet<QgsMapLayerDependency> dependencies() const;
977 
983  QString refreshOnNotifyMessage() const { return mRefreshOnNofifyMessage; }
984 
990  bool isRefreshOnNotifyEnabled() const { return mIsRefreshOnNofifyEnabled; }
991 
992  public slots:
993 
1003  void setMinimumScale( double scale );
1004 
1014  void setMaximumScale( double scale );
1015 
1023  void setScaleBasedVisibility( const bool enabled );
1024 
1033  void triggerRepaint( bool deferredUpdate = false );
1034 
1039  void emitStyleChanged();
1040 
1049  virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
1050 
1056  void setRefreshOnNotifyEnabled( bool enabled );
1057 
1065  void setRefreshOnNofifyMessage( const QString &message ) { mRefreshOnNofifyMessage = message; }
1066 
1067  signals:
1068 
1070  void statusChanged( const QString &status );
1071 
1077  void nameChanged();
1078 
1080  void crsChanged();
1081 
1088  void repaintRequested( bool deferredUpdate = false );
1089 
1091  void recalculateExtents() const;
1092 
1094  void dataChanged();
1095 
1097  void blendModeChanged( QPainter::CompositionMode blendMode );
1098 
1103  void rendererChanged();
1104 
1112  void styleChanged();
1113 
1118  void legendChanged();
1119 
1124  void renderer3DChanged();
1125 
1130  void configChanged();
1131 
1135  void dependenciesChanged();
1136 
1143  void willBeDeleted();
1144 
1150  void autoRefreshIntervalChanged( int interval );
1151 
1158  void metadataChanged();
1159 
1160  private slots:
1161 
1162  void onNotifiedTriggerRepaint( const QString &message );
1163 
1164  protected:
1165 
1171  void clone( QgsMapLayer *layer ) const;
1172 
1174  virtual void setExtent( const QgsRectangle &rect );
1175 
1177  void setValid( bool valid );
1178 
1183  virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
1184 
1189  virtual bool writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const;
1190 
1195  void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
1196 
1198  void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
1199 
1201  void readStyleManager( const QDomNode &layerNode );
1203  void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
1204 
1209  void writeCommonStyle( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
1210 
1215  void readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context );
1216 
1217 #ifndef SIP_RUN
1218 #if 0
1219  void connectNotify( const char *signal ) override;
1221 #endif
1222 #endif
1223 
1225  void appendError( const QgsErrorMessage &error ) { mError.append( error );}
1227  void setError( const QgsError &error ) { mError = error;}
1228 
1231 
1233  bool mValid = false;
1234 
1236  QString mDataSource;
1237 
1239  QString mLayerName;
1240 
1241  QString mShortName;
1242  QString mTitle;
1243 
1245  QString mAbstract;
1246  QString mKeywordList;
1247 
1249  QString mDataUrl;
1251 
1253  QString mAttribution;
1255 
1257  QString mMetadataUrl;
1260 
1262  QString mLegendUrl;
1264 
1267 
1269  QSet<QgsMapLayerDependency> mDependencies;
1270 
1272  bool hasDependencyCycle( const QSet<QgsMapLayerDependency> &layers ) const;
1273 
1274  bool mIsRefreshOnNofifyEnabled = false;
1276 
1277  private:
1278 
1279  virtual QString baseURI( PropertyType type ) const;
1280  QString saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag );
1281  QString loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag );
1282  bool loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type );
1283 
1288  virtual bool isReadOnly() const;
1289 
1294 
1296  QString mID;
1297 
1299  QgsMapLayer::LayerType mLayerType;
1300 
1302  QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
1303 
1305  QString mTag;
1306 
1307  //set some generous defaults for scale based visibility
1308 
1310  double mMinScale = 0;
1312  double mMaxScale = 100000000;
1314  bool mScaleBasedVisibility = false;
1315 
1317  QUndoStack *mUndoStack = nullptr;
1318 
1319  QUndoStack *mUndoStackStyles = nullptr;
1320 
1322  QgsObjectCustomProperties mCustomProperties;
1323 
1325  QgsMapLayerLegend *mLegend = nullptr;
1326 
1328  QgsMapLayerStyleManager *mStyleManager = nullptr;
1329 
1331  QTimer *mRefreshTimer = nullptr;
1332 
1333  QgsLayerMetadata mMetadata;
1334 
1336  QgsAbstract3DRenderer *m3DRenderer = nullptr;
1337 
1338 };
1339 
1341 
1342 #ifndef SIP_RUN
1343 
1349 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
1350 
1356 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
1357 #endif
1358 
1359 #endif
QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:309
QString mShortName
Definition: qgsmaplayer.h:1241
void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:320
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:271
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:1254
QString mKeywordList
Definition: qgsmaplayer.h:1246
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
Definition: qgsmaplayer.h:186
QString mDataUrlFormat
Definition: qgsmaplayer.h:1250
QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:253
PropertyType
Maplayer has a style and a metadata property.
Definition: qgsmaplayer.h:105
QString mLegendUrlFormat
Definition: qgsmaplayer.h:1263
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:386
void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
Definition: qgsmaplayer.h:338
Abstract base class for spatial data provider implementations.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1349
Q_DECLARE_METATYPE(QModelIndex)
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:1239
bool isRefreshOnNotifyEnabled() const
Returns true if the refresh on provider nofification is enabled.
Definition: qgsmaplayer.h:990
QgsError mError
Error.
Definition: qgsmaplayer.h:1266
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:1230
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:1257
void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
Definition: qgsmaplayer.h:823
QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:365
void setKeywordList(const QString &keywords)
Sets the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:225
void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:193
#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:1269
QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:233
QString mDataUrl
DataUrl of the layer.
Definition: qgsmaplayer.h:1249
#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:818
Reads and writes project states.
Definition: qgsproject.h:82
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:262
QString mTitle
Definition: qgsmaplayer.h:1242
virtual bool readSld(const QDomNode &node, QString &errorMessage)
Definition: qgsmaplayer.h:754
QString legendUrlFormat() const
Returns the format for a URL based layer legend.
Definition: qgsmaplayer.h:828
QString mMetadataUrlFormat
Definition: qgsmaplayer.h:1259
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:813
void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:356
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:201
QString mAttribution
Attribution of the layer.
Definition: qgsmaplayer.h:1253
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:1245
A structured metadata store for a map layer.
QString mRefreshOnNofifyMessage
Definition: qgsmaplayer.h:1275
Contains information about the context of a rendering operation.
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:1236
QgsError is container for error messages (report).
Definition: qgserror.h:83
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1356
#define SIP_OUT
Definition: qgis_sip.h:51
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:1262
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:300
void appendError(const QgsErrorMessage &error)
Add error message.
Definition: qgsmaplayer.h:1225
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:381
QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:329
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request. ...
Definition: qgsmaplayer.h:282
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:1065
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:983
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:244
QString mMetadataUrlType
Definition: qgsmaplayer.h:1258
void setError(const QgsError &error)
Set error message.
Definition: qgsmaplayer.h:1227
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:209
QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:291
QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:347