Skip to content
Permalink
Browse files
Better method to autogenerate composer names
  • Loading branch information
nyalldawson committed Mar 21, 2017
1 parent b6c7618 commit 148e2bcf5231b1504651ef53dfa97dbf931fe7f3
@@ -19,6 +19,7 @@ class QgsLayoutManager : QObject
QDomElement writeXml( QDomDocument &doc ) const;
bool saveAsTemplate( const QString &name, QDomDocument &doc ) const;
QgsComposition *duplicateComposition( const QString &name, const QString &newName );
QString generateUniqueTitle() const;

signals:
void compositionAdded( const QString& name );
@@ -1082,7 +1082,6 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh

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

QShortcut *zoomInShortCut = new QShortcut( QKeySequence( tr( "Ctrl++" ) ), this );
connect( zoomInShortCut, &QShortcut::activated, mMapCanvas, &QgsMapCanvas::zoomIn );
@@ -1268,7 +1267,6 @@ QgisApp::QgisApp()
, mMapStyleWidget( nullptr )
, mComposerManager( nullptr )
, mpTileScaleWidget( nullptr )
, mLastComposerId( 0 )
, mpGpsWidget( nullptr )
, mLastMapToolMessage( nullptr )
, mLogViewer( nullptr )
@@ -6957,6 +6955,7 @@ bool QgisApp::uniqueComposerTitle( QWidget *parent, QString &composerTitle, bool
else
{
titleValid = true;
newTitle = QgsProject::instance()->layoutManager()->generateUniqueTitle();
}
}
else if ( cNames.indexOf( newTitle, 1 ) >= 0 )
@@ -6977,11 +6976,9 @@ bool QgisApp::uniqueComposerTitle( QWidget *parent, QString &composerTitle, bool

QgsComposer *QgisApp::createNewComposer( QString title )
{
//ask user about name
mLastComposerId++;
if ( title.isEmpty() )
{
title = tr( "Composer %1" ).arg( mLastComposerId );
title = QgsProject::instance()->layoutManager()->generateUniqueTitle();
}
//create new composition object
QgsComposition *composition = new QgsComposition( QgsProject::instance() );
@@ -7060,7 +7057,6 @@ void QgisApp::deletePrintComposers()
emit composerRemoved( c->view() );
delete ( c );
}
mLastComposerId = 0;
}

void QgisApp::composerMenuAboutToShow()
@@ -1884,8 +1884,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QList<QgsDecorationItem *> mDecorationItems;

int mLastComposerId;

//! Persistent GPS toolbox
QgsGPSInformationWidget *mpGpsWidget = nullptr;

@@ -175,6 +175,23 @@ QgsComposition *QgsLayoutManager::duplicateComposition( const QString &name, con
}
}

QString QgsLayoutManager::generateUniqueTitle() const
{
QStringList names;
Q_FOREACH ( QgsComposition *c, mCompositions )
{
names << c->name();
}
QString name;
int id = 1;
while ( name.isEmpty() || names.contains( name ) )
{
name = tr( "Composer %1" ).arg( id );
id++;
}
return name;
}

QgsComposition *QgsLayoutManager::createCompositionFromXml( const QDomElement &element, const QDomDocument &doc ) const
{
QDomNodeList compositionNodeList = element.elementsByTagName( QStringLiteral( "Composition" ) );
@@ -113,6 +113,12 @@ class CORE_EXPORT QgsLayoutManager : public QObject
*/
QgsComposition *duplicateComposition( const QString &name, const QString &newName );

/**
* Generates a unique title for a new composition, which does not
* clash with any already contained by the manager.
*/
QString generateUniqueTitle() const;

signals:

//! Emitted when a composition has been added to the manager
@@ -233,6 +233,24 @@ def testDuplicateComposition(self):
self.assertEqual(result.paperHeight(), 200)
self.assertEqual(result.paperWidth(), 100)

def testGenerateUniqueTitle(self):
project = QgsProject()
manager = QgsLayoutManager(project)
self.assertEqual(manager.generateUniqueTitle(), 'Composer 1')

composition = QgsComposition(project)
composition.setName(manager.generateUniqueTitle())
manager.addComposition(composition)

self.assertEqual(manager.generateUniqueTitle(), 'Composer 2')
composition2 = QgsComposition(project)
composition2.setName(manager.generateUniqueTitle())
manager.addComposition(composition2)

self.assertEqual(manager.generateUniqueTitle(), 'Composer 3')
manager.clear()
self.assertEqual(manager.generateUniqueTitle(), 'Composer 1')


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

0 comments on commit 148e2bc

Please sign in to comment.