Skip to content

Commit

Permalink
Merge pull request #30074 from m-kuhn/composer_templates
Browse files Browse the repository at this point in the history
Fix "default layout templates" button
  • Loading branch information
m-kuhn committed Jun 4, 2019
2 parents 416e387 + b950c9e commit 95fb325
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
38 changes: 26 additions & 12 deletions src/app/layout/qgslayoutmanagerdialog.cpp
Expand Up @@ -94,12 +94,15 @@ QgsLayoutManagerDialog::QgsLayoutManagerDialog( QWidget *parent, Qt::WindowFlags

mUserTemplatesDir = QgsApplication::qgisSettingsDirPath() + "/composer_templates";
QMap<QString, QString> userTemplateMap = defaultTemplates( true );
this->addTemplates( userTemplateMap );
addTemplates( userTemplateMap );

// TODO QGIS 4: Remove this, default templates should not be shipped in the application folder
mDefaultTemplatesDir = QgsApplication::pkgDataPath() + "/composer_templates";
QMap<QString, QString> defaultTemplateMap = defaultTemplates( false );
this->addTemplates( defaultTemplateMap );
this->addTemplates( this->otherTemplates() );
addTemplates( defaultTemplateMap );
addTemplates( otherTemplates() );

mTemplatesDefaultDirBtn->setToolTip( tr( "Use <i>Settings --> Options --> Layouts --> Layout Paths</i> to configure the folders in which QGIS will search for print layout templates." ) );

toggleButtons();
}
Expand Down Expand Up @@ -148,6 +151,7 @@ void QgsLayoutManagerDialog::addTemplates( const QMap<QString, QString> &templat

void QgsLayoutManagerDialog::activate()
{
updateTemplateButtonEnabledState();
raise();
setWindowState( windowState() & ~Qt::WindowMinimized );
activateWindow();
Expand All @@ -163,9 +167,8 @@ QMap<QString, QString> QgsLayoutManagerDialog::defaultTemplates( bool fromUser )
QMap<QString, QString> QgsLayoutManagerDialog::otherTemplates() const
{
QMap<QString, QString> templateMap;
QStringList paths = QgsApplication::layoutTemplatePaths();
const auto constPaths = paths;
for ( const QString &path : constPaths )
const QStringList paths = QgsApplication::layoutTemplatePaths();
for ( const QString &path : paths )
{
QMap<QString, QString> templates = templatesFromPath( path );
QMap<QString, QString>::const_iterator templateIt = templates.constBegin();
Expand All @@ -187,13 +190,12 @@ QMap<QString, QString> QgsLayoutManagerDialog::templatesFromPath( const QString
return templateMap;
}

QFileInfoList fileInfoList = templateDir.entryInfoList( QDir::Files );
QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
const QFileInfoList fileInfoList = templateDir.entryInfoList( QDir::Files );
for ( const QFileInfo &info : fileInfoList )
{
if ( infoIt->suffix().compare( QLatin1String( "qpt" ), Qt::CaseInsensitive ) == 0 )
if ( info.suffix().compare( QLatin1String( "qpt" ), Qt::CaseInsensitive ) == 0 )
{
templateMap.insert( infoIt->baseName(), infoIt->absoluteFilePath() );
templateMap.insert( info.baseName(), info.absoluteFilePath() );
}
}
return templateMap;
Expand Down Expand Up @@ -286,7 +288,14 @@ void QgsLayoutManagerDialog::mTemplate_currentIndexChanged( int indx )

void QgsLayoutManagerDialog::mTemplatesDefaultDirBtn_pressed()
{
openLocalDirectory( mDefaultTemplatesDir );
if ( QDir( mDefaultTemplatesDir ).exists() )
openLocalDirectory( mDefaultTemplatesDir );
else
{
const QStringList paths = QgsApplication::layoutTemplatePaths();
if ( !paths.empty() )
openLocalDirectory( paths.at( 0 ) );
}
}

void QgsLayoutManagerDialog::mTemplatesUserDirBtn_pressed()
Expand Down Expand Up @@ -332,6 +341,11 @@ void QgsLayoutManagerDialog::openLocalDirectory( const QString &localDirPath )
}
}

void QgsLayoutManagerDialog::updateTemplateButtonEnabledState()
{
mTemplatesDefaultDirBtn->setEnabled( QDir( mDefaultTemplatesDir ).exists() || !QgsApplication::layoutTemplatePaths().empty() );
}

#ifdef Q_OS_MAC
void QgsLayoutManagerDialog::showEvent( QShowEvent *event )
{
Expand Down
6 changes: 4 additions & 2 deletions src/app/layout/qgslayoutmanagerdialog.h
Expand Up @@ -48,18 +48,20 @@ class QgsLayoutManagerDialog: public QDialog, private Ui::QgsLayoutManagerBase

/**
* Returns the default templates (key: template name, value: absolute path to template file)
* \param fromUser whether to return user templates from ~/.qgis/composer_templates
* \param fromUser whether to return user templates from [profile folder]/composer_templates
*/
QMap<QString, QString> defaultTemplates( bool fromUser = false ) const;
QMap<QString, QString> otherTemplates() const;

QMap<QString, QString> templatesFromPath( const QString &path ) const;

/**
* Open local directory with user's system, creating it if not present
* Opens local directory with user's system and tries to create it if not present
*/
void openLocalDirectory( const QString &localDirPath );

void updateTemplateButtonEnabledState();

QString mDefaultTemplatesDir;
QString mUserTemplatesDir;
QPushButton *mCreateReportButton = nullptr;
Expand Down

0 comments on commit 95fb325

Please sign in to comment.