Skip to content

Commit

Permalink
Implement sort order customisation for field groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 2654454 commit aef0432
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
16 changes: 16 additions & 0 deletions python/core/layout/qgsreportsectionfieldgroup.sip
Expand Up @@ -77,6 +77,22 @@ Returns the field associated with this section.
Sets the ``field`` associated with this section.

.. seealso:: :py:func:`field()`
%End

bool sortAscending() const;
%Docstring
Returns true if the field values should be sorted ascending,
or false for descending sort.

.. seealso:: :py:func:`setSortAscending()`
%End

void setSortAscending( bool sortAscending );
%Docstring
Sets whether the field values should be sorted ascending. Set to true to sort
ascending, or false for descending sort.

.. seealso:: :py:func:`sortAscending()`
%End

virtual QgsReportSectionFieldGroup *clone() const /Factory/;
Expand Down
15 changes: 14 additions & 1 deletion src/core/layout/qgsreportsectionfieldgroup.cpp
Expand Up @@ -39,6 +39,7 @@ QgsReportSectionFieldGroup *QgsReportSectionFieldGroup::clone() const

copy->setLayer( mCoverageLayer.get() );
copy->setField( mField );
copy->setSortAscending( mSortAscending );

return copy.release();
}
Expand Down Expand Up @@ -108,6 +109,7 @@ void QgsReportSectionFieldGroup::setParentSection( QgsAbstractReportSection *par
bool QgsReportSectionFieldGroup::writePropertiesToElement( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) const
{
element.setAttribute( QStringLiteral( "field" ), mField );
element.setAttribute( QStringLiteral( "ascending" ), mSortAscending ? "1" : "0" );

if ( mCoverageLayer )
{
Expand All @@ -129,6 +131,7 @@ bool QgsReportSectionFieldGroup::writePropertiesToElement( QDomElement &element,
bool QgsReportSectionFieldGroup::readPropertiesFromElement( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context )
{
mField = element.attribute( QStringLiteral( "field" ) );
mSortAscending = element.attribute( QStringLiteral( "ascending" ) ).toInt();

QString layerId = element.attribute( QStringLiteral( "coverageLayer" ) );
QString layerName = element.attribute( QStringLiteral( "coverageLayerName" ) );
Expand All @@ -148,13 +151,23 @@ bool QgsReportSectionFieldGroup::readPropertiesFromElement( const QDomElement &e
return true;
}

bool QgsReportSectionFieldGroup::sortAscending() const
{
return mSortAscending;
}

void QgsReportSectionFieldGroup::setSortAscending( bool sortAscending )
{
mSortAscending = sortAscending;
}

QgsFeatureRequest QgsReportSectionFieldGroup::buildFeatureRequest() const
{
QgsFeatureRequest request;
QString filter = context().layerFilters.value( mCoverageLayer.get() );
if ( !filter.isEmpty() )
request.setFilterExpression( filter );
request.addOrderBy( mField, true );
request.addOrderBy( mField, mSortAscending );
return request;
}

Expand Down
15 changes: 15 additions & 0 deletions src/core/layout/qgsreportsectionfieldgroup.h
Expand Up @@ -83,6 +83,20 @@ class CORE_EXPORT QgsReportSectionFieldGroup : public QgsAbstractReportSection
*/
void setField( const QString &field ) { mField = field; }

/**
* Returns true if the field values should be sorted ascending,
* or false for descending sort.
* \see setSortAscending()
*/
bool sortAscending() const;

/**
* Sets whether the field values should be sorted ascending. Set to true to sort
* ascending, or false for descending sort.
* \see sortAscending()
*/
void setSortAscending( bool sortAscending );

QgsReportSectionFieldGroup *clone() const override SIP_FACTORY;
bool beginRender() override;
QgsLayout *nextBody( bool &ok ) override;
Expand All @@ -98,6 +112,7 @@ class CORE_EXPORT QgsReportSectionFieldGroup : public QgsAbstractReportSection

QgsVectorLayerRef mCoverageLayer;
QString mField;
bool mSortAscending = true;
int mFieldIndex = -1;
QgsFeatureIterator mFeatures;
QSet< QVariant > mEncounteredValues;
Expand Down
13 changes: 7 additions & 6 deletions tests/src/python/test_qgsreport.py
Expand Up @@ -398,34 +398,35 @@ def testFieldGroup(self):
child3.setLayer(ptLayer)
child3.setBody(child3_body)
child3.setField('town')
child3.setSortAscending(False)
child2.appendChild(child3)
self.assertTrue(r.beginRender())
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'NSW', 'Sydney'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'QLD', 'Beerburrum'])
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'QLD', 'Emerald'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'QLD', 'Brisbane'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'QLD', 'Emerald'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'VIC', 'Geelong'])
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'QLD', 'Beerburrum'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'VIC', 'Melbourne'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['NZ', 'state1', 'town1'])
self.assertEqual(r.layout().reportContext().feature().attributes(), ['Australia', 'VIC', 'Geelong'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['NZ', 'state1', 'town2'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['NZ', 'state1', 'town1'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
self.assertEqual(r.layout().reportContext().feature().attributes(), ['NZ', 'state2', 'town2'])
self.assertTrue(r.next())
self.assertEqual(r.layout(), child3_body)
Expand Down

0 comments on commit aef0432

Please sign in to comment.