QGIS API Documentation  2.17.0-Master (bf77d09)
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  Q_PROPERTY( QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged )
76  Q_PROPERTY( QString homePath READ homePath NOTIFY homePathChanged )
77 
78  public:
79 
80  // TODO XXX Should have semantics for saving project if dirty as last gasp?
81  ~QgsProject();
82 
84  static QgsProject * instance();
85 
91  Q_DECL_DEPRECATED inline void title( const QString & title ) { setTitle( title ); }
92 
98  void setTitle( const QString& title );
99 
103  QString title() const;
104 
108  bool isDirty() const;
109 
116  Q_DECL_DEPRECATED inline void dirty( bool b ) { setDirty( b ); }
117 
123  void setFileName( const QString& name );
124 
130  QString fileName() const;
131 
136  QFileInfo fileInfo() const;
137 
141  void clear();
142 
154  bool read( const QFileInfo& file );
155 
166  bool read();
167 
180  bool read( QDomNode& layerNode );
181 
188  bool write( const QFileInfo& file );
189 
194  bool write();
195 
201  Q_DECL_DEPRECATED void clearProperties();
202 
203 
204  /* key value mutators
205  *
206  * keys would be the familiar QSettings-like '/' delimited entries, implying
207  * a hierarchy of keys and corresponding values
208  *
209  * @note The key string must be valid xml tag names in order to be saved to the file.
210  */
212  bool writeEntry( const QString & scope, const QString & key, bool value );
214  bool writeEntry( const QString & scope, const QString & key, double value );
215  bool writeEntry( const QString & scope, const QString & key, int value );
216  bool writeEntry( const QString & scope, const QString & key, const QString & value );
217  bool writeEntry( const QString & scope, const QString & key, const QStringList & value );
218 
225  QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = nullptr ) const;
226 
227  QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = nullptr ) const;
228  int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = nullptr ) const;
229  double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = nullptr ) const;
230  bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = nullptr ) const;
231 
232 
234  bool removeEntry( const QString & scope, const QString & key );
235 
236 
241  QStringList entryList( const QString & scope, const QString & key ) const;
242 
247  QStringList subkeyList( const QString & scope, const QString & key ) const;
248 
249 
252  // TODO Now slightly broken since re-factoring. Won't print out top-level key
253  // and redundantly prints sub-keys.
254  void dumpProperties() const;
255 
259  QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;
260 
262  QString readPath( QString filename ) const;
263 
265  QString error() const;
266 
270  void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
271 
273  QString layerIsEmbedded( const QString& id ) const;
274 
279  bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
280  QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
281 
285  QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
286 
288  void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType unit, double tolerance,
289  bool avoidIntersection );
290 
292  bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
293  bool& avoidIntersection ) const;
294 
296  void setTopologicalEditing( bool enabled );
297 
299  bool topologicalEditing() const;
300 
305  QGis::UnitType distanceUnits() const;
306 
311  QgsUnitTypes::AreaUnit areaUnits() const;
312 
315  QString homePath() const;
316 
317  QgsRelationManager* relationManager() const;
318 
322  QgsLayerTreeGroup* layerTreeRoot() const;
323 
327  QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }
328 
332  QgsVisibilityPresetCollection* visibilityPresetCollection();
333 
337  void setNonIdentifiableLayers( QList<QgsMapLayer*> layers );
338 
342  void setNonIdentifiableLayers( const QStringList& layerIds );
343 
347  QStringList nonIdentifiableLayers() const;
348 
356  bool autoTransaction() const;
357 
367  void setAutoTransaction( bool autoTransaction );
368 
378 
384  bool evaluateDefaultValues() const;
385 
386 
392  void setEvaluateDefaultValues( bool evaluateDefaultValues );
393 
394  protected:
398  void setError( const QString& errorMessage );
399 
403  void clearError();
404 
407  bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
408 
410  void initializeEmbeddedSubtree( const QString& projectFilePath, QgsLayerTreeGroup* group );
411 
413  void loadEmbeddedNodes( QgsLayerTreeGroup* group );
414 
415  signals:
417  void readProject( const QDomDocument & );
418 
420  void writeProject( QDomDocument & );
421 
430  void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
431 
440  void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
441 
443  void projectSaved();
444 
446  void oldProjectVersionWarning( const QString& );
447 
449  // @param i current layer
450  // @param n number of layers
451  void layerLoaded( int i, int n );
452 
453  void loadingLayer( const QString& );
454 
455  void snapSettingsChanged();
456 
458  void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
459 
461  void fileNameChanged();
462 
464  void homePathChanged();
465 
469  void variablesChanged();
470 
471  public slots:
472 
480  void setDirty( bool b = true );
481 
487  void emitVariablesChanged();
488 
489  private slots:
490  void onMapLayersAdded( const QList<QgsMapLayer*>& layers );
491  void cleanTransactionGroups( bool force = false );
492 
493  private:
494 
495  QgsProject(); // private 'cause it's a singleton
496 
497  QgsProject( QgsProject const & ); // private 'cause it's a singleton
498 
499  struct Imp;
500 
502  QScopedPointer<Imp> imp_;
503 
504  static QgsProject * theProject_;
505 
512  bool _getMapLayers( QDomDocument const &doc, QList<QDomNode>& brokenNodes );
513 
517  void processLayerJoins( QgsVectorLayer* layer );
518 
519  QString mErrorMessage;
520 
521  QgsProjectBadLayerHandler* mBadLayerHandler;
522 
527  QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
528 
529  void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
530  QStringList& avoidIntersectionList ) const;
531 
532  QgsRelationManager* mRelationManager;
533 
534  QgsLayerTreeGroup* mRootGroup;
535 
536  QgsLayerTreeRegistryBridge* mLayerTreeRegistryBridge;
537 
540 
541  QScopedPointer<QgsVisibilityPresetCollection> mVisibilityPresetCollection;
542 };
543 
544 
547 class CORE_EXPORT QgsProjectBadLayerHandler
548 {
549  public:
550  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) = 0;
552 };
553 
554 
558 {
559  public:
560  virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) override;
561 
562 };
563 
564 
568 CORE_EXPORT QgsProjectVersion getVersion( QDomDocument const &doc );
569 
570 #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:327
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
SnappingType
Snap to vertex, to segment or both.
Definition: qgssnapper.h:67
CORE_EXPORT QgsProjectVersion getVersion(QDomDocument const &doc)
Return the version string found in the given DOM document.
Definition: qgsproject.cpp:579
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:116
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:551
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:557
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:547