Skip to content

Commit

Permalink
Port method to determine whether page should be exported
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent f3fcb68 commit 5828f5d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -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;

Expand Down
47 changes: 47 additions & 0 deletions tests/src/core/testqgslayout.cpp
Expand Up @@ -24,6 +24,8 @@
#include "qgslayoutundostack.h"
#include "qgslayoutitemlabel.h"
#include "qgslayoutitempolyline.h"
#include "qgslayoutitemhtml.h"
#include "qgslayoutframe.h"

class TestQgsLayout: public QObject
{
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5828f5d

Please sign in to comment.