QGIS API Documentation  2.99.0-Master (c558d51)
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 <memory>
25 #include <QHash>
26 #include <QList>
27 #include <QObject>
28 #include <QPair>
29 #include <QFileInfo>
30 #include <QStringList>
31 
32 //#include <QDomDocument>qgp
33 #include "qgssnapper.h"
34 #include "qgsunittypes.h"
35 #include "qgssnappingconfig.h"
36 #include "qgsprojectversion.h"
39 #include "qgsprojectproperty.h"
40 
41 class QFileInfo;
42 class QDomDocument;
43 class QDomElement;
44 class QDomNode;
45 
46 class QgsLayerTreeGroup;
48 class QgsMapLayer;
51 class QgsRelationManager;
52 class QgsTolerance;
54 class QgsVectorLayer;
55 
56 
71 // TODO Might want to consider moving from Singleton; i.e., allowing more than one
72 // project. Just as the GIMP can have simultaneous multiple images, perhaps
73 // QGIS can one day have simultaneous multiple projects.
74 
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 )
82  Q_PROPERTY( QgsMapThemeCollection* mapThemeCollection READ mapThemeCollection NOTIFY mapThemeCollectionChanged )
83  Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
84  Q_PROPERTY( QStringList avoidIntersectionsList READ avoidIntersectionsList WRITE setAvoidIntersectionsList NOTIFY avoidIntersectionsListChanged )
85 
86  public:
88  static QgsProject* instance();
89 
90  ~QgsProject();
91 
97  void setTitle( const QString& title );
98 
102  QString title() const;
103 
107  bool isDirty() const;
108 
114  void setFileName( const QString& name );
115 
121  QString fileName() const;
122 
127  QFileInfo fileInfo() const;
128 
135  QgsCoordinateReferenceSystem crs() const;
136 
143  void setCrs( const QgsCoordinateReferenceSystem& crs );
144 
151  QString ellipsoid() const;
152 
159  void setEllipsoid( const QString& ellipsoid );
160 
164  void clear();
165 
177  bool read( const QFileInfo& file );
178 
189  bool read();
190 
203  bool read( QDomNode& layerNode );
204 
211  bool write( const QFileInfo& file );
212 
217  bool write();
218 
228  bool writeEntry( const QString& scope, const QString& key, bool value );
229 
239  bool writeEntry( const QString& scope, const QString& key, double value );
240 
249  bool writeEntry( const QString& scope, const QString& key, int value );
250 
259  bool writeEntry( const QString& scope, const QString& key, const QString& value );
260 
269  bool writeEntry( const QString& scope, const QString& key, const QStringList& value );
270 
277  QStringList readListEntry( const QString& scope, const QString& key, const QStringList& def = QStringList(), bool* ok = nullptr ) const;
278 
279  QString readEntry( const QString& scope, const QString& key, const QString& def = QString::null, bool* ok = nullptr ) const;
280  int readNumEntry( const QString& scope, const QString& key, int def = 0, bool* ok = nullptr ) const;
281  double readDoubleEntry( const QString& scope, const QString& key, double def = 0, bool* ok = nullptr ) const;
282  bool readBoolEntry( const QString& scope, const QString& key, bool def = false, bool* ok = nullptr ) const;
283 
284 
286  bool removeEntry( const QString& scope, const QString& key );
287 
288 
293  QStringList entryList( const QString& scope, const QString& key ) const;
294 
299  QStringList subkeyList( const QString& scope, const QString& key ) const;
300 
301 
304  // TODO Now slightly broken since re-factoring. Won't print out top-level key
305  // and redundantly prints sub-keys.
306  void dumpProperties() const;
307 
313  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
314 
316  QString readPath( 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  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, 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 
385  QgsLayerTreeGroup* layerTreeRoot() const;
386 
390  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
391 
396  QgsMapThemeCollection* mapThemeCollection();
397 
401  void setNonIdentifiableLayers( const QList<QgsMapLayer *> &layers );
402 
406  void setNonIdentifiableLayers( const QStringList& layerIds );
407 
411  QStringList nonIdentifiableLayers() const;
412 
420  bool autoTransaction() const;
421 
431  void setAutoTransaction( bool autoTransaction );
432 
441  QMap< QPair< QString, QString>, QgsTransactionGroup*> transactionGroups();
442 
448  bool evaluateDefaultValues() const;
449 
450 
456  void setEvaluateDefaultValues( bool evaluateDefaultValues );
457 
459 
465  QgsSnappingConfig snappingConfig() const;
466 
472  void setSnappingConfig( const QgsSnappingConfig& snappingConfig );
473 
479  QStringList avoidIntersectionsList() const;
480 
486  void setAvoidIntersectionsList( const QStringList& avoidIntersectionsList );
487 
493  QgsStringMap variables() const;
494 
499  void setVariables( const QgsStringMap& variables );
500 
501  signals:
503  void readProject( const QDomDocument& );
504 
506  void writeProject( QDomDocument& );
507 
516  void readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerNode );
517 
526  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
527 
529  void projectSaved();
530 
532  void oldProjectVersionWarning( const QString& );
533 
539  void layerLoaded( int i, int n );
540 
541  void loadingLayer( const QString& );
542 
544  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
545 
547  void fileNameChanged();
548 
550  void homePathChanged();
551 
553  void snappingConfigChanged();
554 
558  void variablesChanged();
559 
566  void transactionGroupsChanged();
567 
573  void topologicalEditingChanged();
574 
580  void avoidIntersectionsListChanged();
581 
593  void mapThemeCollectionChanged();
594 
595  public slots:
596 
604  void setDirty( bool b = true );
605 
606  private slots:
607  void onMapLayersAdded( const QList<QgsMapLayer*>& layers );
608  void onMapLayersRemoved( const QList<QgsMapLayer*>& layers );
609  void cleanTransactionGroups( bool force = false );
610 
611  private:
612 
618  explicit QgsProject( QObject* parent = nullptr );
619 
620  static QgsProject* sProject;
621 
628  bool _getMapLayers( const QDomDocument& doc, QList<QDomNode>& brokenNodes );
629 
633  void processLayerJoins( QgsVectorLayer* layer );
634 
638  void setError( const QString& errorMessage );
639 
643  void clearError();
644 
647  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
648 
650  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
651 
653  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
654 
655  QString mErrorMessage;
656 
657  QgsProjectBadLayerHandler* mBadLayerHandler;
658 
663  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
664 
665  QgsSnappingConfig mSnappingConfig;
666 
667  QgsRelationManager* mRelationManager;
668 
669  QgsLayerTreeGroup* mRootGroup;
670 
671  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
672 
674  QMap< QPair< QString, QString>, QgsTransactionGroup*> mTransactionGroups;
675 
676  QScopedPointer<QgsMapThemeCollection> mMapThemeCollection;
677 
678  QgsStringMap mVariables;
679 
680  QFile mFile; // current physical project file
681  mutable QgsPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
682  QString mTitle; // project title
683  bool mAutoTransaction; // transaction grouped editing
684  bool mEvaluateDefaultValues; // evaluate default values immediately
685  bool mDirty; // project has been modified since it has been read or saved
686 };
687 
691 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc );
692 
693 #endif
Layer tree group node serves as a container for layers and further groups.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
Helper functions for various unit types.
Definition: qgsunittypes.h:35
QMap< QString, QString > QgsStringMap
Definition: qgis.h:328
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:28
Listens to the updates in map layer registry and does changes in layer tree.
QgsPropertyKey node.
Reads and writes project states.
Definition: qgsproject.h:75
Abstract interface for generating an expression context.
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.