QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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"
20 
22  : QgsLayout( project )
23  , mAtlas( new QgsLayoutAtlas( this ) )
24 {
25 }
26 
28 {
29  QDomDocument currentDoc;
30 
31  QgsReadWriteContext context;
32  QDomElement elem = writeXml( currentDoc, context );
33  currentDoc.appendChild( elem );
34 
35  std::unique_ptr< QgsPrintLayout > newLayout = qgis::make_unique< QgsPrintLayout >( project() );
36  bool ok = false;
37  newLayout->loadFromTemplate( currentDoc, context, true, &ok );
38  if ( !ok )
39  {
40  return nullptr;
41  }
42 
43  return newLayout.release();
44 }
45 
47 {
48  return project();
49 }
50 
51 QIcon QgsPrintLayout::icon() const
52 {
53  return QgsApplication::getThemeIcon( QStringLiteral( "mIconLayout.svg" ) );
54 }
55 
57 {
58  return mAtlas;
59 }
60 
61 void QgsPrintLayout::setName( const QString &name )
62 {
63  mName = name;
64  emit nameChanged( name );
65 }
66 
67 QDomElement QgsPrintLayout::writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const
68 {
69  QDomElement layoutElem = QgsLayout::writeXml( document, context );
70  layoutElem.setAttribute( QStringLiteral( "name" ), mName );
71  mAtlas->writeXml( layoutElem, document, context );
72  return layoutElem;
73 }
74 
75 bool QgsPrintLayout::readXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
76 {
77  if ( !QgsLayout::readXml( layoutElement, document, context ) )
78  return false;
79 
80  QDomElement atlasElem = layoutElement.firstChildElement( QStringLiteral( "Atlas" ) );
81  mAtlas->readXml( atlasElem, document, context );
82 
83  setName( layoutElement.attribute( QStringLiteral( "name" ) ) );
84 
85  return true;
86 }
87 
88 QDomElement QgsPrintLayout::writeLayoutXml( QDomDocument &document, const QgsReadWriteContext &context ) const
89 {
90  return writeXml( document, context );
91 }
92 
93 bool QgsPrintLayout::readLayoutXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context )
94 {
95  return readXml( layoutElement, document, context );
96 }
97 
99 {
101 
102  if ( mAtlas->enabled() )
103  {
105  }
106 
107  return context;
108 }
109 
111 {
112  reloadSettings();
113 }
114 
116 {
118 }
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:929
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.
bool enabled() const
Returns whether the atlas generation is enabled.
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:392
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.
QgsMasterLayoutInterface::Type layoutType() const override
Returns the master layout type.
void setName(const QString &name) override
Sets the layout&#39;s name.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Returns the layout&#39;s state encapsulated in a DOM element.
Definition: qgslayout.cpp:804
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:376
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:129
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.