Hide guides from layout exports

This commit is contained in:
Nyall Dawson 2017-12-18 16:45:10 +10:00
parent 0052eb830e
commit a1128a52a4
5 changed files with 53 additions and 8 deletions

View File

@ -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 );

View File

@ -20,6 +20,7 @@
#include "qgslayoutpagecollection.h"
#include "qgsogrutils.h"
#include "qgspaintenginehack.h"
#include "qgslayoutguidecollection.h"
#include <QImageWriter>
#include <QSize>
@ -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 )
@ -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

View File

@ -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;

View File

@ -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.

View File

@ -25,10 +25,13 @@ from qgis.core import (QgsMultiRenderChecker,
QgsProject,
QgsMargins,
QgsLayoutItemShape,
QgsLayoutGuide,
QgsRectangle,
QgsLayoutItemPage,
QgsLayoutItemMap,
QgsLayoutPoint,
QgsLayoutMeasurement,
QgsUnitTypes,
QgsSimpleFillSymbolLayer,
QgsFillSymbol)
from qgis.PyQt.QtCore import QSize, QSizeF, QDir, QRectF, Qt
@ -188,6 +191,10 @@ class TestQgsLayoutExporter(unittest.TestCase):
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))