Skip to content

Commit 8c8ac61

Browse files
committed
Port composer template saving and duplicating to layout manager
1 parent 8478b7a commit 8c8ac61

File tree

7 files changed

+85
-41
lines changed

7 files changed

+85
-41
lines changed

python/core/composer/qgslayoutmanager.sip

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ class QgsLayoutManager : QObject
1313
bool removeComposition( QgsComposition* composition );
1414

1515
void clear();
16-
QList< QgsComposition* > compositions() const;
17-
QgsComposition* compositionByName( const QString& name ) const;
18-
bool readXml( const QDomElement& element, const QDomDocument& doc );
19-
QDomElement writeXml( QDomDocument& doc ) const;
16+
QList< QgsComposition * > compositions() const;
17+
QgsComposition *compositionByName( const QString &name ) const;
18+
bool readXml( const QDomElement &element, const QDomDocument &doc );
19+
QDomElement writeXml( QDomDocument &doc ) const;
20+
bool saveAsTemplate( const QString &name, QDomDocument &doc ) const;
21+
QgsComposition *duplicateComposition( const QString &name, const QString &newName );
2022

2123
signals:
2224
void compositionAdded( const QString& name );

src/app/composer/qgscomposer.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "ui_qgssvgexportoptions.h"
6969
#include "qgspanelwidgetstack.h"
7070
#include "qgssettings.h"
71+
#include "qgslayoutmanager.h"
7172

7273
#include <QCloseEvent>
7374
#include <QCheckBox>
@@ -113,7 +114,7 @@ QgsComposer::QgsComposer( QgsComposition *composition )
113114
, mQgis( QgisApp::instance() )
114115
{
115116
setupUi( this );
116-
setWindowTitle( mComposition->name() );
117+
setTitle( mComposition->name() );
117118
setAttribute( Qt::WA_DeleteOnClose );
118119
#if QT_VERSION >= 0x050600
119120
setDockOptions( dockOptions() | QMainWindow::GroupedDragging ) ;
@@ -2951,7 +2952,7 @@ void QgsComposer::on_mActionNewComposer_triggered()
29512952
void QgsComposer::on_mActionDuplicateComposer_triggered()
29522953
{
29532954
QString newTitle;
2954-
if ( !mQgis->uniqueComposerTitle( this, newTitle, false, title() + tr( " copy" ) ) )
2955+
if ( !mQgis->uniqueComposerTitle( this, newTitle, false, mComposition->name() + tr( " copy" ) ) )
29552956
{
29562957
return;
29572958
}
@@ -3015,7 +3016,7 @@ void QgsComposer::on_mActionSaveAsTemplate_triggered()
30153016
}
30163017

30173018
QDomDocument saveDocument;
3018-
templateXml( saveDocument );
3019+
QgsProject::instance()->layoutManager()->saveAsTemplate( mComposition->name(), saveDocument );
30193020

30203021
if ( templateFile.write( saveDocument.toByteArray() ) == -1 )
30213022
{
@@ -3360,11 +3361,11 @@ void QgsComposer::restoreWindowState()
33603361
}
33613362
}
33623363

3363-
void QgsComposer::writeXml( QDomNode &parentNode, QDomDocument &doc )
3364+
void QgsComposer::templateXml( QDomDocument &doc )
33643365
{
33653366
QDomElement composerElem = doc.createElement( QStringLiteral( "Composer" ) );
33663367
composerElem.setAttribute( QStringLiteral( "title" ), mTitle );
3367-
parentNode.appendChild( composerElem );
3368+
doc.appendChild( composerElem );
33683369

33693370
//store composition
33703371
if ( mComposition )
@@ -3376,11 +3377,6 @@ void QgsComposer::writeXml( QDomNode &parentNode, QDomDocument &doc )
33763377
mComposition->atlasComposition().writeXml( composerElem, doc );
33773378
}
33783379

3379-
void QgsComposer::templateXml( QDomDocument &doc )
3380-
{
3381-
writeXml( doc, doc );
3382-
}
3383-
33843380
void QgsComposer::createCompositionWidget()
33853381
{
33863382
if ( !mComposition )

src/app/composer/qgscomposer.h

-3
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
487487
//! Changes elements that are not suitable for this project
488488
void cleanupAfterTemplateRead();
489489

490-
//! Writes state under DOM element
491-
void writeXml( QDomNode &parentNode, QDomDocument &doc );
492-
493490
//! Removes all the item from the graphics scene and deletes them
494491
void deleteItemWidgets();
495492

src/app/qgisapp.cpp

+7-24
Original file line numberDiff line numberDiff line change
@@ -6987,7 +6987,11 @@ QgsComposer *QgisApp::createNewComposer( QString title )
69876987
QgsComposition *composition = new QgsComposition( QgsProject::instance() );
69886988
composition->setName( title );
69896989
QgsProject::instance()->layoutManager()->addComposition( composition );
6990+
return openComposer( composition );
6991+
}
69906992

