QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
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  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 
279  QString writePath( QString filename, QString relativeBasePath = QString::null ) const;
280 
283  QString readPath( QString filename ) const;
284 
287  QString error() const;
288 
292  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
293 
295  QString layerIsEmbedded( const QString& id ) const;
296 
301  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
302  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
303 
307  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath );
308 
311  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
312  bool avoidIntersection );
313 
316  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
317  bool& avoidIntersection ) const;
318 
321  void setTopologicalEditing( bool enabled );
322 
325  bool topologicalEditing() const;
326 
330  QString homePath() const;
331 
332  QgsRelationManager* relationManager() const;
333 
337  QgsLayerTreeGroup* layerTreeRoot() const;
338 
342  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
343 
344  protected:
345 
348  void setError( QString errorMessage );
349 
352  void clearError();
353 
354  //Creates layer and adds it to maplayer registry
356  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
357 
359  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
360 
362  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
363 
364  signals:
366  void readProject( const QDomDocument & );
367 
369  void writeProject( QDomDocument & );
370 
379  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
380 
389  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
390 
392  void projectSaved();
393 
395  void oldProjectVersionWarning( QString );
396 
398  // @param i current layer
399  // @param n number of layers
400  void layerLoaded( int i, int n );
401 
402  void loadingLayer( QString );
403 
404  void snapSettingsChanged();
405 
406  private:
407 
408  QgsProject(); // private 'cause it's a singleton
409 
410  QgsProject( QgsProject const & ); // private 'cause it's a singleton
411 
412  struct Imp;
413 
415  std::auto_ptr<Imp> imp_;
416 
417  static QgsProject * theProject_;
418 
419  QPair< bool, QList<QDomNode> > _getMapLayers( QDomDocument const &doc );
420 
421  QString mErrorMessage;
422 
423  QgsProjectBadLayerHandler* mBadLayerHandler;
424 
428  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
429 
430  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
431  QStringList& avoidIntersectionList ) const;
432 
433  QgsRelationManager* mRelationManager;
434 
435  QgsLayerTreeGroup* mRootGroup;
436 
437  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
438 
439 }; // QgsProject
440 
441 
444 class CORE_EXPORT QgsProjectBadLayerHandler
445 {
446  public:
447  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom ) = 0;
449 };
450 
451 
455 {
456  public:
457  virtual void handleBadLayers( QList<QDomNode> layers, QDomDocument projectDom );
458 
459 };
460 
461 #endif
Layer tree group node serves as a container for layers and further groups.
Definition: qgslayertreegroup.h:31
Base class for all map layer types.
Definition: qgsmaplayer.h:48
QgsLayerTreeRegistryBridge * layerTreeRegistryBridge() const
Return pointer to the helper class that synchronizes map layer registry with layer tree...
Definition: qgsproject.h:342
SnappingType
Snap to vertex, to segment or both.
Definition: qgssnapper.h:66
Listens to the updates in map layer registry and does changes in layer tree.
Definition: qgslayertreeregistrybridge.h:39
Reads and writes project states.
Definition: qgsproject.h:67
virtual ~QgsProjectBadLayerHandler()
Definition: qgsproject.h:448
QString file
Definition: qgssvgcache.cpp:76
This class manages a set of relations between layers.
Definition: qgsrelationmanager.h:32
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:454
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:470
Interface for classes that handle missing layer files when reading project file.
Definition: qgsproject.h:444
virtual void handleBadLayers(QList< QDomNode > layers, QDomDocument projectDom)=0