Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[composer] Attach compositions to QgsProject
This adds a new QgsLayoutManager class which is used for storage and serializing/deserializing of compositions. A QgsLayoutManager is attached to QgsProject. This allows core code to access the compositions attached to a project. The intention is to move all handling of compositions from app to core, making it easy for server to access project compositions without resorting to fragile xml parsing.
- Loading branch information
1 parent
63f7bee
commit 3a1ac1a
Showing
13 changed files
with
554 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
class QgsLayoutManager : QObject | ||
{ | ||
%TypeHeaderCode | ||
#include <qgslayoutmanager.h> | ||
%End | ||
public: | ||
|
||
explicit QgsLayoutManager( QgsProject* project = 0 ); | ||
|
||
~QgsLayoutManager(); | ||
bool addComposition( QgsComposition* composition /Transfer/ ); | ||
bool removeComposition( QgsComposition* composition ); | ||
|
||
void clear(); | ||
QList< QgsComposition* > compositions() const; | ||
QgsComposition* compositionByName( const QString& name ) const; | ||
bool readXml( const QDomElement& element, const QDomDocument& doc ); | ||
QDomElement writeXml( QDomDocument& doc ) const; | ||
|
||
signals: | ||
void compositionAdded( const QString& name ); | ||
void compositionRemoved( const QString& name ); | ||
void compositionAboutToBeRemoved( const QString& name ); | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/*************************************************************************** | ||
qgslayoutmanager.cpp | ||
-------------------- | ||
Date : January 2017 | ||
Copyright : (C) 2017 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgslayoutmanager.h" | ||
#include "qgsproject.h" | ||
|
||
QgsLayoutManager::QgsLayoutManager( QgsProject *project ) | ||
: QObject( project ) | ||
, mProject( project ) | ||
{ | ||
|
||
} | ||
|
||
QgsLayoutManager::~QgsLayoutManager() | ||
{ | ||
clear(); | ||
} | ||
|
||
bool QgsLayoutManager::addComposition( QgsComposition *composition ) | ||
{ | ||
if ( !composition ) | ||
return false; | ||
|
||
// check for duplicate name | ||
Q_FOREACH ( QgsComposition *c, mCompositions ) | ||
{ | ||
if ( c->name() == composition->name() ) | ||
return false; | ||
} | ||
|
||
mCompositions << composition; | ||
emit compositionAdded( composition->name() ); | ||
mProject->setDirty( true ); | ||
return true; | ||
} | ||
|
||
bool QgsLayoutManager::removeComposition( QgsComposition *composition ) | ||
{ | ||
if ( !composition ) | ||
return false; | ||
|
||
if ( !mCompositions.contains( composition ) ) | ||
return false; | ||
|
||
QString name = composition->name(); | ||
emit compositionAboutToBeRemoved( name ); | ||
mCompositions.removeAll( composition ); | ||
delete composition; | ||
emit compositionRemoved( name ); | ||
mProject->setDirty( true ); | ||
return true; | ||
} | ||
|
||
void QgsLayoutManager::clear() | ||
{ | ||
Q_FOREACH ( QgsComposition *c, mCompositions ) | ||
{ | ||
removeComposition( c ); | ||
} | ||
} | ||
|
||
QList<QgsComposition *> QgsLayoutManager::compositions() const | ||
{ | ||
return mCompositions; | ||
} | ||
|
||
QgsComposition *QgsLayoutManager::compositionByName( const QString &name ) const | ||
{ | ||
Q_FOREACH ( QgsComposition *c, mCompositions ) | ||
{ | ||
if ( c->name() == name ) | ||
return c; | ||
} | ||
return nullptr; | ||
} | ||
|
||
bool QgsLayoutManager::readXml( const QDomElement &element, const QDomDocument &doc ) | ||
{ | ||
clear(); | ||
|
||
QDomElement layoutsElem = element; | ||
if ( element.tagName() != QStringLiteral( "Layouts" ) ) | ||
{ | ||
layoutsElem = element.firstChildElement( QStringLiteral( "Layouts" ) ); | ||
} | ||
if ( layoutsElem.isNull() ) | ||
return false; | ||
|
||
//restore each composer | ||
bool result = true; | ||
QDomNodeList composerNodes = element.elementsByTagName( QStringLiteral( "Composer" ) ); | ||
for ( int i = 0; i < composerNodes.size(); ++i ) | ||
{ | ||
QgsComposition *c = createCompositionFromXml( composerNodes.at( i ).toElement(), doc ); | ||
if ( !c ) | ||
{ | ||
result = false; | ||
continue; | ||
} | ||
result = result && addComposition( c ); | ||
} | ||
return result; | ||
} | ||
|
||
QDomElement QgsLayoutManager::writeXml( QDomDocument &doc ) const | ||
{ | ||
QDomElement layoutsElem = doc.createElement( QStringLiteral( "Layouts" ) ); | ||
Q_FOREACH ( QgsComposition *c, mCompositions ) | ||
{ | ||
QDomElement composerElem = doc.createElement( QStringLiteral( "Composer" ) ); | ||
|
||
layoutsElem.appendChild( composerElem ); | ||
|
||
c->writeXml( composerElem, doc ); | ||
c->atlasComposition().writeXml( composerElem, doc ); | ||
} | ||
return layoutsElem; | ||
} | ||
|
||
QgsComposition *QgsLayoutManager::createCompositionFromXml( const QDomElement &element, const QDomDocument &doc ) const | ||
{ | ||
QDomNodeList compositionNodeList = element.elementsByTagName( QStringLiteral( "Composition" ) ); | ||
|
||
if ( compositionNodeList.size() > 0 ) | ||
{ | ||
std::unique_ptr< QgsComposition > c( new QgsComposition( mProject ) ); | ||
QDomElement compositionElem = compositionNodeList.at( 0 ).toElement(); | ||
if ( !c->readXml( compositionElem, doc ) ) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
// read atlas parameters - must be done before adding items | ||
QDomElement atlasElem = element.firstChildElement( QStringLiteral( "Atlas" ) ); | ||
c->atlasComposition().readXml( atlasElem, doc ); | ||
|
||
//read and restore all the items | ||
c->addItemsFromXml( element, doc ); | ||
|
||
//make sure z values are consistent | ||
c->refreshZList(); | ||
return c.release(); | ||
} | ||
else | ||
{ | ||
return nullptr; | ||
} | ||
} |
Oops, something went wrong.