30#include "moc_qgslayoutmanager.cpp"
32using namespace Qt::StringLiterals;
71 QList<QgsPrintLayout *> result;
72 const QList<QgsMasterLayoutInterface *> constLayouts(
mObjects );
73 result.reserve( constLayouts.size() );
74 for (
const auto &layout : constLayouts )
78 result.push_back( _item );
92 QDomElement layoutsElem = element;
93 if ( element.tagName() !=
"Layouts"_L1 )
95 layoutsElem = element.firstChildElement( u
"Layouts"_s );
97 if ( layoutsElem.isNull() )
100 layoutsElem = doc.documentElement();
105 QDomNodeList composerNodes = element.elementsByTagName( u
"Composer"_s );
107 for (
int i = 0; i < composerNodes.size(); ++i )
110 QString legacyTitle = composerNodes.at( i ).toElement().attribute( u
"title"_s );
112 QDomNodeList compositionNodes = composerNodes.at( i ).toElement().elementsByTagName( u
"Composition"_s );
113 for (
int j = 0; j < compositionNodes.size(); ++j )
118 if ( l->name().isEmpty() )
119 l->setName( legacyTitle );
124 bool isDuplicateName =
false;
125 QString originalName = l->name();
128 isDuplicateName =
false;
131 if ( l->name() == layout->name() )
133 isDuplicateName =
true;
137 if ( isDuplicateName )
139 l->setName( u
"%1 %2"_s.arg( originalName ).arg(
id ) );
143 while ( isDuplicateName );
146 result = added && result;
154 profile.
switchTask( tr(
"Creating layouts" ) );
157 const QDomNodeList layoutNodes = layoutsElem.childNodes();
158 for (
int i = 0; i < layoutNodes.size(); ++i )
160 if ( layoutNodes.at( i ).nodeName() !=
"Layout"_L1 )
163 const QString layoutName = layoutNodes.at( i ).toElement().attribute( u
"name"_s );
166 auto l = std::make_unique< QgsPrintLayout >(
mProject );
167 l->undoStack()->blockCommands(
true );
168 if ( !l->readLayoutXml( layoutNodes.at( i ).toElement(), doc, context ) )
173 l->undoStack()->blockCommands(
false );
180 profile.
switchTask( tr(
"Creating reports" ) );
181 const QDomNodeList reportNodes = element.elementsByTagName( u
"Report"_s );
182 for (
int i = 0; i < reportNodes.size(); ++i )
184 const QString layoutName = reportNodes.at( i ).toElement().attribute( u
"name"_s );
187 auto r = std::make_unique< QgsReport >(
mProject );
188 if ( !r->readLayoutXml( reportNodes.at( i ).toElement(), doc, context ) )
203 QDomElement layoutsElem = doc.createElement( u
"Layouts"_s );
209 QDomElement layoutElem = l->writeLayoutXml( doc, context );
210 layoutsElem.appendChild( layoutElem );
220 std::unique_ptr< QgsMasterLayoutInterface > newLayout( layout->
clone() );
226 newLayout->setName( newName );
249 while ( name.isEmpty() || names.contains( name ) )
254 name = tr(
"Layout %1" ).arg(
id );
257 name = tr(
"Report %1" ).arg(
id );
276 if ( !l->layoutAccept( visitor ) )
296 else if ( QgsReport *r =
dynamic_cast< QgsReport *
>( layout ) )
298 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.