QGIS API Documentation  2.99.0-Master (0a63d1f)
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;
52 class QgsRelationManager;
53 class QgsTolerance;
55 class QgsVectorLayer;
56 
57 
72 class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
73 {
74  Q_OBJECT
75  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
76  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
77  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
78  Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs )
79  Q_PROPERTY( QgsMapThemeCollection* mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
80  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
81  Q_PROPERTY( QList<QgsVectorLayer*> avoidIntersectionsLayers READ avoidIntersectionsLayers WRITE setAvoidIntersectionsLayers NOTIFY avoidIntersectionsLayersChanged )
82 
83  public:
85  static QgsProject* instance();
86 
92  explicit QgsProject( QObject* parent = nullptr );
93 
94  ~QgsProject();
95 
101  void setTitle( const QString& title );
102 
106  QString title() const;
107 
111  bool isDirty() const;
112 
118  void setFileName( const QString& name );
119 
125  QString fileName() const;
126 
131  QFileInfo fileInfo() const;
132 
139  QgsCoordinateReferenceSystem crs() const;
140 
147  void setCrs( const QgsCoordinateReferenceSystem& crs );
148 
155  QString ellipsoid() const;
156 
163  void setEllipsoid( const QString& ellipsoid );
164 
168  void clear();
169 
174  bool read( const QString& filename );
175 
179  bool read();
180 
189  bool readLayer( const QDomNode& layerNode );
190 
200  bool write( const QString& filename );
201 
206  bool write();
207 
217  bool writeEntry( const QString& scope, const QString& key, bool value );
218 
228  bool writeEntry( const QString& scope, const QString& key, double value );
229 
238  bool writeEntry( const QString& scope, const QString& key, int value );
239 
248  bool writeEntry( const QString& scope, const QString& key, const QString& value );
249 
258  bool writeEntry( const QString& scope, const QString& key, const QStringList& value );
259 
266  QStringList readListEntry( const QString& scope, const QString& key, const QStringList& def = QStringList(), bool* ok = nullptr ) const;
267 
268  QString readEntry( const QString& scope, const QString& key, const QString& def = QString::null, bool* ok = nullptr ) const;
269  int readNumEntry( const QString& scope, const QString& key, int def = 0, bool* ok = nullptr ) const;
270  double readDoubleEntry( const QString& scope, const QString& key, double def = 0, bool* ok = nullptr ) const;
271  bool readBoolEntry( const QString& scope, const QString& key, bool def = false, bool* ok = nullptr ) const;
272 
273 
275  bool removeEntry( const QString& scope, const QString& key );
276 
277 
282  QStringList entryList( const QString& scope, const QString& key ) const;
283 
288  QStringList subkeyList( const QString& scope, const QString& key ) const;
289 
290 
293  // TODO Now slightly broken since re-factoring. Won't print out top-level key
294  // and redundantly prints sub-keys.
295  void dumpProperties() const;
296 
302  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
303 
305  QString readPath( QString filename ) const;
306 
308  QString error() const;
309 
313  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
314 
316  QString layerIsEmbedded( const QString& id ) const;
317 
322  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
323  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
324 
328  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
329 
331  void setTopologicalEditing( bool enabled );
332 
334  bool topologicalEditing() const;
335 
341  QgsUnitTypes::DistanceUnit distanceUnits() const;
342 
349  void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
350 
355  QgsUnitTypes::AreaUnit areaUnits() const;
356 
363  void setAreaUnits( QgsUnitTypes::AreaUnit unit );
364 
367  QString homePath() const;
368 
369  QgsRelationManager* relationManager() const;
370 
374  QgsLayerTreeGroup* layerTreeRoot() const;
375 
379  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
380 
385  QgsMapThemeCollection* mapThemeCollection();
386 
390  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
391 
395  void setNonIdentifiableLayers( const QStringList& layerIds );
396 
400  QStringList nonIdentifiableLayers() const;
401 
409  bool autoTransaction() const;
410 
420  void setAutoTransaction( bool autoTransaction );
421 
430  QMap< QPair< QString, QString>, QgsTransactionGroup*> transactionGroups();
431 
437  bool evaluateDefaultValues() const;
438 
439 
445  void setEvaluateDefaultValues( bool evaluateDefaultValues );
446 
448 
454  QgsSnappingConfig snappingConfig() const;
455 
461  void setSnappingConfig( const QgsSnappingConfig& snappingConfig );
462 
468  QList<QgsVectorLayer*> avoidIntersectionsLayers() const;
469 
475  void setAvoidIntersectionsLayers( const QList<QgsVectorLayer*>& layers );
476 
482  QVariantMap customVariables() const;
483 
488  void setCustomVariables( const QVariantMap& customVariables );
489 
490  //
491  // Functionality from QgsMapLayerRegistry
492  //
493 
495  int count() const;
496 
503  //TODO QGIS 3.0 - rename theLayerId to layerId
504  QgsMapLayer* mapLayer( const QString& theLayerId ) const;
505 
512  QList<QgsMapLayer *> mapLayersByName( const QString& layerName ) const;
513 
519  QMap<QString, QgsMapLayer*> mapLayers() const;
520 
531  template <typename T>
532  QVector<T> layers() const
533  {
534  QVector<T> layers;
535  QMap<QString, QgsMapLayer*>::const_iterator layerIt = mMapLayers.constBegin();
536  for ( ; layerIt != mMapLayers.constEnd(); ++layerIt )
537  {
538  T tLayer = qobject_cast<T>( layerIt.value() );
539  if ( tLayer )
540  {
541  layers << tLayer;
542  }
543  }
544  return layers;
545  }
546 
572  QList<QgsMapLayer *> addMapLayers( const QList<QgsMapLayer*>& theMapLayers,
573  bool addToLegend = true,
574  bool takeOwnership = true );
575 
603  QgsMapLayer* addMapLayer( QgsMapLayer * theMapLayer, bool addToLegend = true, bool takeOwnership = true );
604 
619  // TODO QGIS 3.0 - rename theLayerIds to layerIds
620  void removeMapLayers( const QStringList& theLayerIds );
621 
635  //TODO QGIS 3.0 - add PyName alias to avoid list type conversion error
636  void removeMapLayers( const QList<QgsMapLayer*>& layers );
637 
651  // TODO QGIS 3.0 - rename theLayerId to layerId
652  void removeMapLayer( const QString& theLayerId );
653 
667  void removeMapLayer( QgsMapLayer* layer );
668 
679  void removeAllMapLayers();
680 
686  void reloadAllLayers();
687 
688  signals:
690  void readProject( const QDomDocument& );
691 
693  void writeProject( QDomDocument& );
694 
703  void readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerNode );
704 
713  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
714 
716  void projectSaved();
717 
719  void oldProjectVersionWarning( const QString& );
720 
726  void layerLoaded( int i, int n );
727 
728  void loadingLayer( const QString& );
729 
731  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
732 
734  void fileNameChanged();
735 
737  void homePathChanged();
738 
740  void snappingConfigChanged();
741 
745  void customVariablesChanged();
746 
753  void transactionGroupsChanged();
754 
760  void topologicalEditingChanged();
761 
767  void avoidIntersectionsLayersChanged();
768 
780  void mapThemeCollectionChanged();
781 
782  //
783  // signals from QgsMapLayerRegistry
784  //
785 
793  // TODO QGIS 3.0 - rename theLayerIds to layerIds
794  void layersWillBeRemoved( const QStringList& theLayerIds );
795 
803  void layersWillBeRemoved( const QList<QgsMapLayer*>& layers );
804 
814  //TODO QGIS 3.0 - rename theLayerId to layerId
815  void layerWillBeRemoved( const QString& theLayerId );
816 
826  void layerWillBeRemoved( QgsMapLayer* layer );
827 
834  //TODO QGIS 3.0 - rename theLayerIds to layerIds
835  void layersRemoved( const QStringList& theLayerIds );
836 
845  //TODO QGIS 3.0 - rename theLayerId to layerId
846  void layerRemoved( const QString& theLayerId );
847 
854  //TODO QGIS 3.0 - rename to past tense
855  void removeAll();
856 
867  //TODO QGIS 3.0 - rename theMapLayers to mapLayers
868  void layersAdded( const QList<QgsMapLayer *>& theMapLayers );
869 
878  // TODO QGIS 3.0 - rename theMapLayer to layer
879  void layerWasAdded( QgsMapLayer* theMapLayer );
880 
889  //TODO QGIS 3.0 rename theMapLayers to mapLayers
890  void legendLayersAdded( const QList<QgsMapLayer*>& theMapLayers );
891 
892  public slots:
893 
901  void setDirty( bool b = true );
902 
903  private slots:
904  void onMapLayersAdded( const QList<QgsMapLayer*>& layers );
905  void onMapLayersRemoved( const QList<QgsMapLayer*>& layers );
906  void cleanTransactionGroups( bool force = false );
907 
908  void onMapLayerDeleted( QObject* obj );
909 
910  private:
911 
912  static QgsProject* sProject;
913 
920  bool _getMapLayers( const QDomDocument& doc, QList<QDomNode>& brokenNodes );
921 
925  void processLayerJoins( QgsVectorLayer* layer );
926 
930  void setError( const QString& errorMessage );
931 
935  void clearError();
936 
939  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
940 
942  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
943 
945  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
946 
947  QMap<QString, QgsMapLayer*> mMapLayers;
948 
949  QString mErrorMessage;
950 
951  QgsProjectBadLayerHandler* mBadLayerHandler;
952 
957  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
958 
959  QgsSnappingConfig mSnappingConfig;
960 
961  QgsRelationManager* mRelationManager;
962 
963  QgsLayerTreeGroup* mRootGroup;
964 
965  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
966 
968  QMap< QPair< QString, QString>, QgsTransactionGroup*> mTransactionGroups;
969 
970  QScopedPointer<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:50
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:585
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:72
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:532
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.
Represents a vector layer which manages a vector based data sets.
Interface for classes that handle missing layer files when reading project file.