QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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::writeXml
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout's state encapsulated in a DOM element.
Definition: qgsprintlayout.cpp:69
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsexpressioncontextutils.h
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsPrintLayout::name
QString name
Definition: qgsprintlayout.h:33
qgsreadwritecontext.h
QgsPrintLayout::createExpressionContext
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout's current state.
Definition: qgsprintlayout.cpp:100
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsStyleEntityVisitorInterface::visitExit
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
Definition: qgsstyleentityvisitor.h:183
QgsMasterLayoutInterface::PrintLayout
@ PrintLayout
Individual print layout (QgsPrintLayout)
Definition: qgsmasterlayoutinterface.h:68
QgsLayout::createExpressionContext
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout's current state.
Definition: qgslayout.cpp:395
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsPrintLayout
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Definition: qgsprintlayout.h:31
QgsLayoutAtlas::enabled
bool enabled() const
Returns whether the atlas generation is enabled.
Definition: qgslayoutatlas.h:67
QgsLayout::readXml
virtual bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the collection's state from a DOM element.
Definition: qgslayout.cpp:949
QgsStyleEntityVisitorInterface::NodeType::PrintLayout
@ PrintLayout
An individual print layout.
QgsPrintLayout::nameChanged
void nameChanged(const QString &name)
Emitted when the layout's name is changed.
QgsMasterLayoutInterface::Type
Type
Master layout type.
Definition: qgsmasterlayoutinterface.h:67
QgsPrintLayout::readLayoutXml
bool readLayoutXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the layout's state from a DOM element.
Definition: qgsprintlayout.cpp:95
QgsStyleEntityVisitorInterface::Node
Contains information relating to a node (i.e.
Definition: qgsstyleentityvisitor.h:111
QgsPrintLayout::readXml
bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection's state from a DOM element.
Definition: qgsprintlayout.cpp:77
QgsPrintLayout::writeLayoutXml
QDomElement writeLayoutXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout's state encapsulated in a DOM element.
Definition: qgsprintlayout.cpp:90
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:490
QgsLayout::accept
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgslayout.cpp:789
QgsStyleEntityVisitorInterface::visitEnter
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
Definition: qgsstyleentityvisitor.h:169
QgsPrintLayout::layoutProject
QgsProject * layoutProject() const override
The project associated with the layout.
Definition: qgsprintlayout.cpp:48
QgsLayoutAtlas::writeXml
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the objects's state in a DOM element.
Definition: qgslayoutatlas.cpp:54
QgsPrintLayout::layoutType
QgsMasterLayoutInterface::Type layoutType() const override
Returns the master layout type.
Definition: qgsprintlayout.cpp:130
QgsLayout::writeXml
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns the layout's state encapsulated in a DOM element.
Definition: qgslayout.cpp:823
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
qgsprintlayout.h
QgsPrintLayout::atlas
QgsLayoutAtlas * atlas()
Returns the print layout's atlas.
Definition: qgsprintlayout.cpp:58
QgsLayoutAtlas::readXml
bool readXml(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the objects's state from a DOM element.
Definition: qgslayoutatlas.cpp:92
QgsExpressionContextUtils::atlasScope
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
Definition: qgsexpressioncontextutils.cpp:580
QgsLayout::project
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
QgsPrintLayout::setName
void setName(const QString &name) override
Sets the layout's name.
Definition: qgsprintlayout.cpp:63
QgsPrintLayout::layoutAccept
bool layoutAccept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Definition: qgsprintlayout.cpp:117
QgsLayoutAtlas
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
Definition: qgslayoutatlas.h:42
QgsPrintLayout::clone
QgsPrintLayout * clone() const override
Creates a clone of the layout.
Definition: qgsprintlayout.cpp:29
qgsstyleentityvisitor.h
QgsPrintLayout::updateSettings
void updateSettings() override
Refreshes the layout when global layout related options change.
Definition: qgsprintlayout.cpp:112
QgsPrintLayout::icon
QIcon icon() const override
Returns an icon for the layout.
Definition: qgsprintlayout.cpp:53
QgsLayout::reloadSettings
void reloadSettings()
Refreshes the layout when global layout related options change.
Definition: qgslayout.cpp:379
QgsPrintLayout::QgsPrintLayout
QgsPrintLayout(QgsProject *project)
Constructor for QgsPrintLayout.
Definition: qgsprintlayout.cpp:23
qgslayoutatlas.h