QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  const QgsReadWriteContext context;
34  const 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  layoutProject()->setDirty( true );
68 }
69 
70 QDomElement QgsPrintLayout::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
71 {
72  QDomElement layoutElem = QgsLayout::writeXml( document, context );
73  layoutElem.setAttribute( QStringLiteral( "name" ), mName );
74  mAtlas->writeXml( layoutElem, document, context );
75  return layoutElem;
76 }
77 
78 bool QgsPrintLayout::readXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
79 {
80  if ( !QgsLayout::readXml( layoutElement, document, context ) )
81  return false;
82 
83  const QDomElement atlasElem = layoutElement.firstChildElement( QStringLiteral( "Atlas" ) );
84  mAtlas->readXml( atlasElem, document, context );
85 
86  setName( layoutElement.attribute( QStringLiteral( "name" ) ) );
87 
88  return true;
89 }
90 
91 QDomElement QgsPrintLayout::writeLayoutXml( QDomDocument &document, const QgsReadWriteContext &context ) const
92 {
93  return writeXml( document, context );
94 }
95 
96 bool QgsPrintLayout::readLayoutXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
97 {
98  return readXml( layoutElement, document, context );
99 }
100 
102 {
104 
105  if ( mAtlas->enabled() )
106  {
108  }
109 
110  return context;
111 }
112 
114 {
115  reloadSettings();
116 }
117 
119 {
120  // NOTE: if visitEnter returns false it means "don't visit the layout", not "abort all further visitations"
121  if ( !visitor->visitEnter( QgsStyleEntityVisitorInterface::Node( QgsStyleEntityVisitorInterface::NodeType::PrintLayout, QStringLiteral( "layout" ), mName ) ) )
122  return true;
123 
124  if ( !accept( visitor ) )
125  return false;
126  if ( !visitor->visitExit( QgsStyleEntityVisitorInterface::Node( QgsStyleEntityVisitorInterface::NodeType::PrintLayout, QStringLiteral( "layout" ), mName ) ) )
127  return false;
128  return true;
129 }
130 
132 {
134 }
QgsPrintLayout::writeXml
QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout's state encapsulated in a DOM element.
Definition: qgsprintlayout.cpp:70
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
qgsexpressioncontextutils.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
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:101
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
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:103
QgsPrintLayout
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Definition: qgsprintlayout.h:30
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:950
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:66
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:96
QgsProject::setDirty
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:576
QgsStyleEntityVisitorInterface::Node
Contains information relating to a node (i.e.
Definition: qgsstyleentityvisitor.h:110
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:78
QgsPrintLayout::writeLayoutXml
QDomElement writeLayoutXml(QDomDocument &document, const QgsReadWriteContext &context) const override
Returns the layout's state encapsulated in a DOM element.
Definition: qgsprintlayout.cpp:91
QgsExpressionContext::appendScope
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Definition: qgsexpressioncontext.cpp:494
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:57
QgsPrintLayout::layoutType
QgsMasterLayoutInterface::Type layoutType() const override
Returns the master layout type.
Definition: qgsprintlayout.cpp:131
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:95
QgsExpressionContextUtils::atlasScope
static QgsExpressionContextScope * atlasScope(const QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
Definition: qgsexpressioncontextutils.cpp:660
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
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:118
QgsLayoutAtlas
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
Definition: qgslayoutatlas.h:41
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:113
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