QGIS API Documentation  2.99.0-Master (b698612)
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 
44 class QFileInfo;
45 class QDomDocument;
46 class QDomElement;
47 class QDomNode;
48 
49 class QgsLayerTreeGroup;
51 class QgsMapLayer;
53 class QgsPathResolver;
55 class QgsRelationManager;
56 class QgsTolerance;
58 class QgsVectorLayer;
60 class QgsLayoutManager;
61 class QgsLayerTree;
63 
78 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
79 {
80  Q_OBJECT
81  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
82  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
83  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
84  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
85  Q_PROPERTY( QString ellipsoid READ ellipsoid WRITE setEllipsoid NOTIFY ellipsoidChanged )
86  Q_PROPERTY( QgsMapThemeCollection *mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
87  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
88  Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
89  Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
90 
91  public:
93  static QgsProject *instance();
94 
100  explicit QgsProject( QObject *parent SIP_TRANSFERTHIS = 0 );
101 
102  ~QgsProject();
103 
109  void setTitle( const QString &title );
110 
114  QString title() const;
115 
119  bool isDirty() const;
120 
126  void setFileName( const QString &name );
127 
133  QString fileName() const;
134 
139  QFileInfo fileInfo() const;
140 
147  QgsCoordinateReferenceSystem crs() const;
148 
155  void setCrs( const QgsCoordinateReferenceSystem &crs );
156 
163  QString ellipsoid() const;
164 
171  void setEllipsoid( const QString &ellipsoid );
172 
176  void clear();
177 
182  bool read( const QString &filename );
183 
187  bool read();
188 
197  bool readLayer( const QDomNode &layerNode );
198 
208  bool write( const QString &filename );
209 
214  bool write();
215 
225  bool writeEntry( const QString &scope, const QString &key, bool value ) SIP_PYNAME( writeEntryBool );
226 
236  bool writeEntry( const QString &scope, const QString &key, double value ) SIP_PYNAME( writeEntryDouble );
237 
246  bool writeEntry( const QString &scope, const QString &key, int value );
247 
256  bool writeEntry( const QString &scope, const QString &key, const QString &value );
257 
266  bool writeEntry( const QString &scope, const QString &key, const QStringList &value );
267 
274  QStringList readListEntry( const QString &scope, const QString &key, const QStringList &def = QStringList(), bool *ok = nullptr ) const;
275 
276  QString readEntry( const QString &scope, const QString &key, const QString &def = QString(), bool *ok = nullptr ) const;
277  int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
278  double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
279  bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
280 
281 
283  bool removeEntry( const QString &scope, const QString &key );
284 
285 
290  QStringList entryList( const QString &scope, const QString &key ) const;
291 
296  QStringList subkeyList( const QString &scope, const QString &key ) const;
297 
298 
301  // TODO Now slightly broken since re-factoring. Won't print out top-level key
302  // and redundantly prints sub-keys.
303  void dumpProperties() const;
304 
309  QgsPathResolver pathResolver() const;
310 
316  QString writePath( const QString &filename ) const;
317 
319  QString readPath( const QString &filename ) const;
320 
322  QString error() const;
323 
327  void setBadLayerHandler( QgsProjectBadLayerHandler *handler SIP_TRANSFER );
328 
330  QString layerIsEmbedded( const QString &id ) const;
331 
336  bool createEmbeddedLayer( const QString &layerId, const QString &projectFilePath, QList<QDomNode> &brokenNodes,
337  bool saveFlag = true ) SIP_SKIP;
338 
342  QgsLayerTreeGroup *createEmbeddedGroup( const QString &groupName, const QString &projectFilePath, const QStringList &invisibleLayers );
343 
345  void setTopologicalEditing( bool enabled );
346 
348  bool topologicalEditing() const;
349 
355  QgsUnitTypes::DistanceUnit distanceUnits() const;
356 
363  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
364 
369  QgsUnitTypes::AreaUnit areaUnits() const;
370 
377  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
378 
381  QString homePath() const;
382 
383  QgsRelationManager *relationManager() const;
384 
391  const QgsLayoutManager *layoutManager() const SIP_SKIP;
392 
398  QgsLayoutManager *layoutManager();
399 
403  QgsLayerTree *layerTreeRoot() const;
404 
408  QgsLayerTreeRegistryBridge *layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
409 
414  QgsMapThemeCollection *mapThemeCollection();
415 
420  QgsAnnotationManager *annotationManager();
421 
426  const QgsAnnotationManager *annotationManager() const SIP_SKIP;
427 
431  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
432 
436  void setNonIdentifiableLayers( const QStringList &layerIds );
437 
441  QStringList nonIdentifiableLayers() const;
442 
450  bool autoTransaction() const;
451 
461  void setAutoTransaction( bool autoTransaction );
462 
471  QMap< QPair< QString, QString>, QgsTransactionGroup *> transactionGroups() SIP_SKIP;
472 
478  bool evaluateDefaultValues() const;
479 
480 
486  void setEvaluateDefaultValues( bool evaluateDefaultValues );
487 
489 
495  QgsSnappingConfig snappingConfig() const;
496 
502  QList<QgsVectorLayer *> avoidIntersectionsLayers() const;
503 
509  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer *> &layers );
510 
516  QVariantMap customVariables() const;
517 
522  void setCustomVariables( const QVariantMap &customVariables );
523 
528  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
529 
534  const QgsLabelingEngineSettings &labelingEngineSettings() const;
535 
536  //
537  // Functionality from QgsMapLayerRegistry
538  //
539 
544  QgsMapLayerStore *layerStore();
545 
550  SIP_SKIP const QgsMapLayerStore *layerStore() const;
551 
553  int count() const;
554 
561  QgsMapLayer *mapLayer( const QString &layerId ) const;
562 
569  QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
570 
576  QMap<QString, QgsMapLayer *> mapLayers() const;
577 
578 #ifndef SIP_RUN
579 
590  template <typename T>
591  QVector<T> layers() const
592  {
593  return mLayerStore->layers<T>();
594  }
595 #endif
596 
622  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &mapLayers SIP_TRANSFER,
623  bool addToLegend = true,
624  bool takeOwnership SIP_PYARGREMOVE = true );
625 
653  QgsMapLayer *addMapLayer( QgsMapLayer *mapLayer SIP_TRANSFER,
654  bool addToLegend = true,
655  bool takeOwnership SIP_PYARGREMOVE = true );
656 
671  void removeMapLayers( const QStringList &layerIds );
672 
686  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
687  void removeMapLayers( const QList<QgsMapLayer *> &layers );
688 
702  void removeMapLayer( const QString &layerId );
703 
717  void removeMapLayer( QgsMapLayer *layer );
718 
726  QgsMapLayer *takeMapLayer( QgsMapLayer *layer ) SIP_TRANSFERBACK;
727 
738  void removeAllMapLayers();
739 
745  void reloadAllLayers();
746 
747  signals:
749  void readProject( const QDomDocument & );
750 
752  void writeProject( QDomDocument & );
753 
762  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
763 
772  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
773 
775  void projectSaved();
776 
778  void oldProjectVersionWarning( const QString & );
779 
785  void layerLoaded( int i, int n );
786 
787  void loadingLayer( const QString & );
788 
790  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
791 
793  void fileNameChanged();
794 
796  void homePathChanged();
797 
799  void snappingConfigChanged( const QgsSnappingConfig &config );
800 
804  void customVariablesChanged();
805 
811  void crsChanged();
812 
820  void ellipsoidChanged( const QString &ellipsoid );
821 
828  void transactionGroupsChanged();
829 
835  void topologicalEditingChanged();
836 
842  void avoidIntersectionsLayersChanged();
843 
855  void mapThemeCollectionChanged();
856 
861  void labelingEngineSettingsChanged();
862 
863  //
864  // signals from QgsMapLayerRegistry
865  //
866 
874  void layersWillBeRemoved( const QStringList &layerIds );
875 
883  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
884 
894  void layerWillBeRemoved( const QString &layerId );
895 
905  void layerWillBeRemoved( QgsMapLayer *layer );
906 
913  void layersRemoved( const QStringList &layerIds );
914 
923  void layerRemoved( const QString &layerId );
924 
931  //TODO QGIS 3.0 - rename to past tense
932  void removeAll();
933 
944  void layersAdded( const QList<QgsMapLayer *> &layers );
945 
952  void layerWasAdded( QgsMapLayer *layer );
953 
962  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
963 
964  public slots:
965 
971  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
972 
980  void setDirty( bool b = true );
981 
982  private slots:
983  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
984  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
985  void cleanTransactionGroups( bool force = false );
986 
987  private:
988 
989  static QgsProject *sProject;
990 
997  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
998 
1002  void setError( const QString &errorMessage ) SIP_SKIP;
1003 
1007  void clearError() SIP_SKIP;
1008 
1011  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes, const QgsReadWriteContext &context ) SIP_SKIP;
1012 
1014  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group ) SIP_SKIP;
1015 
1017  void loadEmbeddedNodes( QgsLayerTreeGroup *group ) SIP_SKIP;
1018 
1019  std::unique_ptr< QgsMapLayerStore > mLayerStore;
1020 
1021  QString mErrorMessage;
1022 
1023  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1024 
1029  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1030 
1031  QgsSnappingConfig mSnappingConfig;
1032 
1033  QgsRelationManager *mRelationManager = nullptr;
1034 
1035  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1036  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1037 
1038  QgsLayerTree *mRootGroup = nullptr;
1039 
1040  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1041 
1043  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1044 
1045  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1046 
1047  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1048 
1049  QVariantMap mCustomVariables;
1050 
1051  QFile mFile; // current physical project file
1052  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1053  QString mTitle; // project title
1054  bool mAutoTransaction; // transaction grouped editing
1055  bool mEvaluateDefaultValues; // evaluate default values immediately
1057  bool mDirty; // project has been modified since it has been read or saved
1058 };
1059 
1064 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc ) SIP_SKIP;
1065 
1066 #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:34
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:29
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
#define SIP_SKIP
Definition: qgis_sip.h:107
CORE_EXPORT QgsProjectVersion getVersion(QDomDocument const &doc)
Return the version string found in the given DOM document.
Definition: qgsproject.cpp:611
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:24
Reads and writes project states.
Definition: qgsproject.h:78
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:127
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
Definition: qgsproject.h:591
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.
Interface for classes that handle missing layer files when reading project file.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:62