Skip to content

Commit 5828f5d

Browse files
committed
Port method to determine whether page should be exported
1 parent f3fcb68 commit 5828f5d

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/core/layout/qgslayoutpagecollection.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ QList<QgsLayoutItem *> QgsLayoutPageCollection::itemsOnPage( int page ) const
368368
return itemList;
369369
}
370370

371+
bool QgsLayoutPageCollection::shouldExportPage( int page ) const
372+
{
373+
if ( page >= mPages.count() || page < 0 )
374+
{
375+
//page number out of range, of course we shouldn't export it - stop smoking crack!
376+
return false;
377+
}
378+
379+
//check all frame items on page
380+
QList<QgsLayoutFrame *> frames;
381+
itemsOnPage( frames, page );
382+
for ( QgsLayoutFrame *frame : qgis::as_const( frames ) )
383+
{
384+
if ( frame->hidePageIfEmpty() && frame->isEmpty() )
385+
{
386+
//frame is set to hide page if empty, and frame is empty, so we don't want to export this page
387+
return false;
388+
}
389+
}
390+
return true;
391+
}
392+
371393
void QgsLayoutPageCollection::addPage( QgsLayoutItemPage *page )
372394
{
373395
if ( !mBlockUndoCommands )

src/core/layout/qgslayoutpagecollection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
9898
/**
9999
* Returns whether a given \a page index is empty, ie, it contains no items except for the background
100100
* paper item.
101+
* \see shouldExportPage()
101102
*/
102103
bool pageIsEmpty( int page ) const;
103104

tests/src/core/testqgslayout.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "qgslayoutundostack.h"
2525
#include "qgslayoutitemlabel.h"
2626
#include "qgslayoutitempolyline.h"
27+
#include "qgslayoutitemhtml.h"
28+
#include "qgslayoutframe.h"
2729

2830
class TestQgsLayout: public QObject
2931
{
@@ -47,6 +49,7 @@ class TestQgsLayout: public QObject
4749
void layoutItemByUuid();
4850
void undoRedoOccurred();
4951
void itemsOnPage(); //test fetching matching items on a set page
52+
void shouldExportPage();
5053
void pageIsEmpty();
5154
void clear();
5255

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

620+
void TestQgsLayout::shouldExportPage()
621+
{
622+
QgsProject proj;
623+
QgsLayout l( &proj );
624+
QgsLayoutItemPage *page = new QgsLayoutItemPage( &l );
625+
page->setPageSize( "A4" );
626+
l.pageCollection()->addPage( page );
627+
QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l );
628+
page2->setPageSize( "A4" );
629+
l.pageCollection()->addPage( page2 );
630+
631+
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
632+
//frame on page 1
633+
QgsLayoutFrame *frame1 = new QgsLayoutFrame( &l, htmlItem );
634+
frame1->attemptSetSceneRect( QRectF( 0, 0, 100, 100 ) );
635+
//frame on page 2
636+
QgsLayoutFrame *frame2 = new QgsLayoutFrame( &l, htmlItem );
637+
frame2->attemptSetSceneRect( QRectF( 0, 320, 100, 100 ) );
638+
frame2->setHidePageIfEmpty( true );
639+
htmlItem->addFrame( frame1 );
640+
htmlItem->addFrame( frame2 );
641+
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
642+
//short content, so frame 2 should be empty
643+
htmlItem->setHtml( QStringLiteral( "<p><i>Test manual <b>html</b></i></p>" ) );
644+
htmlItem->loadHtml();
645+
646+
QVERIFY( l.pageCollection()->shouldExportPage( 0 ) );
647+
QVERIFY( !l.pageCollection()->shouldExportPage( 1 ) );
648+
649+
//long content, so frame 2 should not be empty
650+
htmlItem->setHtml( QStringLiteral( "<p style=\"height: 10000px\"><i>Test manual <b>html</b></i></p>" ) );
651+
htmlItem->loadHtml();
652+
653+
QVERIFY( l.pageCollection()->shouldExportPage( 0 ) );
654+
QVERIFY( l.pageCollection()->shouldExportPage( 1 ) );
655+
656+
//...and back again...
657+
htmlItem->setHtml( QStringLiteral( "<p><i>Test manual <b>html</b></i></p>" ) );
658+
htmlItem->loadHtml();
659+
660+
QVERIFY( l.pageCollection()->shouldExportPage( 0 ) );
661+
QVERIFY( !l.pageCollection()->shouldExportPage( 1 ) );
662+
}
663+
617664
void TestQgsLayout::pageIsEmpty()
618665
{
619666
QgsProject proj;

0 commit comments

Comments
 (0)