Skip to content

Commit

Permalink
Hide guides from layout exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 18, 2017
1 parent 0052eb8 commit a1128a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
5 changes: 5 additions & 0 deletions python/core/layout/qgslayoutguidecollection.sip
Expand Up @@ -206,6 +206,11 @@ Resets all other pages' guides to match the guides from the specified ``sourcePa
void update();
%Docstring
Updates the position (and visibility) of all guide line items.
%End

QList< QgsLayoutGuide * > guides();
%Docstring
Returns a list of all guides contained in the collection.
%End

QList< QgsLayoutGuide * > guides( Qt::Orientation orientation, int page = -1 );
Expand Down
39 changes: 31 additions & 8 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgslayoutpagecollection.h"
#include "qgsogrutils.h"
#include "qgspaintenginehack.h"
#include "qgslayoutguidecollection.h"
#include <QImageWriter>
#include <QSize>

Expand Down Expand Up @@ -48,6 +49,34 @@ class LayoutContextPreviewSettingRestorer
bool mPreviousSetting = false;
};

class LayoutGuideHider
{
public:

LayoutGuideHider( QgsLayout *layout )
: mLayout( layout )
{
const QList< QgsLayoutGuide * > guides = mLayout->guides().guides();
for ( QgsLayoutGuide *guide : guides )
{
mPrevVisibility.insert( guide, guide->item()->isVisible() );
guide->item()->setVisible( false );
}
}

~LayoutGuideHider()
{
for ( auto it = mPrevVisibility.constBegin(); it != mPrevVisibility.constEnd(); ++it )
{
it.key()->item()->setVisible( it.value() );
}
}

private:
QgsLayout *mLayout = nullptr;
QHash< QgsLayoutGuide *, bool > mPrevVisibility;
};

///@endcond PRIVATE

QgsLayoutExporter::QgsLayoutExporter( QgsLayout *layout )
Expand Down Expand Up @@ -150,18 +179,12 @@ void QgsLayoutExporter::renderRegion( QPainter *painter, const QRectF &region )
( void )cacheRestorer;
LayoutContextPreviewSettingRestorer restorer( mLayout );
( void )restorer;

#if 0 //TODO
setSnapLinesVisible( false );
#endif
LayoutGuideHider guideHider( mLayout );
( void ) guideHider;

painter->setRenderHint( QPainter::Antialiasing, mLayout->context().flags() & QgsLayoutContext::FlagAntialiasing );

mLayout->render( painter, QRectF( 0, 0, paintDevice->width(), paintDevice->height() ), region );

#if 0 // TODO
setSnapLinesVisible( true );
#endif
}

QImage QgsLayoutExporter::renderRegionToImage( const QRectF &region, QSize imageSize, double dpi ) const
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutguidecollection.cpp
Expand Up @@ -467,6 +467,11 @@ void QgsLayoutGuideCollection::update()
}
}

QList<QgsLayoutGuide *> QgsLayoutGuideCollection::guides()
{
return mGuides;
}

QList<QgsLayoutGuide *> QgsLayoutGuideCollection::guides( Qt::Orientation orientation, int page )
{
QList<QgsLayoutGuide *> res;
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutguidecollection.h
Expand Up @@ -235,6 +235,11 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel, public
*/
void update();

/**
* Returns a list of all guides contained in the collection.
*/
QList< QgsLayoutGuide * > guides();

/**
* Returns the list of guides contained in the collection with the specified
* \a orientation and on a matching \a page.
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgslayoutexporter.py
Expand Up @@ -25,10 +25,13 @@
QgsProject,
QgsMargins,
QgsLayoutItemShape,
QgsLayoutGuide,
QgsRectangle,
QgsLayoutItemPage,
QgsLayoutItemMap,
QgsLayoutPoint,
QgsLayoutMeasurement,
QgsUnitTypes,
QgsSimpleFillSymbolLayer,
QgsFillSymbol)
from qgis.PyQt.QtCore import QSize, QSizeF, QDir, QRectF, Qt
Expand Down Expand Up @@ -188,6 +191,10 @@ def testRenderRegion(self):
l = QgsLayout(QgsProject.instance())
l.initializeDefaults()

# add a guide, to ensure it is not included in export
g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters), l.pageCollection().page(0))
l.guides().addGuide(g1)

# add some items
item1 = QgsLayoutItemShape(l)
item1.attemptSetSceneRect(QRectF(10, 20, 100, 150))
Expand Down

0 comments on commit a1128a5

Please sign in to comment.