Add a context class for layouts

Stores information relating to the current context (such as
associated feature and layer) and rendering settings for a layout.
This commit is contained in:
Nyall Dawson 2017-07-07 13:53:14 +10:00
parent d70f53c405
commit b2b35dd084
7 changed files with 424 additions and 0 deletions

View File

@ -378,6 +378,7 @@
%Include gps/qgsgpsdetector.sip
%Include gps/qgsnmeaconnection.sip
%Include gps/qgsgpsdconnection.sip
%Include layout/qgslayoutcontext.sip
%Include layout/qgslayoutitem.sip
%Include layout/qgslayoutitemregistry.sip
%Include layout/qgslayoutobject.sip

View File

@ -0,0 +1,111 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayoutcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
class QgsLayoutContext
{
%Docstring
Stores information relating to the current context and rendering settings for a layout.
.. versionadded:: 3.0
%End
%TypeHeaderCode
#include "qgslayoutcontext.h"
%End
public:
enum Flag
{
FlagDebug,
FlagOutlineOnly,
FlagAntialiasing,
FlagUseAdvancedEffects,
};
typedef QFlags<QgsLayoutContext::Flag> Flags;
QgsLayoutContext();
void setFlags( const QgsLayoutContext::Flags flags );
%Docstring
Sets the combination of ``flags`` that will be used for rendering the layout.
.. seealso:: setFlag()
.. seealso:: flags()
.. seealso:: testFlag()
%End
void setFlag( const QgsLayoutContext::Flag flag, const bool on = true );
%Docstring
Enables or disables a particular rendering ``flag`` for the layout. Other existing
flags are not affected.
.. seealso:: setFlags()
.. seealso:: flags()
.. seealso:: testFlag()
%End
QgsLayoutContext::Flags flags() const;
%Docstring
Returns the current combination of flags used for rendering the layout.
.. seealso:: setFlags()
.. seealso:: setFlag()
.. seealso:: testFlag()
:rtype: QgsLayoutContext.Flags
%End
bool testFlag( const Flag flag ) const;
%Docstring
Check whether a particular rendering ``flag`` is enabled for the layout.
.. seealso:: setFlags()
.. seealso:: setFlag()
.. seealso:: flags()
:rtype: bool
%End
void setFeature( const QgsFeature &feature );
%Docstring
Sets the current ``feature`` for evaluating the layout. This feature may
be used for altering an item's content and appearance for a report
or atlas layout.
.. seealso:: feature()
%End
QgsFeature feature() const;
%Docstring
Returns the current feature for evaluating the layout. This feature may
be used for altering an item's content and appearance for a report
or atlas layout.
.. seealso:: setFeature()
:rtype: QgsFeature
%End
QgsVectorLayer *layer() const;
%Docstring
Returns the vector layer associated with the layout's context.
.. seealso:: setLayer()
:rtype: QgsVectorLayer
%End
void setLayer( QgsVectorLayer *layer );
%Docstring
Sets the vector ``layer`` associated with the layout's context.
.. seealso:: layer()
%End
};
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayoutcontext.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

View File