6993+
QgsComposer *QgisApp::openComposer( QgsComposition *composition )
6994+
{
69916995
QgsComposer *newComposerObject = new QgsComposer( composition );
69926996
connect( newComposerObject, &QgsComposer::aboutToClose, this, [this, newComposerObject]
69936997
{
@@ -7016,42 +7020,21 @@ void QgisApp::deleteComposer( QgsComposer *c )
70167020

70177021
QgsComposer *QgisApp::duplicateComposer( QgsComposer *currentComposer, QString title )
70187022
{
7019-
QgsComposer *newComposer = nullptr;
7020-
7021-
// test that current composer template write is valid
7022-
QDomDocument currentDoc;
7023-
currentComposer->templateXml( currentDoc );
7024-
QDomElement compositionElem = currentDoc.documentElement().firstChildElement( QStringLiteral( "Composition" ) );
7025-
if ( compositionElem.isNull() )
7026-
{
7027-
QgsDebugMsg( "selected composer could not be stored as temporary template" );
7028-
return newComposer;
7029-
}
7030-
70317023
if ( title.isEmpty() )
70327024
{
70337025
// TODO: inject a bit of randomness in auto-titles?
70347026
title = currentComposer->title() + tr( " copy" );
70357027
}
70367028

7037-
newComposer = createNewComposer( title );
7029+
QgsComposition *newComposition = QgsProject::instance()->layoutManager()->duplicateComposition( currentComposer->composition()->name(),
7030+
title );
7031+
QgsComposer *newComposer = openComposer( newComposition );
70387032
if ( !newComposer )
70397033
{
70407034
QgsDebugMsg( "could not create new composer" );
70417035
return newComposer;
70427036
}
7043-
7044-
// hiding composer until template is loaded is much faster, provide feedback to user
7045-
newComposer->hide();
7046-
if ( !newComposer->loadFromTemplate( currentDoc, true ) )
7047-
{
7048-
deleteComposer( newComposer );
7049-
newComposer = nullptr;
7050-
QgsDebugMsg( "Error, composer could not be duplicated" );
7051-
return newComposer;
7052-
}
70537037
newComposer->activate();
7054-
70557038
return newComposer;
70567039
}
70577040

src/app/qgisapp.h

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class QgsAuthManager;
4646
class QgsBookmarks;
4747
class QgsClipboard;
4848
class QgsComposer;
49+
class QgsComposition;
4950
class QgsComposerManager;
5051
class QgsComposerView;
5152
class QgsContrastEnhancement;
@@ -332,6 +333,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
332333
bool uniqueComposerTitle( QWidget *parent, QString &composerTitle, bool acceptEmpty, const QString &currentTitle = QString() );
333334
//! Creates a new composer and returns a pointer to it
334335
QgsComposer *createNewComposer( QString title = QString() );
336+
337+
/**
338+
* Opens a composer window for an existing \a composition.
339+
*/
340+
QgsComposer *openComposer( QgsComposition *composition );
341+
335342
//! Deletes a composer and removes entry from Set
336343
void deleteComposer( QgsComposer *c );
337344

src/core/composer/qgslayoutmanager.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "qgslayoutmanager.h"
1717
#include "qgsproject.h"
18+
#include "qgslogger.h"
1819

1920
QgsLayoutManager::QgsLayoutManager( QgsProject *project )
2021
: QObject( project )
@@ -129,6 +130,51 @@ QDomElement QgsLayoutManager::writeXml( QDomDocument &doc ) const
129130
return layoutsElem;
130131
}
131132

133+
bool QgsLayoutManager::saveAsTemplate( const QString &name, QDomDocument &doc ) const
134+
{
135+
QgsComposition *c = compositionByName( name );
136+
if ( !c )
137+
return false;
138+
139+
QDomElement composerElem = doc.createElement( QStringLiteral( "Composer" ) );
140+
doc.appendChild( composerElem );
141+
c->writeXml( composerElem, doc );
142+
c->atlasComposition().writeXml( composerElem, doc );
143+
return true;
144+
}
145+
146+
QgsComposition *QgsLayoutManager::duplicateComposition( const QString &name, const QString &newName )
147+
{
148+
QDomDocument currentDoc;
149+
if ( !saveAsTemplate( name, currentDoc ) )
150+
return nullptr;
151+
152+
QDomElement compositionElem = currentDoc.documentElement().firstChildElement( QStringLiteral( "Composition" ) );
153+
if ( compositionElem.isNull() )
154+
{
155+
QgsDebugMsg( "selected composer could not be stored as temporary template" );
156+
return nullptr;
157+
}
158+
159+
QgsComposition *newComposition( new QgsComposition( mProject ) );
160+
if ( !newComposition->loadFromTemplate( currentDoc, nullptr, false, true ) )
161+
{
162+
delete newComposition;
163+
return nullptr;
164+
}
165+
166+
newComposition->setName( newName );
167+
if ( !addComposition( newComposition ) )
168+
{
169+
delete newComposition;
170+
return nullptr;
171+
}
172+
else
173+
{
174+
return newComposition;
175+
}
176+
}
177+
132178
QgsComposition *QgsLayoutManager::createCompositionFromXml( const QDomElement &element, const QDomDocument &doc ) const
133179
{
134180
QDomNodeList compositionNodeList = element.elementsByTagName( QStringLiteral( "Composition" ) );

src/core/composer/qgslayoutmanager.h

+13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ class CORE_EXPORT QgsLayoutManager : public QObject
100100
*/
101101
QDomElement writeXml( QDomDocument &doc ) const;
102102

103+
/**
104+
* Saves the composition with matching \a name in template format.
105+
* Returns true if save was successful.
106+
*/
107+
bool saveAsTemplate( const QString &name, QDomDocument &doc ) const;
108+
109+
/**
110+
* Duplicates an existing composition from the manager. The new
111+
* composition will automatically be stored in the manager.
112+
* Returns new composition if duplication was successful.
113+
*/
114+
QgsComposition *duplicateComposition( const QString &name, const QString &newName );
115+
103116
signals:
104117

105118
//! Emitted when a composition has been added to the manager

0 commit comments

Comments
 (0)