QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
31 //for the snap settings
32 #include "qgssnapper.h"
33 #include "qgstolerance.h"
34 
35 //#include <QDomDocument>
36 
37 class QFileInfo;
38 class QDomDocument;
39 class QDomElement;
40 class QDomNode;
41 
42 class QgsLayerTreeGroup;
44 class QgsMapLayer;
46 class QgsRelationManager;
47 class QgsVectorLayer;
48 
67 class CORE_EXPORT QgsProject : public QObject
68 {
69  Q_OBJECT
70 
71  public:
72 
76  ~QgsProject();
77 
79  static QgsProject * instance();
80 
87  void title( const QString & title );
88 
91  void setTitle( const QString& title );
92 
94  const QString & title() const;
96 
102  bool isDirty() const;
103 
104  // ### QGIS 3: remove in favor of setDirty(...)
105  void dirty( bool b );
106 
109  void setDirty( bool b );
111 
112 
117  void setFileName( const QString & name );
118 
120  QString fileName() const;
122 
126  void clear();
127 
128 
146  bool read( const QFileInfo & file );
147  bool read();
149 
150 
163  bool read( QDomNode & layerNode );
164 
165 
176  bool write( const QFileInfo & file );
177  bool write();
179 
185  void clearProperties();
186 
187 
188  /* key value mutators
189 
190  keys would be the familiar QSettings-like '/' delimited entries, implying
191  a hierarchy of keys and corresponding values
192 
193  @note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
194  */
196 
197  bool writeEntry( const QString & scope, const QString & key, bool value );
199  bool writeEntry( const QString & scope, const QString & key, double value );
200  bool writeEntry( const QString & scope, const QString & key, int value );
201  bool writeEntry( const QString & scope, const QString & key, const QString & value );
202  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
204 
214  QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;
215 
216  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = 0 ) const;
217  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = 0 ) const;
218  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = 0 ) const;
219  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = 0 ) const;
221 
222 
224  bool removeEntry( const QString & scope, const QString & key );
225 
226 
231  QStringList entryList( const QString & scope, const QString & key ) const;
232 
237  QStringList subkeyList( const QString & scope, const QString & key ) const;
238 
239 
245  void dumpProperties() const;
246 
248  QString writePath( QString filename, QString relativeBasePath = QString::null ) const;
249 
251  QString readPath( QString filename ) const;
252 
254  QString error() const;
255 
258  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
259 
261  QString layerIsEmbedded( const QString& id ) const;
262 
266  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
267  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
268 
272  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
273 
275  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
276  bool avoidIntersection );
277 
279  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
280  bool& avoidIntersection ) const;
281 
283  void setTopologicalEditing( bool enabled );
284 
286  bool topologicalEditing() const;
287 
290  QString homePath() const;
291 
292  QgsRelationManager* relationManager() const;
293 
297  QgsLayerTreeGroup* layerTreeRoot() const;
298 
302  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
303 
304  protected:
305 
307  void setError( QString errorMessage );
308 
310  void clearError();
311 
312  //Creates layer and adds it to maplayer registry
314  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
315 
317  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
318 
320  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
321 
322  signals:
324  void readProject( const QDomDocument & );
325 
327  void writeProject( QDomDocument & );
328 
337  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
338 
347  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
348 
350  void projectSaved();
351 
353  void oldProjectVersionWarning( QString );
354 
356  // @param i current layer
357  // @param n number of layers
358  void layerLoaded( int i, int n );
359 
360  void loadingLayer( QString );
361 
362  void snapSettingsChanged();
363 
364  private:
365 
366  QgsProject(); // private 'cause it's a singleton
367 
368  QgsProject( QgsProject const & ); // private 'cause it's a singleton
369 
370  struct Imp;
371 
373  QScopedPointer<Imp> imp_;
374 
375  static QgsProject * theProject_;
376 
377  QPair< bool, QList<QDomNode> > _getMapLayers( QDomDocument const &doc );
378 
379  QString mErrorMessage;
380 
381  QgsProjectBadLayerHandler* mBadLayerHandler;
382 
386  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
387 
388  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
389  QStringList& avoidIntersectionList ) const;
390 
391  QgsRelationManager* mRelationManager;
392 
393  QgsLayerTreeGroup* mRootGroup;
394 
395  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
396 
397 }; // QgsProject
398 
399 
401 class CORE_EXPORT QgsProjectBadLayerHandler
402 {
403  public:
404  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom ) = 0;
406 };
407 
408 
411 {
412  public:
413  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom ) override;
414 
415 };
416 
417 #endif