QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 = std::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 }
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:51
void reloadSettings()
Refreshes the layout when global layout related options change.
Definition: qgslayout.cpp:379
virtual bool readXml(const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the collection's state from a DOM element.
Definition: qgslayout.cpp:950
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the layout's current state.
Definition: qgslayout.cpp:395
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns the layout's state encapsulated in a DOM element.
Definition: qgslayout.cpp:823
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
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
@ 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:99
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.