Skip to content
Permalink
Browse files

Cleanup and modernize QgsProject code

  • Loading branch information
m-kuhn committed Oct 23, 2016
1 parent 28abbf9 commit 54b953f43346793c2e0e2701f9a00774dd64999e
@@ -110,6 +110,7 @@
%Include qgspoint.sip
%Include qgspointlocator.sip
%Include qgsproject.sip
%Include qgsprojectbadlayerhandler.sip
%Include qgsprojectproperty.sip
%Include qgssnappingconfig.sip
%Include qgsprojectversion.sip
@@ -20,16 +20,14 @@ class QgsProject : QObject
%End

public:
//! Returns the QgsProject singleton instance
static QgsProject* instance();

// TODO XXX Should have semantics for saving project if dirty as last gasp?
~QgsProject();

//! Returns the QgsProject singleton instance
static QgsProject * instance();

/** Sets the project's title.
* @param title new title
* @note added in 2.4
* @note added in 2.4
* @see title()
*/
void setTitle( const QString& title );
@@ -163,7 +161,7 @@ class QgsProject : QObject
* @note The key string must be valid xml tag names in order to be saved to the file.
* @note available in python bindings as writeEntryBool
*/
bool writeEntry( const QString & scope, const QString & key, bool value ) /PyName=writeEntryBool/;
bool writeEntry( const QString& scope, const QString& key, bool value ) /PyName=writeEntryBool/;

/**
* Write a double entry to the project file.
@@ -206,34 +204,34 @@ class QgsProject : QObject
*/
bool writeEntry( const QString& scope, const QString& key, const QStringList& value );

/** Key value accessors
/**
* Key value accessors
*
* keys would be the familiar QSettings-like '/' delimited entries,
* implying a hierarchy of keys and corresponding values
*
*/
QStringList readListEntry( const QString & scope, const QString & key, const QStringList& def = QStringList(), bool *ok = 0 ) const;
QStringList readListEntry( const QString& scope, const QString& key, const QStringList& def = QStringList(), bool *ok = 0 ) const;

QString readEntry( const QString & scope, const QString & key, const QString & def = QString::null, bool * ok = 0 ) const;
int readNumEntry( const QString & scope, const QString & key, int def = 0, bool * ok = 0 ) const;
double readDoubleEntry( const QString & scope, const QString & key, double def = 0, bool * ok = 0 ) const;
bool readBoolEntry( const QString & scope, const QString & key, bool def = false, bool * ok = 0 ) const;
QString readEntry( const QString& scope, const QString& key, const QString& def = QString::null, bool* ok = 0 ) const;
int readNumEntry( const QString& scope, const QString& key, int def = 0, bool* ok = 0 ) const;
double readDoubleEntry( const QString& scope, const QString& key, double def = 0, bool* ok = 0 ) const;
bool readBoolEntry( const QString& scope, const QString& key, bool def = false, bool* ok = 0 ) const;

/** Remove the given key */
bool removeEntry( const QString & scope, const QString & key );
bool removeEntry( const QString& scope, const QString& key );


/** Return keys with values -- do not return keys that contain other keys
*
* @note equivalent to QSettings entryList()
*/
QStringList entryList( const QString & scope, const QString & key ) const;
QStringList entryList( const QString& scope, const QString& key ) const;

/** Return keys with keys -- do not return keys that contain only values
*
* @note equivalent to QSettings subkeyList()
*/
QStringList subkeyList( const QString & scope, const QString & key ) const;
QStringList subkeyList( const QString& scope, const QString& key ) const;


/** Dump out current project properties to stderr
@@ -243,7 +241,11 @@ class QgsProject : QObject
*/
void dumpProperties() const;

/** Prepare a filename to save it to the project file */
/**
* Prepare a filename to save it to the project file.
* Creates an absolute or relative path according to the project settings.
* Paths written to the project file should be prepared with this method.
*/
QString writePath( const QString& filename, const QString& relativeBasePath = QString::null ) const;

/** Turn filename read from the project file to an absolute path */
@@ -325,8 +327,9 @@ class QgsProject : QObject
*/
QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const;

/** Returns pointer to the project's visibility preset collection.
/** Returns pointer to the project's map theme collection.
* @note added in QGIS 2.12
* @note renamed in QGIS 3.0, formerly QgsVisibilityPresetCollection
*/
QgsMapThemeCollection* mapThemeCollection();

@@ -363,7 +366,8 @@ class QgsProject : QObject
*
* @note Added in QGIS 2.16
*/
void setAutoTransaction(bool autoTransaction );
void setAutoTransaction( bool autoTransaction );

/**
* Should default values be evaluated on provider side when requested and not when committed.
*
@@ -383,10 +387,15 @@ class QgsProject : QObject

/**
* The snapping configuration for this project.
*
* @note Added in QGIS 3.0
*/
QgsSnappingConfig snappingConfig() const;

/**
* The snapping configuration for this project.
*
* @note Added in QGIS 3.0
*/
void setSnappingConfig( const QgsSnappingConfig& snappingConfig );

@@ -402,14 +411,27 @@ class QgsProject : QObject
*
* @note Added in QGIS 3.0
*/
void setAvoidIntersectionsList(const QStringList& avoidIntersectionsList);
void setAvoidIntersectionsList( const QStringList& avoidIntersectionsList );

/**
* A map of custom project variables.
* To get all available variables including generated ones
* use QgsExpressionContextUtils::projectScope() instead.
*/
QgsStringMap variables() const;

