Skip to content

Commit 148e2bc

Browse files
committed
Better method to autogenerate composer names
1 parent b6c7618 commit 148e2bc

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

python/core/composer/qgslayoutmanager.sip

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class QgsLayoutManager : QObject
1919
QDomElement writeXml( QDomDocument &doc ) const;
2020
bool saveAsTemplate( const QString &name, QDomDocument &doc ) const;
2121
QgsComposition *duplicateComposition( const QString &name, const QString &newName );
22+
QString generateUniqueTitle() const;
2223

2324
signals:
2425
void compositionAdded( const QString& name );

src/app/qgisapp.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,6 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
10821082

10831083
mMapCanvas->freeze( false );
10841084
mMapCanvas->clearExtentHistory(); // reset zoomnext/zoomlast
1085-
mLastComposerId = 0;
10861085

10871086
QShortcut *zoomInShortCut = new QShortcut( QKeySequence( tr( "Ctrl++" ) ), this );
10881087
connect( zoomInShortCut, &QShortcut::activated, mMapCanvas, &QgsMapCanvas::zoomIn );
@@ -1268,7 +1267,6 @@ QgisApp::QgisApp()
12681267
, mMapStyleWidget( nullptr )
12691268
, mComposerManager( nullptr )
12701269
, mpTileScaleWidget( nullptr )
1271-
, mLastComposerId( 0 )
12721270
, mpGpsWidget( nullptr )
12731271
, mLastMapToolMessage( nullptr )
12741272
, mLogViewer( nullptr )
@@ -6957,6 +6955,7 @@ bool QgisApp::uniqueComposerTitle( QWidget *parent, QString &composerTitle, bool
69576955
else
69586956
{
69596957
titleValid = true;
6958+
newTitle = QgsProject::instance()->layoutManager()->generateUniqueTitle();
69606959
}
69616960
}
69626961
else if ( cNames.indexOf( newTitle, 1 ) >= 0 )
@@ -6977,11 +6976,9 @@ bool QgisApp::uniqueComposerTitle( QWidget *parent, QString &composerTitle, bool
69776976

69786977
QgsComposer *QgisApp::createNewComposer( QString title )
69796978
{
6980-
//ask user about name
6981-
mLastComposerId++;
69826979
if ( title.isEmpty() )
69836980
{
6984-
title = tr( "Composer %1" ).arg( mLastComposerId );
6981+
title = QgsProject::instance()->layoutManager()->generateUniqueTitle();
69856982
}
69866983
//create new composition object
69876984
QgsComposition *composition = new QgsComposition( QgsProject::instance() );
@@ -7060,7 +7057,6 @@ void QgisApp::deletePrintComposers()
70607057
emit composerRemoved( c->view() );
70617058
delete ( c );
70627059
}
7063-
mLastComposerId = 0;
70647060
}
70657061

70667062
void QgisApp::composerMenuAboutToShow()

src/app/qgisapp.h

-2
Original file line numberDiff line numberDiff line change
@@ -1884,8 +1884,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
18841884

18851885
QList<QgsDecorationItem *> mDecorationItems;
18861886

1887-
int mLastComposerId;
1888-
18891887
//! Persistent GPS toolbox
18901888
QgsGPSInformationWidget *mpGpsWidget = nullptr;
18911889

src/core/composer/qgslayoutmanager.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ QgsComposition *QgsLayoutManager::duplicateComposition( const QString &name, con
175175
}
176176
}
177177

178+
QString QgsLayoutManager::generateUniqueTitle() const
179+
{
180+
QStringList names;
181+
Q_FOREACH ( QgsComposition *c, mCompositions )
182+
{
183+
names << c->name();
184+
}
185+
QString name;
186+
int id = 1;
187+
while ( name.isEmpty() || names.contains( name ) )
188+
{
189+
name = tr( "Composer %1" ).arg( id );
190+
id++;
191+
}
192+
return name;
193+
}
194+
178195
QgsComposition *QgsLayoutManager::createCompositionFromXml( const QDomElement &element, const QDomDocument &doc ) const
179196
{
180197
QDomNodeList compositionNodeList = element.elementsByTagName( QStringLiteral( "Composition" ) );

src/core/composer/qgslayoutmanager.h

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ class CORE_EXPORT QgsLayoutManager : public QObject
113113
*/
114114
QgsComposition *duplicateComposition( const QString &name, const QString &newName );
115115

116+
/**
117+
* Generates a unique title for a new composition, which does not
118+
* clash with any already contained by the manager.
119+
*/
120+
QString generateUniqueTitle() const;
121+
116122
signals:
117123

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

tests/src/python/test_qgslayoutmanager.py

+18
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@ def testDuplicateComposition(self):
233233
self.assertEqual(result.paperHeight(), 200)
234234
self.assertEqual(result.paperWidth(), 100)
235235

236+
def testGenerateUniqueTitle(self):
237+
project = QgsProject()
238+
manager = QgsLayoutManager(project)
239+
self.assertEqual(manager.generateUniqueTitle(), 'Composer 1')
240+
241+
composition = QgsComposition(project)
242+
composition.setName(manager.generateUniqueTitle())
243+
manager.addComposition(composition)
244+
245+
self.assertEqual(manager.generateUniqueTitle(), 'Composer 2')
246+
composition2 = QgsComposition(project)
247+
composition2.setName(manager.generateUniqueTitle())
248+
manager.addComposition(composition2)
249+
250+
self.assertEqual(manager.generateUniqueTitle(), 'Composer 3')
251+
manager.clear()
252+
self.assertEqual(manager.generateUniqueTitle(), 'Composer 1')
253+
236254

237255
if __name__ == '__main__':
238256
unittest.main()

0 commit comments

Comments
 (0)