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(); void update();
%Docstring %Docstring
Updates the position (and visibility) of all guide line items. 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 %End


QList< QgsLayoutGuide * > guides( Qt::Orientation orientation, int page = -1 ); 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 "qgslayoutpagecollection.h"
#include "qgsogrutils.h" #include "qgsogrutils.h"
#include "qgspaintenginehack.h" #include "qgspaintenginehack.h"
#include "qgslayoutguidecollection.h"
#include <QImageWriter> #include <QImageWriter>
#include <QSize> #include <QSize>


Expand Down Expand Up @@ -48,6 +49,34 @@ class LayoutContextPreviewSettingRestorer
bool mPreviousSetting = false; 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 ///@endcond PRIVATE


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

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


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


mLayout->render( painter, QRectF( 0, 0, paintDevice->width(), paintDevice->height() ), region ); 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 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 *> QgsLayoutGuideCollection::guides( Qt::Orientation orientation, int page )
{ {
QList<QgsLayoutGuide *> res; 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(); 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 * Returns the list of guides contained in the collection with the specified
* \a orientation and on a matching \a page. * \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, QgsProject,
QgsMargins, QgsMargins,
QgsLayoutItemShape, QgsLayoutItemShape,
QgsLayoutGuide,
QgsRectangle, QgsRectangle,
QgsLayoutItemPage, QgsLayoutItemPage,
QgsLayoutItemMap, QgsLayoutItemMap,
QgsLayoutPoint, QgsLayoutPoint,
QgsLayoutMeasurement,
QgsUnitTypes,
QgsSimpleFillSymbolLayer, QgsSimpleFillSymbolLayer,
QgsFillSymbol) QgsFillSymbol)
from qgis.PyQt.QtCore import QSize, QSizeF, QDir, QRectF, Qt 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 = QgsLayout(QgsProject.instance())
l.initializeDefaults() 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 # add some items
item1 = QgsLayoutItemShape(l) item1 = QgsLayoutItemShape(l)
item1.attemptSetSceneRect(QRectF(10, 20, 100, 150)) item1.attemptSetSceneRect(QRectF(10, 20, 100, 150))
Expand Down

0 comments on commit a1128a5

Please sign in to comment.