QGIS API Documentation  3.2.0-Bonn (bc43194)
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 #include "qgsreadwritecontext.h"
46 #include "qgsprojectmetadata.h"
47 
48 class QFileInfo;
49 class QDomDocument;
50 class QDomElement;
51 class QDomNode;
52 
53 class QgsLayerTreeGroup;
55 class QgsMapLayer;
57 class QgsPathResolver;
59 class QgsProjectStorage;
60 class QgsRelationManager;
61 class QgsTolerance;
63 class QgsVectorLayer;
65 class QgsLayoutManager;
66 class QgsLayerTree;
69 
85 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
86 {
87  Q_OBJECT
88  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
89  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
90  Q_PROPERTY( QString homePath READ homePath WRITE setPresetHomePath NOTIFY homePathChanged )
91  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
92  Q_PROPERTY( QString ellipsoid READ ellipsoid WRITE setEllipsoid NOTIFY ellipsoidChanged )
93  Q_PROPERTY( QgsMapThemeCollection *mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
94  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
95  Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
96  Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
97  Q_PROPERTY( QgsProjectMetadata metadata READ metadata WRITE setMetadata NOTIFY metadataChanged )
98 
99  public:
101  static QgsProject *instance();
102 
108  explicit QgsProject( QObject *parent SIP_TRANSFERTHIS = nullptr );
109 
110  ~QgsProject() override;
111 
121  void setTitle( const QString &title );
122 
129  QString title() const;
130 
134  bool isDirty() const;
135 
142  void setFileName( const QString &name );
143 
150  QString fileName() const;
151 
163  Q_DECL_DEPRECATED QFileInfo fileInfo() const SIP_DEPRECATED;
164 
171  QgsProjectStorage *projectStorage() const;
172 
177  QDateTime lastModified() const;
178 
184  QString absoluteFilePath() const;
185 
190  QString baseName() const;
191 
198  QgsCoordinateReferenceSystem crs() const;
199 
206  void setCrs( const QgsCoordinateReferenceSystem &crs );
207 
214  QString ellipsoid() const;
215 
222  void setEllipsoid( const QString &ellipsoid );
223 
224 
234  QgsCoordinateTransformContext transformContext() const;
235 
245  void setTransformContext( const QgsCoordinateTransformContext &context );
246 
252  void clear();
253 
259  bool read( const QString &filename );
260 
265  bool read();
266 
276  bool readLayer( const QDomNode &layerNode );
277 
286  bool write( const QString &filename );
287 
293  bool write();
294 
304  bool writeEntry( const QString &scope, const QString &key, bool value ) SIP_PYNAME( writeEntryBool );
305 
315  bool writeEntry( const QString &scope, const QString &key, double value ) SIP_PYNAME( writeEntryDouble );
316 
325  bool writeEntry( const QString &scope, const QString &key, int value );
326 
335  bool writeEntry( const QString &scope, const QString &key, const QString &value );
336 
345  bool writeEntry( const QString &scope, const QString &key, const QStringList &value );
346 
353  QStringList readListEntry( const QString &scope, const QString &key, const QStringList &def = QStringList(), bool *ok = nullptr ) const;
354 
355  QString readEntry( const QString &scope, const QString &key, const QString &def = QString(), bool *ok = nullptr ) const;
356  int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
357  double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
358  bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
359 
360 
362  bool removeEntry( const QString &scope, const QString &key );
363 
364 
370  QStringList entryList( const QString &scope, const QString &key ) const;
371 
377  QStringList subkeyList( const QString &scope, const QString &key ) const;
378 
379 
383  // TODO Now slightly broken since re-factoring. Won't print out top-level key
384  // and redundantly prints sub-keys.
385  void dumpProperties() const;
386 
392  QgsPathResolver pathResolver() const;
393 
399  QString writePath( const QString &filename ) const;
400 
402  QString readPath( const QString &filename ) const;
403 
405  QString error() const;
406 
411  void setBadLayerHandler( QgsProjectBadLayerHandler *handler SIP_TRANSFER );
412 
414  QString layerIsEmbedded( const QString &id ) const;
415 
421  bool createEmbeddedLayer( const QString &layerId, const QString &projectFilePath, QList<QDomNode> &brokenNodes,
422  bool saveFlag = true ) SIP_SKIP;
423 
428  QgsLayerTreeGroup *createEmbeddedGroup( const QString &groupName, const QString &projectFilePath, const QStringList &invisibleLayers );
429 
431  void setTopologicalEditing( bool enabled );
432 
434  bool topologicalEditing() const;
435 
442  QgsUnitTypes::DistanceUnit distanceUnits() const;
443 
450  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
451 
457  QgsUnitTypes::AreaUnit areaUnits() const;
458 
465  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
466 
479  QString homePath() const;
480 
493  QString presetHomePath() const;
494 
495  QgsRelationManager *relationManager() const;
496 
503  const QgsLayoutManager *layoutManager() const SIP_SKIP;
504 
510  QgsLayoutManager *layoutManager();
511 
516  QgsLayerTree *layerTreeRoot() const;
517 
522  QgsLayerTreeRegistryBridge *layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
523 
529  QgsMapThemeCollection *mapThemeCollection();
530 
535  QgsAnnotationManager *annotationManager();
536 
541  const QgsAnnotationManager *annotationManager() const SIP_SKIP;
542 
546  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
547 
551  void setNonIdentifiableLayers( const QStringList &layerIds );
552 
556  QStringList nonIdentifiableLayers() const;
557 
565  bool autoTransaction() const;
566 
576  void setAutoTransaction( bool autoTransaction );
577 
586  QMap< QPair< QString, QString>, QgsTransactionGroup *> transactionGroups() SIP_SKIP;
587 
595  QgsTransactionGroup *transactionGroup( const QString &providerKey, const QString &connString );
596 
602  bool evaluateDefaultValues() const;
603 
609  void setEvaluateDefaultValues( bool evaluateDefaultValues );
610 
612 
618  QgsSnappingConfig snappingConfig() const;
619 
625  QList<QgsVectorLayer *> avoidIntersectionsLayers() const;
626 
632  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer *> &layers );
633 
639  QVariantMap customVariables() const;
640 
645  void setCustomVariables( const QVariantMap &customVariables );
646 
651  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
652 
657  const QgsLabelingEngineSettings &labelingEngineSettings() const;
658 
659  //
660  // Functionality from QgsMapLayerRegistry
661  //
662 
667  QgsMapLayerStore *layerStore();
668 
673  SIP_SKIP const QgsMapLayerStore *layerStore() const;
674 
676  int count() const;
677 
685  QgsMapLayer *mapLayer( const QString &layerId ) const;
686 
694  QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
695 
702  QMap<QString, QgsMapLayer *> mapLayers() const;
703 
707  bool isZipped() const;
708 
709 #ifndef SIP_RUN
710 
722  template <typename T>
723  QVector<T> layers() const
724  {
725  return mLayerStore->layers<T>();
726  }
727 #endif
728 
754  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &mapLayers SIP_TRANSFER,
755  bool addToLegend = true,
756  bool takeOwnership SIP_PYARGREMOVE = true );
757 
785  QgsMapLayer *addMapLayer( QgsMapLayer *mapLayer SIP_TRANSFER,
786  bool addToLegend = true,
787  bool takeOwnership SIP_PYARGREMOVE = true );
788 
803  void removeMapLayers( const QStringList &layerIds );
804 
818  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
819  void removeMapLayers( const QList<QgsMapLayer *> &layers );
820 
834  void removeMapLayer( const QString &layerId );
835 
849  void removeMapLayer( QgsMapLayer *layer );
850 
858  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
859 
870  void removeAllMapLayers();
871 
877  void reloadAllLayers();
878 
883  QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;
884 
896  void setTrustLayerMetadata( bool trust );
897 
908  bool trustLayerMetadata() const { return mTrustLayerMetadata; }
909 
915  const QgsAuxiliaryStorage *auxiliaryStorage() const SIP_SKIP;
916 
922  QgsAuxiliaryStorage *auxiliaryStorage();
923 
930  const QgsProjectMetadata &metadata() const;
931 
938  void setMetadata( const QgsProjectMetadata &metadata );
939 
947  QSet<QgsMapLayer *> requiredLayers() const;
948 
956  void setRequiredLayers( const QSet<QgsMapLayer *> &layers );
957 
958  signals:
959 
967  void cleared();
968 
972  void readProject( const QDomDocument & );
973 
977  void writeProject( QDomDocument & );
978 
987  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
988 
997  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
998 
1002  void projectSaved();
1003 
1007  void oldProjectVersionWarning( const QString & );
1008 
1014  void layerLoaded( int i, int n );
1015 
1017  void loadingLayer( const QString &layerName );
1018 
1025  void loadingLayerMessageReceived( const QString &layerName, const QList<QgsReadWriteContext::ReadWriteMessage> &messages );
1026 
1028  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
1029 
1031  void fileNameChanged();
1032 
1039  void homePathChanged();
1040 
1044  void snappingConfigChanged( const QgsSnappingConfig &config );
1045 
1050  void customVariablesChanged();
1051 
1057  void crsChanged();
1058 
1066  void ellipsoidChanged( const QString &ellipsoid );
1067 
1068 
1075  void transformContextChanged();
1076 
1081  void missingDatumTransforms( const QStringList &missingTransforms );
1082 
1089  void transactionGroupsChanged();
1090 
1096  void topologicalEditingChanged();
1097 
1103  void avoidIntersectionsLayersChanged();
1104 
1116  void mapThemeCollectionChanged();
1117 
1122  void labelingEngineSettingsChanged();
1123 
1130  void metadataChanged();
1131 
1132  //
1133  // signals from QgsMapLayerRegistry
1134  //
1135 
1143  void layersWillBeRemoved( const QStringList &layerIds );
1144 
1152  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
1153 
1163  void layerWillBeRemoved( const QString &layerId );
1164 
1174  void layerWillBeRemoved( QgsMapLayer *layer );
1175 
1182  void layersRemoved( const QStringList &layerIds );
1183 
1192  void layerRemoved( const QString &layerId );
1193 
1200  //TODO QGIS 3.0 - rename to past tense
1201  void removeAll();
1202 
1213  void layersAdded( const QList<QgsMapLayer *> &layers );
1214 
1221  void layerWasAdded( QgsMapLayer *layer );
1222 
1231  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
1232 
1240  void isDirtyChanged( bool dirty );
1241 
1242  public slots:
1243 
1249  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
1250 
1251  // TODO QGIS 4.0 - rename b to dirty
1252 
1260  void setDirty( bool b = true );
1261 
1270  void setPresetHomePath( const QString &path );
1271 
1272  private slots:
1273  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
1274  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
1275  void cleanTransactionGroups( bool force = false );
1276 
1277  private:
1278 
1279  static QgsProject *sProject;
1280 
1289  static void setInstance( QgsProject *project ) SIP_SKIP;
1290 
1298  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
1299 
1304  void setError( const QString &errorMessage ) SIP_SKIP;
1305 
1310  void clearError() SIP_SKIP;
1311 
1316  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, QgsReadWriteContext &context ) SIP_SKIP;
1317 
1319  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
1320 
1322  void loadEmbeddedNodes( QgsLayerTreeGroup *group ) SIP_SKIP;
1323 
1325  bool readProjectFile( const QString &filename );
1326 
1328  bool writeProjectFile( const QString &filename );
1329 
1331  bool unzip( const QString &filename );
1332 
1334  bool zip( const QString &filename );
1335 
1337  bool saveAuxiliaryStorage( const QString &filename = QString() );
1338 
1339  std::unique_ptr< QgsMapLayerStore > mLayerStore;
1340 
1341  QString mErrorMessage;
1342 
1343  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1344 
1350  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1351 
1352  QgsSnappingConfig mSnappingConfig;
1353 
1354  QgsRelationManager *mRelationManager = nullptr;
1355 
1356  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1357  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1358 
1359  QgsLayerTree *mRootGroup = nullptr;
1360 
1361  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1362 
1364  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1365 
1366  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1367 
1368  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1369 
1370  QVariantMap mCustomVariables;
1371 
1372  std::unique_ptr<QgsProjectArchive> mArchive;
1373 
1374  std::unique_ptr<QgsAuxiliaryStorage> mAuxiliaryStorage;
1375 
1376  QFile mFile; // current physical project file
1377 
1382  QString mHomePath;
1383  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1384  bool mAutoTransaction = false; // transaction grouped editing
1385  bool mEvaluateDefaultValues = false; // evaluate default values immediately
1387  bool mDirty = false; // project has been modified since it has been read or saved
1388  int mDirtyBlockCount = 0;
1389  bool mTrustLayerMetadata = false;
1390 
1391  QgsCoordinateTransformContext mTransformContext;
1392 
1393  QgsProjectMetadata mMetadata;
1394 
1396 
1397  // Required by QGIS Server for switching the current project instance
1398  friend class QgsConfigCache;
1399 };
1400 
1422 class CORE_EXPORT QgsProjectDirtyBlocker
1423 {
1424  public:
1425 
1432  : mProject( project )
1433  {
1434  mProject->mDirtyBlockCount++;
1435  }
1436 
1438  QgsProjectDirtyBlocker( const QgsProjectDirtyBlocker &other ) = delete;
1439 
1441  QgsProjectDirtyBlocker &operator=( const QgsProjectDirtyBlocker &other ) = delete;
1442 
1444  {
1445  mProject->mDirtyBlockCount--;
1446  }
1447 
1448  private:
1449  QgsProject *mProject = nullptr;
1450 
1451 #ifdef SIP_RUN
1453 #endif
1454 };
1455 
1461 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc ) SIP_SKIP;
1462 
1463 #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:61
#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:908
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)
Returns the version string found in the given DOM document.
Definition: qgsproject.cpp:712
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:32
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:85
Contains information about the context in which a coordinate transform is executed.
Manages storage of a set of layouts.
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
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:723
This class manages a set of relations between layers.
Stores global configuration for labeling engine.
This class represents a coordinate reference system (CRS).
Abstract interface for project storage - to be implemented by various backends and registered in QgsP...
QgsProjectDirtyBlocker(QgsProject *project)
Constructor for QgsProjectDirtyBlocker.
Definition: qgsproject.h:1431
Temporarily blocks QgsProject "dirtying" for the lifetime of the object.
Definition: qgsproject.h:1422
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
A structured metadata store for a map layer.