QGIS API Documentation  2.99.0-Master (5dc91b4)
qgsproject.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsproject.h
3 
4  Implements persistent project state.
5 
6  -------------------
7  begin : July 23, 2004
8  copyright : (C) 2004 by Mark Coletti
9  email : mcoletti at gmail.com
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #ifndef QGSPROJECT_H
22 #define QGSPROJECT_H
23 
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 #include "qgis.h"
27 #include <memory>
28 #include <QHash>
29 #include <QList>
30 #include <QObject>
31 #include <QPair>
32 #include <QFileInfo>
33 #include <QStringList>
34 
35 #include "qgsunittypes.h"
36 #include "qgssnappingconfig.h"
37 #include "qgsprojectversion.h"
41 #include "qgsprojectproperty.h"
42 #include "qgsmaplayer.h"
43 #include "qgsmaplayerstore.h"
44 #include "qgsarchive.h"
45 
46 class QFileInfo;
47 class QDomDocument;
48 class QDomElement;
49 class QDomNode;
50 
51 class QgsLayerTreeGroup;
53 class QgsMapLayer;
55 class QgsPathResolver;
57 class QgsRelationManager;
58 class QgsTolerance;
60 class QgsVectorLayer;
62 class QgsLayoutManager;
63 class QgsLayerTree;
66 
82 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
83 {
84  Q_OBJECT
85  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
86  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
87  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
88  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
89  Q_PROPERTY( QString ellipsoid READ ellipsoid WRITE setEllipsoid NOTIFY ellipsoidChanged )
90  Q_PROPERTY( QgsMapThemeCollection *mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
91  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
92  Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
93  Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
94 
95  public:
97  static QgsProject *instance();
98 
104  explicit QgsProject( QObject *parent SIP_TRANSFERTHIS = nullptr );
105 
106  ~QgsProject() override;
107 
114  void setTitle( const QString &title );
115 
120  QString title() const;
121 
125  bool isDirty() const;
126 
133  void setFileName( const QString &name );
134 
141  QString fileName() const;
142 
148  QFileInfo fileInfo() const;
149 
156  QgsCoordinateReferenceSystem crs() const;
157 
164  void setCrs( const QgsCoordinateReferenceSystem &crs );
165 
172  QString ellipsoid() const;
173 
180  void setEllipsoid( const QString &ellipsoid );
181 
182 
192  QgsCoordinateTransformContext transformContext() const;
193 
203  void setTransformContext( const QgsCoordinateTransformContext &context );
204 
209  void clear();
210 
216  bool read( const QString &filename );
217 
222  bool read();
223 
233  bool readLayer( const QDomNode &layerNode );
234 
243  bool write( const QString &filename );
244 
250  bool write();
251 
261  bool writeEntry( const QString &scope, const QString &key, bool value ) SIP_PYNAME( writeEntryBool );
262 
272  bool writeEntry( const QString &scope, const QString &key, double value ) SIP_PYNAME( writeEntryDouble );
273 
282  bool writeEntry( const QString &scope, const QString &key, int value );
283 
292  bool writeEntry( const QString &scope, const QString &key, const QString &value );
293 
302  bool writeEntry( const QString &scope, const QString &key, const QStringList &value );
303 
310  QStringList readListEntry( const QString &scope, const QString &key, const QStringList &def = QStringList(), bool *ok = nullptr ) const;
311 
312  QString readEntry( const QString &scope, const QString &key, const QString &def = QString(), bool *ok = nullptr ) const;
313  int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
314  double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
315  bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
316 
317 
319  bool removeEntry( const QString &scope, const QString &key );
320 
321 
327  QStringList entryList( const QString &scope, const QString &key ) const;
328 
334  QStringList subkeyList( const QString &scope, const QString &key ) const;
335 
336 
340  // TODO Now slightly broken since re-factoring. Won't print out top-level key
341  // and redundantly prints sub-keys.
342  void dumpProperties() const;
343 
349  QgsPathResolver pathResolver() const;
350 
356  QString writePath( const QString &filename ) const;
357 
359  QString readPath( const QString &filename ) const;
360 
362  QString error() const;
363 
368  void setBadLayerHandler( QgsProjectBadLayerHandler *handler SIP_TRANSFER );
369 
371  QString layerIsEmbedded( const QString &id ) const;
372 
378  bool createEmbeddedLayer( const QString &layerId, const QString &projectFilePath, QList<QDomNode> &brokenNodes,
379  bool saveFlag = true ) SIP_SKIP;
380 
385  QgsLayerTreeGroup *createEmbeddedGroup( const QString &groupName, const QString &projectFilePath, const QStringList &invisibleLayers );
386 
388  void setTopologicalEditing( bool enabled );
389 
391  bool topologicalEditing() const;
392 
399  QgsUnitTypes::DistanceUnit distanceUnits() const;
400 
407  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
408 
414  QgsUnitTypes::AreaUnit areaUnits() const;
415 
422  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
423 
427  QString homePath() const;
428 
429  QgsRelationManager *relationManager() const;
430 
437  const QgsLayoutManager *layoutManager() const SIP_SKIP;
438 
444  QgsLayoutManager *layoutManager();
445 
450  QgsLayerTree *layerTreeRoot() const;
451 
456  QgsLayerTreeRegistryBridge *layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
457 
463  QgsMapThemeCollection *mapThemeCollection();
464 
469  QgsAnnotationManager *annotationManager();
470 
475  const QgsAnnotationManager *annotationManager() const SIP_SKIP;
476 
480  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
481 
485  void setNonIdentifiableLayers( const QStringList &layerIds );
486 
490  QStringList nonIdentifiableLayers() const;
491 
499  bool autoTransaction() const;
500 
510  void setAutoTransaction( bool autoTransaction );
511 
520  QMap< QPair< QString, QString>, QgsTransactionGroup *> transactionGroups() SIP_SKIP;
521 
527  bool evaluateDefaultValues() const;
528 
529 
535  void setEvaluateDefaultValues( bool evaluateDefaultValues );
536 
538 
544  QgsSnappingConfig snappingConfig() const;
545 
551  QList<QgsVectorLayer *> avoidIntersectionsLayers() const;
552 
558  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer *> &layers );
559 
565  QVariantMap customVariables() const;
566 
571  void setCustomVariables( const QVariantMap &customVariables );
572 
577  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
578 
583  const QgsLabelingEngineSettings &labelingEngineSettings() const;
584 
585  //
586  // Functionality from QgsMapLayerRegistry
587  //
588 
593  QgsMapLayerStore *layerStore();
594 
599  SIP_SKIP const QgsMapLayerStore *layerStore() const;
600 
602  int count() const;
603 
611  QgsMapLayer *mapLayer( const QString &layerId ) const;
612 
620  QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
621 
628  QMap<QString, QgsMapLayer *> mapLayers() const;
629 
633  bool isZipped() const;
634 
635 #ifndef SIP_RUN
636 
648  template <typename T>
649  QVector<T> layers() const
650  {
651  return mLayerStore->layers<T>();
652  }
653 #endif
654 
680  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &mapLayers SIP_TRANSFER,
681  bool addToLegend = true,
682  bool takeOwnership SIP_PYARGREMOVE = true );
683 
711  QgsMapLayer *addMapLayer( QgsMapLayer *mapLayer SIP_TRANSFER,
712  bool addToLegend = true,
713  bool takeOwnership SIP_PYARGREMOVE = true );
714 
729  void removeMapLayers( const QStringList &layerIds );
730 
744  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
745  void removeMapLayers( const QList<QgsMapLayer *> &layers );
746 
760  void removeMapLayer( const QString &layerId );
761 
775  void removeMapLayer( QgsMapLayer *layer );
776 
784  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
785 
796  void removeAllMapLayers();
797 
803  void reloadAllLayers();
804 
809  QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;
810 
822  void setTrustLayerMetadata( bool trust );
823 
834  bool trustLayerMetadata() const { return mTrustLayerMetadata; }
835 
841  const QgsAuxiliaryStorage *auxiliaryStorage() const SIP_SKIP;
842 
848  QgsAuxiliaryStorage *auxiliaryStorage();
849 
850  signals:
852  void readProject( const QDomDocument & );
853 
855  void writeProject( QDomDocument & );
856 
865  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
866 
875  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
876 
878  void projectSaved();
879 
881  void oldProjectVersionWarning( const QString & );
882 
888  void layerLoaded( int i, int n );
889 
890  void loadingLayer( const QString & );
891 
893  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
894 
896  void fileNameChanged();
897 
899  void homePathChanged();
900 
902  void snappingConfigChanged( const QgsSnappingConfig &config );
903 
908  void customVariablesChanged();
909 
915  void crsChanged();
916 
924  void ellipsoidChanged( const QString &ellipsoid );
925 
926 
933  void transformContextChanged();
934 
939  void missingDatumTransforms( const QStringList &missingTransforms );
940 
947  void transactionGroupsChanged();
948 
954  void topologicalEditingChanged();
955 
961  void avoidIntersectionsLayersChanged();
962 
974  void mapThemeCollectionChanged();
975 
980  void labelingEngineSettingsChanged();
981 
982  //
983  // signals from QgsMapLayerRegistry
984  //
985 
993  void layersWillBeRemoved( const QStringList &layerIds );
994 
1002  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
1003 
1013  void layerWillBeRemoved( const QString &layerId );
1014 
1024  void layerWillBeRemoved( QgsMapLayer *layer );
1025 
1032  void layersRemoved( const QStringList &layerIds );
1033 
1042  void layerRemoved( const QString &layerId );
1043 
1050  //TODO QGIS 3.0 - rename to past tense
1051  void removeAll();
1052 
1063  void layersAdded( const QList<QgsMapLayer *> &layers );
1064 
1071  void layerWasAdded( QgsMapLayer *layer );
1072 
1081  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
1082 
1083 
1084 
1085  public slots:
1086 
1092  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
1093 
1101  void setDirty( bool b = true );
1102 
1103  private slots:
1104  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
1105  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
1106  void cleanTransactionGroups( bool force = false );
1107 
1108  private:
1109 
1110  static QgsProject *sProject;
1111 
1119  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
1120 
1125  void setError( const QString &errorMessage ) SIP_SKIP;
1126 
1131  void clearError() SIP_SKIP;
1132 
1137  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsReadWriteContext &context ) SIP_SKIP;
1138 
1140  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
1141 
1143  void loadEmbeddedNodes( QgsLayerTreeGroup *group ) SIP_SKIP;
1144 
1146  bool readProjectFile( const QString &filename );
1147 
1149  bool writeProjectFile( const QString &filename );
1150 
1152  bool unzip( const QString &filename );
1153 
1155  bool zip( const QString &filename );
1156 
1158  bool saveAuxiliaryStorage( const QString &filename = QString() );
1159 
1160  std::unique_ptr< QgsMapLayerStore > mLayerStore;
1161 
1162  QString mErrorMessage;
1163 
1164  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1165 
1171  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1172 
1173  QgsSnappingConfig mSnappingConfig;
1174 
1175  QgsRelationManager *mRelationManager = nullptr;
1176 
1177  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1178  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1179 
1180  QgsLayerTree *mRootGroup = nullptr;
1181 
1182  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1183 
1185  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1186 
1187  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1188 
1189  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1190 
1191  QVariantMap mCustomVariables;
1192 
1193  std::unique_ptr<QgsProjectArchive> mArchive;
1194 
1195  std::unique_ptr<QgsAuxiliaryStorage> mAuxiliaryStorage;
1196 
1197  QFile mFile; // current physical project file
1198  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1199  QString mTitle; // project title
1200  bool mAutoTransaction = false; // transaction grouped editing
1201  bool mEvaluateDefaultValues = false; // evaluate default values immediately
1203  bool mDirty = false; // project has been modified since it has been read or saved
1204  bool mTrustLayerMetadata = false;
1205 
1206  QgsCoordinateTransformContext mTransformContext;
1207 };
1208 
1214 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc ) SIP_SKIP;
1215 
1216 #endif
Layer tree group node serves as a container for layers and further groups.
The class is used as a container of context for various read/write operations on other objects...
Base class for all map layer types.
Definition: qgsmaplayer.h:56
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Manages storage of a set of QgsAnnotation annotation objects.
Helper functions for various unit types.
Definition: qgsunittypes.h:37
Class providing some utility methods to manage auxiliary storage.
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
bool trustLayerMetadata() const
Returns true if the trust option is activated, false otherwise.
Definition: qgsproject.h:834
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
Definition: qgsziputils.cpp:33
#define SIP_SKIP
Definition: qgis_sip.h:119
CORE_EXPORT QgsProjectVersion getVersion(QDomDocument const &doc)
Return the version string found in the given DOM document.
Definition: qgsproject.cpp:634
A class to describe the version of a project.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This is the class is providing tolerance value in map unit values.
Definition: qgstolerance.h:30
Listens to the updates in map layer registry and does changes in layer tree.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Reads and writes project states.
Definition: qgsproject.h:82
Contains information about the context in which a coordinate transform is executed.
Manages storage of a set of layouts.
Abstract interface for generating an expression context.
Project property key node.
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:139
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
Definition: qgsproject.h:649
This class manages a set of relations between layers.
Stores global configuration for labeling engine.
This class represents a coordinate reference system (CRS).
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Container class that allows storage of map themes consisting of visible map layers and layer styles...
This is a container for configuration of the snapping of the project.
Resolves relative paths into absolute paths and vice versa.
Represents a vector layer which manages a vector based data sets.
CORE_EXPORT bool zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.
Interface for classes that handle missing layer files when reading project file.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74