QGIS API Documentation  2.99.0-Master (0cba29c)
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 
133  void setName( const QString &name );
134 
139  QString name() const;
140 
144  virtual QgsDataProvider *dataProvider();
145 
150  virtual const QgsDataProvider *dataProvider() const SIP_SKIP;
151 
155  QString originalName() const;
156 
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 
183  QString title() const { return mTitle; }
184 
190  void setAbstract( const QString &abstract ) { mAbstract = abstract; }
191 
197  QString abstract() const { return mAbstract; }
198 
204  void setKeywordList( const QString &keywords ) { mKeywordList = keywords; }
205 
211  QString keywordList() const { return mKeywordList; }
212 
213  /* Layer dataUrl information */
214 
221  void setDataUrl( const QString &dataUrl ) { mDataUrl = dataUrl; }
222 
229  QString dataUrl() const { return mDataUrl; }
230 
237  void setDataUrlFormat( const QString &dataUrlFormat ) { mDataUrlFormat = dataUrlFormat; }
238 
245  QString dataUrlFormat() const { return mDataUrlFormat; }
246 
247  /* Layer attribution information */
248 
255  void setAttribution( const QString &attrib ) { mAttribution = attrib; }
256 
263  QString attribution() const { return mAttribution; }
264 
271  void setAttributionUrl( const QString &attribUrl ) { mAttributionUrl = attribUrl; }
272 
279  QString attributionUrl() const { return mAttributionUrl; }
280 
281  /* Layer metadataUrl information */
282 
289  void setMetadataUrl( const QString &metaUrl ) { mMetadataUrl = metaUrl; }
290 
297  QString metadataUrl() const { return mMetadataUrl; }
298 
305  void setMetadataUrlType( const QString &metaUrlType ) { mMetadataUrlType = metaUrlType; }
306 
313  QString metadataUrlType() const { return mMetadataUrlType; }
314 
321  void setMetadataUrlFormat( const QString &metaUrlFormat ) { mMetadataUrlFormat = metaUrlFormat; }
322 
329  QString metadataUrlFormat() const { return mMetadataUrlFormat; }
330 
335  void setBlendMode( QPainter::CompositionMode blendMode );
336 
340  QPainter::CompositionMode blendMode() const;
341 
343  bool readOnly() const { return isReadOnly(); }
344 
347  virtual void reload() {}
348 
352  virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) = 0 SIP_FACTORY;
353 
355  virtual QgsRectangle extent() const;
356 
361  bool isValid() const;
362 
368  QString publicSource() const;
369 
374  QString source() const;
375 
380  virtual QStringList subLayers() const;
381 
386  virtual void setLayerOrder( const QStringList &layers );
387 
392  virtual void setSubLayerVisibility( const QString &name, bool visible );
393 
395  virtual bool isEditable() const;
396 
400  virtual bool isSpatial() const;
401 
417  bool readLayerXml( const QDomElement &layerElement, const QgsReadWriteContext &context );
418 
435  bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
436 
441  QStringList customPropertyKeys() const;
442 
447  void setCustomProperty( const QString &key, const QVariant &value );
448 
452  QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;
453 
457  void setCustomProperties( const QgsObjectCustomProperties &properties );
458 
462  void removeCustomProperty( const QString &key );
463 
468  virtual QgsError error() const;
469 
473  QgsCoordinateReferenceSystem crs() const;
474 
476  void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );
477 
479  static QString capitalizeLayerName( const QString &name );
480 
487  virtual QString styleURI() const;
488 
497  virtual QString loadDefaultStyle( bool &resultFlag SIP_OUT );
498 
512  virtual QString loadNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
513 
520  virtual bool loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml SIP_OUT );
521 
530  virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT );
531 
538  virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg ) const;
539 
540 
547  virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;
548 
557  virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT );
558 
572  virtual QString saveNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
573 
581  virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;
582 
590  virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
591 
592  virtual bool readSld( const QDomNode &node, QString &errorMessage )
593  { Q_UNUSED( node ); errorMessage = QStringLiteral( "Layer type %1 not supported" ).arg( type() ); return false; }
594 
595 
596 
603  virtual bool readSymbology( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context ) = 0;
604 
613  virtual bool readStyle( const QDomNode &node, QString &errorMessage, const QgsReadWriteContext &context );
614 
622  virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const = 0;
623 
633  virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;
634 
636  QUndoStack *undoStack();
637 
641  QUndoStack *undoStackStyles();
642 
646  void setLegendUrl( const QString &legendUrl ) { mLegendUrl = legendUrl; }
647 
651  QString legendUrl() const { return mLegendUrl; }
652 
656  void setLegendUrlFormat( const QString &legendUrlFormat ) { mLegendUrlFormat = legendUrlFormat; }
657 
661  QString legendUrlFormat() const { return mLegendUrlFormat; }
662 
668  void setLegend( QgsMapLayerLegend *legend SIP_TRANSFER );
669 
674  QgsMapLayerLegend *legend() const;
675 
680  QgsMapLayerStyleManager *styleManager() const;
681 
690  bool isInScaleRange( double scale ) const;
691 
702  double minimumScale() const;
703 
714  double maximumScale() const;
715 
723  bool hasScaleBasedVisibility() const;
724 
731  bool hasAutoRefreshEnabled() const;
732 
740  int autoRefreshInterval() const;
741 
753  void setAutoRefreshInterval( int interval );
754 
761  void setAutoRefreshEnabled( bool enabled );
762 
769  virtual const QgsLayerMetadata &metadata() const;
770 
777  virtual void setMetadata( const QgsLayerMetadata &metadata );
778 
783  virtual QString htmlMetadata() const;
784 
786  virtual QDateTime timestamp() const;
787 
795  virtual QSet<QgsMapLayerDependency> dependencies() const;
796 
797  public slots:
798 
808  void setMinimumScale( double scale );
809 
819  void setMaximumScale( double scale );
820 
827  void setScaleBasedVisibility( const bool enabled );
828 
837  void triggerRepaint( bool deferredUpdate = false );
838 
842  void emitStyleChanged();
843 
852  virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
853 
854  signals:
855 
857  void statusChanged( const QString &status );
858 
864  void nameChanged();
865 
867  void crsChanged();
868 
874  void repaintRequested( bool deferredUpdate = false );
875 
877  void recalculateExtents() const;
878 
880  void dataChanged();
881 
883  void blendModeChanged( QPainter::CompositionMode blendMode );
884 
888  void rendererChanged();
889 
896  void styleChanged();
897 
902  void legendChanged();
903 
908  void configChanged();
909 
913  void dependenciesChanged();
914 
921  void willBeDeleted();
922 
928  void autoRefreshIntervalChanged( int interval );
929 
936  void metadataChanged();
937 
938  protected:
939 
944  void clone( QgsMapLayer *layer ) const;
945 
947  virtual void setExtent( const QgsRectangle &rect );
948 
950  void setValid( bool valid );
951 
955  virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
956 
960  virtual bool writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const;
961 
962 
966  void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
967 
969  void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
970 
972  void readStyleManager( const QDomNode &layerNode );
974  void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
975 
976 #ifndef SIP_RUN
977 #if 0
978  void connectNotify( const char *signal ) override;
980 #endif
981 #endif
982 
984  void appendError( const QgsErrorMessage &error ) { mError.append( error );}
986  void setError( const QgsError &error ) { mError = error;}
987 
990 
992  bool mValid;
993 
995  QString mDataSource;
996 
998  QString mLayerName;
999 
1003 
1004  QString mShortName;
1005  QString mTitle;
1006 
1008  QString mAbstract;
1009  QString mKeywordList;
1010 
1012  QString mDataUrl;
1014 
1016  QString mAttribution;
1018 
1020  QString mMetadataUrl;
1023 
1025  QString mLegendUrl;
1027 
1030 
1032  QSet<QgsMapLayerDependency> mDependencies;
1033 
1035  bool hasDependencyCycle( const QSet<QgsMapLayerDependency> &layers ) const;
1036 
1037  private:
1038 
1043  virtual bool isReadOnly() const;
1044 
1048 
1050  QString mID;
1051 
1053  QgsMapLayer::LayerType mLayerType;
1054 
1056  QPainter::CompositionMode mBlendMode;
1057 
1059  QString mTag;
1060 
1062  double mMinScale;
1064  double mMaxScale;
1066  bool mScaleBasedVisibility;
1067 
1069  QUndoStack mUndoStack;
1070 
1071  QUndoStack mUndoStackStyles;
1072 
1074  QgsObjectCustomProperties mCustomProperties;
1075 
1077  QgsMapLayerLegend *mLegend = nullptr;
1078 
1080  QgsMapLayerStyleManager *mStyleManager = nullptr;
1081 
1083  QTimer mRefreshTimer;
1084 
1085  QgsLayerMetadata mMetadata;
1086 
1087 };
1088 
1090 
1091 #ifndef SIP_RUN
1092 
1098 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
1099 
1105 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
1106 #endif
1107 
1108 #endif
QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:279
QString mShortName
Definition: qgsmaplayer.h:1004
void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:289
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:245
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:1017
QString mKeywordList
Definition: qgsmaplayer.h:1009
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:1013
QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:229
QString mLegendUrlFormat
Definition: qgsmaplayer.h:1026
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:347
void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
Definition: qgsmaplayer.h:305
Abstract base class for spatial data provider implementations.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1098
Q_DECLARE_METATYPE(QModelIndex)
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:998
QgsError mError
Error.
Definition: qgsmaplayer.h:1029
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:989
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:1020
void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
Definition: qgsmaplayer.h:656
QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:329
void setKeywordList(const QString &keywords)
Sets the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:204
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:92
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
Definition: qgsmaplayer.h:1032
QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:211
QString mDataUrl
DataUrl of the layer.
Definition: qgsmaplayer.h:1012
#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:651
Reads and writes project states.
Definition: qgsproject.h:78
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:992
void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:237
QString mTitle
Definition: qgsmaplayer.h:1005
virtual bool readSld(const QDomNode &node, QString &errorMessage)
Definition: qgsmaplayer.h:592
QString legendUrlFormat() const
Returns the format for a URL based layer legend.
Definition: qgsmaplayer.h:661
QString mMetadataUrlFormat
Definition: qgsmaplayer.h:1022
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:646
void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:321
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:183
QString mAttribution
Attribution of the layer.
Definition: qgsmaplayer.h:1016
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:1008
A structured metadata store for a map layer.
Contains information about the context of a rendering operation.
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:995
QgsError is container for error messages (report).
Definition: qgserror.h:82
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1105
#define SIP_OUT
Definition: qgis_sip.h:51
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:1025
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:271
void appendError(const QgsErrorMessage &error)
Add error message.
Definition: qgsmaplayer.h:984
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:343
QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:297
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request. ...
Definition: qgsmaplayer.h:255
Management of styles for use with one map layer.
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:221
QString mLayerOrigName
Original name of the layer.
Definition: qgsmaplayer.h:1002
QString mMetadataUrlType
Definition: qgsmaplayer.h:1021
void setError(const QgsError &error)
Set error message.
Definition: qgsmaplayer.h:986
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:190
QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:263
QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:313