QGIS/python/core/qgsproject.sip

542 lines
18 KiB
Plaintext
Raw Normal View History

2014-01-27 09:22:24 +01:00
/** \ingroup core
* Reads and writes project states.
@note
Has two general kinds of state to make persistent. (I.e., to read and
write.) First, QGIS proprietary information. Second plug-in information.
A singleton since there shall only be one active project at a time; and
provides canonical location for plug-ins and main app to find/set
properties.
*/
class QgsProject : QObject
{
%TypeHeaderCode
#include <qgsproject.h>
%End
public:
2016-10-21 13:31:42 +02:00
//! Returns the QgsProject singleton instance
static QgsProject* instance();
~QgsProject();
/** Sets the project's title.
* @param title new title
2016-10-21 13:31:42 +02:00
* @note added in 2.4
* @see title()
2016-02-14 03:50:23 +01:00
*/
2014-05-27 23:22:50 +02:00
void setTitle( const QString& title );
/** Returns the project's title.
* @see setTitle()
*/
QString title() const;
/**
* Returns true if the project has been modified since the last write()
*/
bool isDirty() const;
/** Sets the file name associated with the project. This is the file which contains the project's XML
* representation.
* @param name project file name
* @see fileName()
*/
void setFileName( const QString& name );
/** Returns the project's file name. This is the file which contains the project's XML
* representation.
* @see setFileName()
* @see fileInfo()
*/
QString fileName() const;
New framework for context based expressions This commit adds the ability for expressions to be evaluated against specific contexts. It replaces the previous behaviour where expressions were evaluated against a specific feature and could utilise fragile global "special columns". Now, expressions are instead evaluated using a context designed for each individual expression. This is done via QgsExpressionContext and QgsExpressionContextScope objects. A QgsExpressionContextScope encapsulates the variables and functions relating to a specific context. For instance, scopes can be created for "global" variables (such as QGIS version, platform, and user-set variables specified within the QGIS options dialog. Think things like user name, work department, etc), or for "project" variables (eg project path, title, filename, and user-set variables set through the project properties dialog. Project version, reference number, that kind of thing). Many more scopes are planned, including map layer scopes (variables for layer name, id, user-set variables through the layer properties dialog), composer scopes, etc... QgsExpressionContextScopes are 'stacked' into a QgsExpressionContext object. Scopes added later to a QgsExpressionContext will override any variables or functions provided by earlier scopes, so for instance a user could override their global 'author' variable set within QGIS options with a different 'author' set via the project properties dialog. The intended use is that a QgsExpressionContext is created before a batch set of QgsExpression evaluations. Scopes are then added to the context based on what makes sense for that particular expression. Eg, almost all contexts will consist of the global scope and project scope, and then additional scopes as required. So a composer label would be evaluated against a context consisting of the global scope, project scope, composition scope and finally composer item scope. The batch set of expression evaluations would then be performed using this context, after which the context is discarded. In other words, a context is designed for use for one specific set of expression evaluations only.
2015-08-07 15:29:51 +10:00
/** Returns QFileInfo object for the project's associated file.
* @see fileName()
New framework for context based expressions This commit adds the ability for expressions to be evaluated against specific contexts. It replaces the previous behaviour where expressions were evaluated against a specific feature and could utilise fragile global "special columns". Now, expressions are instead evaluated using a context designed for each individual expression. This is done via QgsExpressionContext and QgsExpressionContextScope objects. A QgsExpressionContextScope encapsulates the variables and functions relating to a specific context. For instance, scopes can be created for "global" variables (such as QGIS version, platform, and user-set variables specified within the QGIS options dialog. Think things like user name, work department, etc), or for "project" variables (eg project path, title, filename, and user-set variables set through the project properties dialog. Project version, reference number, that kind of thing). Many more scopes are planned, including map layer scopes (variables for layer name, id, user-set variables through the layer properties dialog), composer scopes, etc... QgsExpressionContextScopes are 'stacked' into a QgsExpressionContext object. Scopes added later to a QgsExpressionContext will override any variables or functions provided by earlier scopes, so for instance a user could override their global 'author' variable set within QGIS options with a different 'author' set via the project properties dialog. The intended use is that a QgsExpressionContext is created before a batch set of QgsExpression evaluations. Scopes are then added to the context based on what makes sense for that particular expression. Eg, almost all contexts will consist of the global scope and project scope, and then additional scopes as required. So a composer label would be evaluated against a context consisting of the global scope, project scope, composition scope and finally composer item scope. The batch set of expression evaluations would then be performed using this context, after which the context is discarded. In other words, a context is designed for use for one specific set of expression evaluations only.
2015-08-07 15:29:51 +10:00
* @note added in QGIS 2.9
*/
QFileInfo fileInfo() const;
/**
* Returns the project's native coordinate reference system.
* @note added in QGIS 2.18
* @see setCrs()
* @see ellipsoid()
*/
QgsCoordinateReferenceSystem crs() const;
/**
* Sets the project's native coordinate reference system.
* @note added in QGIS 2.18
* @see crs()
* @see setEllipsoid()
*/
void setCrs( const QgsCoordinateReferenceSystem& crs );
/**
* Returns a proj string representing the project's ellipsoid setting, eg "WGS84".
* @see setEllipsoid()
* @see crs()
* @note added in QGIS 2.18
*/
QString ellipsoid() const;
/**
* Sets the project's ellipsoid from a proj string representation, eg "WGS84".
* @see ellipsoid()
* @see setCrs()
* @note added in QGIS 2.18
*/
void setEllipsoid( const QString& ellipsoid );
/** Clear the project - removes all settings and resets it back to an empty, default state.
2014-05-27 23:22:50 +02:00
* @note added in 2.4
*/
void clear();
/** Reads a project file.
* @param file name of project file to read
2016-02-14 03:50:23 +01:00
* @note Any current plug-in state is erased
* @note Calling read() performs the following operations:
2016-02-14 03:50:23 +01:00
*
* - Gets the extents
* - Creates maplayers
* - Registers maplayers
2016-02-14 03:50:23 +01:00
*
* @note it's presumed that the caller has already reset the map canvas, map registry, and legend
*/
bool read( const QFileInfo& file );
/** Reads the current project file.
* @note Any current plug-in state is erased
* @note Calling read() performs the following operations:
2016-02-14 03:50:23 +01:00
*
* - Gets the extents
* - Creates maplayers
* - Registers maplayers
*
* @note it's presumed that the caller has already reset the map canvas, map registry, and legend
*/
bool read();
/** Reads the layer described in the associated DOM node.
2016-02-14 03:50:23 +01:00
*
* @param layerNode represents a QgsProject DOM node that encodes a specific layer.
2016-02-14 03:50:23 +01:00
*
* QgsProject raises an exception when one of the QgsProject::read()
* implementations fails. Since the read()s are invoked from qgisapp,
* then qgisapp handles the exception. It prompts the user for the new
* location of the data, if any. If there is a new location, the DOM
2016-02-14 03:50:23 +01:00
* node associated with the layer has its datasource tag corrected.
* Then that node is passed to this member function to be re-opened.
*
*/
bool read( QDomNode& layerNode );
/** Writes the project to a file.
* @param file destination file
* @note calling this implicitly sets the project's filename (see setFileName() )
* @note isDirty() will be set to false if project is successfully written
* @returns true if project was written successfully
*/
bool write( const QFileInfo& file );
/** Writes the project to its current associated file (see fileName() ).
* @note isDirty() will be set to false if project is successfully written
* @returns true if project was written successfully
*/
bool write();
2016-08-19 16:51:18 +02:00
/**
* Write a boolean entry to the project file.
2016-02-14 03:50:23 +01:00
*
2016-08-19 16:51:18 +02:00
* Keys are '/'-delimited entries, implying
2016-02-14 03:50:23 +01:00
* a hierarchy of keys and corresponding values
*
* @note The key string must be valid xml tag names in order to be saved to the file.
2016-08-19 16:51:18 +02:00
* @note available in python bindings as writeEntryBool
2016-02-14 03:50:23 +01:00
*/
2016-10-21 13:31:42 +02:00
bool writeEntry( const QString& scope, const QString& key, bool value ) /PyName=writeEntryBool/;
2016-08-19 16:51:18 +02:00
/**
* Write a double entry to the project file.
*
* Keys are '/'-delimited entries, implying
* a hierarchy of keys and corresponding values
*
* @note The key string must be valid xml tag names in order to be saved to the file.
* @note available in python bindings as writeEntryDouble
*/
bool writeEntry( const QString& scope, const QString& key, double value ) /PyName=writeEntryDouble/;
/**
* Write an integer entry to the project file.
*
* Keys are '/'-delimited entries, implying
* a hierarchy of keys and corresponding values
*
* @note The key string must be valid xml tag names in order to be saved to the file.
*/
bool writeEntry( const QString& scope, const QString& key, int value );
/**
* Write a string entry to the project file.
*
* Keys are '/'-delimited entries, implying
* a hierarchy of keys and corresponding values
*
* @note The key string must be valid xml tag names in order to be saved to the file.
*/
bool writeEntry( const QString& scope, const QString& key, const QString& value );
/**
* Write a string list entry to the project file.
*
* Keys are '/'-delimited entries, implying
* a hierarchy of keys and corresponding values
*
* @note The key string must be valid xml tag names in order to be saved to the file.
*/
bool writeEntry( const QString& scope, const QString& key, const QStringList& value );
2016-10-21 13:31:42 +02:00
/**
* Key value accessors
2016-02-14 03:50:23 +01:00
*
* keys would be the familiar QSettings-like '/' delimited entries,
* implying a hierarchy of keys and corresponding values
*/
2016-10-21 13:31:42 +02:00
QStringList readListEntry( const QString& scope, const QString& key, const QStringList& def = QStringList(), bool *ok = 0 ) const;
2016-10-21 13:31:42 +02:00
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;
2015-07-29 11:52:14 +02:00
/** Remove the given key */
2016-10-21 13:31:42 +02:00
bool removeEntry( const QString& scope, const QString& key );
2015-07-29 11:52:14 +02:00
/** Return keys with values -- do not return keys that contain other keys
2016-02-14 03:50:23 +01:00
*
* @note equivalent to QSettings entryList()
*/
2016-10-21 13:31:42 +02:00
QStringList entryList( const QString& scope, const QString& key ) const;
2015-07-29 11:52:14 +02:00
/** Return keys with keys -- do not return keys that contain only values
2016-02-14 03:50:23 +01:00
*
* @note equivalent to QSettings subkeyList()
*/
2016-10-21 13:31:42 +02:00
QStringList subkeyList( const QString& scope, const QString& key ) const;
2015-07-29 11:52:14 +02:00
/** Dump out current project properties to stderr
2016-02-14 03:50:23 +01:00
*
* @todo XXX Now slightly broken since re-factoring. Won't print out top-level key
* and redundantly prints sub-keys.
*/
void dumpProperties() const;
2016-10-21 13:31:42 +02:00
/**
* 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;
2015-07-29 11:52:14 +02:00
/** Turn filename read from the project file to an absolute path */
QString readPath( QString filename ) const;
2014-11-21 01:17:21 +01:00
/** Return error message from previous read/write */
QString error() const;
/** Change handler for missing layers.
2016-02-14 03:50:23 +01:00
* Deletes old handler and takes ownership of the new one.
*/
2012-09-26 18:46:52 +02:00
void setBadLayerHandler( QgsProjectBadLayerHandler* handler /Transfer/ );
2014-01-27 09:22:24 +01:00
/** Returns project file path if layer is embedded from other project file. Returns empty string if layer is not embedded*/
QString layerIsEmbedded( const QString& id ) const;
2014-01-27 09:22:24 +01:00
/** Creates a maplayer instance defined in an arbitrary project file. Caller takes ownership
2016-02-14 03:50:23 +01:00
* @return the layer or 0 in case of error
* @note not available in Python bindings
*/
/*
bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
*/
2014-05-27 23:22:50 +02:00
/** Create layer group instance defined in an arbitrary project file.
* @note: added in version 2.4
*/
QgsLayerTreeGroup* createEmbeddedGroup( const QString& groupName, const QString& projectFilePath, const QStringList &invisibleLayers );
2014-11-21 01:17:21 +01:00
/** Convenience function to set topological editing */
void setTopologicalEditing( bool enabled );
2012-06-20 16:18:05 +02:00
2014-11-21 01:17:21 +01:00
/** Convenience function to query topological editing status */
bool topologicalEditing() const;
/** Convenience function to query default distance measurement units for project.
* @note added in QGIS 2.14
* @see setDistanceUnits()
* @see areaUnits()
*/
QgsUnitTypes::DistanceUnit distanceUnits() const;
/**
* Sets the default distance measurement units for the project.
* @note added in QGIS 2.18
* @see distanceUnits()
* @see setAreaUnits()
*/
void setDistanceUnits( QgsUnitTypes::DistanceUnit unit );
/** Convenience function to query default area measurement units for project.
* @note added in QGIS 2.14
* @see distanceUnits()
*/
QgsUnitTypes::AreaUnit areaUnits() const;
/**
* Sets the default area measurement units for the project.
* @note added in QGIS 2.18
* @see areaUnits()
* @see setDistanceUnits()
*/
void setAreaUnits( QgsUnitTypes::AreaUnit unit );
/** Return project's home path
2014-11-21 01:17:21 +01:00
@return home path of project (or QString::null if not set) */
QString homePath() const;
2014-01-27 09:22:24 +01:00
QgsRelationManager* relationManager() const;
2014-05-27 23:22:50 +02:00
/** Return pointer to the root (invisible) node of the project's layer tree
* @note added in 2.4
*/
QgsLayerTreeGroup* layerTreeRoot() const;
2014-05-27 23:22:50 +02:00
/** Return pointer to the helper class that synchronizes map layer registry with layer tree
* @note added in 2.4
2014-05-27 23:22:50 +02:00
*/
QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const;
2014-05-27 23:22:50 +02:00
2016-10-21 13:31:42 +02:00
/** Returns pointer to the project's map theme collection.
* @note added in QGIS 2.12
2016-10-21 13:31:42 +02:00
* @note renamed in QGIS 3.0, formerly QgsVisibilityPresetCollection
*/
QgsMapThemeCollection* mapThemeCollection();
/**
* Set a list of layers which should not be taken into account on map identification
*/
void setNonIdentifiableLayers( QList<QgsMapLayer*> layers );
/**
* Set a list of layers which should not be taken into account on map identification
*/
void setNonIdentifiableLayers( const QStringList& layerIds );
/**
* Get the list of layers which currently should not be taken into account on map identification
*/
QStringList nonIdentifiableLayers() const;
/**
* Transactional editing means that on supported datasources (postgres databases) the edit state of
* all tables that originate from the same database are synchronized and executed in a server side
* transaction.
*
* @note Added in QGIS 2.16
*/
bool autoTransaction() const;
/**
* Transactional editing means that on supported datasources (postgres databases) the edit state of
* all tables that originate from the same database are synchronized and executed in a server side
* transaction.
*
* Make sure that this is only called when all layers are not in edit mode.
*
* @note Added in QGIS 2.16
*/
2016-10-21 13:31:42 +02:00
void setAutoTransaction( bool autoTransaction );
2016-06-03 15:13:42 +02:00
/**
* Should default values be evaluated on provider side when requested and not when committed.
*
* @note added in 2.16
*/
bool evaluateDefaultValues() const;
/**
* Defines if default values should be evaluated on provider side when requested and not when committed.
*
* @note added in 2.16
*/
void setEvaluateDefaultValues( bool evaluateDefaultValues );
QgsExpressionContext createExpressionContext() const;
/**
* The snapping configuration for this project.
2016-10-21 13:31:42 +02:00
*
* @note Added in QGIS 3.0
2016-02-14 03:50:23 +01:00
*/
QgsSnappingConfig snappingConfig() const;
2016-10-21 13:31:42 +02:00
/**
* The snapping configuration for this project.
2016-10-21 13:31:42 +02:00
*
* @note Added in QGIS 3.0
2016-02-14 03:50:23 +01:00
*/
void setSnappingConfig( const QgsSnappingConfig& snappingConfig );
2014-05-27 23:22:50 +02:00
/**
* A list of layers with which intersections should be avoided.
*
* @note Added in QGIS 3.0
*/
QStringList avoidIntersectionsList() const;
/**
* A list of layers with which intersections should be avoided.
*
* @note Added in QGIS 3.0
*/
2016-10-21 13:31:42 +02:00
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
2016-10-21 13:31:42 +02:00
void readProject( const QDomDocument& );
//! emitted when project is being written
2016-10-21 13:31:42 +02:00
void writeProject( QDomDocument& );
/**
* Emitted, after the basic initialization of a layer from the project
* file is done. You can use this signal to read additional information
* from the project file.
*
* @param mapLayer The map layer which is being initialized
* @param layerNode The layer node from the project file
*/
2016-10-21 13:31:42 +02:00
void readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerNode );
/**
* Emitted, when a layer is being saved. You can use this method to save
* additional information to the layer.
*
* @param mapLayer The map layer which is being initialized
* @param layerElem The layer element from the project file
* @param doc The document
*/
void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
//! emitted when the project file has been written and closed
void projectSaved();
//! emitted when an old project file is read.
void oldProjectVersionWarning( const QString& );
2016-10-21 13:31:42 +02:00
/**
* 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& );
//! Emitted when the list of layer which are excluded from map identification changes
void nonIdentifiableLayersChanged( QStringList nonIdentifiableLayers );
2016-07-11 08:42:27 +10:00
//! Emitted when the file name of the project changes
void fileNameChanged();
//! Emitted when the home path of the project changes
void homePathChanged();
//! emitted whenever the configuration for snapping has changed
void snappingConfigChanged();
/** Emitted whenever the expression variables stored in the project have been changed.
* @note added in QGIS 3.0
*/
void variablesChanged();
2016-10-18 15:43:28 +02:00
/**
* Emitted whenever a new transaction group has been created or a
* transaction group has been removed.
*
* @note Added in QGIS 3.0
*/
void transactionGroupsChanged();
/**
* Emitted when the topological editing flag has changed.
*
* @note Added in QGIS 3.0
*/
void topologicalEditingChanged();
/**
* Emitted whenever avoidIntersectionsList has changed.
*
* @note Added in QGIS 3.0
*/
void avoidIntersectionsListChanged();
2016-10-23 21:14:42 +02:00
/**
* Emitted when the map theme collection changes.
* This only happens when the map theme collection is reset.
* Any pointer previously received from mapThemeCollection()
* must no longer be used after this signal is emitted.
* You must still connect to signals from the map theme collection
* if you want to be notified about new map themes being added and
* map themes being removed.
*
* @note Added in QGIS 3.0
*/
void mapThemeCollectionChanged();
2016-10-18 15:43:28 +02:00
public slots:
/**
* Flag the project as dirty (modified). If this flag is set, the user will
* be asked to save changes to the project before closing the current project.
*
* @note added in 2.4
* @note promoted to public slot in 2.16
*/
void setDirty( bool b = true );
private:
2016-10-21 13:31:42 +02:00
/**
* Create a new QgsProject.
* Private since it's (still) a singleton.
* You want to use QgsProject::instance() instead.
*/
explicit QgsProject( QObject* parent = nullptr );
};