25 QgsAbstractReportSection::QgsAbstractReportSection( QgsAbstractReportSection *parent )
29 QgsAbstractReportSection::~QgsAbstractReportSection()
31 qDeleteAll( mChildren );
34 QgsProject *QgsAbstractReportSection::project()
36 if ( QgsReport *report = dynamic_cast< QgsReport * >(
this ) )
37 return report->layoutProject();
39 QgsAbstractReportSection *current =
this;
40 while ( QgsAbstractReportSection *parent = current->parentSection() )
42 if ( QgsReport *report = dynamic_cast< QgsReport * >( parent ) )
43 return report->layoutProject();
50 void QgsAbstractReportSection::setContext(
const QgsReportSectionContext &context )
54 if ( context.currentLayer )
65 setReportContext( mHeader.get() );
67 setReportContext( mFooter.get() );
69 for ( QgsAbstractReportSection *section : qgis::as_const( mChildren ) )
71 section->setContext( mContext );
75 bool QgsAbstractReportSection::writeXml( QDomElement &parentElement, QDomDocument &doc,
const QgsReadWriteContext &context )
const 77 QDomElement element = doc.createElement( QStringLiteral(
"Section" ) );
78 element.setAttribute( QStringLiteral(
"type" ), type() );
80 element.setAttribute( QStringLiteral(
"headerEnabled" ), mHeaderEnabled ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
83 QDomElement headerElement = doc.createElement( QStringLiteral(
"header" ) );
84 headerElement.appendChild( mHeader->writeXml( doc, context ) );
85 element.appendChild( headerElement );
87 element.setAttribute( QStringLiteral(
"footerEnabled" ), mFooterEnabled ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
90 QDomElement footerElement = doc.createElement( QStringLiteral(
"footer" ) );
91 footerElement.appendChild( mFooter->writeXml( doc, context ) );
92 element.appendChild( footerElement );
95 for ( QgsAbstractReportSection *section : mChildren )
97 section->writeXml( element, doc, context );
100 writePropertiesToElement( element, doc, context );
102 parentElement.appendChild( element );
106 bool QgsAbstractReportSection::readXml(
const QDomElement &element,
const QDomDocument &doc,
const QgsReadWriteContext &context )
108 if ( element.nodeName() != QStringLiteral(
"Section" ) )
113 mHeaderEnabled = element.attribute( QStringLiteral(
"headerEnabled" ), QStringLiteral(
"0" ) ).toInt();
114 mFooterEnabled = element.attribute( QStringLiteral(
"footerEnabled" ), QStringLiteral(
"0" ) ).toInt();
115 const QDomElement headerElement = element.firstChildElement( QStringLiteral(
"header" ) );
116 if ( !headerElement.isNull() )
118 const QDomElement headerLayoutElem = headerElement.firstChild().toElement();
119 std::unique_ptr< QgsLayout > header = qgis::make_unique< QgsLayout >( project() );
120 header->readXml( headerLayoutElem, doc, context );
121 mHeader = std::move( header );
123 const QDomElement footerElement = element.firstChildElement( QStringLiteral(
"footer" ) );
124 if ( !footerElement.isNull() )
126 const QDomElement footerLayoutElem = footerElement.firstChild().toElement();
127 std::unique_ptr< QgsLayout > footer = qgis::make_unique< QgsLayout >( project() );
128 footer->readXml( footerLayoutElem, doc, context );
129 mFooter = std::move( footer );
132 const QDomNodeList sectionItemList = element.childNodes();
133 for (
int i = 0; i < sectionItemList.size(); ++i )
135 const QDomElement currentSectionElem = sectionItemList.at( i ).toElement();
136 if ( currentSectionElem.nodeName() != QStringLiteral(
"Section" ) )
139 const QString sectionType = currentSectionElem.attribute( QStringLiteral(
"type" ) );
142 std::unique_ptr< QgsAbstractReportSection > section;
143 if ( sectionType == QLatin1String(
"SectionFieldGroup" ) )
145 section = qgis::make_unique< QgsReportSectionFieldGroup >();
147 else if ( sectionType == QLatin1String(
"SectionLayout" ) )
149 section = qgis::make_unique< QgsReportSectionLayout >();
154 appendChild( section.get() );
155 section->readXml( currentSectionElem, doc, context );
156 ( void )section.release();
160 bool result = readPropertiesFromElement( element, doc, context );
164 void QgsAbstractReportSection::reloadSettings()
167 mHeader->reloadSettings();
169 mFooter->reloadSettings();
172 QString QgsAbstractReportSection::filePath(
const QString &baseFilePath,
const QString &extension )
174 QString base = QStringLiteral(
"%1_%2" ).arg( baseFilePath ).arg( mSectionNumber, 4, 10, QChar(
'0' ) );
175 if ( !extension.startsWith(
'.' ) )
181 QgsLayout *QgsAbstractReportSection::layout()
183 return mCurrentLayout;
186 bool QgsAbstractReportSection::beginRender()
194 for ( QgsAbstractReportSection *child : qgis::as_const( mChildren ) )
196 result = result && child->beginRender();
201 bool QgsAbstractReportSection::next()
205 switch ( mNextSection )
213 if ( mHeaderEnabled && mHeader )
215 if ( prepareHeader() )
217 mCurrentLayout = mHeader.get();
229 mNextSection = Children;
237 mCurrentLayout = body;
246 bool bodiesAvailable =
false;
250 while ( mNextChild < mChildren.count() )
253 if ( mChildren.at( mNextChild )->next() )
255 mCurrentLayout = mChildren.at( mNextChild )->layout();
267 QgsLayout *body = nextBody( bodiesAvailable );
268 if ( bodiesAvailable )
272 for ( QgsAbstractReportSection *section : qgis::as_const( mChildren ) )
279 mCurrentLayout = body;
283 while ( bodiesAvailable );
286 mNextSection = Footer;
296 if ( mFooterEnabled && mFooter )
298 if ( prepareFooter() )
300 mCurrentLayout = mFooter.get();
313 mCurrentLayout =
nullptr;
317 bool QgsAbstractReportSection::endRender()
324 for ( QgsAbstractReportSection *child : qgis::as_const( mChildren ) )
326 result = result && child->endRender();
331 void QgsAbstractReportSection::reset()
333 mCurrentLayout =
nullptr;
335 mNextSection = Header;
336 for ( QgsAbstractReportSection *section : qgis::as_const( mChildren ) )
342 bool QgsAbstractReportSection::prepareHeader()
347 bool QgsAbstractReportSection::prepareFooter()
352 void QgsAbstractReportSection::setHeader(
QgsLayout *header )
354 mHeader.reset( header );
357 void QgsAbstractReportSection::setFooter(
QgsLayout *footer )
359 mFooter.reset( footer );
362 int QgsAbstractReportSection::row()
const 365 return mParent->childSections().indexOf( const_cast<QgsAbstractReportSection *>(
this ) );
370 QgsAbstractReportSection *QgsAbstractReportSection::childSection(
int index )
372 return mChildren.value( index );
375 void QgsAbstractReportSection::appendChild( QgsAbstractReportSection *section )
377 section->setParentSection(
this );
378 mChildren.append( section );
381 void QgsAbstractReportSection::insertChild(
int index, QgsAbstractReportSection *section )
383 section->setParentSection(
this );
384 index = std::max( 0, index );
385 index = std::min( index, mChildren.count() );
386 mChildren.insert( index, section );
389 void QgsAbstractReportSection::removeChild( QgsAbstractReportSection *section )
391 mChildren.removeAll( section );
395 void QgsAbstractReportSection::removeChildAt(
int index )
397 if ( index < 0 || index >= mChildren.count() )
400 QgsAbstractReportSection *section = mChildren.at( index );
401 removeChild( section );
404 void QgsAbstractReportSection::copyCommonProperties( QgsAbstractReportSection *destination )
const 406 destination->mHeaderEnabled = mHeaderEnabled;
408 destination->mHeader.reset( mHeader->clone() );
410 destination->mHeader.reset();
412 destination->mFooterEnabled = mFooterEnabled;
414 destination->mFooter.reset( mFooter->clone() );
416 destination->mFooter.reset();
418 qDeleteAll( destination->mChildren );
419 destination->mChildren.clear();
421 for ( QgsAbstractReportSection *child : qgis::as_const( mChildren ) )
423 destination->appendChild( child->clone() );
427 bool QgsAbstractReportSection::writePropertiesToElement( QDomElement &, QDomDocument &,
const QgsReadWriteContext & )
const 432 bool QgsAbstractReportSection::readPropertiesFromElement(
const QDomElement &,
const QDomDocument &,
const QgsReadWriteContext & )
The class is used as a container of context for various read/write operations on other objects...
void setLayer(QgsVectorLayer *layer)
Sets the vector layer associated with the layout's context.
virtual QgsLayout * layout()=0
Returns the layout associated with the iterator.
Reads and writes project states.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
void setFeature(const QgsFeature &feature)
Sets the current feature for evaluating the layout.