mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-02 00:02:12 -05:00
Port method to determine whether page should be exported
This commit is contained in:
parent
f3fcb68ec4
commit
5828f5d343
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user