QGIS API Documentation  2.99.0-Master (cd0ba91)
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;
65 
81 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
82 {
83  Q_OBJECT
84  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
85  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
86  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
87  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
88  Q_PROPERTY( QString ellipsoid READ ellipsoid WRITE setEllipsoid NOTIFY ellipsoidChanged )
89  Q_PROPERTY( QgsMapThemeCollection *mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
90  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
91  Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
92  Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
93 
94  public:
96  static QgsProject *instance();
97 
103  explicit QgsProject( QObject *parent SIP_TRANSFERTHIS = 0 );
104 
105  ~QgsProject();
106 
113  void setTitle( const QString &title );
114 
119  QString title() const;
120 
124  bool isDirty() const;
125 
132  void setFileName( const QString &name );
133 
140  QString fileName() const;
141 
147  QFileInfo fileInfo() const;
148 
155  QgsCoordinateReferenceSystem crs() const;
156 
163  void setCrs( const QgsCoordinateReferenceSystem &crs );
164 
171  QString ellipsoid() const;
172 
179  void setEllipsoid( const QString &ellipsoid );
180 
185  void clear();
186 
192  bool read( const QString &filename );
193 
198  bool read();
199 
209  bool readLayer( const QDomNode &layerNode );
210 
220  bool write( const QString &filename );
221 
227  bool write();
228 
238  bool writeEntry( const QString &scope, const QString &key, bool value ) SIP_PYNAME( writeEntryBool );
239 
249  bool writeEntry( const QString &scope, const QString &key, double value ) SIP_PYNAME( writeEntryDouble );
250 
259  bool writeEntry( const QString &scope, const QString &key, int value );
260 
269  bool writeEntry( const QString &scope, const QString &key, const QString &value );
270 
279  bool writeEntry( const QString &scope, const QString &key, const QStringList &value );
280 
287  QStringList readListEntry( const QString &scope, const QString &key, const QStringList &def = QStringList(), bool *ok = nullptr ) const;
288 
289  QString readEntry( const QString &scope, const QString &key, const QString &def = QString(), bool *ok = nullptr ) const;
290  int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
291  double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
292  bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
293 
294 
296  bool removeEntry( const QString &scope, const QString &key );
297 
298 
304  QStringList entryList( const QString &scope, const QString &key ) const;
305 
311  QStringList subkeyList( const QString &scope, const QString &key ) const;
312 
313 
317  // TODO Now slightly broken since re-factoring. Won't print out top-level key
318  // and redundantly prints sub-keys.
319  void dumpProperties() const;
320 
326  QgsPathResolver pathResolver() const;
327 
333  QString writePath( const QString &filename ) const;
334 
336  QString readPath( const QString &filename ) const;
337 
339  QString error() const;
340 
345  void setBadLayerHandler( QgsProjectBadLayerHandler *handler SIP_TRANSFER );
346 
348  QString layerIsEmbedded( const QString &id ) const;
349 
355  bool createEmbeddedLayer( const QString &layerId, const QString &projectFilePath, QList<QDomNode> &brokenNodes,
356  bool saveFlag = true ) SIP_SKIP;
357 
362  QgsLayerTreeGroup *createEmbeddedGroup( const QString &groupName, const QString &projectFilePath, const QStringList &invisibleLayers );
363 
365  void setTopologicalEditing( bool enabled );
366 
368  bool topologicalEditing() const;
369 
376  QgsUnitTypes::DistanceUnit distanceUnits() const;
377 
384  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
385 
391  QgsUnitTypes::AreaUnit areaUnits() const;
392 
399  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
400 
404  QString homePath() const;
405 
406  QgsRelationManager *relationManager() const;
407 
414  const QgsLayoutManager *layoutManager() const SIP_SKIP;
415 
421  QgsLayoutManager *layoutManager();
422 
427  QgsLayerTree *layerTreeRoot() const;
428 
433  QgsLayerTreeRegistryBridge *layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
434 
440  QgsMapThemeCollection *mapThemeCollection();
441 
446  QgsAnnotationManager *annotationManager();
447 
452  const QgsAnnotationManager *annotationManager() const SIP_SKIP;
453 
457  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
458 
462  void setNonIdentifiableLayers( const QStringList &layerIds );
463 
467  QStringList nonIdentifiableLayers() const;
468 
476  bool autoTransaction() const;
477 
487  void setAutoTransaction( bool autoTransaction );
488 
497  QMap< QPair< QString, QString>, QgsTransactionGroup *> transactionGroups() SIP_SKIP;
498 
504  bool evaluateDefaultValues() const;
505 
506 
512  void setEvaluateDefaultValues( bool evaluateDefaultValues );
513 
515 
521  QgsSnappingConfig snappingConfig() const;
522 
528  QList<QgsVectorLayer *> avoidIntersectionsLayers() const;
529 
535  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer *> &layers );
536 
542  QVariantMap customVariables() const;
543 
548  void setCustomVariables( const QVariantMap &customVariables );
549 
554  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
555 
560  const QgsLabelingEngineSettings &labelingEngineSettings() const;
561 
562  //
563  // Functionality from QgsMapLayerRegistry
564  //
565 
570  QgsMapLayerStore *layerStore();
571 
576  SIP_SKIP const QgsMapLayerStore *layerStore() const;
577 
579  int count() const;
580 
588  QgsMapLayer *mapLayer( const QString &layerId ) const;
589 
597  QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
598 
605  QMap<QString, QgsMapLayer *> mapLayers() const;
606 
610  bool isZipped() const;
611 
612 #ifndef SIP_RUN
613 
625  template <typename T>
626  QVector<T> layers() const
627  {
628  return mLayerStore->layers<T>();
629  }
630 #endif
631 
657  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &mapLayers SIP_TRANSFER,
658  bool addToLegend = true,
659  bool takeOwnership SIP_PYARGREMOVE = true );
660 
688  QgsMapLayer *addMapLayer( QgsMapLayer *mapLayer SIP_TRANSFER,
689  bool addToLegend = true,
690  bool takeOwnership SIP_PYARGREMOVE = true );
691 
706  void removeMapLayers( const QStringList &layerIds );
707 
721  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
722  void removeMapLayers( const QList<QgsMapLayer *> &layers );
723 
737  void removeMapLayer( const QString &layerId );
738 
752  void removeMapLayer( QgsMapLayer *layer );
753 
761  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
762 
773  void removeAllMapLayers();
774 
780  void reloadAllLayers();
781 
786  QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;
787 
799  void setTrustLayerMetadata( bool trust );
800 
811  bool trustLayerMetadata() const { return mTrustLayerMetadata; }
812 
818  const QgsAuxiliaryStorage *auxiliaryStorage() const SIP_SKIP;
819 
825  QgsAuxiliaryStorage *auxiliaryStorage();
826 
827  signals:
829  void readProject( const QDomDocument & );
830 
832  void writeProject( QDomDocument & );
833 
842  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
843 
852  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
853 
855  void projectSaved();
856 
858  void oldProjectVersionWarning( const QString & );
859 
865  void layerLoaded( int i, int n );
866 
867  void loadingLayer( const QString & );
868 
870  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
871 
873  void fileNameChanged();
874 
876  void homePathChanged();
877 
879  void snappingConfigChanged( const QgsSnappingConfig &config );
880 
885  void customVariablesChanged();
886 
892  void crsChanged();
893 
901  void ellipsoidChanged( const QString &ellipsoid );
902 
909  void transactionGroupsChanged();
910 
916  void topologicalEditingChanged();
917 
923  void avoidIntersectionsLayersChanged();
924 
936  void mapThemeCollectionChanged();
937 
942  void labelingEngineSettingsChanged();
943 
944  //
945  // signals from QgsMapLayerRegistry
946  //
947 
955  void layersWillBeRemoved( const QStringList &layerIds );
956 
964  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
965 
975  void layerWillBeRemoved( const QString &layerId );
976 
986  void layerWillBeRemoved( QgsMapLayer *layer );
987 
994  void layersRemoved( const QStringList &layerIds );
995 
1004  void layerRemoved( const QString &layerId );
1005 
1012  //TODO QGIS 3.0 - rename to past tense
1013  void removeAll();
1014 
1025  void layersAdded( const QList<QgsMapLayer *> &layers );
1026 
1033  void layerWasAdded( QgsMapLayer *layer );
1034 
1043  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
1044 
1045 
1046 
1047  public slots:
1048 
1054  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
1055 
1063  void setDirty( bool b = true );
1064 
1065  private slots:
1066  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
1067  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
1068  void cleanTransactionGroups( bool force = false );
1069 
1070  private:
1071 
1072  static QgsProject *sProject;
1073 
1081  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
1082 
1087  void setError( const QString &errorMessage ) SIP_SKIP;
1088 
1093  void clearError() SIP_SKIP;
1094 
1099  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsReadWriteContext &context ) SIP_SKIP;
1100 
1102  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
1103 
1105  void loadEmbeddedNodes( QgsLayerTreeGroup *group ) SIP_SKIP;
1106 
1108  bool readProjectFile( const QString &filename );
1109 
1111  bool writeProjectFile( const QString &filename );
1112 
1114  bool unzip( const QString &filename );
1115 
1117  bool zip( const QString &filename );
1118 
1120  bool saveAuxiliaryStorage( const QString &filename = QString() );
1121 
1122  std::unique_ptr< QgsMapLayerStore > mLayerStore;
1123 
1124  QString mErrorMessage;
1125 
1126  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1127 
1133  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1134 
1135  QgsSnappingConfig mSnappingConfig;
1136 
1137  QgsRelationManager *mRelationManager = nullptr;
1138 
1139  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1140  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1141 
1142  QgsLayerTree *mRootGroup = nullptr;
1143 
1144  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1145 
1147  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1148 
1149  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1150 
1151  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1152 
1153  QVariantMap mCustomVariables;
1154 
1155  std::unique_ptr<QgsProjectArchive> mArchive;
1156 
1157  std::unique_ptr<QgsAuxiliaryStorage> mAuxiliaryStorage;
1158 
1159  QFile mFile; // current physical project file
1160  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1161  QString mTitle; // project title
1162  bool mAutoTransaction = false; // transaction grouped editing
1163  bool mEvaluateDefaultValues = false; // evaluate default values immediately
1165  bool mDirty = false; // project has been modified since it has been read or saved
1166  bool mTrustLayerMetadata = false;
1167 };
1168 
1174 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc ) SIP_SKIP;
1175 
1176 #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:811
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: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: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:81
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:626
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