Skip to content
Permalink
Browse files

Port a bunch of low-level methods to layouts

Relating to expression contexts and variables
  • Loading branch information
nyalldawson committed Jul 7, 2017
1 parent 15b65fa commit dd370373beb089096f3177165ceb79792656dd9c
@@ -7,7 +7,7 @@
************************************************************************/


class QgsLayout : QGraphicsScene
class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
{
%Docstring
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
@@ -24,7 +24,17 @@ class QgsLayout : QGraphicsScene
ZMapTool,
};

QgsLayout();
QgsLayout( QgsProject *project );
%Docstring
Construct a new layout linked to the specified ``project``.
%End

QgsProject *project() const;
%Docstring
The project associated with the layout. Used to get access to layers, map themes,
relations and various other bits. It is never null.
:rtype: QgsProject
%End

QString name() const;
%Docstring
@@ -117,6 +127,61 @@ class QgsLayout : QGraphicsScene
%End


virtual QgsExpressionContext createExpressionContext() const;

%Docstring
Creates an expression context relating to the layout's current state. The context includes
scopes for global, project, layout and layout context properties.
:rtype: QgsExpressionContext
%End

void setCustomProperty( const QString &key, const QVariant &value );
%Docstring
Set a custom property for the layout.
\param key property key. If a property with the same key already exists it will be overwritten.
\param value property value
.. seealso:: customProperty()
.. seealso:: removeCustomProperty()
.. seealso:: customProperties()
%End

QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
%Docstring
Read a custom property from the layout.
\param key property key
\param defaultValue default value to return if property with matching key does not exist
:return: value of matching property
.. seealso:: setCustomProperty()
.. seealso:: removeCustomProperty()
.. seealso:: customProperties()
:rtype: QVariant
%End

void removeCustomProperty( const QString &key );
%Docstring
Remove a custom property from the layout.
\param key property key
.. seealso:: setCustomProperty()
.. seealso:: customProperty()
.. seealso:: customProperties()
%End

QStringList customProperties() const;
%Docstring
Return list of keys stored in custom properties for the layout.
.. seealso:: setCustomProperty()
.. seealso:: customProperty()
.. seealso:: removeCustomProperty()
:rtype: list of str
%End

signals:

void variablesChanged();
%Docstring
Emitted whenever the expression variables stored in the layout have been changed.
%End

};


@@ -9,7 +9,7 @@



