Skip to content

Commit

Permalink
Add reprt iteration test
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent cdf5cf2 commit 935dfa3
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 6 deletions.
9 changes: 8 additions & 1 deletion python/core/layout/qgsabstractreportsection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -217,6 +223,7 @@ Removes the child section at the specified ``index``, deleting it.
{
Header,
Body,
Children,
Footer,
End,
};
Expand Down Expand Up @@ -263,7 +270,7 @@ is transferred to the report section.

virtual bool beginRender();

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


};
Expand Down
20 changes: 16 additions & 4 deletions src/core/layout/qgsabstractreportsection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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;
}
}
9 changes: 8 additions & 1 deletion src/core/layout/qgsabstractreportsection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
};
Expand Down Expand Up @@ -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:

Expand Down
120 changes: 120 additions & 0 deletions tests/src/python/test_qgsreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.