QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgslayoutatlas.h"
19 #include "qgsreadwritecontext.h"
21 #include "qgsstyleentityvisitor.h"
22 
24  : QgsLayout( project )
25  , mAtlas( new QgsLayoutAtlas( this ) )
26 {
27 }
28 
30 {
31  QDomDocument currentDoc;
32 
33  QgsReadWriteContext context;
34  QDomElement elem = writeXml( currentDoc, context );
35  currentDoc.appendChild( elem );
36 
37  std::unique_ptr< QgsPrintLayout > newLayout = qgis::make_unique< QgsPrintLayout >( project() );
38  bool ok = false;
39  newLayout->loadFromTemplate( currentDoc, context, true, &ok );
40  if ( !ok )
41  {
42  return nullptr;
43  }
44 
45  return newLayout.release();
46 }
47 
49 {
50  return project();
51 }
52 
53 QIcon QgsPrintLayout::icon() const
54 {
55  return QgsApplication::getThemeIcon( QStringLiteral( "mIconLayout.svg" ) );
56 }
57 
59 {
60  return mAtlas;
61 }
62 
63 void QgsPrintLayout::setName( const QString &name )
64 {
65  mName = name;
66  emit nameChanged( name );
67 }
68 
69 QDomElement QgsPrintLayout::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
70 {
71  QDomElement layoutElem = QgsLayout::writeXml( document, context );
72  layoutElem.setAttribute( QStringLiteral( "name" ), mName );
73  mAtlas->writeXml( layoutElem, document, context );
74  return layoutElem;
75 }
76 
77 bool QgsPrintLayout::readXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
78 {
79  if ( !QgsLayout::readXml( layoutElement, document, context ) )
80  return false;
81 
82  QDomElement atlasElem = layoutElement.firstChildElement( QStringLiteral( "Atlas" ) );
83  mAtlas->readXml( atlasElem, document, context );
84 
85  setName( layoutElement.attribute( QStringLiteral( "name" ) ) );
86 
87  return true;
88 }
89 
90 QDomElement QgsPrintLayout::writeLayoutXml( QDomDocument &document, const QgsReadWriteContext &context ) const
91 {
92  return writeXml( document, context );
93 }
94 
95 bool QgsPrintLayout::readLayoutXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
96 {
97  return readXml( layoutElement, document, context );
98 }
99 
101 {
103 
104  if ( mAtlas->enabled() )
105  {
107  }
108 
109  return context;
110 }
111 
113 {
114  reloadSettings();
115 }
116 
118 {
119  // NOTE: if visitEnter returns false it means "don't visit the layout", not "abort all further visitations"
120  if ( !visitor->visitEnter( QgsStyleEntityVisitorInterface::Node( QgsStyleEntityVisitorInterface::NodeType::PrintLayout, QStringLiteral( "layout" ), mName ) ) )
121  return true;
122 
123  if ( !accept( visitor ) )
124  return false;
125  if ( !visitor->visitExit( QgsStyleEntityVisitorInterface::Node( QgsStyleEntityVisitorInterface::NodeType::PrintLayout, QStringLiteral( "layout" ), mName ) ) )
126  return false;
127  return true;
128 }
129 
131 {
133 }
QgsPrintLayout * clone() const override
Creates a clone of the layout.
QgsPrintLayout(QgsProject *project)
Constructor for QgsPrintLayout.
The class is used as a container of context for various read/write operations on other objects...
virtual bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the collection&#39;s state from a DOM element.
Definition: qgslayout.cpp:948
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout&#39;s current state.
bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection&#39;s state from a DOM element.
An interface for classes which can visit style entity (e.g.
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the objects&#39;s state in a DOM element.
bool readXml(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the objects&#39;s state from a DOM element.
QgsProject * layoutProject() const override
The project associated with the layout.
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
Contains information relating to a node (i.e.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:89
QgsLayoutAtlas * atlas()
Returns the print layout&#39;s atlas.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout&#39;s state encapsulated in a DOM element.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout&#39;s current state.
Definition: qgslayout.cpp:394
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
QIcon icon() const override
Returns an icon for the layout.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void nameChanged(const QString &name)
Emitted when the layout&#39;s name is changed.
bool readLayoutXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the layout&#39;s state from a DOM element.
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:131
bool enabled() const
Returns whether the atlas generation is enabled.
QgsMasterLayoutInterface::Type layoutType() const override
Returns the master layout type.
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns the layout&#39;s state encapsulated in a DOM element.
Definition: qgslayout.cpp:822
bool layoutAccept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void setName(const QString &name) override
Sets the layout&#39;s name.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
void updateSettings() override
Refreshes the layout when global layout related options change.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgslayout.cpp:788
void reloadSettings()
Refreshes the layout when global layout related options change.
Definition: qgslayout.cpp:378
Individual print layout (QgsPrintLayout)
QString name() const override
Returns the layout&#39;s name.
QDomElement writeLayoutXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout&#39;s state encapsulated in a DOM element.