Skip to content

Commit a9896ea

Browse files
committed
Port a bunch of QgsLayoutManager methods to use QgsLayouts
1 parent 3a0b751 commit a9896ea

File tree

8 files changed

+315
-29
lines changed

8 files changed

+315
-29
lines changed

python/core/layout/qgslayout.sip

+6
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
537537
and updated.
538538
%End
539539

540+
void nameChanged( const QString &name );
541+
%Docstring
542+
Emitted when the layout's name is changed.
543+
.. seealso:: setName()
544+
%End
545+
540546
};
541547

542548

src/app/layout/qgslayoutdesignerdialog.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
633633
{
634634
mLayout->guides().clear();
635635
} );
636+
connect( mLayout, &QgsLayout::nameChanged, this, &QgsLayoutDesignerDialog::setWindowTitle );
636637

637638
mActionShowGrid->setChecked( mLayout->context().gridVisible() );
638639
mActionSnapGrid->setChecked( mLayout->snapper().snapToGrid() );

src/app/qgisapp.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7315,7 +7315,7 @@ bool QgisApp::uniqueComposerTitle( QWidget *parent, QString &composerTitle, bool
73157315
else
73167316
{
73177317
titleValid = true;
7318-
newTitle = QgsProject::instance()->layoutManager()->generateUniqueTitle();
7318+
newTitle = QgsProject::instance()->layoutManager()->generateUniqueComposerTitle();
73197319
}
73207320
}
73217321
else if ( cNames.indexOf( newTitle, 1 ) >= 0 )
@@ -7380,7 +7380,7 @@ bool QgisApp::uniqueLayoutTitle( QWidget *parent, QString &title, bool acceptEmp
73807380
else
73817381
{
73827382
titleValid = true;
7383-
newTitle = QgsProject::instance()->layoutManager()->generateUniqueTitle();
7383+
newTitle = QgsProject::instance()->layoutManager()->generateUniqueComposerTitle();
73847384
}
73857385
}
73867386
else if ( cNames.indexOf( newTitle, 1 ) >= 0 )
@@ -7403,7 +7403,7 @@ QgsComposer *QgisApp::createNewComposer( QString title )
74037403
{
74047404
if ( title.isEmpty() )
74057405
{
7406-
title = QgsProject::instance()->layoutManager()->generateUniqueTitle();
7406+
title = QgsProject::instance()->layoutManager()->generateUniqueComposerTitle();
74077407
}
74087408
//create new composition object
74097409
QgsComposition *composition = new QgsComposition( QgsProject::instance() );

src/core/composer/qgslayoutmanager.cpp

+82-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ bool QgsLayoutManager::addComposition( QgsComposition *composition )
5353
return true;
5454
}
5555

56+
bool QgsLayoutManager::addLayout( QgsLayout *layout )
57+
{
58+
if ( !layout )
59+
return false;
60+
61+
// check for duplicate name
62+
for ( QgsLayout *l : qgis::as_const( mLayouts ) )
63+
{
64+
if ( l->name() == layout->name() )
65+
return false;
66+
}
67+
68+
connect( layout, &QgsLayout::nameChanged, this, [this, layout]( const QString & newName )
69+
{
70+
emit layoutRenamed( layout, newName );
71+
} );
72+
emit layoutAboutToBeAdded( layout->name() );
73+
mLayouts << layout;
74+
emit layoutAdded( layout->name() );
75+
mProject->setDirty( true );
76+
return true;
77+
}
78+
5679
bool QgsLayoutManager::removeComposition( QgsComposition *composition )
5780
{
5881
if ( !composition )
@@ -70,19 +93,46 @@ bool QgsLayoutManager::removeComposition( QgsComposition *composition )
7093
return true;
7194
}
7295

96+
bool QgsLayoutManager::removeLayout( QgsLayout *layout )
97+
{
98+
if ( !layout )
99+
return false;
100+
101+
if ( !mLayouts.contains( layout ) )
102+
return false;
103+
104+
QString name = layout->name();
105+
emit layoutAboutToBeRemoved( name );
106+
mLayouts.removeAll( layout );
107+
delete layout;
108+
emit layoutRemoved( name );
109+
mProject->setDirty( true );
110+
return true;
111+
}
112+
73113
void QgsLayoutManager::clear()
74114
{
75115
Q_FOREACH ( QgsComposition *c, mCompositions )
76116
{
77117
removeComposition( c );
78118
}
119+
const QList< QgsLayout * > layouts = mLayouts;
120+
for ( QgsLayout *l : layouts )
121+
{
122+
removeLayout( l );
123+
}
79124
}
80125

81126
QList<QgsComposition *> QgsLayoutManager::compositions() const
82127
{
83128
return mCompositions;
84129
}
85130

