Skip to content

Commit

Permalink
Code shuffle, to make QgsLayoutContext aware of parent QgsLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 4a7813b commit 92003c8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
5 changes: 4 additions & 1 deletion python/core/layout/qgslayoutcontext.sip
Expand Up @@ -32,7 +32,10 @@ class QgsLayoutContext : QObject
typedef QFlags<QgsLayoutContext::Flag> Flags;


QgsLayoutContext();
QgsLayoutContext( QgsLayout *layout /TransferThis/ );
%Docstring
Constructor for QgsLayoutContext.
%End

void setFlags( const QgsLayoutContext::Flags flags );
%Docstring
Expand Down
27 changes: 19 additions & 8 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -30,6 +30,7 @@

QgsLayout::QgsLayout( QgsProject *project )
: mProject( project )
, mContext( new QgsLayoutContext( this ) )
, mSnapper( QgsLayoutSnapper( this ) )
, mGridSettings( this )
, mPageCollection( new QgsLayoutPageCollection( this ) )
Expand Down Expand Up @@ -281,32 +282,42 @@ QgsLayoutItem *QgsLayout::layoutItemAt( QPointF position, const QgsLayoutItem *b

double QgsLayout::convertToLayoutUnits( const QgsLayoutMeasurement &measurement ) const
{
return mContext.measurementConverter().convert( measurement, mUnits ).length();
return mContext->measurementConverter().convert( measurement, mUnits ).length();
}

QSizeF QgsLayout::convertToLayoutUnits( const QgsLayoutSize &size ) const
{
return mContext.measurementConverter().convert( size, mUnits ).toQSizeF();
return mContext->measurementConverter().convert( size, mUnits ).toQSizeF();
}

QPointF QgsLayout::convertToLayoutUnits( const QgsLayoutPoint &point ) const
{
return mContext.measurementConverter().convert( point, mUnits ).toQPointF();
return mContext->measurementConverter().convert( point, mUnits ).toQPointF();
}

QgsLayoutMeasurement QgsLayout::convertFromLayoutUnits( const double length, const QgsUnitTypes::LayoutUnit unit ) const
{
return mContext.measurementConverter().convert( QgsLayoutMeasurement( length, mUnits ), unit );
return mContext->measurementConverter().convert( QgsLayoutMeasurement( length, mUnits ), unit );
}

QgsLayoutSize QgsLayout::convertFromLayoutUnits( const QSizeF &size, const QgsUnitTypes::LayoutUnit unit ) const
{
return mContext.measurementConverter().convert( QgsLayoutSize( size.width(), size.height(), mUnits ), unit );
return mContext->measurementConverter().convert( QgsLayoutSize( size.width(), size.height(), mUnits ), unit );
}

QgsLayoutPoint QgsLayout::convertFromLayoutUnits( const QPointF &point, const QgsUnitTypes::LayoutUnit unit ) const
{
return mContext.measurementConverter().convert( QgsLayoutPoint( point.x(), point.y(), mUnits ), unit );
return mContext->measurementConverter().convert( QgsLayoutPoint( point.x(), point.y(), mUnits ), unit );
}

QgsLayoutContext &QgsLayout::context()
{
return *mContext;
}

const QgsLayoutContext &QgsLayout::context() const
{
return *mContext;
}

QgsLayoutGuideCollection &QgsLayout::guides()
Expand Down Expand Up @@ -709,7 +720,7 @@ void QgsLayout::writeXmlLayoutSettings( QDomElement &element, QDomDocument &docu
element.setAttribute( QStringLiteral( "name" ), mName );
element.setAttribute( QStringLiteral( "units" ), QgsUnitTypes::encodeUnit( mUnits ) );
element.setAttribute( QStringLiteral( "worldFileMap" ), mWorldFileMapId );
element.setAttribute( QStringLiteral( "printResolution" ), mContext.dpi() );
element.setAttribute( QStringLiteral( "printResolution" ), mContext->dpi() );
}

QDomElement QgsLayout::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
Expand Down Expand Up @@ -753,7 +764,7 @@ bool QgsLayout::readXmlLayoutSettings( const QDomElement &layoutElement, const Q
setName( layoutElement.attribute( QStringLiteral( "name" ) ) );
setUnits( QgsUnitTypes::decodeLayoutUnit( layoutElement.attribute( QStringLiteral( "units" ) ) ) );
mWorldFileMapId = layoutElement.attribute( QStringLiteral( "worldFileMap" ) );
mContext.setDpi( layoutElement.attribute( QStringLiteral( "printResolution" ), "300" ).toDouble() );
mContext->setDpi( layoutElement.attribute( QStringLiteral( "printResolution" ), "300" ).toDouble() );
emit changed();

return true;
Expand Down
8 changes: 4 additions & 4 deletions src/core/layout/qgslayout.h
Expand Up @@ -18,7 +18,6 @@

#include "qgis_core.h"
#include <QGraphicsScene>
#include "qgslayoutcontext.h"
#include "qgslayoutsnapper.h"
#include "qgsexpressioncontextgenerator.h"
#include "qgslayoutgridsettings.h"
Expand All @@ -30,6 +29,7 @@ class QgsLayoutModel;
class QgsLayoutMultiFrame;
class QgsLayoutPageCollection;
class QgsLayoutUndoStack;
class QgsLayoutContext;

/**
* \ingroup core
Expand Down Expand Up @@ -318,13 +318,13 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
* Returns a reference to the layout's context, which stores information relating to the
* current context and rendering settings for the layout.
*/
QgsLayoutContext &context() { return mContext; }
QgsLayoutContext &context();

/**
* Returns a reference to the layout's context, which stores information relating to the
* current context and rendering settings for the layout.
*/
SIP_SKIP const QgsLayoutContext &context() const { return mContext; }
SIP_SKIP const QgsLayoutContext &context() const;

/**
* Returns a reference to the layout's snapper, which stores handles layout snap grids and lines
Expand Down Expand Up @@ -629,7 +629,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
QgsObjectCustomProperties mCustomProperties;

QgsUnitTypes::LayoutUnit mUnits = QgsUnitTypes::LayoutMillimeters;
QgsLayoutContext mContext;
QgsLayoutContext *mContext = nullptr;
QgsLayoutSnapper mSnapper;
QgsLayoutGridSettings mGridSettings;

Expand Down
8 changes: 5 additions & 3 deletions src/core/layout/qgslayoutcontext.cpp
Expand Up @@ -16,10 +16,12 @@

#include "qgslayoutcontext.h"
#include "qgsfeature.h"
#include "qgslayout.h"


QgsLayoutContext::QgsLayoutContext()
: mFlags( FlagAntialiasing | FlagUseAdvancedEffects )
QgsLayoutContext::QgsLayoutContext( QgsLayout *layout )
: QObject( layout )
, mFlags( FlagAntialiasing | FlagUseAdvancedEffects )
, mLayout( layout )
{}

void QgsLayoutContext::setFlags( const QgsLayoutContext::Flags flags )
Expand Down
7 changes: 6 additions & 1 deletion src/core/layout/qgslayoutcontext.h
Expand Up @@ -49,7 +49,10 @@ class CORE_EXPORT QgsLayoutContext : public QObject
};
Q_DECLARE_FLAGS( Flags, Flag )

QgsLayoutContext();
/**
* Constructor for QgsLayoutContext.
*/
QgsLayoutContext( QgsLayout *layout SIP_TRANSFERTHIS );

/**
* Sets the combination of \a flags that will be used for rendering the layout.
Expand Down Expand Up @@ -245,6 +248,8 @@ class CORE_EXPORT QgsLayoutContext : public QObject

Flags mFlags = nullptr;

QgsLayout *mLayout = nullptr;

int mCurrentExportLayer = -1;

QgsFeature mFeature;
Expand Down
16 changes: 8 additions & 8 deletions tests/src/core/testqgslayoutcontext.cpp
Expand Up @@ -75,14 +75,14 @@ void TestQgsLayoutContext::cleanup()

void TestQgsLayoutContext::creation()
{
QgsLayoutContext *context = new QgsLayoutContext();
QgsLayoutContext *context = new QgsLayoutContext( nullptr );
QVERIFY( context );
delete context;
}

void TestQgsLayoutContext::flags()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );
QSignalSpy spyFlagsChanged( &context, &QgsLayoutContext::flagsChanged );

//test getting and setting flags
Expand All @@ -108,7 +108,7 @@ void TestQgsLayoutContext::flags()

void TestQgsLayoutContext::feature()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );

//test removing feature
context.setFeature( QgsFeature() );
Expand All @@ -124,7 +124,7 @@ void TestQgsLayoutContext::feature()

void TestQgsLayoutContext::layer()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );

//test clearing layer
context.setLayer( nullptr );
Expand All @@ -144,7 +144,7 @@ void TestQgsLayoutContext::layer()

void TestQgsLayoutContext::dpi()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );

QSignalSpy spyDpiChanged( &context, &QgsLayoutContext::dpiChanged );
context.setDpi( 600 );
Expand All @@ -160,7 +160,7 @@ void TestQgsLayoutContext::dpi()

void TestQgsLayoutContext::renderContextFlags()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );
context.setFlags( 0 );
QgsRenderContext::Flags flags = context.renderContextFlags();
QVERIFY( !( flags & QgsRenderContext::Antialiasing ) );
Expand All @@ -182,7 +182,7 @@ void TestQgsLayoutContext::renderContextFlags()

void TestQgsLayoutContext::boundingBoxes()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );
context.setBoundingBoxesVisible( false );
QVERIFY( !context.boundingBoxesVisible() );
context.setBoundingBoxesVisible( true );
Expand All @@ -191,7 +191,7 @@ void TestQgsLayoutContext::boundingBoxes()

void TestQgsLayoutContext::exportLayer()
{
QgsLayoutContext context;
QgsLayoutContext context( nullptr );
// must default to -1
QCOMPARE( context.currentExportLayer(), -1 );
context.setCurrentExportLayer( 1 );
Expand Down

0 comments on commit 92003c8

Please sign in to comment.