QGIS API Documentation  2.6.0-Brighton
 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 
180 
182  // DEPRECATED typedef QPair< QString, QVariant > PropertyValue;
183  // DEPRECATED typedef QValueList< PropertyValue > Properties;
184 
208  // DEPRECATED Properties & properties( QString const & scope );
209 
215  void clearProperties();
216 
217 
218  /* key value mutators
219 
220  keys would be the familiar QSettings-like '/' delimited entries, implying
221  a hierarchy of keys and corresponding values
222 
223  @note The key string <em>must</em> include '/'s. E.g., "/foo" not "foo".
224  */
226 
227  bool writeEntry( const QString & scope, const QString & key, bool value );
229  bool writeEntry( const QString & scope, const QString & key, double value );
230  bool writeEntry( const QString & scope, const QString & key, int value );
231  bool writeEntry( const QString & scope, const QString & key, const QString & value );
232  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
234 
244  QStringList readListEntry( const QString & scope, const QString & key, QStringList def = QStringList(), bool *ok = 0 ) const;
245 
246  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = 0 ) const;
247  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = 0 ) const;
248  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = 0 ) const;
249  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = 0 ) const;
251 
252 
254  bool removeEntry( const QString & scope, const QString & key );
255 
256 
261  QStringList entryList( const QString & scope, const QString & key ) const;
262 
267  QStringList subkeyList( const QString & scope, const QString & key ) const;
268 
269 
275  void dumpProperties() const;
276 
278  QString writePath( QString filename, QString relativeBasePath = QString::null ) const;
279 
281  QString readPath( QString filename ) const;
282 
284  QString error() const;
285 
288  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
289 
291  QString layerIsEmbedded( const QString& id ) const;
292 
296  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
297  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
298 
302  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath );
303 
305  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
306  bool avoidIntersection );
307 
309  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
310  bool& avoidIntersection ) const;
311 
313  void setTopologicalEditing( bool enabled );
314 
316  bool topologicalEditing() const;
317 
320  QString homePath() const;
321 
322  QgsRelationManager* relationManager() const;
323 
327  QgsLayerTreeGroup* layerTreeRoot() const;
328 
332  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
333 
334  protected:
335 
337  void setError( QString errorMessage );
338 
340  void clearError();
341 
342  //Creates layer and adds it to maplayer registry
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( QString );
384 
386  // @param i current layer
387  // @param n number of layers
388  void layerLoaded( int i, int n );
389 
390  void loadingLayer( 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  std::auto_ptr<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 
416  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
417 
418  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
419  QStringList& avoidIntersectionList ) const;
420 
421  QgsRelationManager* mRelationManager;
422 
423  QgsLayerTreeGroup* mRootGroup;
424 
425  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
426 
427 }; // QgsProject
428 
429 
431 class CORE_EXPORT QgsProjectBadLayerHandler
432 {
433  public:
434  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom ) = 0;
436 };
437 
438 
441 {
442  public:
443  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom );
444 
445 };
446 
447 #endif