Skip to content
Permalink
Browse files

Add reprt iteration test

  • Loading branch information
nyalldawson committed Dec 29, 2017
1 parent cdf5cf2 commit 935dfa32d823e4d2d3c9c6266390cdda7fd434ed
@@ -54,6 +54,12 @@ implementations.
virtual bool endRender();


virtual QgsLayout *nextBody();
%Docstring
Returns the next body layout to export, or a None if
no body layouts remain for this section.
%End

bool headerEnabled() const;
%Docstring
Returns true if the header for the section is enabled.
@@ -217,6 +223,7 @@ Removes the child section at the specified ``index``, deleting it.
{
Header,
Body,
Children,
Footer,
End,
};
@@ -263,7 +270,7 @@ is transferred to the report section.

virtual bool beginRender();

virtual bool next();
virtual QgsLayout *nextBody();


};
@@ -77,7 +77,19 @@ bool QgsAbstractReportSection::next()

case Body:
{
// if we have a body, use it
if ( QgsLayout *body = nextBody() )
{
mCurrentLayout = body;
return true;
}

mNextSection = Children;
FALLTHROUGH;
}

case Children:
{
// we iterate through all the section's children...
while ( mNextChild < mChildren.count() )
{
@@ -227,15 +239,15 @@ bool QgsReportSectionLayout::beginRender()
return QgsAbstractReportSection::beginRender();
}

bool QgsReportSectionLayout::next()
QgsLayout *QgsReportSectionLayout::nextBody()
{
if ( !mExportedBody )
if ( !mExportedBody && mBody )
{
mExportedBody = true;
return true;
return mBody.get();
}
else
{
return false;
return nullptr;
}
}
@@ -67,6 +67,12 @@ class CORE_EXPORT QgsAbstractReportSection : public QgsAbstractLayoutIterator
bool next() override;
bool endRender() override;

/**
* Returns the next body layout to export, or a nullptr if
* no body layouts remain for this section.
*/
virtual QgsLayout *nextBody() { return nullptr; }

/**
* Returns true if the header for the section is enabled.
* \see setHeaderEnabled()
@@ -194,6 +200,7 @@ class CORE_EXPORT QgsAbstractReportSection : public QgsAbstractLayoutIterator
{
Header, //!< Header for section
Body, //!< Body of section
Children, //!< Child sections
Footer, //!< Footer for section
End, //!< End of section (i.e. past all available content)
};
@@ -248,7 +255,7 @@ class CORE_EXPORT QgsReportSectionLayout : public QgsAbstractReportSection

QgsReportSectionLayout *clone() const override SIP_FACTORY;
bool beginRender() override;
bool next() override;
QgsLayout *nextBody() override;

private:

@@ -131,6 +131,126 @@ def testReportSectionLayout(self):
r.setBody(body)
self.assertEqual(r.body(), body)

def testIteration(self):
p = QgsProject()
r = QgsReport()

# empty report
self.assertTrue(r.beginRender())
self.assertFalse(r.next())

# add a header
r.setHeaderEnabled(True)
report_header = QgsLayout(p)
r.setHeader(report_header)

self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertFalse(r.next())

# add a footer
r.setFooterEnabled(True)
report_footer = QgsLayout(p)
r.setFooter(report_footer)

self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_footer)
self.assertFalse(r.next())

# add a child
child1 = QgsReportSectionLayout()
child1_body = QgsLayout(p)
child1.setBody(child1_body)
r.appendChild(child1)
self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_body)
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_footer)
self.assertFalse(r.next())

# header and footer on child
child1_header = QgsLayout(p)
child1.setHeader(child1_header)
child1.setHeaderEnabled(True)
child1_footer = QgsLayout(p)
child1.setFooter(child1_footer)
child1.setFooterEnabled(True)
self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_body)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_footer)
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_footer)
self.assertFalse(r.next())

# add another child
child2 = QgsReportSectionLayout()
child2_header = QgsLayout(p)
child2.setHeader(child2_header)
child2.setHeaderEnabled(True)
child2_footer = QgsLayout(p)
child2.setFooter(child2_footer)
child2.setFooterEnabled(True)
r.appendChild(child2)
self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_body)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_footer)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2_footer)
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_footer)
self.assertFalse(r.next())

# add a child to child2
child2a = QgsReportSectionLayout()
child2a_header = QgsLayout(p)
child2a.setHeader(child2a_header)
child2a.setHeaderEnabled(True)
child2a_footer = QgsLayout(p)
child2a.setFooter(child2a_footer)
child2a.setFooterEnabled(True)
child2.appendChild(child2a)
self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_body)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child1_footer)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2a_header)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2a_footer)
self.assertTrue(r.next())
self.assertEqual(r.layout(), child2_footer)
self.assertTrue(r.next())
self.assertEqual(r.layout(), report_footer)
self.assertFalse(r.next())


if __name__ == '__main__':
unittest.main()

0 comments on commit 935dfa3

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