QGIS API Documentation  2.14.0-Essen
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;
51 
70 class CORE_EXPORT QgsProject : public QObject
71 {
72  Q_OBJECT
73 
74  public:
75 
79  ~QgsProject();
80 
82  static QgsProject * instance();
83 
90  Q_DECL_DEPRECATED inline void title( const QString & title ) { setTitle( title ); }
91 
95  void setTitle( const QString& title );
96 
98  QString title() const;
100 
106  bool isDirty() const;
107 
108  // ### QGIS 3: remove in favor of setDirty(...)
109  void dirty( bool b );
110 
113  void setDirty( bool b );
115 
116 
121  void setFileName( const QString & name );
122 
124  QString fileName() const;
126 
130  QFileInfo fileInfo() const;
131 
135  void clear();
136 
137 
155  bool read( const QFileInfo & file );
156  bool read();
158 
159 
172  bool read( QDomNode & layerNode );
173 
174 
185  bool write( const QFileInfo & file );
186  bool write();
188 
194  void clearProperties();
195 
196 
197  /* key value mutators
198  *
199  * keys would be the familiar QSettings-like '/' delimited entries, implying
200  * a hierarchy of keys and corresponding values
201  *
202  * @note The key string must be valid xml tag names in order to be saved to the file.
203  */
205  bool writeEntry( const QString & scope, const QString & key, bool value );
208  bool writeEntry( const QString & scope, const QString & key, double value );
209  bool writeEntry( const QString & scope, const QString & key, int value );
210  bool writeEntry( const QString & scope, const QString & key, const QString & value );
211  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
213 
221  QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = nullptr ) const;
222 
223  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = nullptr ) const;
224  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = nullptr ) const;
225  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = nullptr ) const;
226  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = nullptr ) const;
228 
229 
231  bool removeEntry( const QString & scope, const QString & key );
232 
233 
238  QStringList entryList( const QString & scope, const QString & key ) const;
239 
244  QStringList subkeyList( const QString & scope, const QString & key ) const;
245 
246 
252  void dumpProperties() const;
253 
255  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
256 
258  QString readPath( QString filename ) const;
259 
261  QString error() const;
262 
266  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
267 
269  QString layerIsEmbedded( const QString& id ) const;
270 
275  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
276  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
277 
281  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
282 
284  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
285  bool avoidIntersection );
286 
288  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
289  bool& avoidIntersection ) const;
290 
292  void setTopologicalEditing( bool enabled );
293 
295  bool topologicalEditing() const;
296 
301  QGis::UnitType distanceUnits() const;
302 
307  QgsUnitTypes::AreaUnit areaUnits() const;
308 
311  QString homePath() const;
312 
313  QgsRelationManager* relationManager() const;
314 
318  QgsLayerTreeGroup* layerTreeRoot() const;
319 
323  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
324 
328  QgsVisibilityPresetCollection* visibilityPresetCollection();
329 
330  protected:
331 
335  void setError( const QString& errorMessage );
336 
340  void clearError();
341 
344  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
345 
347  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
348 
350  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
351 
352  signals:
354  void readProject( const QDomDocument & );
355 
357  void writeProject( QDomDocument & );
358 
367  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
368 
377  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
378 
380  void projectSaved();
381 
383  void oldProjectVersionWarning( const QString& );
384 
386  // @param i current layer
387  // @param n number of layers
388  void layerLoaded( int i, int n );
389 
390  void loadingLayer( const QString& );
391 
392  void snapSettingsChanged();
393 
394  private:
395 
396  QgsProject(); // private 'cause it's a singleton
397 
398  QgsProject( QgsProject const & ); // private 'cause it's a singleton
399 
400  struct Imp;
401 
403  QScopedPointer<Imp> imp_;
404 
405  static QgsProject * theProject_;
406 
407  QPair< bool, QList<QDomNode> > _getMapLayers( QDomDocument const &doc );
408 
409  QString mErrorMessage;
410 
411  QgsProjectBadLayerHandler* mBadLayerHandler;
412 
417  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
418 
419  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
420  QStringList& avoidIntersectionList ) const;
421 
422  QgsRelationManager* mRelationManager;
423 
424  QgsLayerTreeGroup* mRootGroup;
425 
426  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
427 
428  QScopedPointer<QgsVisibilityPresetCollection> mVisibilityPresetCollection;
429 
430 }; // QgsProject
431 
432 
434 class CORE_EXPORT QgsProjectBadLayerHandler
435 {
436  public:
437  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) = 0;
439 };
440 
441 
444 {
445  public:
446  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) override;
447 
448 };
449 
450 #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:323
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:90
Listens to the updates in map layer registry and does changes in layer tree.
Reads and writes project states.
Definition: qgsproject.h:70
virtual ~QgsProjectBadLayerHandler()
Definition: qgsproject.h:438
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:443
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:434