Skip to content
Permalink
Browse files

Implement sort order customisation for field groups

  • Loading branch information
nyalldawson committed Dec 29, 2017
1 parent 2654454 commit aef0432fdccfa3a7143a0e3c49392e4326d6b1e4
@@ -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/;
@@ -39,6 +39,7 @@ QgsReportSectionFieldGroup *QgsReportSectionFieldGroup::clone() const

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

return copy.release();
}
@@ -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 )
{
@@ -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" ) );
@@ -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;
}

@@ -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;
@@ -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;
@@ -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)

0 comments on commit aef0432

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