Skip to content

Commit

Permalink
Port a bunch of low-level methods to layouts
Browse files Browse the repository at this point in the history
Relating to expression contexts and variables
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent 15b65fa commit dd37037
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 27 deletions.
69 changes: 67 additions & 2 deletions python/core/layout/qgslayout.sip
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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

};


Expand Down
10 changes: 9 additions & 1 deletion python/core/layout/qgslayoutobject.sip
Expand Up @@ -9,7 +9,7 @@



class QgsLayoutObject: QObject
class QgsLayoutObject: QObject, QgsExpressionContextGenerator
{
%Docstring
A base class for objects which belong to a layout.
Expand Down Expand Up @@ -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:


Expand Down
31 changes: 31 additions & 0 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -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/;
Expand Down
46 changes: 44 additions & 2 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
70 changes: 68 additions & 2 deletions src/core/layout/qgslayout.h
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions src/core/layout/qgslayoutobject.cpp
Expand Up @@ -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();
}
}
9 changes: 8 additions & 1 deletion src/core/layout/qgslayoutobject.h
Expand Up @@ -21,6 +21,7 @@
#include "qgis_sip.h"
#include "qgspropertycollection.h"
#include "qgsobjectcustomproperties.h"
#include "qgsexpressioncontextgenerator.h"
#include <QObject>
#include <QDomNode>
#include <QMap>
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit dd37037

Please sign in to comment.