QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
758 
759  signals:
761  void readProject( const QDomDocument & );
762 
764  void writeProject( QDomDocument & );
765 
774  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
775 
784  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
785 
787  void projectSaved();
788 
790  void oldProjectVersionWarning( const QString & );
791 
797  void layerLoaded( int i, int n );
798 
799  void loadingLayer( const QString & );
800 
802  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
803 
805  void fileNameChanged();
806 
808  void homePathChanged();
809 
811  void snappingConfigChanged( const QgsSnappingConfig &config );
812 
816  void customVariablesChanged();
817 
823  void crsChanged();
824 
832  void ellipsoidChanged( const QString &ellipsoid );
833 
840  void transactionGroupsChanged();
841 
847  void topologicalEditingChanged();
848 
854  void avoidIntersectionsLayersChanged();
855 
867  void mapThemeCollectionChanged();
868 
873  void labelingEngineSettingsChanged();
874 
875  //
876  // signals from QgsMapLayerRegistry
877  //
878 
886  void layersWillBeRemoved( const QStringList &layerIds );
887 
895  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
896 
906  void layerWillBeRemoved( const QString &layerId );
907 
917  void layerWillBeRemoved( QgsMapLayer *layer );
918 
925  void layersRemoved( const QStringList &layerIds );
926 
935  void layerRemoved( const QString &layerId );
936 
943  //TODO QGIS 3.0 - rename to past tense
944  void removeAll();
945 
956  void layersAdded( const QList<QgsMapLayer *> &layers );
957 
964  void layerWasAdded( QgsMapLayer *layer );
965 
974  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
975 
976 
977 
978  public slots:
979 
985  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
986 
994  void setDirty( bool b = true );
995 
996  private slots:
997  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
998  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
999  void cleanTransactionGroups( bool force = false );
1000 
1001  private:
1002 
1003  static QgsProject *sProject;
1004 
1011  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
1012 
1016  void setError( const QString &errorMessage ) SIP_SKIP;
1017 
1021  void clearError() SIP_SKIP;
1022 
1025  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsReadWriteContext &context ) SIP_SKIP;
1026 
1028  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
1029 
1031  void loadEmbeddedNodes( QgsLayerTreeGroup *group ) SIP_SKIP;
1032 
1034  bool readProjectFile( const QString &filename );
1035 
1037  bool writeProjectFile( const QString &filename );
1038 
1040  bool unzip( const QString &filename );
1041 
1043  bool zip( const QString &filename );
1044 
1045  std::unique_ptr< QgsMapLayerStore > mLayerStore;
1046 
1047  QString mErrorMessage;
1048 
1049  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1050 
1055  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1056 
1057  QgsSnappingConfig mSnappingConfig;
1058 
1059  QgsRelationManager *mRelationManager = nullptr;
1060 
1061  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1062  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1063 
1064  QgsLayerTree *mRootGroup = nullptr;
1065 
1066  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1067 
1069  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1070 
1071  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1072 
1073  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1074 
1075  QVariantMap mCustomVariables;
1076 
1077  std::unique_ptr<QgsProjectArchive> mArchive;
1078 
1079  QFile mFile; // current physical project file
1080  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1081  QString mTitle; // project title
1082  bool mAutoTransaction; // transaction grouped editing
1083  bool mEvaluateDefaultValues; // evaluate default values immediately
1085  bool mDirty; // project has been modified since it has been read or saved
1086 };
1087 
1092 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc ) SIP_SKIP;
1093 
1094 #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...
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:618
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