class QgsLayoutObject: QObject
class QgsLayoutObject: QObject, QgsExpressionContextGenerator
{
%Docstring
A base class for objects which belong to a layout.
@@ -151,6 +151,14 @@ class QgsLayoutObject: QObject
:rtype: list of str
%End

virtual QgsExpressionContext createExpressionContext() const;

%Docstring
Creates an expression context relating to the objects' current state. The context includes
scopes for global, project and layout properties.
:rtype: QgsExpressionContext
%End

protected:


@@ -854,6 +854,37 @@ class QgsExpressionContextUtils
\param variables new set of layer variables
.. seealso:: setCompositionVariable()
.. seealso:: compositionScope()
%End

static QgsExpressionContextScope *layoutScope( const QgsLayout *layout ) /Factory/;
%Docstring
Creates a new scope which contains variables and functions relating to a QgsLayout ``layout``.
For instance, number of pages and page sizes.
.. versionadded:: 3.0
:rtype: QgsExpressionContextScope
%End

static void setLayoutVariable( QgsLayout *layout, const QString &name, const QVariant &value );
%Docstring
Sets a layout context variable. This variable will be contained within scopes retrieved via
layoutScope().
\param layout target layout
\param name variable name
\param value variable value
.. seealso:: setLayoutVariables()
.. seealso:: layoutScope()
.. versionadded:: 3.0
%End

static void setLayoutVariables( QgsLayout *layout, const QVariantMap &variables );
%Docstring
Sets all layout context variables. Existing layout variables will be removed and replaced
with the variables specified.
\param layout target layout
\param variables new set of layer variables
.. seealso:: setLayoutVariable()
.. seealso:: layoutScope()
.. versionadded:: 3.0
%End

static QgsExpressionContextScope *atlasScope( const QgsAtlasComposition *atlas ) /Factory/;
@@ -16,10 +16,14 @@

#include "qgslayout.h"

QgsLayout::QgsLayout()
QgsLayout::QgsLayout( QgsProject *project )
: QGraphicsScene()
{
, mProject( project )
{}

QgsProject *QgsLayout::project() const
{
return mProject;
}

double QgsLayout::convertToLayoutUnits( const QgsLayoutMeasurement &measurement ) const
@@ -51,3 +55,41 @@ QgsLayoutPoint QgsLayout::convertFromLayoutUnits( const QPointF &point, const Qg
{
return mContext.measurementConverter().convert( QgsLayoutPoint( point.x(), point.y(), mUnits ), unit );
}

QgsExpressionContext QgsLayout::createExpressionContext() const
{
QgsExpressionContext context = QgsExpressionContext();
context.appendScope( QgsExpressionContextUtils::globalScope() );
context.appendScope( QgsExpressionContextUtils::projectScope( mProject ) );
context.appendScope( QgsExpressionContextUtils::layoutScope( this ) );
#if 0 //TODO
if ( mAtlasComposition.enabled() )
{
context.appendScope( QgsExpressionContextUtils::atlasScope( &mAtlasComposition ) );
}
#endif
return context;
}

void QgsLayout::setCustomProperty( const QString &key, const QVariant &value )
{
mCustomProperties.setValue( key, value );

if ( key.startsWith( QLatin1String( "variable" ) ) )
emit variablesChanged();
}

QVariant QgsLayout::customProperty( const QString &key, const QVariant &defaultValue ) const
{
return mCustomProperties.value( key, defaultValue );
}

void QgsLayout::removeCustomProperty( const QString &key )
{
mCustomProperties.remove( key );
}

QStringList QgsLayout::customProperties() const
{
return mCustomProperties.keys();
}
@@ -19,14 +19,15 @@
#include "qgis_core.h"
#include <QGraphicsScene>
#include "qgslayoutcontext.h"
#include "qgsexpressioncontextgenerator.h"

/**
* \ingroup core
* \class QgsLayout
* \brief Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsLayout : public QGraphicsScene
class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContextGenerator
{
Q_OBJECT

@@ -38,7 +39,17 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene
ZMapTool = 10000, //!< Z-Value for temporary map tool items
};

QgsLayout();
/**
* Construct a new layout linked to the specified \a project.
*/
QgsLayout( QgsProject *project );

/**
* The project associated with the layout. Used to get access to layers, map themes,
* relations and various other bits. It is never null.
*
*/
QgsProject *project() const;

/**
* Returns the layout's name.
@@ -127,10 +138,65 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene
*/
SIP_SKIP const QgsLayoutContext &context() const { return mContext; }

/**
* Creates an expression context relating to the layout's current state. The context includes
* scopes for global, project, layout and layout context properties.
*/
QgsExpressionContext createExpressionContext() const override;

/**
* Set a custom property for the layout.
* \param key property key. If a property with the same key already exists it will be overwritten.
* \param value property value
* \see customProperty()
* \see removeCustomProperty()
* \see customProperties()
*/
void setCustomProperty( const QString &key, const QVariant &value );

/**
* Read a custom property from the layout.
* \param key property key
* \param defaultValue default value to return if property with matching key does not exist
* \returns value of matching property
* \see setCustomProperty()
* \see removeCustomProperty()
* \see customProperties()
*/
QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;

/**
* Remove a custom property from the layout.
* \param key property key
* \see setCustomProperty()
* \see customProperty()
* \see customProperties()
*/
void removeCustomProperty( const QString &key );

/**
* Return list of keys stored in custom properties for the layout.
* \see setCustomProperty()
* \see customProperty()
* \see removeCustomProperty()
*/
QStringList customProperties() const;

signals:

/**
* Emitted whenever the expression variables stored in the layout have been changed.
*/
void variablesChanged();

private:

QgsProject *mProject = nullptr;

QString mName;

QgsObjectCustomProperties mCustomProperties;

QgsUnitTypes::LayoutUnit mUnits = QgsUnitTypes::LayoutMillimeters;
QgsLayoutContext mContext;

@@ -110,3 +110,15 @@ QStringList QgsLayoutObject::customProperties() const
{
return mCustomProperties.keys();
}

QgsExpressionContext QgsLayoutObject::createExpressionContext() const
{
if ( mLayout )
{
return mLayout->createExpressionContext();
}
else
{
return QgsExpressionContext() << QgsExpressionContextUtils::globalScope();
}
}
@@ -21,6 +21,7 @@
#include "qgis_sip.h"
#include "qgspropertycollection.h"
#include "qgsobjectcustomproperties.h"
#include "qgsexpressioncontextgenerator.h"
#include <QObject>
#include <QDomNode>
#include <QMap>
@@ -33,7 +34,7 @@ class QPainter;
* A base class for objects which belong to a layout.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsLayoutObject: public QObject
class CORE_EXPORT QgsLayoutObject: public QObject, public QgsExpressionContextGenerator
{
Q_OBJECT
public:
@@ -172,6 +173,12 @@ class CORE_EXPORT QgsLayoutObject: public QObject
*/
QStringList customProperties() const;

/**
* Creates an expression context relating to the objects' current state. The context includes
* scopes for global, project and layout properties.
*/
QgsExpressionContext createExpressionContext() const override;

protected:

QgsLayout *mLayout = nullptr;

0 comments on commit dd37037

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