mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Implement sort order customisation for field groups
This commit is contained in:
parent
2654454c0b
commit
aef0432fdc
@ -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,6 +398,7 @@ class TestQgsReport(unittest.TestCase):
|
||||
child3.setLayer(ptLayer)
|
||||
child3.setBody(child3_body)
|
||||
child3.setField('town')
|
||||
child3.setSortAscending(False)
|
||||
child2.appendChild(child3)
|
||||
self.assertTrue(r.beginRender())
|
||||
self.assertTrue(r.next())
|
||||
@ -405,27 +406,27 @@ class TestQgsReport(unittest.TestCase):
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user