QGIS API Documentation  3.23.0-Master (22c16f2067)
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 }
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:101
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:519
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.