QGIS API Documentation  2.99.0-Master (b681b7b)
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 <QDomDocument>qgp
34 #include "qgssnapper.h"
35 #include "qgsunittypes.h"
36 #include "qgssnappingconfig.h"
37 #include "qgsprojectversion.h"
40 #include "qgsprojectproperty.h"
41 
42 class QFileInfo;
43 class QDomDocument;
44 class QDomElement;
45 class QDomNode;
46 
47 class QgsLayerTreeGroup;
49 class QgsMapLayer;
51 class QgsPathResolver;
53 class QgsRelationManager;
54 class QgsTolerance;
56 class QgsVectorLayer;
58 
59 
74 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
75 {
76  Q_OBJECT
77  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
78  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
79  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
80  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs )
81  Q_PROPERTY( QgsMapThemeCollection* mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
82  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
83  Q_PROPERTY( QgsRelationManager* relationManager READ relationManager )
84  Q_PROPERTY( QList<QgsVectorLayer*> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
85 
86  public:
88  static QgsProject* instance();
89 
95  explicit QgsProject( QObject* parent = nullptr );
96 
97  ~QgsProject();
98 
104  void setTitle( const QString& title );
105 
109  QString title() const;
110 
114  bool isDirty() const;
115 
121  void setFileName( const QString& name );
122 
128  QString fileName() const;
129 
134  QFileInfo fileInfo() const;
135 
142  QgsCoordinateReferenceSystem crs() const;
143 
150  void setCrs( const QgsCoordinateReferenceSystem& crs );
151 
158  QString ellipsoid() const;
159 
166  void setEllipsoid( const QString& ellipsoid );
167 
171  void clear();
172 
177  bool read( const QString& filename );
178 
182  bool read();
183 
192  bool readLayer( const QDomNode& layerNode );
193 
203  bool write( const QString& filename );
204 
209  bool write();
210 
220  bool writeEntry( const QString& scope, const QString& key, bool value );
221 
231  bool writeEntry( const QString& scope, const QString& key, double value );
232 
241  bool writeEntry( const QString& scope, const QString& key, int value );
242 
251  bool writeEntry( const QString& scope, const QString& key, const QString& value );
252 
261  bool writeEntry( const QString& scope, const QString& key, const QStringList& value );
262 
269  QStringList readListEntry( const QString& scope, const QString& key, const QStringList& def = QStringList(), bool* ok = nullptr ) const;
270 
271  QString readEntry( const QString& scope, const QString& key, const QString& def = QString::null, bool* ok = nullptr ) const;
272  int readNumEntry( const QString& scope, const QString& key, int def = 0, bool* ok = nullptr ) const;
273  double readDoubleEntry( const QString& scope, const QString& key, double def = 0, bool* ok = nullptr ) const;
274  bool readBoolEntry( const QString& scope, const QString& key, bool def = false, bool* ok = nullptr ) const;
275 
276 
278  bool removeEntry( const QString& scope, const QString& key );
279 
280 
285  QStringList entryList( const QString& scope, const QString& key ) const;
286 
291  QStringList subkeyList( const QString& scope, const QString& key ) const;
292 
293 
296  // TODO Now slightly broken since re-factoring. Won't print out top-level key
297  // and redundantly prints sub-keys.
298  void dumpProperties() const;
299 
304  QgsPathResolver pathResolver() const;
305 
311  QString writePath( const QString& filename ) const;
312 
314  QString readPath( const QString& filename ) const;
315 
317  QString error() const;
318 
322  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
323 
325  QString layerIsEmbedded( const QString& id ) const;
326 
331  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
332  bool saveFlag = true );
333 
337  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
338 
340  void setTopologicalEditing( bool enabled );
341 
343  bool topologicalEditing() const;
344 
350  QgsUnitTypes::DistanceUnit distanceUnits() const;
351 
358  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
359 
364  QgsUnitTypes::AreaUnit areaUnits() const;
365 
372  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
373 
376  QString homePath() const;
377 
378  QgsRelationManager* relationManager() const;
379 
383  QgsLayerTreeGroup* layerTreeRoot() const;
384 
388  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
389 
394  QgsMapThemeCollection* mapThemeCollection();
395 
400  QgsAnnotationManager* annotationManager();
401 
405  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
406 
410  void setNonIdentifiableLayers( const QStringList& layerIds );
411 
415  QStringList nonIdentifiableLayers() const;
416 
424  bool autoTransaction() const;
425 
435  void setAutoTransaction( bool autoTransaction );
436 
445  QMap< QPair< QString, QString>, QgsTransactionGroup*> transactionGroups();
446 
452  bool evaluateDefaultValues() const;
453 
454 
460  void setEvaluateDefaultValues( bool evaluateDefaultValues );
461 
463 
469  QgsSnappingConfig snappingConfig() const;
470 
476  void setSnappingConfig( const QgsSnappingConfig& snappingConfig );
477 
483  QList<QgsVectorLayer*> avoidIntersectionsLayers() const;
484 
490  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer*>& layers );
491 
497  QVariantMap customVariables() const;
498 
503  void setCustomVariables( const QVariantMap& customVariables );
504 
505  //
506  // Functionality from QgsMapLayerRegistry
507  //
508 
510  int count() const;
511 
518  QgsMapLayer* mapLayer( const QString& layerId ) const;
519 
526  QList<QgsMapLayer *> mapLayersByName( const QString& layerName ) const;
527 
533  QMap<QString, QgsMapLayer*> mapLayers() const;
534 
545  template <typename T>
546  QVector<T> layers() const
547  {
548  QVector<T> layers;
549  QMap<QString, QgsMapLayer*>::const_iterator layerIt = mMapLayers.constBegin();
550  for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
551  {
552  T tLayer = qobject_cast<T>( layerIt.value() );
553  if ( tLayer )
554  {
555  layers << tLayer;
556  }
557  }
558  return layers;
559  }
560 
586  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer*>& mapLayers,
587  bool addToLegend = true,
588  bool takeOwnership = true );
589 
617  QgsMapLayer* addMapLayer( QgsMapLayer * mapLayer, bool addToLegend = true, bool takeOwnership = true );
618 
633  void removeMapLayers( const QStringList& layerIds );
634 
648  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
649  void removeMapLayers( const QList<QgsMapLayer*>& layers );
650 
664  void removeMapLayer( const QString& layerId );
665 
679  void removeMapLayer( QgsMapLayer* layer );
680 
691  void removeAllMapLayers();
692 
698  void reloadAllLayers();
699 
700  signals:
702  void readProject( const QDomDocument& );
703 
705  void writeProject( QDomDocument& );
706 
715  void readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerNode );
716 
725  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
726 
728  void projectSaved();
729 
731  void oldProjectVersionWarning( const QString& );
732 
738  void layerLoaded( int i, int n );
739 
740  void loadingLayer( const QString& );
741 
743  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
744 
746  void fileNameChanged();
747 
749  void homePathChanged();
750 
752  void snappingConfigChanged();
753 
757  void customVariablesChanged();
758 
765  void transactionGroupsChanged();
766 
772  void topologicalEditingChanged();
773 
779  void avoidIntersectionsLayersChanged();
780 
792  void mapThemeCollectionChanged();
793 
794  //
795  // signals from QgsMapLayerRegistry
796  //
797 
805  void layersWillBeRemoved( const QStringList& layerIds );
806 
814  void layersWillBeRemoved( const QList<QgsMapLayer*>& layers );
815 
825  void layerWillBeRemoved( const QString& layerId );
826 
836  void layerWillBeRemoved( QgsMapLayer* layer );
837 
844  void layersRemoved( const QStringList& layerIds );
845 
854  void layerRemoved( const QString& layerId );
855 
862  //TODO QGIS 3.0 - rename to past tense
863  void removeAll();
864 
875  void layersAdded( const QList<QgsMapLayer *>& layers );
876 
883  void layerWasAdded( QgsMapLayer* layer );
884 
893  void legendLayersAdded( const QList<QgsMapLayer*>& layers );
894 
895  public slots:
896 
904  void setDirty( bool b = true );
905 
906  private slots:
907  void onMapLayersAdded( const QList<QgsMapLayer*>& layers );
908  void onMapLayersRemoved( const QList<QgsMapLayer*>& layers );
909  void cleanTransactionGroups( bool force = false );
910 
911  void onMapLayerDeleted( QObject* obj );
912 
913  private:
914 
915  static QgsProject* sProject;
916 
923  bool _getMapLayers( const QDomDocument& doc, QList<QDomNode>& brokenNodes );
924 
928  void setError( const QString& errorMessage );
929 
933  void clearError();
934 
937  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes );
938 
940  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
941 
943  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
944 
945  QMap<QString, QgsMapLayer*> mMapLayers;
946 
947  QString mErrorMessage;
948 
949  QgsProjectBadLayerHandler* mBadLayerHandler = nullptr;
950 
955  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
956 
957  QgsSnappingConfig mSnappingConfig;
958 
959  QgsRelationManager* mRelationManager = nullptr;
960 
961  std::unique_ptr<QgsAnnotationManager> mAnnotationManager;
962 
963  QgsLayerTreeGroup* mRootGroup = nullptr;
964 
965  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge = nullptr;
966 
968  QMap< QPair< QString, QString>, QgsTransactionGroup*> mTransactionGroups;
969 
970  std::unique_ptr<QgsMapThemeCollection> mMapThemeCollection;
971 
972  QVariantMap mCustomVariables;
973 
974  QFile mFile; // current physical project file
975  mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
976  QString mTitle; // project title
977  bool mAutoTransaction; // transaction grouped editing
978  bool mEvaluateDefaultValues; // evaluate default values immediately
979  bool mDirty; // project has been modified since it has been read or saved
980 };
981 
985 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc );
986 
987 #endif
Layer tree group node serves as a container for layers and further groups.
Base class for all map layer types.
Definition: qgsmaplayer.h:52
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...
CORE_EXPORT QgsProjectVersion getVersion(QDomDocument const &doc)
Return the version string found in the given DOM document.
Definition: qgsproject.cpp:583
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:74
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:546
This class manages a set of relations between layers.
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.