131+
QList<QgsLayout *> QgsLayoutManager::layouts() const
132+
{
133+
return mLayouts;
134+
}
135+
86136
QgsComposition *QgsLayoutManager::compositionByName( const QString &name ) const
87137
{
88138
Q_FOREACH ( QgsComposition *c, mCompositions )
@@ -93,6 +143,16 @@ QgsComposition *QgsLayoutManager::compositionByName( const QString &name ) const
93143
return nullptr;
94144
}
95145

146+
QgsLayout *QgsLayoutManager::layoutByName( const QString &name ) const
147+
{
148+
for ( QgsLayout *l : mLayouts )
149+
{
150+
if ( l->name() == name )
151+
return l;
152+
}
153+
return nullptr;
154+
}
155+
96156
bool QgsLayoutManager::readXml( const QDomElement &element, const QDomDocument &doc )
97157
{
98158
clear();
@@ -204,22 +264,19 @@ QgsLayout *QgsLayoutManager::duplicateLayout( const QgsLayout *layout, const QSt
204264
}
205265

206266
newLayout->setName( newName );
207-
#if 0 //TODO
208-
if ( !addComposition( newComposition ) )
267+
QgsLayout *l = newLayout.get();
268+
if ( !addLayout( l ) )
209269
{
210-
delete newComposition;
211270
return nullptr;
212271
}
213272
else
214273
{
215-
return newComposition;
274+
( void )newLayout.release(); //ownership was transferred successfully
275+
return l;
216276
}
217-
#endif
218-
219-
return newLayout.release();
220277
}
221278

222-
QString QgsLayoutManager::generateUniqueTitle() const
279+
QString QgsLayoutManager::generateUniqueComposerTitle() const
223280
{
224281
QStringList names;
225282
Q_FOREACH ( QgsComposition *c, mCompositions )
@@ -236,6 +293,23 @@ QString QgsLayoutManager::generateUniqueTitle() const
236293
return name;
237294
}
238295