@ -350,6 +350,7 @@ SET(QGIS_CORE_SRCS
dxf/qgsdxfpallabeling.cpp
layout/qgslayout.cpp
layout/qgslayoutcontext.cpp
layout/qgslayoutitem.cpp
layout/qgslayoutitemregistry.cpp
layout/qgslayoutmeasurement.cpp
@ -671,6 +672,7 @@ SET(QGIS_CORE_MOC_HDRS
gps/qgsgpsdconnection.h
layout/qgslayout.h
layout/qgslayoutcontext.h
layout/qgslayoutitem.h
layout/qgslayoutitemregistry.h
layout/qgslayoutobject.h

View File

@ -0,0 +1,56 @@
/***************************************************************************
qgslayoutcontext.cpp
--------------------
begin : July 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "qgslayoutcontext.h"
#include "qgsfeature.h"
QgsLayoutContext::QgsLayoutContext()
: mFlags( FlagAntialiasing | FlagUseAdvancedEffects )
{}
void QgsLayoutContext::setFlags( const QgsLayoutContext::Flags flags )
{
mFlags = flags;
}
void QgsLayoutContext::setFlag( const QgsLayoutContext::Flag flag, const bool on )
{
if ( on )
mFlags |= flag;
else
mFlags &= ~flag;
}
QgsLayoutContext::Flags QgsLayoutContext::flags() const
{
return mFlags;
}
bool QgsLayoutContext::testFlag( const QgsLayoutContext::Flag flag ) const
{
return mFlags.testFlag( flag );
}
QgsVectorLayer *QgsLayoutContext::layer() const
{
return mLayer;
}
void QgsLayoutContext::setLayer( QgsVectorLayer *layer )
{
mLayer = layer;
}

View File

@ -0,0 +1,123 @@
/***************************************************************************
qgslayoutcontext.h
-------------------
begin : July 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef QGSLAYOUTCONTEXT_H
#define QGSLAYOUTCONTEXT_H
#include "qgis_core.h"
#include "qgsfeature.h"
#include "qgsvectorlayer.h"
#include <QtGlobal>
class QgsFeature;
class QgsVectorLayer;
/**
* \ingroup Layout
* \class QgsLayoutContext
* \brief Stores information relating to the current context and rendering settings for a layout.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsLayoutContext
{
public:
//! Flags for controlling how a layout is rendered
enum Flag
{
FlagDebug = 1 << 1, //!< Debug/testing mode, items are drawn as solid rectangles.
FlagOutlineOnly = 1 << 2, //!< Render items as outlines only.
FlagAntialiasing = 1 << 3, //!< Use antialiasing when drawing items.
FlagUseAdvancedEffects = 1 << 4, //!< Enable advanced effects such as blend modes.
};
Q_DECLARE_FLAGS( Flags, Flag )
QgsLayoutContext();
/**
* Sets the combination of \a flags that will be used for rendering the layout.
* \see setFlag()
* \see flags()
* \see testFlag()
*/
void setFlags( const QgsLayoutContext::Flags flags );
/**
* Enables or disables a particular rendering \a flag for the layout. Other existing
* flags are not affected.
* \see setFlags()
* \see flags()
* \see testFlag()
*/
void setFlag( const QgsLayoutContext::Flag flag, const bool on = true );
/**
* Returns the current combination of flags used for rendering the layout.
* \see setFlags()
* \see setFlag()
* \see testFlag()
*/
QgsLayoutContext::Flags flags() const;
/**
* Check whether a particular rendering \a flag is enabled for the layout.
* \see setFlags()
* \see setFlag()
* \see flags()
*/
bool testFlag( const Flag flag ) const;
/**
* Sets the current \a feature for evaluating the layout. This feature may
* be used for altering an item's content and appearance for a report
* or atlas layout.
* \see feature()
*/
void setFeature( const QgsFeature &feature ) { mFeature = feature; }
/**
* Returns the current feature for evaluating the layout. This feature may
* be used for altering an item's content and appearance for a report
* or atlas layout.
* \see setFeature()
*/
QgsFeature feature() const { return mFeature; }
/**
* Returns the vector layer associated with the layout's context.
* \see setLayer()
*/
QgsVectorLayer *layer() const;
/**
* Sets the vector \a layer associated with the layout's context.
* \see layer()
*/
void setLayer( QgsVectorLayer *layer );
private:
Flags mFlags = 0;
QgsFeature mFeature;
QPointer< QgsVectorLayer > mLayer;
};
#endif //QGSLAYOUTCONTEXT_H

View File

@ -127,6 +127,7 @@ SET(TESTS
testqgslabelingengine.cpp
testqgslayertree.cpp
testqgslayout.cpp
testqgslayoutcontext.cpp
testqgslayoutitem.cpp
testqgslayoutobject.cpp
testqgslayoutunits.cpp

View File

@ -0,0 +1,130 @@
/***************************************************************************
testqgslayoutcontext.cpp
------------------------
begin : November 2014
copyright : (C) 2014 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "qgslayoutcontext.h"
#include "qgis.h"
#include "qgsfeature.h"
#include "qgsvectorlayer.h"
#include <QObject>
#include "qgstest.h"
class TestQgsLayoutContext: public QObject
{
Q_OBJECT
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void creation(); //test creation of QgsLayout
void flags(); //test QgsLayout flags
void feature();
void layer();
private:
QString mReport;
};
void TestQgsLayoutContext::initTestCase()
{
mReport = "<h1>Layout Context Tests</h1>\n";
}
void TestQgsLayoutContext::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}
void TestQgsLayoutContext::init()
{
}
void TestQgsLayoutContext::cleanup()
{
}
void TestQgsLayoutContext::creation()
{
QgsLayoutContext *context = new QgsLayoutContext();
QVERIFY( context );
delete context;
}
void TestQgsLayoutContext::flags()
{
QgsLayoutContext context;
//test getting and setting flags
context.setFlags( QgsLayoutContext::Flags( QgsLayoutContext::FlagAntialiasing | QgsLayoutContext::FlagUseAdvancedEffects ) );
QVERIFY( context.flags() == ( QgsLayoutContext::FlagAntialiasing | QgsLayoutContext::FlagUseAdvancedEffects ) );
QVERIFY( context.testFlag( QgsLayoutContext::FlagAntialiasing ) );
QVERIFY( context.testFlag( QgsLayoutContext::FlagUseAdvancedEffects ) );
QVERIFY( ! context.testFlag( QgsLayoutContext::FlagDebug ) );
context.setFlag( QgsLayoutContext::FlagDebug );
QVERIFY( context.testFlag( QgsLayoutContext::FlagDebug ) );
context.setFlag( QgsLayoutContext::FlagDebug, false );
QVERIFY( ! context.testFlag( QgsLayoutContext::FlagDebug ) );
}
void TestQgsLayoutContext::feature()
{
QgsLayoutContext context;
//test removing feature
context.setFeature( QgsFeature() );
QVERIFY( !context.feature().isValid() );
//test setting/getting feature
QgsFeature testFeature;
testFeature.initAttributes( 1 );
testFeature.setAttribute( 0, "Test" );
context.setFeature( testFeature );
QCOMPARE( context.feature().attribute( 0 ), testFeature.attribute( 0 ) );
}
void TestQgsLayoutContext::layer()
{
QgsLayoutContext context;
//test clearing layer
context.setLayer( nullptr );
QVERIFY( !context.layer() );
//test setting/getting layer
QgsVectorLayer *layer = new QgsVectorLayer( "Point?field=id_a:integer", "A", "memory" );
context.setLayer( layer );
QCOMPARE( context.layer(), layer );
//clear layer
context.setLayer( nullptr );
QVERIFY( !context.layer() );
delete layer;
}
QGSTEST_MAIN( TestQgsLayoutContext )
#include "testqgslayoutcontext.moc"