23using namespace Qt::StringLiterals;
27QgsReportSectionFieldGroup::QgsReportSectionFieldGroup( QgsAbstractReportSection *parent )
28 : QgsAbstractReportSection( parent )
33QString QgsReportSectionFieldGroup::description()
const
36 return QObject::tr(
"Group: %1 - %2" ).arg( mCoverageLayer->name(), mField );
38 return QObject::tr(
"Group" );
41QIcon QgsReportSectionFieldGroup::icon()
const
46QgsReportSectionFieldGroup *QgsReportSectionFieldGroup::clone()
const
48 auto copy = std::make_unique< QgsReportSectionFieldGroup >(
nullptr );
49 copyCommonProperties( copy.get() );
53 copy->mBody.reset( mBody->clone() );
58 copy->setLayer( mCoverageLayer.get() );
59 copy->setField( mField );
60 copy->setSortAscending( mSortAscending );
61 copy->setBodyEnabled( mBodyEnabled );
63 return copy.release();
66bool QgsReportSectionFieldGroup::beginRender()
68 if ( !mCoverageLayer )
71 if ( !mField.isEmpty() )
73 mFieldIndex = mCoverageLayer->fields().lookupField( mField );
74 if ( mFieldIndex < 0 )
78 mBody->reportContext().setLayer( mCoverageLayer.get() );
82 return QgsAbstractReportSection::beginRender();
85bool QgsReportSectionFieldGroup::prepareHeader()
90 if ( !mFeatures.isValid() )
92 mFeatures = mCoverageLayer->getFeatures( buildFeatureRequest() );
95 mHeaderFeature = getNextFeature();
96 header()->reportContext().blockSignals(
true );
97 header()->reportContext().setLayer( mCoverageLayer.get() );
98 header()->reportContext().blockSignals(
false );
99 header()->reportContext().setFeature( mHeaderFeature );
100 mSkipNextRequest =
true;
101 mNoFeatures = !mHeaderFeature.isValid();
102 return mHeaderVisibility == AlwaysInclude || !mNoFeatures;
105bool QgsReportSectionFieldGroup::prepareFooter()
107 return mFooterVisibility == AlwaysInclude || !mNoFeatures;
110QgsLayout *QgsReportSectionFieldGroup::nextBody(
bool &ok )
112 if ( !mFeatures.isValid() )
114 mFeatures = mCoverageLayer->getFeatures( buildFeatureRequest() );
118 if ( !mSkipNextRequest )
120 f = getNextFeature();
125 mSkipNextRequest =
false;
133 if (
auto *lFooter = footer() )
135 lFooter->reportContext().blockSignals(
true );
136 lFooter->reportContext().setLayer( mCoverageLayer.get() );
137 lFooter->reportContext().blockSignals(
false );
138 lFooter->reportContext().setFeature( mLastFeature );
146 updateChildContexts( f );
149 if ( mBody && mBodyEnabled )
151 mBody->reportContext().blockSignals(
true );
152 mBody->reportContext().setLayer( mCoverageLayer.get() );
153 mBody->reportContext().blockSignals(
false );
154 mBody->reportContext().setFeature( f );
157 return mBodyEnabled ? mBody.get() :
nullptr;
160void QgsReportSectionFieldGroup::reset()
162 QgsAbstractReportSection::reset();
163 mEncounteredValues.clear();
164 mSkipNextRequest =
false;
171void QgsReportSectionFieldGroup::setParentSection( QgsAbstractReportSection *parent )
173 QgsAbstractReportSection::setParentSection( parent );
174 if ( !mCoverageLayer )
175 mCoverageLayer.resolveWeakly( project() );
178void QgsReportSectionFieldGroup::reloadSettings()
180 QgsAbstractReportSection::reloadSettings();
182 mBody->reloadSettings();
185bool QgsReportSectionFieldGroup::writePropertiesToElement( QDomElement &element, QDomDocument &doc,
const QgsReadWriteContext &context )
const
187 element.setAttribute( u
"headerVisibility"_s,
static_cast< int >( mHeaderVisibility ) );
188 element.setAttribute( u
"footerVisibility"_s,
static_cast< int >( mFooterVisibility ) );
189 element.setAttribute( u
"field"_s, mField );
190 element.setAttribute( u
"ascending"_s, mSortAscending ? u
"1"_s : u
"0"_s );
191 element.setAttribute( u
"bodyEnabled"_s, mBodyEnabled ? u
"1"_s : u
"0"_s );
192 if ( mCoverageLayer )
194 element.setAttribute( u
"coverageLayer"_s, mCoverageLayer.layerId );
195 element.setAttribute( u
"coverageLayerName"_s, mCoverageLayer.name );
196 element.setAttribute( u
"coverageLayerSource"_s, mCoverageLayer.source );
197 element.setAttribute( u
"coverageLayerProvider"_s, mCoverageLayer.provider );
202 QDomElement bodyElement = doc.createElement( u
"body"_s );
203 bodyElement.appendChild( mBody->writeXml( doc, context ) );
204 element.appendChild( bodyElement );
209bool QgsReportSectionFieldGroup::readPropertiesFromElement(
const QDomElement &element,
const QDomDocument &doc,
const QgsReadWriteContext &context )
211 mHeaderVisibility =
static_cast< SectionVisibility
>( element.attribute( u
"headerVisibility"_s ).toInt() );
212 mFooterVisibility =
static_cast< SectionVisibility
>( element.attribute( u
"footerVisibility"_s ).toInt() );
213 mField = element.attribute( u
"field"_s );
214 mSortAscending = element.attribute( u
"ascending"_s ).toInt();
215 mBodyEnabled = element.attribute( u
"bodyEnabled"_s ).toInt();
216 QString layerId = element.attribute( u
"coverageLayer"_s );
217 QString layerName = element.attribute( u
"coverageLayerName"_s );
218 QString layerSource = element.attribute( u
"coverageLayerSource"_s );
219 QString layerProvider = element.attribute( u
"coverageLayerProvider"_s );
220 mCoverageLayer =
QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
221 mCoverageLayer.resolveWeakly( project() );
223 const QDomElement bodyElement = element.firstChildElement( u
"body"_s );
224 if ( !bodyElement.isNull() )
226 const QDomElement bodyLayoutElem = bodyElement.firstChild().toElement();
227 auto body = std::make_unique< QgsLayout >( project() );
228 body->readXml( bodyLayoutElem, doc, context );
229 mBody = std::move( body );
234bool QgsReportSectionFieldGroup::sortAscending()
const
236 return mSortAscending;
239void QgsReportSectionFieldGroup::setSortAscending(
bool sortAscending )
241 mSortAscending = sortAscending;
247 QVariantMap filter = context().fieldFilters;
249 QStringList filterParts;
250 for (
auto filterIt = filter.constBegin(); filterIt != filter.constEnd(); ++filterIt )
253 int fieldIndex = mCoverageLayer->fields().lookupField( filterIt.key() );
254 if ( fieldIndex >= 0 )
260 if ( !filterParts.empty() )
262 QString filterString = u
"(%1)"_s.arg( filterParts.join(
") AND ("_L1 ) );
270QgsFeature QgsReportSectionFieldGroup::getNextFeature()
273 QVariant currentValue;
275 while ( first || ( ( !mBody || !mBodyEnabled ) && mEncounteredValues.contains( currentValue ) ) )
277 if ( !mFeatures.nextFeature( f ) )
283 currentValue = f.
attribute( mFieldIndex );
286 mEncounteredValues.insert( currentValue );
290void QgsReportSectionFieldGroup::updateChildContexts(
const QgsFeature &feature )
292 QgsReportSectionContext
c = context();
294 if ( mCoverageLayer )
295 c.currentLayer = mCoverageLayer.get();
297 QVariantMap currentFilter =
c.fieldFilters;
298 currentFilter.insert( mField, feature.
attribute( mFieldIndex ) );
299 c.fieldFilters = currentFilter;
301 const QList< QgsAbstractReportSection * > sections = childSections();
302 for ( QgsAbstractReportSection *section : std::as_const( sections ) )
304 section->setContext(
c );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
Wrapper for iterator of features from vector data provider or vector layer.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & addOrderBy(const QString &expression, bool ascending=true)
Adds a new OrderByClause, appending it as the least important one.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
A container for the context for various read/write operations on objects.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
_LayerRef< QgsVectorLayer > QgsVectorLayerRef