/**
* A map of custom project variables.
* Be careful not to set generated variables.
*/
void setVariables( const QgsStringMap& variables );

signals:
//! emitted when project is being read
void readProject( const QDomDocument & );
void readProject( const QDomDocument& );

//! emitted when project is being written
void writeProject( QDomDocument & );
void writeProject( QDomDocument& );

/**
* Emitted, after the basic initialization of a layer from the project
@@ -419,7 +441,7 @@ class QgsProject : QObject
* @param mapLayer The map layer which is being initialized
* @param layerNode The layer node from the project file
*/
void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
void readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerNode );

/**
* Emitted, when a layer is being saved. You can use this method to save
@@ -437,9 +459,11 @@ class QgsProject : QObject
//! emitted when an old project file is read.
void oldProjectVersionWarning( const QString& );

//! emitted when a layer from a projects was read
// @param i current layer
// @param n number of layers
/**
* Emitted when a layer from a projects was read.
* @param i current layer
* @param n number of layers
*/
void layerLoaded( int i, int n );

void loadingLayer( const QString& );
@@ -493,41 +517,12 @@ class QgsProject : QObject
*/
void setDirty( bool b = true );

/** Causes the project to emit the variablesChanged() signal. This should
* be called whenever expression variables related to the project are changed.
* @see variablesChanged()
* @note added in QGIS 3.0
*/
void emitVariablesChanged();

private:

QgsProject(); // private 'cause it's a singleton

}; // QgsProject


/** Interface for classes that handle missing layer files when reading project file. */
class QgsProjectBadLayerHandler
{
%TypeHeaderCode
#include <qgsproject.h>
%End

public:
virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom ) = 0;
virtual ~QgsProjectBadLayerHandler();
};


/** Default bad layer handler which ignores any missing layers. */
class QgsProjectBadLayerDefaultHandler : QgsProjectBadLayerHandler
{
%TypeHeaderCode
#include <qgsproject.h>
%End

public:
virtual void handleBadLayers( const QList<QDomNode>& layers, const QDomDocument& projectDom );
/**
* Create a new QgsProject.
* Private since it's (still) a singleton.
* You want to use QgsProject::instance() instead.
*/
explicit QgsProject( QObject* parent = nullptr );

};
@@ -0,0 +1,104 @@

/***************************************************************************
qgsprojectbadlayerhandler.sip - QgsProjectBadLayerHandler

---------------------
begin : 22.10.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/** Interface for classes that handle missing layer files when reading project file. */
class QgsProjectBadLayerHandler
{
%TypeHeaderCode
#include <qgsprojectbadlayerhandler.h>
%End

public:
/**
* This method will be called whenever the project tries to load layers which
* cannot be accessed. It should inform the user about this and if possible offer
* to fix the unavailable layers by setting a valid datasource, e.g. by showing a file
* dialog.
*
* The default implementation will dismiss all bad layers and write information to the
* log.
*
* @note Added in QGIS 3.0
* @see QgsProjectBadLayerGuiHandler
*/
virtual void handleBadLayers( const QList<QDomNode>& layers );
virtual ~QgsProjectBadLayerHandler();


protected:

//! file data representation
enum DataType
{
IS_VECTOR, //!< Vector data
IS_RASTER, //!< Raster data
IS_BOGUS //!< Bogus data
};

//! the flavors for data storage
enum ProviderType {
IS_FILE, //!< Saved in a file
IS_DATABASE, //!< Saved in a database
IS_URL, //!< Retrieved from a URL
IS_Unknown //!< Unknown type
};


/**
* Returns data type associated with the given QgsProject file Dom node
*
* The Dom node should represent the state associated with a specific layer.
*
* @note Added in QGIS 3.0
*/
DataType dataType( const QDomNode & layerNode );

/**
* Return the data source for the given layer
*
* The QDomNode is a QgsProject Dom node corresponding to a map layer state.
*
* Essentially dumps datasource tag.
*
* @note Added in QGIS 3.0
*/
QString dataSource( const QDomNode& layerNode );

/**
* Return the physical storage type associated with the given layer
*
* The QDomNode is a QgsProject Dom node corresponding to a map layer state.
*
* If the provider tag is "ogr", then it's a file type.
*
* However, if the layer is a raster, then there won't be a
* provider tag. It will always have an associated file.
*
* If the layer doesn't fall into either of the previous two categories, then
* it's either a database or URL. If the datasource tag has "url=", then it's
* URL based and if it has "dbname=">, then the layer data is in a database.
*
* @note Added in QGIS 3.0
*/
ProviderType providerType( const QDomNode& layerNode );

/**
* Set the datasource element to the new value
*
* @note Added in QGIS 3.0
*/
void setDataSource( QDomNode& layerNode, const QString& dataSource );
};
@@ -113,17 +113,18 @@ class QgsPropertyKey : QgsProperty
%End

public:
QgsPropertyKey( const QString name = "" );
virtual ~ QgsPropertyKey();
QgsPropertyKey( const QString& name = QString() );
virtual ~QgsPropertyKey();

/// every key has a name
// @{
// @note not available in python bindings
// QString name() const;

QString &name();
// @}
/**
* The name of the property is used as identifier.
*/
QString name() const;

/**
* The name of the property is used as identifier.
*/
void setName( const QString& name );

/** If this key has a value, it will be stored by its name in its
* properties

0 comments on commit 54b953f

Please sign in to comment.
You can’t perform that action at this time.