QGIS API Documentation  2.13.0-Master
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 
36 //#include <QDomDocument>
37 
38 class QFileInfo;
39 class QDomDocument;
40 class QDomElement;
41 class QDomNode;
42 
43 class QgsLayerTreeGroup;
45 class QgsMapLayer;
47 class QgsRelationManager;
48 class QgsVectorLayer;
50 
69 class CORE_EXPORT QgsProject : public QObject
70 {
71  Q_OBJECT
72 
73  public:
74 
78  ~QgsProject();
79 
81  static QgsProject * instance();
82 
89  Q_DECL_DEPRECATED inline void title( const QString & title ) { setTitle( title ); }
90 
93  void setTitle( const QString& title );
94 
96  QString title() const;
98 
104  bool isDirty() const;
105 
106  // ### QGIS 3: remove in favor of setDirty(...)
107  void dirty( bool b );
108 
111  void setDirty( bool b );
113 
114 
119  void setFileName( const QString & name );
120 
122  QString fileName() const;
124 
128  QFileInfo fileInfo() const;
129 
133  void clear();
134 
135 
153  bool read( const QFileInfo & file );
154  bool read();
156 
157 
170  bool read( QDomNode & layerNode );
171 
172 
183  bool write( const QFileInfo & file );
184  bool write();
186 
192  void clearProperties();
193 
194 
195  /* key value mutators
196 
197  keys would be the familiar QSettings-like '/' delimited entries, implying
198  a hierarchy of keys and corresponding values
199 
200  @note The key string must be valid xml tag names in order to be saved to the file.
201  */
203  bool writeEntry( const QString & scope, const QString & key, bool value );
206  bool writeEntry( const QString & scope, const QString & key, double value );
207  bool writeEntry( const QString & scope, const QString & key, int value );
208  bool writeEntry( const QString & scope, const QString & key, const QString & value );
209  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
211 
219  QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = nullptr ) const;
220 
221  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = nullptr ) const;
222  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = nullptr ) const;
223  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = nullptr ) const;
224  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = nullptr ) const;
226 
227 
229  bool removeEntry( const QString & scope, const QString & key );
230 
231 
236  QStringList entryList( const QString & scope, const QString & key ) const;
237 
242  QStringList subkeyList( const QString & scope, const QString & key ) const;
243 
244 
250  void dumpProperties() const;
251 
253  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
254 
256  QString readPath( QString filename ) const;
257 
259  QString error() const;
260 
263  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
264 
266  QString layerIsEmbedded( const QString& id ) const;
267 
272  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
273  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
274 
278  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
279 
281  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
282  bool avoidIntersection );
283 
285  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
286  bool& avoidIntersection ) const;
287 
289  void setTopologicalEditing( bool enabled );
290 
292  bool topologicalEditing() const;
293 
296  QString homePath() const;
297 
298  QgsRelationManager* relationManager() const;
299 
303  QgsLayerTreeGroup* layerTreeRoot() const;
304 
308  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
309 
313  QgsVisibilityPresetCollection* visibilityPresetCollection();
314 
315  protected:
316 
320  void setError( const QString& errorMessage );
321 
325  void clearError();
326 
329  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
330 
332  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
333 
335  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
336 
337  signals:
339  void readProject( const QDomDocument & );
340 
342  void writeProject( QDomDocument & );
343 
352  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
353 
362  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
363 
365  void projectSaved();
366 
368  void oldProjectVersionWarning( const QString& );
369 
371  // @param i current layer
372  // @param n number of layers
373  void layerLoaded( int i, int n );
374 
375  void loadingLayer( const QString& );
376 
377  void snapSettingsChanged();
378 
379  private:
380 
381  QgsProject(); // private 'cause it's a singleton
382 
383  QgsProject( QgsProject const & ); // private 'cause it's a singleton
384 
385  struct Imp;
386 
388  QScopedPointer<Imp> imp_;
389 
390  static QgsProject * theProject_;
391 
392  QPair< bool, QList<QDomNode> > _getMapLayers( QDomDocument const &doc );
393 
394  QString mErrorMessage;
395 
396  QgsProjectBadLayerHandler* mBadLayerHandler;
397 
401  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
402 
403  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
404  QStringList& avoidIntersectionList ) const;
405 
406  QgsRelationManager* mRelationManager;
407 
408  QgsLayerTreeGroup* mRootGroup;
409 
410  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
411 
412  QScopedPointer<QgsVisibilityPresetCollection> mVisibilityPresetCollection;
413 
414 }; // QgsProject
415 
416 
418 class CORE_EXPORT QgsProjectBadLayerHandler
419 {
420  public:
421  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) = 0;
423 };
424 
425 
428 {
429  public:
430  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) override;
431 
432 };
433 
434 #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:308
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:89
Listens to the updates in map layer registry and does changes in layer tree.
Reads and writes project states.
Definition: qgsproject.h:69
virtual ~QgsProjectBadLayerHandler()
Definition: qgsproject.h:422
This class manages a set of relations between layers.
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:427
Represents a vector layer which manages a vector based data sets.
Interface for classes that handle missing layer files when reading project file.
Definition: qgsproject.h:418