26 QgsAbstractReportSection::QgsAbstractReportSection( QgsAbstractReportSection *parent )
30 QgsAbstractReportSection::~QgsAbstractReportSection()
32 qDeleteAll( mChildren );
35 QgsProject *QgsAbstractReportSection::project()
37 if ( QgsReport *report = dynamic_cast< QgsReport * >(
this ) )
38 return report->layoutProject();
40 QgsAbstractReportSection *current =
this;
41 while ( QgsAbstractReportSection *parent = current->parentSection() )
43 if ( QgsReport *report = dynamic_cast< QgsReport * >( parent ) )
44 return report->layoutProject();
51 void QgsAbstractReportSection::setContext(
const QgsReportSectionContext &context )
55 if ( context.currentLayer )
66 setReportContext( mHeader.get() );
68 setReportContext( mFooter.get() );
70 for ( QgsAbstractReportSection *section : qgis::as_const( mChildren ) )
72 section->setContext( mContext );
76 bool QgsAbstractReportSection::writeXml( QDomElement &parentElement, QDomDocument &doc,
const QgsReadWriteContext &context )
const 78 QDomElement element = doc.createElement( QStringLiteral(
"Section" ) );
79 element.setAttribute( QStringLiteral(
"type" ), type() );
81 element.setAttribute( QStringLiteral(
"headerEnabled" ), mHeaderEnabled ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
84 QDomElement headerElement = doc.createElement( QStringLiteral(
"header" ) );
85 headerElement.appendChild( mHeader->writeXml( doc, context ) );
86 element.appendChild( headerElement );
88 element.setAttribute( QStringLiteral(
"footerEnabled" ), mFooterEnabled ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
91 QDomElement footerElement = doc.createElement( QStringLiteral(
"footer" ) );
92 footerElement.appendChild( mFooter->writeXml( doc, context ) );
93 element.appendChild( footerElement );
96 for ( QgsAbstractReportSection *section : mChildren )
98 section->writeXml( element, doc, context );
101 writePropertiesToElement( element, doc, context );
103 parentElement.appendChild( element );
107 bool QgsAbstractReportSection::readXml(
const QDomElement &element,
const QDomDocument &doc,
const QgsReadWriteContext &context )
109 if ( element.nodeName() != QStringLiteral(
"Section" ) )
114 mHeaderEnabled = element.attribute( QStringLiteral(
"headerEnabled" ), QStringLiteral(
"0" ) ).toInt();
115 mFooterEnabled = element.attribute( QStringLiteral(
"footerEnabled" ), QStringLiteral(
"0" ) ).toInt();
116 const QDomElement headerElement = element.firstChildElement( QStringLiteral(
"header" ) );
117 if ( !headerElement.isNull() )
119 const QDomElement headerLayoutElem = headerElement.firstChild().toElement();
120 std::unique_ptr< QgsLayout > header = qgis::make_unique< QgsLayout >( project() );
121 header->readXml( headerLayoutElem, doc, context );
122 mHeader = std::move( header );
124 const QDomElement footerElement = element.firstChildElement( QStringLiteral(
"footer" ) );
125 if ( !footerElement.isNull() )
127 const QDomElement footerLayoutElem = footerElement.firstChild().toElement();
128 std::unique_ptr< QgsLayout > footer = qgis::make_unique< QgsLayout >( project() );
129 footer->readXml( footerLayoutElem, doc, context );
130 mFooter = std::move( footer );
133 const QDomNodeList sectionItemList = element.childNodes();
134 for (
int i = 0; i < sectionItemList.size(); ++i )
136 const QDomElement currentSectionElem = sectionItemList.at( i ).toElement();
137 if ( currentSectionElem.nodeName() != QStringLiteral(
"Section" ) )
140 const QString sectionType = currentSectionElem.attribute( QStringLiteral(
"type" ) );
143 std::unique_ptr< QgsAbstractReportSection > section;
144 if ( sectionType == QLatin1String(
"SectionFieldGroup" ) )
146 section = qgis::make_unique< QgsReportSectionFieldGroup >();
148 else if ( sectionType == QLatin1String(
"SectionLayout" ) )
150 section = qgis::make_unique< QgsReportSectionLayout >();
155 appendChild( section.get() );
156 section->readXml( currentSectionElem, doc, context );
157 ( void )section.release();
161 bool result = readPropertiesFromElement( element, doc, context );
165 void QgsAbstractReportSection::reloadSettings()
168 mHeader->reloadSettings();
170 mFooter->reloadSettings();
173 QString QgsAbstractReportSection::filePath(
const QString &baseFilePath,
const QString &extension )
175 QString base = QStringLiteral(
"%1_%2" ).arg( baseFilePath ).arg( mSectionNumber, 4, 10, QChar(
'0' ) );
176 if ( !extension.startsWith(
'.' ) )
182 QgsLayout *QgsAbstractReportSection::layout()
184 return mCurrentLayout;
187 bool QgsAbstractReportSection::beginRender()
195 for ( QgsAbstractReportSection *child : qgis::as_const( mChildren ) )
197 result = result && child->beginRender();
202 bool QgsAbstractReportSection::next()
206 switch ( mNextSection )
214 if ( mHeaderEnabled && mHeader )
216 if ( prepareHeader() )
218 mCurrentLayout = mHeader.get();
230 mNextSection = Children;
238 mCurrentLayout = body;
247 bool bodiesAvailable =
false;
251 while ( mNextChild < mChildren.count() )
254 if ( mChildren.at( mNextChild )->next() )
256 mCurrentLayout = mChildren.at( mNextChild )->layout();
268 QgsLayout *body = nextBody( bodiesAvailable );
269 if ( bodiesAvailable )
273 for ( QgsAbstractReportSection *section : qgis::as_const( mChildren ) )
280 mCurrentLayout = body;
284 while ( bodiesAvailable );
287 mNextSection = Footer;
297 if ( mFooterEnabled && mFooter )
299 if ( prepareFooter() )
301 mCurrentLayout = mFooter.get();
314 mCurrentLayout =
nullptr;
318 bool QgsAbstractReportSection::endRender()
325 for ( QgsAbstractReportSection *child : qgis::as_const( mChildren ) )
327 result = result && child->endRender();
332 void QgsAbstractReportSection::reset()
334 mCurrentLayout =
nullptr;
336 mNextSection = Header;
337 for ( QgsAbstractReportSection *section : qgis::as_const( mChildren ) )
343 bool QgsAbstractReportSection::prepareHeader()
348 bool QgsAbstractReportSection::prepareFooter()
353 void QgsAbstractReportSection::setHeader(
QgsLayout *header )
355 mHeader.reset( header );
358 void QgsAbstractReportSection::setFooter(
QgsLayout *footer )
360 mFooter.reset( footer );
363 int QgsAbstractReportSection::row()
const 366 return mParent->childSections().indexOf( const_cast<QgsAbstractReportSection *>(
this ) );
371 QgsAbstractReportSection *QgsAbstractReportSection::childSection(
int index )
373 return mChildren.value( index );
376 void QgsAbstractReportSection::appendChild( QgsAbstractReportSection *section )
378 section->setParentSection(
this );
379 mChildren.append( section );
382 void QgsAbstractReportSection::insertChild(
int index, QgsAbstractReportSection *section )
384 section->setParentSection(
this );
385 index = std::max( 0, index );
386 index = std::min( index, mChildren.count() );
387 mChildren.insert( index, section );
390 void QgsAbstractReportSection::removeChild( QgsAbstractReportSection *section )
392 mChildren.removeAll( section );
396 void QgsAbstractReportSection::removeChildAt(
int index )
398 if ( index < 0 || index >= mChildren.count() )
401 QgsAbstractReportSection *section = mChildren.at( index );
402 removeChild( section );
405 void QgsAbstractReportSection::copyCommonProperties( QgsAbstractReportSection *destination )
const 407 destination->mHeaderEnabled = mHeaderEnabled;
409 destination->mHeader.reset( mHeader->clone() );
411 destination->mHeader.reset();
413 destination->mFooterEnabled = mFooterEnabled;
415 destination->mFooter.reset( mFooter->clone() );
417 destination->mFooter.reset();
419 qDeleteAll( destination->mChildren );
420 destination->mChildren.clear();
422 for ( QgsAbstractReportSection *child : qgis::as_const( mChildren ) )
424 destination->appendChild( child->clone() );
428 bool QgsAbstractReportSection::writePropertiesToElement( QDomElement &, QDomDocument &,
const QgsReadWriteContext & )
const 433 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.