40 if ( !layout || mLayouts.contains( layout ) )
46 if ( l->name() == layout->
name() )
54 if (
QgsPrintLayout *l = dynamic_cast< QgsPrintLayout * >( layout ) )
61 else if ( QgsReport *r = dynamic_cast< QgsReport * >( layout ) )
63 connect( r, &QgsReport::nameChanged,
this, [
this, r](
const QString & newName )
81 if ( !mLayouts.contains( layout ) )
84 QString name = layout->
name();
86 mLayouts.removeAll( layout );
95 const QList< QgsMasterLayoutInterface * >
layouts = mLayouts;
109 QList<QgsPrintLayout *> result;
110 const QList<QgsMasterLayoutInterface *> _layouts( mLayouts );
111 result.reserve( _layouts.size() );
112 for (
const auto &layout : _layouts )
114 QgsPrintLayout *_item( dynamic_cast<QgsPrintLayout *>( layout ) );
116 result.push_back( _item );
125 if ( l->name() == name )
135 QDomElement layoutsElem = element;
136 if ( element.tagName() != QStringLiteral(
"Layouts" ) )
138 layoutsElem = element.firstChildElement( QStringLiteral(
"Layouts" ) );
140 if ( layoutsElem.isNull() )
143 layoutsElem = doc.documentElement();
148 QDomNodeList composerNodes = element.elementsByTagName( QStringLiteral(
"Composer" ) );
149 for (
int i = 0; i < composerNodes.size(); ++i )
152 QString legacyTitle = composerNodes.at( i ).toElement().attribute( QStringLiteral(
"title" ) );
154 QDomNodeList compositionNodes = composerNodes.at( i ).toElement().elementsByTagName( QStringLiteral(
"Composition" ) );
155 for (
int j = 0; j < compositionNodes.size(); ++j )
160 if ( l->name().isEmpty() )
161 l->setName( legacyTitle );
166 bool isDuplicateName =
false;
167 QString originalName = l->name();
170 isDuplicateName =
false;
173 if ( l->name() == layout->name() )
175 isDuplicateName =
true;
179 if ( isDuplicateName )
181 l->setName( QStringLiteral(
"%1 %2" ).arg( originalName ).arg(
id ) );
185 while ( isDuplicateName );
188 result = added && result;
197 const QDomNodeList layoutNodes = layoutsElem.childNodes();
198 for (
int i = 0; i < layoutNodes.size(); ++i )
200 if ( layoutNodes.at( i ).nodeName() != QStringLiteral(
"Layout" ) )
203 std::unique_ptr< QgsPrintLayout > l = qgis::make_unique< QgsPrintLayout >( mProject );
204 l->undoStack()->blockCommands(
true );
205 if ( !l->readLayoutXml( layoutNodes.at( i ).toElement(), doc, context ) )
210 l->undoStack()->blockCommands(
false );
221 const QDomNodeList reportNodes = element.elementsByTagName( QStringLiteral(
"Report" ) );
222 for (
int i = 0; i < reportNodes.size(); ++i )
224 std::unique_ptr< QgsReport > r = qgis::make_unique< QgsReport >( mProject );
225 if ( !r->readLayoutXml( reportNodes.at( i ).toElement(), doc, context ) )
244 QDomElement layoutsElem = doc.createElement( QStringLiteral(
"Layouts" ) );
250 QDomElement layoutElem = l->writeLayoutXml( doc, context );
251 layoutsElem.appendChild( layoutElem );
261 std::unique_ptr< QgsMasterLayoutInterface > newLayout( layout->
clone() );
282 names.reserve( mLayouts.size() );
289 while ( name.isEmpty() || names.contains( name ) )
294 name = tr(
"Layout %1" ).arg(
id );
297 name = tr(
"Report %1" ).arg(
id );
void setDirty(bool b=true)
Flag the project as dirty (modified).
The class is used as a container of context for various read/write operations on other objects...
void layoutAboutToBeRemoved(const QString &name)
Emitted when a layout is about to be removed from the manager.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
QgsMasterLayoutInterface * layoutByName(const QString &name) const
Returns the layout with a matching name, or nullptr if no matching layouts were found.
virtual QgsMasterLayoutInterface * clone() const =0
Creates a clone of the layout.
static std::unique_ptr< QgsPrintLayout > createLayoutFromCompositionXml(const QDomElement &composerElement, QgsProject *project)
createLayoutFromCompositionXml is a factory that creates layout instances from a QGIS 2...
virtual void setName(const QString &name)=0
Sets the layout's name.
bool readXml(const QDomElement &element, const QDomDocument &doc)
Reads the manager's state from a DOM element, restoring all layouts present in the XML document...
QList< QgsPrintLayout *> printLayouts() const
Returns a list of all print layouts contained in the manager.
void layoutAboutToBeAdded(const QString &name)
Emitted when a layout is about to be added to the manager.
QgsLayoutManager(QgsProject *project=nullptr)
Constructor for QgsLayoutManager.
QgsPathResolver pathResolver() const
Returns path resolver object with considering whether the project uses absolute or relative paths and...
QDomElement writeXml(QDomDocument &doc) const
Returns a DOM element representing the state of the manager.
Reads and writes project states.
void layoutRemoved(const QString &name)
Emitted when a layout was removed from the manager.
void layoutAdded(const QString &name)
Emitted when a layout has been added to the manager.
void layoutRenamed(QgsMasterLayoutInterface *layout, const QString &newName)
Emitted when a layout is renamed.
void clear()
Removes and deletes all layouts from the manager.
void nameChanged(const QString &name)
Emitted when the layout's name is changed.
virtual QString name() const =0
Returns the layout's name.
QString generateUniqueTitle(QgsMasterLayoutInterface::Type type=QgsMasterLayoutInterface::PrintLayout) const
Generates a unique title for a new layout of the specified type, which does not clash with any alread...
bool removeLayout(QgsMasterLayoutInterface *layout)
Removes a layout from the manager.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
QList< QgsMasterLayoutInterface *> layouts() const
Returns a list of all layouts contained in the manager.
Interface for master layout type objects, such as print layouts and reports.
QgsMasterLayoutInterface * duplicateLayout(const QgsMasterLayoutInterface *layout, const QString &newName)
Duplicates an existing layout from the manager.
~QgsLayoutManager() override
Individual print layout (QgsPrintLayout)
bool addLayout(QgsMasterLayoutInterface *layout)
Adds a layout to the manager.