QGIS API Documentation  2.99.0-Master (9caa722)
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"
40 #include "qgsprojectproperty.h"
41 #include "qgsmaplayer.h"
42 #include "qgsmaplayerstore.h"
43 #include "qgsarchive.h"
44 
45 class QFileInfo;
46 class QDomDocument;
47 class QDomElement;
48 class QDomNode;
49 
50 class QgsLayerTreeGroup;
52 class QgsMapLayer;
54 class QgsPathResolver;
56 class QgsRelationManager;
57 class QgsTolerance;
59 class QgsVectorLayer;
61 class QgsLayoutManager;
62 class QgsLayerTree;
64 
79 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
80 {
81  Q_OBJECT
82  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
83  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
84  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
85  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
86  Q_PROPERTY( QString ellipsoid READ ellipsoid WRITE setEllipsoid NOTIFY ellipsoidChanged )
87  Q_PROPERTY( QgsMapThemeCollection *mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
88  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
89  Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
90  Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
91 
92  public:
94  static QgsProject *instance();
95 
101  explicit QgsProject( QObject *parent SIP_TRANSFERTHIS = 0 );
102 
103  ~QgsProject();
104 
110  void setTitle( const QString &title );
111 
115  QString title() const;
116 
120  bool isDirty() const;
121 
127  void setFileName( const QString &name );
128 
134  QString fileName() const;
135 
140  QFileInfo fileInfo() const;
141 
148  QgsCoordinateReferenceSystem crs() const;
149 
156  void setCrs( const QgsCoordinateReferenceSystem &crs );
157 
164  QString ellipsoid() const;
165 
172  void setEllipsoid( const QString &ellipsoid );
173 
177  void clear();
178 
183  bool read( const QString &filename );
184 
188  bool read();
189 
198  bool readLayer( const QDomNode &layerNode );
199 
209  bool write( const QString &filename );
210 
215  bool write();
216 
226  bool writeEntry( const QString &scope, const QString &key, bool value ) SIP_PYNAME( writeEntryBool );
227 
237  bool writeEntry( const QString &scope, const QString &key, double value ) SIP_PYNAME( writeEntryDouble );
238 
247  bool writeEntry( const QString &scope, const QString &key, int value );
248 
257  bool writeEntry( const QString &scope, const QString &key, const QString &value );
258 
267  bool writeEntry( const QString &scope, const QString &key, const QStringList &value );
268 
275  QStringList readListEntry( const QString &scope, const QString &key, const QStringList &def = QStringList(), bool *ok = nullptr ) const;
276 
277  QString readEntry( const QString &scope, const QString &key, const QString &def = QString(), bool *ok = nullptr ) const;
278  int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
279  double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
280  bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
281 
282 
284  bool removeEntry( const QString &scope, const QString &key );
285 
286 
291  QStringList entryList( const QString &scope, const QString &key ) const;
292 
297  QStringList subkeyList( const QString &scope, const QString &key ) const;
298 
299 
302  // TODO Now slightly broken since re-factoring. Won't print out top-level key
303  // and redundantly prints sub-keys.
304  void dumpProperties() const;
305 
310  QgsPathResolver pathResolver() const;
311 
317  QString writePath( const QString &filename ) const;
318 
320  QString readPath( const QString &filename ) const;
321 
323  QString error() const;
324 
328  void setBadLayerHandler( QgsProjectBadLayerHandler *handler SIP_TRANSFER );
329 
331  QString layerIsEmbedded( const QString &id ) const;
332 
337  bool createEmbeddedLayer( const QString &layerId, const QString &projectFilePath, QList<QDomNode> &brokenNodes,
338  bool saveFlag = true ) SIP_SKIP;
339 
343  QgsLayerTreeGroup *createEmbeddedGroup( const QString &groupName, const QString &projectFilePath, const QStringList &invisibleLayers );
344 
346  void setTopologicalEditing( bool enabled );
347 
349  bool topologicalEditing() const;
350 
356  QgsUnitTypes::DistanceUnit distanceUnits() const;
357 
364  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
365 
370  QgsUnitTypes::AreaUnit areaUnits() const;
371 
378  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
379 
382  QString homePath() const;
383 
384  QgsRelationManager *relationManager() const;
385 
392  const QgsLayoutManager *layoutManager() const SIP_SKIP;
393 
399  QgsLayoutManager *layoutManager();
400 
404  QgsLayerTree *layerTreeRoot() const;
405 
409  QgsLayerTreeRegistryBridge *layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
410 
415  QgsMapThemeCollection *mapThemeCollection();
416 
421  QgsAnnotationManager *annotationManager();
422 
427  const QgsAnnotationManager *annotationManager() const SIP_SKIP;
428 
432  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
433 
437  void setNonIdentifiableLayers( const QStringList &layerIds );
438 
442  QStringList nonIdentifiableLayers() const;
443 
451  bool autoTransaction() const;
452 
462  void setAutoTransaction( bool autoTransaction );
463 
472  QMap< QPair< QString, QString>, QgsTransactionGroup *> transactionGroups() SIP_SKIP;
473 
479  bool evaluateDefaultValues() const;
480 
481 
487  void setEvaluateDefaultValues( bool evaluateDefaultValues );
488 
490 
496  QgsSnappingConfig snappingConfig() const;
497 
503  QList<QgsVectorLayer *> avoidIntersectionsLayers() const;
504 
510  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer *> &layers );
511 
517  QVariantMap customVariables() const;
518 
523  void setCustomVariables( const QVariantMap &customVariables );
524 
529  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
530 
535  const QgsLabelingEngineSettings &labelingEngineSettings() const;
536 
537  //
538  // Functionality from QgsMapLayerRegistry
539  //
540 
545  QgsMapLayerStore *layerStore();
546 
551  SIP_SKIP const QgsMapLayerStore *layerStore() const;
552 
554  int count() const;
555 
562  QgsMapLayer *mapLayer( const QString &layerId ) const;
563 
570  QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
571 
577  QMap<QString, QgsMapLayer *> mapLayers() const;
578 
582  bool isZipped() const;
583 
584 #ifndef SIP_RUN
585 
596  template <typename T>
597  QVector<T> layers() const
598  {
599  return mLayerStore->layers<T>();
600  }
601 #endif
602 
628  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &mapLayers SIP_TRANSFER,
629  bool addToLegend = true,
630  bool takeOwnership SIP_PYARGREMOVE = true );
631 
659  QgsMapLayer *addMapLayer( QgsMapLayer *mapLayer SIP_TRANSFER,
660  bool addToLegend = true,
661  bool takeOwnership SIP_PYARGREMOVE = true );
662 
677  void removeMapLayers( const QStringList &layerIds );
678 
692  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
693  void removeMapLayers( const QList<QgsMapLayer *> &layers );
694 
708  void removeMapLayer( const QString &layerId );
709 
723  void removeMapLayer( QgsMapLayer *layer );
724 
732  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
733 
744  void removeAllMapLayers();
745 
751  void reloadAllLayers();
752 
756  QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;
757 
769  void setTrustLayerMetadata( bool trust );
770 
781  bool trustLayerMetadata() const { return mTrustLayerMetadata; }
782 
783  signals:
785  void readProject( const QDomDocument & );
786 
788  void writeProject( QDomDocument & );
789 
798  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
799 
808  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
809 
811  void projectSaved();
812 
814  void oldProjectVersionWarning( const QString & );
815 
821  void layerLoaded( int i, int n );
822 
823  void loadingLayer( const QString & );
824 
826  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
827 
829  void fileNameChanged();
830 
832  void homePathChanged();
833 
835  void snappingConfigChanged( const QgsSnappingConfig &config );
836 
840  void customVariablesChanged();
841 
847  void crsChanged();
848 
856  void ellipsoidChanged( const QString &ellipsoid );
857 
864  void transactionGroupsChanged();
865 
871  void topologicalEditingChanged();
872 
878  void avoidIntersectionsLayersChanged();
879 
891  void mapThemeCollectionChanged();
892 
897  void labelingEngineSettingsChanged();
898 
899  //
900  // signals from QgsMapLayerRegistry
901  //
902 
910  void layersWillBeRemoved( const QStringList &layerIds );
911 
919  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
920 
930  void layerWillBeRemoved( const QString &layerId );
931 
941  void layerWillBeRemoved( QgsMapLayer *layer );
942 
949  void layersRemoved( const QStringList &layerIds );
950 
959  void layerRemoved( const QString &layerId );
960 
967  //TODO QGIS 3.0 - rename to past tense
968  void removeAll();
969 
980  void layersAdded( const QList<QgsMapLayer *> &layers );
981 
988  void layerWasAdded( QgsMapLayer *layer );
989 
998  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
999 
1000 
1001 
1002  public slots:
1003 
1009  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
1010 
1018  void setDirty( bool b = true );
1019 
1020  private slots:
1021  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
1022  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
1023  void cleanTransactionGroups( bool force = false );
1024 
1025  private:
1026 
1027  static QgsProject *sProject;
1028 
1035  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
1036 
1040  void setError( const QString &errorMessage ) SIP_SKIP;
1041 
1045  void clearError() SIP_SKIP;
1046 
1049  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsReadWriteContext &context ) SIP_SKIP;
1050 
1052  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
1053 
1055  void loadEmbeddedNodes( QgsLayerTreeGroup *group ) SIP_SKIP;
1056 
1058  bool readProjectFile( const QString &filename );
1059 
1061  bool writeProjectFile( const QString &filename );
1062 
1064  bool unzip( const QString &filename );
1065 
1067  bool zip( const QString &filename );
1068 
1069  std::unique_ptr< QgsMapLayerStore > mLayerStore;
1070 
1071  QString mErrorMessage;
1072 
1073  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1074 
1079  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1080 
1081  QgsSnappingConfig mSnappingConfig;
1082 
1083  QgsRelationManager *mRelationManager = nullptr;
1084 
1085  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1086  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1087 
1088  QgsLayerTree *mRootGroup = nullptr;
1089 
1090  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1091 
1093  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1094 
1095  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1096 
1097  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1098 
1099  QVariantMap mCustomVariables;
1100 
1101  std::unique_ptr<QgsProjectArchive> mArchive;
1102 
1103  QFile mFile; // current physical project file
1104  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1105  QString mTitle; // project title
1106  bool mAutoTransaction; // transaction grouped editing
1107  bool mEvaluateDefaultValues; // evaluate default values immediately
1109  bool mDirty; // project has been modified since it has been read or saved
1110  bool mTrustLayerMetadata = false;
1111 };
1112 
1117 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc ) SIP_SKIP;
1118 
1119 #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:54
#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:36
#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:781
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:31
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:619
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:29
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:79
Manages storage of a set of compositions.
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:597
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