QGIS API Documentation  2.99.0-Master (cb63e82)
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 <memory>
26 #include <QHash>
27 #include <QList>
28 #include <QObject>
29 #include <QPair>
30 #include <QFileInfo>
31 #include <QStringList>
32 
33 #include "qgsunittypes.h"
34 #include "qgssnappingconfig.h"
35 #include "qgsprojectversion.h"
38 #include "qgsprojectproperty.h"
39 #include "qgsmaplayer.h"
40 
41 class QFileInfo;
42 class QDomDocument;
43 class QDomElement;
44 class QDomNode;
45 
46 class QgsLayerTreeGroup;
48 class QgsMapLayer;
50 class QgsPathResolver;
52 class QgsRelationManager;
53 class QgsTolerance;
55 class QgsVectorLayer;
57 class QgsLayoutManager;
58 class QgsLayerTree;
60 
75 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
76 {
77  Q_OBJECT
78  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
79  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
80  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
81  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
82  Q_PROPERTY( QString ellipsoid READ ellipsoid WRITE setEllipsoid NOTIFY ellipsoidChanged )
83  Q_PROPERTY( QgsMapThemeCollection *mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
84  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
85  Q_PROPERTY( QgsRelationManager *relationManager READ relationManager )
86  Q_PROPERTY( QList<QgsVectorLayer *> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
87 
88  public:
90  static QgsProject *instance();
91 
97  explicit QgsProject( QObject *parent = nullptr );
98 
99  ~QgsProject();
100 
106  void setTitle( const QString &title );
107 
111  QString title() const;
112 
116  bool isDirty() const;
117 
123  void setFileName( const QString &name );
124 
130  QString fileName() const;
131 
136  QFileInfo fileInfo() const;
137 
144  QgsCoordinateReferenceSystem crs() const;
145 
152  void setCrs( const QgsCoordinateReferenceSystem &crs );
153 
160  QString ellipsoid() const;
161 
168  void setEllipsoid( const QString &ellipsoid );
169 
173  void clear();
174 
179  bool read( const QString &filename );
180 
184  bool read();
185 
194  bool readLayer( const QDomNode &layerNode );
195 
205  bool write( const QString &filename );
206 
211  bool write();
212 
222  bool writeEntry( const QString &scope, const QString &key, bool value );
223 
233  bool writeEntry( const QString &scope, const QString &key, double value );
234 
243  bool writeEntry( const QString &scope, const QString &key, int value );
244 
253  bool writeEntry( const QString &scope, const QString &key, const QString &value );
254 
263  bool writeEntry( const QString &scope, const QString &key, const QStringList &value );
264 
271  QStringList readListEntry( const QString &scope, const QString &key, const QStringList &def = QStringList(), bool *ok = nullptr ) const;
272 
273  QString readEntry( const QString &scope, const QString &key, const QString &def = QString::null, bool *ok = nullptr ) const;
274  int readNumEntry( const QString &scope, const QString &key, int def = 0, bool *ok = nullptr ) const;
275  double readDoubleEntry( const QString &scope, const QString &key, double def = 0, bool *ok = nullptr ) const;
276  bool readBoolEntry( const QString &scope, const QString &key, bool def = false, bool *ok = nullptr ) const;
277 
278 
280  bool removeEntry( const QString &scope, const QString &key );
281 
282 
287  QStringList entryList( const QString &scope, const QString &key ) const;
288 
293  QStringList subkeyList( const QString &scope, const QString &key ) const;
294 
295 
298  // TODO Now slightly broken since re-factoring. Won't print out top-level key
299  // and redundantly prints sub-keys.
300  void dumpProperties() const;
301 
306  QgsPathResolver pathResolver() const;
307 
313  QString writePath( const QString &filename ) const;
314 
316  QString readPath( const QString &filename ) const;
317 
319  QString error() const;
320 
324  void setBadLayerHandler( QgsProjectBadLayerHandler *handler );
325 
327  QString layerIsEmbedded( const QString &id ) const;
328 
333  bool createEmbeddedLayer( const QString &layerId, const QString &projectFilePath, QList<QDomNode> &brokenNodes,
334  bool saveFlag = true );
335 
339  QgsLayerTreeGroup *createEmbeddedGroup( const QString &groupName, const QString &projectFilePath, const QStringList &invisibleLayers );
340 
342  void setTopologicalEditing( bool enabled );
343 
345  bool topologicalEditing() const;
346 
352  QgsUnitTypes::DistanceUnit distanceUnits() const;
353 
360  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
361 
366  QgsUnitTypes::AreaUnit areaUnits() const;
367 
374  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
375 
378  QString homePath() const;
379 
380  QgsRelationManager *relationManager() const;
381 
387  const QgsLayoutManager *layoutManager() const;
388 
394  QgsLayoutManager *layoutManager();
395 
399  QgsLayerTree *layerTreeRoot() const;
400 
404  QgsLayerTreeRegistryBridge *layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
405 
410  QgsMapThemeCollection *mapThemeCollection();
411 
416  QgsAnnotationManager *annotationManager();
417 
421  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
422 
426  void setNonIdentifiableLayers( const QStringList &layerIds );
427 
431  QStringList nonIdentifiableLayers() const;
432 
440  bool autoTransaction() const;
441 
451  void setAutoTransaction( bool autoTransaction );
452 
461  QMap< QPair< QString, QString>, QgsTransactionGroup *> transactionGroups();
462 
468  bool evaluateDefaultValues() const;
469 
470 
476  void setEvaluateDefaultValues( bool evaluateDefaultValues );
477 
479 
485  QgsSnappingConfig snappingConfig() const;
486 
492  QList<QgsVectorLayer *> avoidIntersectionsLayers() const;
493 
499  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer *> &layers );
500 
506  QVariantMap customVariables() const;
507 
512  void setCustomVariables( const QVariantMap &customVariables );
513 
518  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
519 
524  const QgsLabelingEngineSettings &labelingEngineSettings() const;
525 
526  //
527  // Functionality from QgsMapLayerRegistry
528  //
529 
531  int count() const;
532 
539  QgsMapLayer *mapLayer( const QString &layerId ) const;
540 
547  QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
548 
554  QMap<QString, QgsMapLayer *> mapLayers() const;
555 
566  template <typename T>
567  QVector<T> layers() const
568  {
569  QVector<T> layers;
570  QMap<QString, QgsMapLayer *>::const_iterator layerIt = mMapLayers.constBegin();
571  for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
572  {
573  T tLayer = qobject_cast<T>( layerIt.value() );
574  if ( tLayer )
575  {
576  layers << tLayer;
577  }
578  }
579  return layers;
580  }
581 
607  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer *> &mapLayers,
608  bool addToLegend = true,
609  bool takeOwnership = true );
610 
638  QgsMapLayer *addMapLayer( QgsMapLayer *mapLayer, bool addToLegend = true, bool takeOwnership = true );
639 
654  void removeMapLayers( const QStringList &layerIds );
655 
669  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
670  void removeMapLayers( const QList<QgsMapLayer *> &layers );
671 
685  void removeMapLayer( const QString &layerId );
686 
700  void removeMapLayer( QgsMapLayer *layer );
701 
709  QgsMapLayer *takeMapLayer( QgsMapLayer *layer );
710 
721  void removeAllMapLayers();
722 
728  void reloadAllLayers();
729 
730  signals:
732  void readProject( const QDomDocument & );
733 
735  void writeProject( QDomDocument & );
736 
745  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
746 
755  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
756 
758  void projectSaved();
759 
761  void oldProjectVersionWarning( const QString & );
762 
768  void layerLoaded( int i, int n );
769 
770  void loadingLayer( const QString & );
771 
773  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
774 
776  void fileNameChanged();
777 
779  void homePathChanged();
780 
782  void snappingConfigChanged( const QgsSnappingConfig &config );
783 
787  void customVariablesChanged();
788 
794  void crsChanged();
795 
803  void ellipsoidChanged( const QString &ellipsoid );
804 
811  void transactionGroupsChanged();
812 
818  void topologicalEditingChanged();
819 
825  void avoidIntersectionsLayersChanged();
826 
838  void mapThemeCollectionChanged();
839 
844  void labelingEngineSettingsChanged();
845 
846  //
847  // signals from QgsMapLayerRegistry
848  //
849 
857  void layersWillBeRemoved( const QStringList &layerIds );
858 
866  void layersWillBeRemoved( const QList<QgsMapLayer *> &layers );
867 
877  void layerWillBeRemoved( const QString &layerId );
878 
888  void layerWillBeRemoved( QgsMapLayer *layer );
889 
896  void layersRemoved( const QStringList &layerIds );
897 
906  void layerRemoved( const QString &layerId );
907 
914  //TODO QGIS 3.0 - rename to past tense
915  void removeAll();
916 
927  void layersAdded( const QList<QgsMapLayer *> &layers );
928 
935  void layerWasAdded( QgsMapLayer *layer );
936 
945  void legendLayersAdded( const QList<QgsMapLayer *> &layers );
946 
947  public slots:
948 
954  void setSnappingConfig( const QgsSnappingConfig &snappingConfig );
955 
963  void setDirty( bool b = true );
964 
965  private slots:
966  void onMapLayersAdded( const QList<QgsMapLayer *> &layers );
967  void onMapLayersRemoved( const QList<QgsMapLayer *> &layers );
968  void cleanTransactionGroups( bool force = false );
969 
970  void onMapLayerDeleted( QObject *obj );
971 
972  private:
973 
974  static QgsProject *sProject;
975 
982  bool _getMapLayers( const QDomDocument &doc, QList<QDomNode> &brokenNodes );
983 
987  void setError( const QString &errorMessage );
988 
992  void clearError();
993 
996  bool addLayer( const QDomElement &layerElem, QList<QDomNode> &brokenNodes );
997 
999  void initializeEmbeddedSubtree( const QString &projectFilePath, QgsLayerTreeGroup *group );
1000 
1002  void loadEmbeddedNodes( QgsLayerTreeGroup *group );
1003 
1004  QMap<QString, QgsMapLayer *> mMapLayers;
1005 
1006  QString mErrorMessage;
1007 
1008  QgsProjectBadLayerHandler *mBadLayerHandler = nullptr;
1009 
1014  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
1015 
1016  QgsSnappingConfig mSnappingConfig;
1017 
1018  QgsRelationManager *mRelationManager = nullptr;
1019 
1020  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
1021  std::unique_ptr<QgsLayoutManager> mLayoutManager;
1022 
1023  QgsLayerTree *mRootGroup = nullptr;
1024 
1025  QgsLayerTreeRegistryBridge *mLayerTreeRegistryBridge = nullptr;
1026 
1028  QMap< QPair< QString, QString>, QgsTransactionGroup *> mTransactionGroups;
1029 
1030  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
1031 
1032  std::unique_ptr<QgsLabelingEngineSettings> mLabelingEngineSettings;
1033 
1034  QVariantMap mCustomVariables;
1035 
1036  QFile mFile; // current physical project file
1037  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1038  QString mTitle; // project title
1039  bool mAutoTransaction; // transaction grouped editing
1040  bool mEvaluateDefaultValues; // evaluate default values immediately
1042  bool mDirty; // project has been modified since it has been read or saved
1043 };
1044 
1048 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc );
1049 
1050 #endif
Layer tree group node serves as a container for layers and further groups.
Base class for all map layer types.
Definition: qgsmaplayer.h:53
Manages storage of a set of QgsAnnotation annotation objects.
Helper functions for various unit types.
Definition: qgsunittypes.h:36
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 QgsProjectVersion getVersion(QDomDocument const &doc)
Return the version string found in the given DOM document.
Definition: qgsproject.cpp:593
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.
Reads and writes project states.
Definition: qgsproject.h:75
Manages storage of a set of compositions.
Abstract interface for generating an expression context.
Project property key node.
QVector< T > layers() const
Returns a list of registered map layers with a specified layer type.
Definition: qgsproject.h:567
This class manages a set of relations between layers.
Stores global configuration for labeling engine.
This class represents a coordinate reference system (CRS).
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.