Skip to content
Permalink
Browse files

Port method to determine whether page should be exported

  • Loading branch information
nyalldawson committed Dec 5, 2017
1 parent f3fcb68 commit 5828f5d343132fd18b6f2e5f0a803fe34baa6c62
@@ -368,6 +368,28 @@ QList<QgsLayoutItem *> QgsLayoutPageCollection::itemsOnPage( int page ) const
return itemList;
}

bool QgsLayoutPageCollection::shouldExportPage( int page ) const
{
if ( page >= mPages.count() || page < 0 )
{
//page number out of range, of course we shouldn't export it - stop smoking crack!
return false;
}

//check all frame items on page
QList<QgsLayoutFrame *> frames;
itemsOnPage( frames, page );
for ( QgsLayoutFrame *frame : qgis::as_const( frames ) )
{
if ( frame->hidePageIfEmpty() && frame->isEmpty() )
{
//frame is set to hide page if empty, and frame is empty, so we don't want to export this page
return false;
}
}
return true;
}

void QgsLayoutPageCollection::addPage( QgsLayoutItemPage *page )
{
if ( !mBlockUndoCommands )
@@ -98,6 +98,7 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
/**
* Returns whether a given \a page index is empty, ie, it contains no items except for the background
* paper item.
* \see shouldExportPage()
*/
bool pageIsEmpty( int page ) const;

@@ -24,6 +24,8 @@
#include "qgslayoutundostack.h"
#include "qgslayoutitemlabel.h"
#include "qgslayoutitempolyline.h"
#include "qgslayoutitemhtml.h"
#include "qgslayoutframe.h"

class TestQgsLayout: public QObject
{
@@ -47,6 +49,7 @@ class TestQgsLayout: public QObject
void layoutItemByUuid();
void undoRedoOccurred();
void itemsOnPage(); //test fetching matching items on a set page
void shouldExportPage();
void pageIsEmpty();
void clear();

@@ -614,6 +617,50 @@ void TestQgsLayout::itemsOnPage()
QCOMPARE( labels.length(), 0 );
}

void TestQgsLayout::shouldExportPage()
{
QgsProject proj;
QgsLayout l( &proj );
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
page->setPageSize( "A4" );
l.pageCollection()->addPage( page );
QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l );
page2->setPageSize( "A4" );
l.pageCollection()->addPage( page2 );

QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
//frame on page 1
QgsLayoutFrame *frame1 = new QgsLayoutFrame( &l, htmlItem );
frame1->attemptSetSceneRect( QRectF( 0, 0, 100, 100 ) );
//frame on page 2
QgsLayoutFrame *frame2 = new QgsLayoutFrame( &l, htmlItem );
frame2->attemptSetSceneRect( QRectF( 0, 320, 100, 100 ) );
frame2->setHidePageIfEmpty( true );
htmlItem->addFrame( frame1 );
htmlItem->addFrame( frame2 );
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
//short content, so frame 2 should be empty
htmlItem->setHtml( QStringLiteral( "<p><i>Test manual <b>html</b></i></p>" ) );
htmlItem->loadHtml();

QVERIFY( l.pageCollection()->shouldExportPage( 0 ) );
QVERIFY( !l.pageCollection()->shouldExportPage( 1 ) );

//long content, so frame 2 should not be empty
htmlItem->setHtml( QStringLiteral( "<p style=\"height: 10000px\"><i>Test manual <b>html</b></i></p>" ) );
htmlItem->loadHtml();

QVERIFY( l.pageCollection()->shouldExportPage( 0 ) );
QVERIFY( l.pageCollection()->shouldExportPage( 1 ) );

//...and back again...
htmlItem->setHtml( QStringLiteral( "<p><i>Test manual <b>html</b></i></p>" ) );
htmlItem->loadHtml();

QVERIFY( l.pageCollection()->shouldExportPage( 0 ) );
QVERIFY( !l.pageCollection()->shouldExportPage( 1 ) );
}

void TestQgsLayout::pageIsEmpty()
{
QgsProject proj;

0 comments on commit 5828f5d

Please sign in to comment.
You can’t perform that action at this time.