28#include "moc_qgslayoutmanager.cpp"
67 QList<QgsPrintLayout *> result;
68 const QList<QgsMasterLayoutInterface *> constLayouts(
mObjects );
69 result.reserve( constLayouts.size() );
70 for (
const auto &layout : constLayouts )
74 result.push_back( _item );
88 QDomElement layoutsElem = element;
89 if ( element.tagName() != QLatin1String(
"Layouts" ) )
91 layoutsElem = element.firstChildElement( QStringLiteral(
"Layouts" ) );
93 if ( layoutsElem.isNull() )
96 layoutsElem = doc.documentElement();
101 QDomNodeList composerNodes = element.elementsByTagName( QStringLiteral(
"Composer" ) );
103 for (
int i = 0; i < composerNodes.size(); ++i )
106 QString legacyTitle = composerNodes.at( i ).toElement().attribute( QStringLiteral(
"title" ) );
108 QDomNodeList compositionNodes = composerNodes.at( i ).toElement().elementsByTagName( QStringLiteral(
"Composition" ) );
109 for (
int j = 0; j < compositionNodes.size(); ++j )
114 if ( l->name().isEmpty() )
115 l->setName( legacyTitle );
120 bool isDuplicateName =
false;
121 QString originalName = l->name();
124 isDuplicateName =
false;
127 if ( l->name() == layout->name() )
129 isDuplicateName =
true;
133 if ( isDuplicateName )
135 l->setName( QStringLiteral(
"%1 %2" ).arg( originalName ).arg(
id ) );
139 while ( isDuplicateName );
142 result = added && result;
150 profile.
switchTask( tr(
"Creating layouts" ) );
153 const QDomNodeList layoutNodes = layoutsElem.childNodes();
154 for (
int i = 0; i < layoutNodes.size(); ++i )
156 if ( layoutNodes.at( i ).nodeName() != QLatin1String(
"Layout" ) )
159 const QString layoutName = layoutNodes.at( i ).toElement().attribute( QStringLiteral(
"name" ) );
162 auto l = std::make_unique< QgsPrintLayout >(
mProject );
163 l->undoStack()->blockCommands(
true );
164 if ( !l->readLayoutXml( layoutNodes.at( i ).toElement(), doc, context ) )
169 l->undoStack()->blockCommands(
false );
176 profile.
switchTask( tr(
"Creating reports" ) );
177 const QDomNodeList reportNodes = element.elementsByTagName( QStringLiteral(
"Report" ) );
178 for (
int i = 0; i < reportNodes.size(); ++i )
180 const QString layoutName = reportNodes.at( i ).toElement().attribute( QStringLiteral(
"name" ) );
183 auto r = std::make_unique< QgsReport >(
mProject );
184 if ( !r->readLayoutXml( reportNodes.at( i ).toElement(), doc, context ) )
199 QDomElement layoutsElem = doc.createElement( QStringLiteral(
"Layouts" ) );
205 QDomElement layoutElem = l->writeLayoutXml( doc, context );
206 layoutsElem.appendChild( layoutElem );
216 std::unique_ptr< QgsMasterLayoutInterface > newLayout( layout->
clone() );
222 newLayout->setName( newName );
245 while ( name.isEmpty() || names.contains( name ) )
250 name = tr(
"Layout %1" ).arg(
id );
253 name = tr(
"Report %1" ).arg(
id );
272 if ( !l->layoutAccept( visitor ) )
292 else if ( QgsReport *r =
dynamic_cast< QgsReport *
>( layout ) )
294 connect( r, &QgsReport::nameChanged,
this, [
this, r](
const QString & newName )
QgsMasterLayoutInterface * objectByName(const QString &name) const
QList< QgsMasterLayoutInterface * > mObjects
bool addObject(QgsMasterLayoutInterface *object)
bool removeObject(QgsMasterLayoutInterface *object)
QgsAbstractProjectStoredObjectManager(QgsProject *project=nullptr)
static std::unique_ptr< QgsPrintLayout > createLayoutFromCompositionXml(const QDomElement &composerElement, QgsProject *project)
createLayoutFromCompositionXml is a factory that creates layout instances from a QGIS 2....
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.
QgsLayoutManager(QgsProject *project=nullptr)
Constructor for QgsLayoutManager.
QList< QgsMasterLayoutInterface * > layouts() const
Returns a list of all layouts contained in the manager.
QList< QgsPrintLayout * > printLayouts() const
Returns a list of all print layouts contained in the manager.
void setupObjectConnections(QgsMasterLayoutInterface *layout) override
void layoutAboutToBeRemoved(const QString &name)
Emitted when a layout is about to be removed from the manager.
~QgsLayoutManager() override
bool removeLayout(QgsMasterLayoutInterface *layout)
Removes a layout from the manager.
QgsMasterLayoutInterface * layoutByName(const QString &name) const
Returns the layout with a matching name, or nullptr if no matching layouts were found.
void clear()
Removes and deletes all layouts from the manager.
bool addLayout(QgsMasterLayoutInterface *layout)
Adds a layout to the manager.
void layoutAboutToBeAdded(const QString &name)
Emitted when a layout is about to be added to the manager.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated within ...
void layoutRenamed(QgsMasterLayoutInterface *layout, const QString &newName)
Emitted when a layout is renamed.
void layoutRemoved(const QString &name)
Emitted when a layout was removed from the manager.
QDomElement writeXml(QDomDocument &doc) const
Returns a DOM element representing the state of the manager.
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...
void layoutAdded(const QString &name)
Emitted when a layout has been added to the manager.
QgsMasterLayoutInterface * duplicateLayout(const QgsMasterLayoutInterface *layout, const QString &newName)
Duplicates an existing layout from the manager.
Interface for master layout type objects, such as print layouts and reports.
@ Report
Report (QgsReport).
@ PrintLayout
Individual print layout (QgsPrintLayout).
virtual QgsMasterLayoutInterface * clone() const =0
Creates a clone of the layout.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
void nameChanged(const QString &name)
Emitted when the layout's name is changed.
void objectAdded(const QString &name)
Emitted when an object has been added to the manager.
void objectRemoved(const QString &name)
Emitted when an object was removed from the manager.
QgsProject * mProject
Associated project.
void objectAboutToBeAdded(const QString &name)
Emitted when an object is about to be added to the manager.
void objectAboutToBeRemoved(const QString &name)
Emitted when an object is about to be removed from the manager.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
A container for the context for various read/write operations on objects.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
Scoped object for logging of the runtime for a single operation or group of operations.
void switchTask(const QString &name)
Switches the current task managed by the scoped profile to a new task with the given name.
An interface for classes which can visit style entity (e.g.
@ Layouts
Layout collection.
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
Contains information relating to a node (i.e.