QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
23  : QgsLayout( project )
24  , mAtlas( new QgsLayoutAtlas( this ) )
25 {
26 }
27 
29 {
30  QDomDocument currentDoc;
31 
32  QgsReadWriteContext context;
33  QDomElement elem = writeXml( currentDoc, context );
34  currentDoc.appendChild( elem );
35 
36  std::unique_ptr< QgsPrintLayout > newLayout = qgis::make_unique< QgsPrintLayout >( project() );
37  bool ok = false;
38  newLayout->loadFromTemplate( currentDoc, context, true, &ok );
39  if ( !ok )
40  {
41  return nullptr;
42  }
43 
44  return newLayout.release();
45 }
46 
48 {
49  return project();
50 }
51 
52 QIcon QgsPrintLayout::icon() const
53 {
54  return QgsApplication::getThemeIcon( QStringLiteral( "mIconLayout.svg" ) );
55 }
56 
58 {
59  return mAtlas;
60 }
61 
62 void QgsPrintLayout::setName( const QString &name )
63 {
64  mName = name;
65  emit nameChanged( name );
66 }
67 
68 QDomElement QgsPrintLayout::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
69 {
70  QDomElement layoutElem = QgsLayout::writeXml( document, context );
71  layoutElem.setAttribute( QStringLiteral( "name" ), mName );
72  mAtlas->writeXml( layoutElem, document, context );
73  return layoutElem;
74 }
75 
76 bool QgsPrintLayout::readXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
77 {
78  if ( !QgsLayout::readXml( layoutElement, document, context ) )
79  return false;
80 
81  QDomElement atlasElem = layoutElement.firstChildElement( QStringLiteral( "Atlas" ) );
82  mAtlas->readXml( atlasElem, document, context );
83 
84  setName( layoutElement.attribute( QStringLiteral( "name" ) ) );
85 
86  return true;
87 }
88 
89 QDomElement QgsPrintLayout::writeLayoutXml( QDomDocument &document, const QgsReadWriteContext &context ) const
90 {
91  return writeXml( document, context );
92 }
93 
94 bool QgsPrintLayout::readLayoutXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
95 {
96  return readXml( layoutElement, document, context );
97 }
98 
100 {
102 
103  if ( mAtlas->enabled() )
104  {
106  }
107 
108  return context;
109 }
110 
112 {
113  reloadSettings();
114 }
115 
117 {
119 }
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:930
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.
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.
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 ...
Reads and writes project states.
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:393
static QgsExpressionContextScope * atlasScope(QgsLayoutAtlas *atlas)
Creates a new scope which contains variables and functions relating to a QgsLayoutAtlas.
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:130
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:806
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.
void reloadSettings()
Refreshes the layout when global layout related options change.
Definition: qgslayout.cpp:377
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.