QGIS API Documentation 3.41.0-Master (3291e9ff48b)
Loading...
Searching...
No Matches
qgsprintlayout.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprintlayout.cpp
3 -------------------
4 begin : December 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsprintlayout.h"
18#include "moc_qgsprintlayout.cpp"
19#include "qgslayoutatlas.h"
20#include "qgsreadwritecontext.h"
23
25 : QgsLayout( project )
26 , mAtlas( new QgsLayoutAtlas( this ) )
27{
28}
29
31{
32 QDomDocument currentDoc;
33
34 const QgsReadWriteContext context;
35 const QDomElement elem = writeXml( currentDoc, context );
36 currentDoc.appendChild( elem );
37
38 std::unique_ptr< QgsPrintLayout > newLayout = std::make_unique< QgsPrintLayout >( project() );
39 bool ok = false;
40 newLayout->loadFromTemplate( currentDoc, context, true, &ok );
41 if ( !ok )
42 {
43 return nullptr;
44 }
45
46 return newLayout.release();
47}
48
50{
51 return project();
52}
53
55{
56 return QgsApplication::getThemeIcon( QStringLiteral( "mIconLayout.svg" ) );
57}
58
60{
61 return mAtlas;
62}
63
64void QgsPrintLayout::setName( const QString &name )
65{
66 mName = name;
67 emit nameChanged( name );
68 layoutProject()->setDirty( true );
69}
70
71QDomElement QgsPrintLayout::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
72{
73 QDomElement layoutElem = QgsLayout::writeXml( document, context );
74 layoutElem.setAttribute( QStringLiteral( "name" ), mName );
75 mAtlas->writeXml( layoutElem, document, context );
76 return layoutElem;
77}
78
79bool QgsPrintLayout::readXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
80{
81 if ( !QgsLayout::readXml( layoutElement, document, context ) )
82 return false;
83
84 const QDomElement atlasElem = layoutElement.firstChildElement( QStringLiteral( "Atlas" ) );
85 mAtlas->readXml( atlasElem, document, context );
86
87 setName( layoutElement.attribute( QStringLiteral( "name" ) ) );
88
89 return true;
90}
91
92QDomElement QgsPrintLayout::writeLayoutXml( QDomDocument &document, const QgsReadWriteContext &context ) const
93{
94 return writeXml( document, context );
95}
96
97bool QgsPrintLayout::readLayoutXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
98{
99 return readXml( layoutElement, document, context );
100}
101
103{
105
106 if ( mAtlas->enabled() )
107 {
109 }
110
111 return context;
112}
113
118
120{
121 // NOTE: if visitEnter returns false it means "don't visit the layout", not "abort all further visitations"
123 return true;
124
125 if ( !accept( visitor ) )
126 return false;
128 return false;
129 return true;
130}
131
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the objects's state in a DOM element.
bool enabled() const
Returns whether the atlas generation is enabled.
bool readXml(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the objects's state from a DOM element.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
void reloadSettings()
Refreshes the layout when global layout related options change.
virtual bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the collection's state from a DOM element.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout's current state.
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns the layout's state encapsulated in a DOM element.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
QgsProject * project() const
The project associated with the layout.
@ PrintLayout
Individual print layout (QgsPrintLayout)
Print layout, a QgsLayout subclass for static or atlas-based layouts.
QgsProject * layoutProject() const override
The project associated with the layout.
QgsLayoutAtlas * atlas()
Returns the print layout's atlas.
void updateSettings() override
Refreshes the layout when global layout related options change.
bool layoutAccept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
QgsPrintLayout(QgsProject *project)
Constructor for QgsPrintLayout.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout's current state.
QIcon icon() const override
Returns an icon for the layout.
QgsPrintLayout * clone() const override
Creates a clone of the layout.
void setName(const QString &name) override
Sets the layout's name.
QgsMasterLayoutInterface::Type layoutType() const override
Returns the master layout type.
QDomElement writeLayoutXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout's state encapsulated in a DOM element.
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout's state encapsulated in a DOM element.
void nameChanged(const QString &name)
Emitted when the layout's name is changed.
bool readLayoutXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the layout's state from a DOM element.
bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection's state from a DOM element.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
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.
An interface for classes which can visit style entity (e.g.
@ PrintLayout
An individual print layout.
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.