QGIS API Documentation  2.15.0-Master (972fc9f)
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;
52 
67 // TODO Might want to consider moving from Singleton; i.e., allowing more than one
68 // project. Just as the GIMP can have simultaneous multiple images, perhaps
69 // QGIS can one day have simultaneous multiple projects.
70 
71 class CORE_EXPORT QgsProject : public QObject
72 {
73  Q_OBJECT
74  Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
75 
76  public:
77 
78  // TODO XXX Should have semantics for saving project if dirty as last gasp?
79  ~QgsProject();
80 
82  static QgsProject * instance();
83 
89  Q_DECL_DEPRECATED inline void title( const QString & title ) { setTitle( title ); }
90 
96  void setTitle( const QString& title );
97 
101  QString title() const;
102 
106  bool isDirty() const;
107 
114  Q_DECL_DEPRECATED inline void dirty( bool b ) { setDirty( b ); }
115 
121  void setFileName( const QString& name );
122 
128  QString fileName() const;
129 
134  QFileInfo fileInfo() const;
135 
139  void clear();
140 
152  bool read( const QFileInfo& file );
153 
164  bool read();
165 
178  bool read( QDomNode& layerNode );
179 
186  bool write( const QFileInfo& file );
187 
192  bool write();
193 
199  Q_DECL_DEPRECATED void clearProperties();
200 
201 
202  /* key value mutators
203  *
204  * keys would be the familiar QSettings-like '/' delimited entries, implying
205  * a hierarchy of keys and corresponding values
206  *
207  * @note The key string must be valid xml tag names in order to be saved to the file.
208  */
210  bool writeEntry( const QString & scope, const QString & key, bool value );
212  bool writeEntry( const QString & scope, const QString & key, double value );
213  bool writeEntry( const QString & scope, const QString & key, int value );
214  bool writeEntry( const QString & scope, const QString & key, const QString & value );
215  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
216 
223  QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = nullptr ) const;
224 
225  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = nullptr ) const;
226  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = nullptr ) const;
227  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = nullptr ) const;
228  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = nullptr ) const;
229 
230 
232  bool removeEntry( const QString & scope, const QString & key );
233 
234 
239  QStringList entryList( const QString & scope, const QString & key ) const;
240 
245  QStringList subkeyList( const QString & scope, const QString & key ) const;
246 
247 
250  // TODO Now slightly broken since re-factoring. Won't print out top-level key
251  // and redundantly prints sub-keys.
252  void dumpProperties() const;
253 
257  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
258 
260  QString readPath( QString filename ) const;
261 
263  QString error() const;
264 
268  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
269 
271  QString layerIsEmbedded( const QString& id ) const;
272 
277  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
278  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
279 
283  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
284 
286  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
287  bool avoidIntersection );
288 
290  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
291  bool& avoidIntersection ) const;
292 
294  void setTopologicalEditing( bool enabled );
295 
297  bool topologicalEditing() const;
298 
303  QGis::UnitType distanceUnits() const;
304 
309  QgsUnitTypes::AreaUnit areaUnits() const;
310 
313  QString homePath() const;
314 
315  QgsRelationManager* relationManager() const;
316 
320  QgsLayerTreeGroup* layerTreeRoot() const;
321 
325  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
326 
330  QgsVisibilityPresetCollection* visibilityPresetCollection();
331 
335  void setNonIdentifiableLayers( QList<QgsMapLayer*> layers );
336 
340  void setNonIdentifiableLayers( const QStringList& layerIds );
341 
345  QStringList nonIdentifiableLayers() const;
346 
354  bool autoTransaction() const;
355 
365  void setAutoTransaction( bool autoTransaction );
366 
376 
382  bool evaluateDefaultValues() const;
383 
384 
390  void setEvaluateDefaultValues( bool evaluateDefaultValues );
391 
392  protected:
396  void setError( const QString& errorMessage );
397 
401  void clearError();
402 
405  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
406 
408  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
409 
411  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
412 
413  signals:
415  void readProject( const QDomDocument & );
416 
418  void writeProject( QDomDocument & );
419 
428  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
429 
438  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
439 
441  void projectSaved();
442 
444  void oldProjectVersionWarning( const QString& );
445 
447  // @param i current layer
448  // @param n number of layers
449  void layerLoaded( int i, int n );
450 
451  void loadingLayer( const QString& );
452 
453  void snapSettingsChanged();
454 
456  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
457 
458  public slots:
459 
467  void setDirty( bool b = true );
468 
469  private slots:
470  void onMapLayersAdded( const QList<QgsMapLayer*>& layers );
471  void cleanTransactionGroups( bool force = false );
472 
473  private:
474 
475  QgsProject(); // private 'cause it's a singleton
476 
477  QgsProject( QgsProject const & ); // private 'cause it's a singleton
478 
479  struct Imp;
480 
482  QScopedPointer<Imp> imp_;
483 
484  static QgsProject * theProject_;
485 
492  bool _getMapLayers( QDomDocument const &doc, QList<QDomNode>& brokenNodes );
493 
497  void processLayerJoins( QgsVectorLayer* layer );
498 
499  QString mErrorMessage;
500 
501  QgsProjectBadLayerHandler* mBadLayerHandler;
502 
507  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
508 
509  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
510  QStringList& avoidIntersectionList ) const;
511 
512  QgsRelationManager* mRelationManager;
513 
514  QgsLayerTreeGroup* mRootGroup;
515 
516  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
517 
520 
521  QScopedPointer<QgsVisibilityPresetCollection> mVisibilityPresetCollection;
522 };
523 
524 
526 class CORE_EXPORT QgsProjectBadLayerHandler
527 {
528  public:
529  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) = 0;
531 };
532 
533 
536 {
537  public:
538  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) override;
539 
540 };
541 
542 
546 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc );
547 
548 #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:325
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
CORE_EXPORT QgsProjectVersion getVersion(QDomDocument const &doc)
Return the version string found in the given DOM document.
Definition: qgsproject.cpp:564
Q_DECL_DEPRECATED void title(const QString &title)
Every project has an associated title string.
Definition: qgsproject.h:89
A class to describe the version of a project.
Q_DECL_DEPRECATED void dirty(bool b)
Flag the project as dirty (modified).
Definition: qgsproject.h:114
Listens to the updates in map layer registry and does changes in layer tree.
Reads and writes project states.
Definition: qgsproject.h:71
virtual ~QgsProjectBadLayerHandler()
Definition: qgsproject.h:530
This class manages a set of relations between layers.
UnitType
Map units that qgis supports.
Definition: qgis.h:159
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:535
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:526