296+
QString QgsLayoutManager::generateUniqueTitle() const
297+
{
298+
QStringList names;
299+
for ( QgsLayout *l : mLayouts )
300+
{
301+
names << l->name();
302+
}
303+
QString name;
304+
int id = 1;
305+
while ( name.isEmpty() || names.contains( name ) )
306+
{
307+
name = tr( "Layout %1" ).arg( id );
308+
id++;
309+
}
310+
return name;
311+
}
312+
239313
QgsComposition *QgsLayoutManager::createCompositionFromXml( const QDomElement &element, const QDomDocument &doc ) const
240314
{
241315
QDomNodeList compositionNodeList = element.elementsByTagName( QStringLiteral( "Composition" ) );

src/core/composer/qgslayoutmanager.h

+60-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgis_core.h"
2020
#include "qgis.h"
2121
#include "qgscomposition.h"
22+
#include "qgslayout.h"
2223
#include <QObject>
2324

2425
class QgsProject;
@@ -28,13 +29,13 @@ class QgsProject;
2829
* \class QgsLayoutManager
2930
* \since QGIS 3.0
3031
*
31-
* \brief Manages storage of a set of compositions.
32+
* \brief Manages storage of a set of layouts.
3233
*
3334
* QgsLayoutManager handles the storage, serializing and deserializing
34-
* of QgsCompositions. Usually this class is not constructed directly, but
35+
* of QgsLayouts. Usually this class is not constructed directly, but
3536
* rather accessed through a QgsProject via QgsProject::layoutManager().
3637
*
37-
* QgsLayoutManager retains ownership of all the compositions contained
38+
* QgsLayoutManager retains ownership of all the layouts contained
3839
* in the manager.
3940
*/
4041

@@ -61,6 +62,15 @@ class CORE_EXPORT QgsLayoutManager : public QObject
6162
*/
6263
bool addComposition( QgsComposition *composition SIP_TRANSFER );
6364

65+
/**
66+
* Adds a \a layout to the manager. Ownership of the layout is transferred to the manager.
67+
* Returns true if the addition was successful, or false if the layout could not be added (eg
68+
* as a result of a duplicate layout name).
69+
* \see removeLayout()
70+
* \see layoutAdded()
71+
*/
72+
bool addLayout( QgsLayout *layout SIP_TRANSFER );
73+
6474
/**
6575
* Removes a composition from the manager. The composition is deleted.
6676
* Returns true if the removal was successful, or false if the removal failed (eg as a result
@@ -73,8 +83,19 @@ class CORE_EXPORT QgsLayoutManager : public QObject
7383
bool removeComposition( QgsComposition *composition );
7484

7585
/**
76-
* Removes and deletes all compositions from the manager.
77-
* \see removeComposition()
86+
* Removes a \a layout from the manager. The layout is deleted.
87+
* Returns true if the removal was successful, or false if the removal failed (eg as a result
88+
* of removing a layout which is not contained in the manager).
89+
* \see addLayout()
90+
* \see layoutRemoved()
91+
* \see layoutAboutToBeRemoved()
92+
* \see clear()
93+
*/
94+
bool removeLayout( QgsLayout *layout );
95+
96+
/**
97+
* Removes and deletes all layouts from the manager.
98+
* \see removeLayout()
7899
*/
79100
void clear();
80101

@@ -83,14 +104,25 @@ class CORE_EXPORT QgsLayoutManager : public QObject
83104
*/
84105
QList< QgsComposition * > compositions() const;
85106

107+
/**
108+
* Returns a list of all layouts contained in the manager.
109+
*/
110+
QList< QgsLayout * > layouts() const;
111+
86112
/**
87113
* Returns the composition with a matching name, or nullptr if no matching compositions
88114
* were found.
89115
*/
90116
QgsComposition *compositionByName( const QString &name ) const;
91117

92118
/**
93-
* Reads the manager's state from a DOM element, restoring all compositions
119+
* Returns the layout with a matching name, or nullptr if no matching layouts
120+
* were found.
121+
*/
122+
QgsLayout *layoutByName( const QString &name ) const;
123+
124+
/**
125+
* Reads the manager's state from a DOM element, restoring all layouts
94126
* present in the XML document.
95127
* \see writeXml()
96128
*/
@@ -126,30 +158,52 @@ class CORE_EXPORT QgsLayoutManager : public QObject
126158
* Generates a unique title for a new composition, which does not
127159
* clash with any already contained by the manager.
128160
*/
161+
QString generateUniqueComposerTitle() const;
162+
163+
/**
164+
* Generates a unique title for a new layout, which does not
165+
* clash with any already contained by the manager.
166+
*/
129167
QString generateUniqueTitle() const;
130168

131169
signals:
132170

133171
//! Emitted when a composition is about to be added to the manager
134172
void compositionAboutToBeAdded( const QString &name );
135173

174+
//! Emitted when a layout is about to be added to the manager
175+
void layoutAboutToBeAdded( const QString &name );
176+
136177
//! Emitted when a composition has been added to the manager
137178
void compositionAdded( const QString &name );
138179

180+
//! Emitted when a layout has been added to the manager
181+
void layoutAdded( const QString &name );
182+
139183
//! Emitted when a composition was removed from the manager
140184
void compositionRemoved( const QString &name );
141185

186+
//! Emitted when a layout was removed from the manager
187+
void layoutRemoved( const QString &name );
188+
142189
//! Emitted when a composition is about to be removed from the manager
143190
void compositionAboutToBeRemoved( const QString &name );
144191

192+
//! Emitted when a layout is about to be removed from the manager
193+
void layoutAboutToBeRemoved( const QString &name );
194+
145195
//! Emitted when a composition is renamed
146196
void compositionRenamed( QgsComposition *composition, const QString &newName );
147197

198+
//! Emitted when a layout is renamed
199+
void layoutRenamed( QgsLayout *layout, const QString &newName );
200+
148201
private:
149202

150203
QgsProject *mProject = nullptr;
151204

152205
QList< QgsComposition * > mCompositions;
206+
QList< QgsLayout * > mLayouts;
153207

154208
QgsComposition *createCompositionFromXml( const QDomElement &element, const QDomDocument &doc ) const;
155209

src/core/layout/qgslayout.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ QgsLayoutExporter &QgsLayout::exporter()
117117
return mExporter;
118118
}
119119

120+
void QgsLayout::setName( const QString &name )
121+
{
122+
mName = name;
123+
emit nameChanged( name );
124+
}
125+
120126
QList<QgsLayoutItem *> QgsLayout::selectedLayoutItems( const bool includeLockedItems )
121127
{
122128
QList<QgsLayoutItem *> layoutItemList;

src/core/layout/qgslayout.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class QgsLayoutMultiFrame;
4040
class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContextGenerator, public QgsLayoutUndoObjectInterface
4141
{
4242
Q_OBJECT
43+
Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
4344

4445
public:
4546

@@ -108,7 +109,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
108109
* Sets the layout's name.
109110
* \see name()
110111
*/
111-
void setName( const QString &name ) { mName = name; }
112+
void setName( const QString &name );
112113

113114
/**
114115
* Returns a list of layout items of a specific type.
@@ -585,6 +586,12 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
585586
*/
586587
void refreshed();
587588

589+
/**
590+
* Emitted when the layout's name is changed.
591+
* \see setName()
592+
*/
593+
void nameChanged( const QString &name );
594+
588595
private:
589596

590597
QgsProject *mProject = nullptr;

0 commit comments

Comments
 (0)