QGIS API Documentation  2.99.0-Master (ba079d8)
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 
700  double minimumScale() const;
701 
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 
802  void setMinimumScale( double scale );
803 
811  void setMaximumScale( double scale );
812 
819  void setScaleBasedVisibility( const bool enabled );
820 
829  void triggerRepaint( bool deferredUpdate = false );
830 
834  void emitStyleChanged();
835 
844  virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
845 
846  signals:
847 
849  void statusChanged( const QString &status );
850 
856  void nameChanged();
857 
859  void crsChanged();
860 
866  void repaintRequested( bool deferredUpdate = false );
867 
869  void recalculateExtents() const;
870 
872  void dataChanged();
873 
875  void blendModeChanged( QPainter::CompositionMode blendMode );
876 
880  void rendererChanged();
881 
888  void styleChanged();
889 
894  void legendChanged();
895 
900  void configChanged();
901 
905  void dependenciesChanged();
906 
913  void willBeDeleted();
914 
920  void autoRefreshIntervalChanged( int interval );
921 
928  void metadataChanged();
929 
930  protected:
931 
936  void clone( QgsMapLayer *layer ) const;
937 
939  virtual void setExtent( const QgsRectangle &rect );
940 
942  void setValid( bool valid );
943 
947  virtual bool readXml( const QDomNode &layer_node, const QgsReadWriteContext &context );
948 
952  virtual bool writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const;
953 
954 
958  void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
959 
961  void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
962 
964  void readStyleManager( const QDomNode &layerNode );
966  void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
967 
968 #ifndef SIP_RUN
969 #if 0
970  void connectNotify( const char *signal ) override;
972 #endif
973 #endif
974 
976  void appendError( const QgsErrorMessage &error ) { mError.append( error );}
978  void setError( const QgsError &error ) { mError = error;}
979 
982 
984  bool mValid;
985 
987  QString mDataSource;
988 
990  QString mLayerName;
991 
994  QString mLayerOrigName;
995 
996  QString mShortName;
997  QString mTitle;
998 
1000  QString mAbstract;
1001  QString mKeywordList;
1002 
1004  QString mDataUrl;
1006 
1008  QString mAttribution;
1010 
1012  QString mMetadataUrl;
1015 
1017  QString mLegendUrl;
1019 
1022 
1024  QSet<QgsMapLayerDependency> mDependencies;
1025 
1027  bool hasDependencyCycle( const QSet<QgsMapLayerDependency> &layers ) const;
1028 
1029  private:
1030 
1035  virtual bool isReadOnly() const;
1036 
1040 
1042  QString mID;
1043 
1045  QgsMapLayer::LayerType mLayerType;
1046 
1048  QPainter::CompositionMode mBlendMode;
1049 
1051  QString mTag;
1052 
1054  double mMinScale;
1056  double mMaxScale;
1058  bool mScaleBasedVisibility;
1059 
1061  QUndoStack mUndoStack;
1062 
1063  QUndoStack mUndoStackStyles;
1064 
1066  QgsObjectCustomProperties mCustomProperties;
1067 
1069  QgsMapLayerLegend *mLegend = nullptr;
1070 
1072  QgsMapLayerStyleManager *mStyleManager = nullptr;
1073 
1075  QTimer mRefreshTimer;
1076 
1077  QgsLayerMetadata mMetadata;
1078 
1079 };
1080 
1082 
1083 #ifndef SIP_RUN
1084 
1090 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
1091 
1097 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
1098 #endif
1099 
1100 #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:996
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:1009
QString mKeywordList
Definition: qgsmaplayer.h:1001
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:1005
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:1018
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:1090
Q_DECLARE_METATYPE(QModelIndex)
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:990
QgsError mError
Error.
Definition: qgsmaplayer.h:1021
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:981
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:1012
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:102
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:1024
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:1004
#define SIP_TRANSFER
Definition: qgis_sip.h:24
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
#define SIP_END
Definition: qgis_sip.h:148
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:57
bool mValid
Indicates if the layer is valid and can be drawn.
Definition: qgsmaplayer.h:984
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:997
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:1014
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:1008
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:1000
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:987
QgsError is container for error messages (report).
Definition: qgserror.h:82
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1097
#define SIP_OUT
Definition: qgis_sip.h:39
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:1017
This class represents a coordinate reference system (CRS).
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:143
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:976
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:994
QString mMetadataUrlType
Definition: qgsmaplayer.h:1013
void setError(const QgsError &error)
Set error message.
Definition: qgsmaplayer.h:978
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