QGIS API Documentation  2.99.0-Master (716ff6c)
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 
38 class QgsDataProvider;
39 class QgsMapLayerLegend;
42 class QgsPathResolver;
43 class QgsProject;
44 
45 class QDomDocument;
46 class QKeyEvent;
47 class QPainter;
48 
53 class CORE_EXPORT QgsMapLayer : public QObject
54 {
55  Q_OBJECT
56 
57  Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
58  Q_PROPERTY( int autoRefreshInterval READ autoRefreshInterval WRITE setAutoRefreshInterval NOTIFY autoRefreshIntervalChanged )
59 
60 #ifdef SIP_RUN
62  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sipCpp );
63 
64  sipType = 0;
65 
66  if ( layer )
67  {
68  switch ( layer->type() )
69  {
71  sipType = sipType_QgsVectorLayer;
72  break;
74  sipType = sipType_QgsRasterLayer;
75  break;
77  sipType = sipType_QgsPluginLayer;
78  break;
79  default:
80  sipType = nullptr;
81  break;
82  }
83  }
84  SIP_END
85 #endif
86 
87  public:
88 
90  enum LayerType
91  {
94  PluginLayer
95  };
96 
102  QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, const QString &name = QString(), const QString &source = QString() );
103 
104  virtual ~QgsMapLayer();
105 
107  QgsMapLayer( QgsMapLayer const & ) = delete;
109  QgsMapLayer &operator=( QgsMapLayer const & ) = delete;
110 
113  QgsMapLayer::LayerType type() const;
114 
116  QString id() const;
117 
124  void setName( const QString &name );
125 
130  QString name() const;
131 
135  virtual QgsDataProvider *dataProvider() { return nullptr; }
136 
141  virtual const QgsDataProvider *dataProvider() const SIP_SKIP { return nullptr; }
142 
146  QString originalName() const { return mLayerOrigName; }
147 
153  void setShortName( const QString &shortName ) { mShortName = shortName; }
154 
160  QString shortName() const { return mShortName; }
161 
167  void setTitle( const QString &title ) { mTitle = title; }
168 
174  QString title() const { return mTitle; }
175 
181  void setAbstract( const QString &abstract ) { mAbstract = abstract; }
182 
188  QString abstract() const { return mAbstract; }
189 
195  void setKeywordList( const QString &keywords ) { mKeywordList = keywords; }
196 
202  QString keywordList() const { return mKeywordList; }
203 
204  /* Layer dataUrl information */
205 
212  void setDataUrl( const QString &dataUrl ) { mDataUrl = dataUrl; }
213 
220  QString dataUrl() const { return mDataUrl; }
221 
228  void setDataUrlFormat( const QString &dataUrlFormat ) { mDataUrlFormat = dataUrlFormat; }
229 
236  QString dataUrlFormat() const { return mDataUrlFormat; }
237 
238  /* Layer attribution information */
239 
246  void setAttribution( const QString &attrib ) { mAttribution = attrib; }
247 
254  QString attribution() const { return mAttribution; }
255 
262  void setAttributionUrl( const QString &attribUrl ) { mAttributionUrl = attribUrl; }
263 
270  QString attributionUrl() const { return mAttributionUrl; }
271 
272  /* Layer metadataUrl information */
273 
280  void setMetadataUrl( const QString &metaUrl ) { mMetadataUrl = metaUrl; }
281 
288  QString metadataUrl() const { return mMetadataUrl; }
289 
296  void setMetadataUrlType( const QString &metaUrlType ) { mMetadataUrlType = metaUrlType; }
297 
304  QString metadataUrlType() const { return mMetadataUrlType; }
305 
312  void setMetadataUrlFormat( const QString &metaUrlFormat ) { mMetadataUrlFormat = metaUrlFormat; }
313 
320  QString metadataUrlFormat() const { return mMetadataUrlFormat; }
321 
326  void setBlendMode( QPainter::CompositionMode blendMode );
327 
331  QPainter::CompositionMode blendMode() const;
332 
334  bool readOnly() const { return isReadOnly(); }
335 
338  virtual void reload() {}
339 
343  virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) = 0 SIP_FACTORY;
344 
346  virtual QgsRectangle extent() const;
347 
352  bool isValid() const;
353 
359  QString publicSource() const;
360 
365  QString source() const;
366 
371  virtual QStringList subLayers() const;
372 
377  virtual void setLayerOrder( const QStringList &layers );
378 
383  virtual void setSubLayerVisibility( const QString &name, bool visible );
384 
386  virtual bool isEditable() const;
387 
391  virtual bool isSpatial() const { return true; }
392 
408  bool readLayerXml( const QDomElement &layerElement, const QgsPathResolver &pathResolver );
409 
426  bool writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsPathResolver &pathResolver ) const;
427 
432  void setCustomProperty( const QString &key, const QVariant &value );
433 
437  QVariant customProperty( const QString &value, const QVariant &defaultValue = QVariant() ) const;
438 
442  void removeCustomProperty( const QString &key );
443 
448  virtual QgsError error() const { return mError; }
449 
453  QgsCoordinateReferenceSystem crs() const;
454 
456  void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );
457 
459  static QString capitalizeLayerName( const QString &name );
460 
467  virtual QString styleURI() const;
468 
477  virtual QString loadDefaultStyle( bool &resultFlag SIP_OUT );
478 
492  virtual QString loadNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
493 
500  virtual bool loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml SIP_OUT );
501 
510  virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT );
511 
518  virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg ) const;
519 
520 
527  virtual void exportSldStyle( QDomDocument &doc, QString &errorMsg ) const;
528 
537  virtual QString saveDefaultStyle( bool &resultFlag SIP_OUT );
538 
552  virtual QString saveNamedStyle( const QString &uri, bool &resultFlag SIP_OUT );
553 
561  virtual QString saveSldStyle( const QString &uri, bool &resultFlag ) const;
562 
570  virtual QString loadSldStyle( const QString &uri, bool &resultFlag );
571 
572  virtual bool readSld( const QDomNode &node, QString &errorMessage )
573  { Q_UNUSED( node ); errorMessage = QStringLiteral( "Layer type %1 not supported" ).arg( type() ); return false; }
574 
575 
576 
582  virtual bool readSymbology( const QDomNode &node, QString &errorMessage ) = 0;
583 
591  virtual bool readStyle( const QDomNode &node, QString &errorMessage );
592 
599  virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage ) const = 0;
600 
609  virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage ) const;
610 
612  QUndoStack *undoStack();
613 
617  QUndoStack *undoStackStyles();
618 
622  void setLegendUrl( const QString &legendUrl ) { mLegendUrl = legendUrl; }
623 
627  QString legendUrl() const { return mLegendUrl; }
628 
632  void setLegendUrlFormat( const QString &legendUrlFormat ) { mLegendUrlFormat = legendUrlFormat; }
633 
637  QString legendUrlFormat() const { return mLegendUrlFormat; }
638 
644  void setLegend( QgsMapLayerLegend *legend SIP_TRANSFER );
645 
650  QgsMapLayerLegend *legend() const;
651 
656  QgsMapLayerStyleManager *styleManager() const;
657 
666  bool isInScaleRange( double scale ) const;
667 
676  double minimumScale() const;
677 
686  double maximumScale() const;
687 
695  bool hasScaleBasedVisibility() const;
696 
703  bool hasAutoRefreshEnabled() const;
704 
712  int autoRefreshInterval() const;
713 
725  void setAutoRefreshInterval( int interval );
726 
733  void setAutoRefreshEnabled( bool enabled );
734 
739  virtual QString htmlMetadata() const;
740 
742  virtual QDateTime timestamp() const { return QDateTime() ; }
743 
751  virtual QSet<QgsMapLayerDependency> dependencies() const;
752 
753  public slots:
754 
762  void setMinimumScale( double scale );
763 
771  void setMaximumScale( double scale );
772 
779  void setScaleBasedVisibility( const bool enabled );
780 
789  void triggerRepaint( bool deferredUpdate = false );
790 
794  void emitStyleChanged();
795 
804  virtual bool setDependencies( const QSet<QgsMapLayerDependency> &layers );
805 
806  signals:
807 
809  void statusChanged( const QString &status );
810 
816  void nameChanged();
817 
819  void crsChanged();
820 
826  void repaintRequested( bool deferredUpdate = false );
827 
829  void recalculateExtents() const;
830 
832  void dataChanged();
833 
835  void blendModeChanged( QPainter::CompositionMode blendMode );
836 
840  void rendererChanged();
841 
848  void styleChanged();
849 
854  void legendChanged();
855 
860  void configChanged();
861 
865  void dependenciesChanged();
866 
873  void willBeDeleted();
874 
880  void autoRefreshIntervalChanged( int interval );
881 
882  protected:
884  virtual void setExtent( const QgsRectangle &rect );
885 
887  void setValid( bool valid );
888 
892  virtual bool readXml( const QDomNode &layer_node );
893 
897  virtual bool writeXml( QDomNode &layer_node, QDomDocument &document ) const;
898 
899 
903  void readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith = QString() );
904 
906  void writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const;
907 
909  void readStyleManager( const QDomNode &layerNode );
911  void writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const;
912 
913 #ifndef SIP_RUN
914 #if 0
915  void connectNotify( const char *signal ) override;
917 #endif
918 #endif
919 
921  void appendError( const QgsErrorMessage &error ) { mError.append( error );}
923  void setError( const QgsError &error ) { mError = error;}
924 
927 
929  bool mValid;
930 
932  QString mDataSource;
933 
935  QString mLayerName;
936 
939  QString mLayerOrigName;
940 
941  QString mShortName;
942  QString mTitle;
943 
945  QString mAbstract;
946  QString mKeywordList;
947 
949  QString mDataUrl;
950  QString mDataUrlFormat;
951 
953  QString mAttribution;
955 
957  QString mMetadataUrl;
960 
962  QString mLegendUrl;
964 
967 
969  QSet<QgsMapLayerDependency> mDependencies;
970 
972  bool hasDependencyCycle( const QSet<QgsMapLayerDependency> &layers ) const;
973 
974  private:
975 
980  virtual bool isReadOnly() const { return true; }
981 
985 
987  QString mID;
988 
990  QgsMapLayer::LayerType mLayerType;
991 
993  QPainter::CompositionMode mBlendMode;
994 
996  QString mTag;
997 
999  double mMinScale;
1001  double mMaxScale;
1003  bool mScaleBasedVisibility;
1004 
1006  QUndoStack mUndoStack;
1007 
1008  QUndoStack mUndoStackStyles;
1009 
1011  QgsObjectCustomProperties mCustomProperties;
1012 
1014  QgsMapLayerLegend *mLegend = nullptr;
1015 
1017  QgsMapLayerStyleManager *mStyleManager = nullptr;
1018 
1020  QTimer mRefreshTimer;
1021 
1022 };
1023 
1025 
1026 #ifndef SIP_RUN
1027 
1033 typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;
1034 
1040 typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;
1041 #endif
1042 
1043 #endif
QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:270
QString mShortName
Definition: qgsmaplayer.h:941
void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:280
QString dataUrlFormat() const
Returns the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:236
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:53
void setShortName(const QString &shortName)
Sets the short name of the layer used by QGIS Server to identify the layer.
Definition: qgsmaplayer.h:153
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
Definition: qgsmaplayer.h:391
virtual const QgsDataProvider * dataProvider() const
Returns the layer&#39;s data provider in a const-correct manner.
Definition: qgsmaplayer.h:141
QString mAttributionUrl
Definition: qgsmaplayer.h:954
QString mKeywordList
Definition: qgsmaplayer.h:946
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
Definition: qgsmaplayer.h:160
QString mDataUrlFormat
Definition: qgsmaplayer.h:950
QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:220
virtual QgsError error() const
Get current status error.
Definition: qgsmaplayer.h:448
QString mLegendUrlFormat
Definition: qgsmaplayer.h:963
virtual void reload()
Synchronises with changes in the datasource.
Definition: qgsmaplayer.h:338
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
Definition: qgsmaplayer.h:296
Abstract base class for spatial data provider implementations.
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
Definition: qgsmaplayer.h:742
#define SIP_FACTORY
Definition: qgis.h:424
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1033
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:935
QgsError mError
Error.
Definition: qgsmaplayer.h:966
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis.h:469
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:926
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:957
void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
Definition: qgsmaplayer.h:632
QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:320
void setKeywordList(const QString &keywords)
Sets the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:195
#define SIP_OUT
Definition: qgis.h:411
void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:167
QString originalName() const
Returns the original name of the layer.
Definition: qgsmaplayer.h:146
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:90
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
Definition: qgsmaplayer.h:969
QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:202
QString mDataUrl
DataUrl of the layer.
Definition: qgsmaplayer.h:949
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
QString legendUrl() const
Returns the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:627
Reads and writes project states.
Definition: qgsproject.h:75
QgsErrorMessage represents single error message.
Definition: qgserror.h:31
bool mValid
Indicates if the layer is valid and can be drawn.
Definition: qgsmaplayer.h:929
void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:228
QString mTitle
Definition: qgsmaplayer.h:942
virtual bool readSld(const QDomNode &node, QString &errorMessage)
Definition: qgsmaplayer.h:572
QString legendUrlFormat() const
Returns the format for a URL based layer legend.
Definition: qgsmaplayer.h:637
QString mMetadataUrlFormat
Definition: qgsmaplayer.h:959
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer&#39;s legend.
Definition: qgsmaplayer.h:622
void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:312
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:174
#define SIP_TRANSFER
Definition: qgis.h:396
QString mAttribution
Attribution of the layer.
Definition: qgsmaplayer.h:953
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:945
Contains information about the context of a rendering operation.
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:932
#define SIP_SKIP
Definition: qgis.h:439
QgsError is container for error messages (report).
Definition: qgserror.h:82
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1040
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:962
This class represents a coordinate reference system (CRS).
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:262
void appendError(const QgsErrorMessage &error)
Add error message.
Definition: qgsmaplayer.h:921
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:334
QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:288
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request. ...
Definition: qgsmaplayer.h:246
Resolves relative paths into absolute paths and vice versa.
Management of styles for use with one map layer.
#define SIP_END
Definition: qgis.h:474
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:212
QString mLayerOrigName
Original name of the layer.
Definition: qgsmaplayer.h:939
QString mMetadataUrlType
Definition: qgsmaplayer.h:958
virtual QgsDataProvider * dataProvider()
Returns the layer&#39;s data provider.
Definition: qgsmaplayer.h:135
void setError(const QgsError &error)
Set error message.
Definition: qgsmaplayer.h:923
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:181
QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:254
QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
Definition: qgsmaplayer.h:304