QGIS API Documentation
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 "qgsprojectversion.h"
26 #include <QHash>
27 #include <QList>
28 #include <QObject>
29 #include <QPair>
30 #include <QFileInfo>
31 
32 //for the snap settings
33 #include "qgssnapper.h"
34 #include "qgstolerance.h"
35 #include "qgsunittypes.h"
36 
37 //#include <QDomDocument>
38 
39 class QFileInfo;
40 class QDomDocument;
41 class QDomElement;
42 class QDomNode;
43 
44 class QgsLayerTreeGroup;
46 class QgsMapLayer;
48 class QgsRelationManager;
49 class QgsVectorLayer;
52 
71 class CORE_EXPORT QgsProject : public QObject
72 {
73  Q_OBJECT
74  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
75 
76  public:
77 
81  ~QgsProject();
82 
84  static QgsProject * instance();
85 
92  Q_DECL_DEPRECATED inline void title( const QString & title ) { setTitle( title ); }
93 
97  void setTitle( const QString& title );
98 
100  QString title() const;
102 
108  bool isDirty() const;
109 
116  Q_DECL_DEPRECATED inline void dirty( bool b ) { setDirty( b ); }
117 
124  void setDirty( bool b );
126 
127 
132  void setFileName( const QString & name );
133 
135  QString fileName() const;
137 
141  QFileInfo fileInfo() const;
142 
146  void clear();
147 
148 
166  bool read( const QFileInfo & file );
167  bool read();
169 
170 
183  bool read( QDomNode & layerNode );
184 
185 
196  bool write( const QFileInfo & file );
197  bool write();
199 
205  void clearProperties();
206 
207 
208  /* key value mutators
209  *
210  * keys would be the familiar QSettings-like '/' delimited entries, implying
211  * a hierarchy of keys and corresponding values
212  *
213  * @note The key string must be valid xml tag names in order to be saved to the file.
214  */
216  bool writeEntry( const QString & scope, const QString & key, bool value );
219  bool writeEntry( const QString & scope, const QString & key, double value );
220  bool writeEntry( const QString & scope, const QString & key, int value );
221  bool writeEntry( const QString & scope, const QString & key, const QString & value );
222  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
224 
232  QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = nullptr ) const;
233 
234  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = nullptr ) const;
235  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = nullptr ) const;
236  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = nullptr ) const;
237  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = nullptr ) const;
239 
240 
242  bool removeEntry( const QString & scope, const QString & key );
243 
244 
249  QStringList entryList( const QString & scope, const QString & key ) const;
250 
255  QStringList subkeyList( const QString & scope, const QString & key ) const;
256 
257 
263  void dumpProperties() const;
264 
266  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
267 
269  QString readPath( QString filename ) const;
270 
272  QString error() const;
273 
277  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
278 
280  QString layerIsEmbedded( const QString& id ) const;
281 
286  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
287  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
288 
292  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
293 
295  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
296  bool avoidIntersection );
297 
299  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
300  bool& avoidIntersection ) const;
301 
303  void setTopologicalEditing( bool enabled );
304 
306  bool topologicalEditing() const;
307 
312  QGis::UnitType distanceUnits() const;
313 
318  QgsUnitTypes::AreaUnit areaUnits() const;
319 
322  QString homePath() const;
323 
324  QgsRelationManager* relationManager() const;
325 
329  QgsLayerTreeGroup* layerTreeRoot() const;
330 
334  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
335 
339  QgsVisibilityPresetCollection* visibilityPresetCollection();
340 
344  void setNonIdentifiableLayers( QList<QgsMapLayer*> layers );
345 
349  void setNonIdentifiableLayers( const QStringList& layerIds );
350 
354  QStringList nonIdentifiableLayers() const;
355 
363  bool autoTransaction() const;
364 
374  void setAutoTransaction( bool autoTransaction );
375 
385 
386  protected:
390  void setError( const QString& errorMessage );
391 
395  void clearError();
396 
399  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
400 
402  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
403 
405  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
406 
407  signals:
409  void readProject( const QDomDocument & );
410 
412  void writeProject( QDomDocument & );
413 
422  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
423 
432  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
433 
435  void projectSaved();
436 
438  void oldProjectVersionWarning( const QString& );
439 
441  // @param i current layer
442  // @param n number of layers
443  void layerLoaded( int i, int n );
444 
445  void loadingLayer( const QString& );
446 
447  void snapSettingsChanged();
448 
450  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
451 
452  private slots:
453  void addToTransactionGroups( const QList<QgsMapLayer*> layers );
454  void cleanTransactionGroups( bool force = false );
455 
456  private:
457 
458  QgsProject(); // private 'cause it's a singleton
459 
460  QgsProject( QgsProject const & ); // private 'cause it's a singleton
461 
462  struct Imp;
463 
465  QScopedPointer<Imp> imp_;
466 
467  static QgsProject * theProject_;
468 
469  QPair< bool, QList<QDomNode> > _getMapLayers( QDomDocument const &doc );
470 
471  QString mErrorMessage;
472 
473  QgsProjectBadLayerHandler* mBadLayerHandler;
474 
479  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
480 
481  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
482  QStringList& avoidIntersectionList ) const;
483 
484  QgsRelationManager* mRelationManager;
485 
486  QgsLayerTreeGroup* mRootGroup;
487 
488  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
489 
492 
493  QScopedPointer<QgsVisibilityPresetCollection> mVisibilityPresetCollection;
494 }; // QgsProject
495 
496 
498 class CORE_EXPORT QgsProjectBadLayerHandler
499 {
500  public:
501  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) = 0;
503 };
504 
505 
508 {
509  public:
510  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) override;
511 
512 };
513 
514 #endif
Layer tree group node serves as a container for layers and further groups.
virtual void handleBadLayers(const QList< QDomNode > &layers, const QDomDocument &projectDom)=0
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsLayerTreeRegistryBridge * layerTreeRegistryBridge() const
Return pointer to the helper class that synchronizes map layer registry with layer tree...
Definition: qgsproject.h:334
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
SnappingType
Snap to vertex, to segment or both.
Definition: qgssnapper.h:66
Q_DECL_DEPRECATED void title(const QString &title)
Every project has an associated title string.
Definition: qgsproject.h:92
Q_DECL_DEPRECATED void dirty(bool b)
Flag the project as dirty (modified).
Definition: qgsproject.h:116
Listens to the updates in map layer registry and does changes in layer tree.
Reads and writes project states.
Definition: qgsproject.h:71
virtual ~QgsProjectBadLayerHandler()
Definition: qgsproject.h:502
This class manages a set of relations between layers.
UnitType
Map units that qgis supports.
Definition: qgis.h:155
Container class that allows storage of visibility presets consisting of visible map layers and layer ...
Default bad layer handler which ignores any missing layers.
Definition: qgsproject.h:507
Represents a vector layer which manages a vector based data sets.
AreaUnit
Units of area.
Definition: qgsunittypes.h:49
Interface for classes that handle missing layer files when reading project file.
Definition: qgsproject.h:498