QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  const 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 <em>must</em> include '/'s. E.g., "/foo" not "foo".
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 
221  QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;
222 
223  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = 0 ) const;
224  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = 0 ) const;
225  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = 0 ) const;
226  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = 0 ) 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( QString filename, QString relativeBasePath = QString::null ) const;
256 
258  QString readPath( QString filename ) const;
259 
261  QString error() const;
262 
265  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
266 
268  QString layerIsEmbedded( const QString& id ) const;
269 
273  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
274  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
275 
279  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
280 
282  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
283  bool avoidIntersection );
284 
286  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
287  bool& avoidIntersection ) const;
288 
290  void setTopologicalEditing( bool enabled );
291 
293  bool topologicalEditing() const;
294 
297  QString homePath() const;
298 
299  QgsRelationManager* relationManager() const;
300 
304  QgsLayerTreeGroup* layerTreeRoot() const;
305 
309  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
310 
314  QgsVisibilityPresetCollection* visibilityPresetCollection();
315 
316  protected:
317 
319  void setError( QString errorMessage );
320 
322  void clearError();
323 
324  //Creates layer and adds it to maplayer registry
326  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
327 
329  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
330 
332  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
333 
334  signals:
336  void readProject( const QDomDocument & );
337 
339  void writeProject( QDomDocument & );
340 
349  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
350 
359  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
360 
362  void projectSaved();
363 
365  void oldProjectVersionWarning( QString );
366 
368  // @param i current layer
369  // @param n number of layers
370  void layerLoaded( int i, int n );
371 
372  void loadingLayer( QString );
373 
374  void snapSettingsChanged();
375 
376  private:
377 
378  QgsProject(); // private 'cause it's a singleton
379 
380  QgsProject( QgsProject const & ); // private 'cause it's a singleton
381 
382  struct Imp;
383 
385  QScopedPointer<Imp> imp_;
386 
387  static QgsProject * theProject_;
388 
389  QPair< bool, QList<QDomNode> > _getMapLayers( QDomDocument const &doc );
390 
391  QString mErrorMessage;
392 
393  QgsProjectBadLayerHandler* mBadLayerHandler;
394 
398  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
399 
400  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
401  QStringList& avoidIntersectionList ) const;
402 
403  QgsRelationManager* mRelationManager;
404 
405  QgsLayerTreeGroup* mRootGroup;
406 
407  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
408 
409  QScopedPointer<QgsVisibilityPresetCollection> mVisibilityPresetCollection;
410 
411 }; // QgsProject
412 
413 
415 class CORE_EXPORT QgsProjectBadLayerHandler
416 {
417  public:
418  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom ) = 0;
420 };
421 
422 
425 {
426  public:
427  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom ) override;
428 
429 };
430 
431 #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
QgsLayerTreeRegistryBridge * layerTreeRegistryBridge() const
Return pointer to the helper class that synchronizes map layer registry with layer tree...
Definition: qgsproject.h:309
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:419
This class manages a set of relations between layers.
Container class that allows storage of visibility presets consisting of visible map layers and layer ...
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
Default bad layer handler which ignores any missing layers.
Definition: qgsproject.h:424
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:415
virtual void handleBadLayers(QList< QDomNode > layers, QDomDocument projectDom)=0