Skip to content

Commit

Permalink
[layouts] Add vector simplification method to QgsLayoutRendereContext
Browse files Browse the repository at this point in the history
Allows specification of simplify method to use when rendering layouts
  • Loading branch information
nyalldawson committed Aug 7, 2019
1 parent 615e245 commit daf3491
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
39 changes: 39 additions & 0 deletions python/core/auto_generated/layout/qgslayoutrendercontext.sip.in
Expand Up @@ -221,6 +221,45 @@ Sets the text render ``format``, which dictates how text is rendered (e.g. as pa
.. seealso:: :py:func:`textRenderFormat`

.. versionadded:: 3.4.3
%End

void setSimplifyMethod( const QgsVectorSimplifyMethod &method );
%Docstring
Sets the simplification setting to use when rendering vector layers.

If the simplify ``method`` is enabled, it apply to all vector layers rendered inside map items.

This can be used to specify global simplification methods to apply during map exports,
e.g. to allow vector layers to be simplified to an appropriate maximum level of detail
during PDF exports (avoiding excessive PDF size due to huge numbers of vertices).

The default is to use no simplification.

.. note::

This simplification method is only used during non-preview renders.

.. seealso:: :py:func:`simplifyMethod`


.. versionadded:: 3.10
%End

const QgsVectorSimplifyMethod &simplifyMethod() const;
%Docstring
Returns the simplification settings to use when rendering vector layers.

If enabled, it will apply to all vector layers rendered for the map.

The default is to use no simplification.

.. note::

This simplification method is only used during non-preview renders.

.. seealso:: :py:func:`setSimplifyMethod`

.. versionadded:: 3.10
%End

signals:
Expand Down
4 changes: 4 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -301,6 +301,7 @@ class LayoutContextSettingsRestorer
, mPreviousFlags( layout->renderContext().flags() )
, mPreviousTextFormat( layout->renderContext().textRenderFormat() )
, mPreviousExportLayer( layout->renderContext().currentExportLayer() )
, mPreviousSimplifyMethod( layout->renderContext().simplifyMethod() )
{
}

Expand All @@ -310,6 +311,7 @@ class LayoutContextSettingsRestorer
mLayout->renderContext().setFlags( mPreviousFlags );
mLayout->renderContext().setTextRenderFormat( mPreviousTextFormat );
mLayout->renderContext().setCurrentExportLayer( mPreviousExportLayer );
mLayout->renderContext().setSimplifyMethod( mPreviousSimplifyMethod );
}

LayoutContextSettingsRestorer( const LayoutContextSettingsRestorer &other ) = delete;
Expand All @@ -321,6 +323,8 @@ class LayoutContextSettingsRestorer
QgsLayoutRenderContext::Flags mPreviousFlags = nullptr;
QgsRenderContext::TextRenderFormat mPreviousTextFormat = QgsRenderContext::TextFormatAlwaysOutlines;
int mPreviousExportLayer = 0;
QgsVectorSimplifyMethod mPreviousSimplifyMethod;

};
///@endcond PRIVATE

Expand Down
4 changes: 3 additions & 1 deletion src/core/layout/qgslayoutrendercontext.cpp
Expand Up @@ -21,7 +21,9 @@ QgsLayoutRenderContext::QgsLayoutRenderContext( QgsLayout *layout )
: QObject( layout )
, mFlags( FlagAntialiasing | FlagUseAdvancedEffects )
, mLayout( layout )
{}
{
mSimplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
}

void QgsLayoutRenderContext::setFlags( const QgsLayoutRenderContext::Flags flags )
{
Expand Down
35 changes: 35 additions & 0 deletions src/core/layout/qgslayoutrendercontext.h
Expand Up @@ -223,6 +223,39 @@ class CORE_EXPORT QgsLayoutRenderContext : public QObject
mTextRenderFormat = format;
}

/**
* Sets the simplification setting to use when rendering vector layers.
*
* If the simplify \a method is enabled, it apply to all vector layers rendered inside map items.
*
* This can be used to specify global simplification methods to apply during map exports,
* e.g. to allow vector layers to be simplified to an appropriate maximum level of detail
* during PDF exports (avoiding excessive PDF size due to huge numbers of vertices).
*
* The default is to use no simplification.
*
* \note This simplification method is only used during non-preview renders.
*
* \see simplifyMethod()
*
* \since QGIS 3.10
*/
void setSimplifyMethod( const QgsVectorSimplifyMethod &method ) { mSimplifyMethod = method; }

/**
* Returns the simplification settings to use when rendering vector layers.
*
* If enabled, it will apply to all vector layers rendered for the map.
*
* The default is to use no simplification.
*
* \note This simplification method is only used during non-preview renders.
*
* \see setSimplifyMethod()
* \since QGIS 3.10
*/
const QgsVectorSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }

signals:

/**
Expand Down Expand Up @@ -255,6 +288,8 @@ class CORE_EXPORT QgsLayoutRenderContext : public QObject

QgsRenderContext::TextRenderFormat mTextRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;

QgsVectorSimplifyMethod mSimplifyMethod;

friend class QgsLayoutExporter;
friend class TestQgsLayout;
friend class LayoutContextPreviewSettingRestorer;
Expand Down
13 changes: 13 additions & 0 deletions tests/src/core/testqgslayoutcontext.cpp
Expand Up @@ -48,6 +48,7 @@ class TestQgsLayoutContext: public QObject
void exportLayer();
void geometry();
void scales();
void simplifyMethod();

private:
QString mReport;
Expand Down Expand Up @@ -273,5 +274,17 @@ void TestQgsLayoutContext::scales()
QCOMPARE( context.predefinedScales(), QVector< qreal >() << 1 << 5 << 10 << 15 );
}

void TestQgsLayoutContext::simplifyMethod()
{
QgsLayout l( QgsProject::instance() );
QgsLayoutRenderContext context( &l );
// must default to no simplification
QCOMPARE( context.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::NoSimplification );
QgsVectorSimplifyMethod simplify;
simplify.setSimplifyHints( QgsVectorSimplifyMethod::GeometrySimplification );
context.setSimplifyMethod( simplify );
QCOMPARE( context.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::GeometrySimplification );
}

QGSTEST_MAIN( TestQgsLayoutContext )
#include "testqgslayoutcontext.moc"

0 comments on commit daf3491

Please sign in to comment.