Skip to content

Commit

Permalink
Add other composer templates loading paths
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Nov 6, 2015
1 parent 65fb72a commit 49a16fc
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 54 deletions.
67 changes: 44 additions & 23 deletions src/app/composer/qgscomposermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,12 @@ QgsComposerManager::QgsComposerManager( QWidget * parent, Qt::WindowFlags f ): Q

mUserTemplatesDir = QgsApplication::qgisSettingsDirPath() + "/composer_templates";
QMap<QString, QString> userTemplateMap = defaultTemplates( true );
if ( userTemplateMap.size() > 0 )
{
mTemplate->insertSeparator( mTemplate->count() );
QMap<QString, QString>::const_iterator templateIt = userTemplateMap.constBegin();
for ( ; templateIt != userTemplateMap.constEnd(); ++templateIt )
{
mTemplate->addItem( templateIt.key(), templateIt.value() );
}
}
this->addTemplates( userTemplateMap );

mDefaultTemplatesDir = QgsApplication::pkgDataPath() + "/composer_templates";
QMap<QString, QString> defaultTemplateMap = defaultTemplates( false );
if ( defaultTemplateMap.size() > 0 )
{
mTemplate->insertSeparator( mTemplate->count() );
QMap<QString, QString>::const_iterator templateIt = defaultTemplateMap.constBegin();
for ( ; templateIt != defaultTemplateMap.constEnd(); ++templateIt )
{
mTemplate->addItem( templateIt.key(), templateIt.value() );
}
}
this->addTemplates( defaultTemplateMap );
this->addTemplates( this->otherTemplates() );

mTemplatePathLineEdit->setText( settings.value( "/UI/ComposerManager/templatePath", QString() ).toString() );

Expand Down Expand Up @@ -135,6 +120,20 @@ void QgsComposerManager::refreshComposers()
}
}

void QgsComposerManager::addTemplates( QMap<QString, QString> templates )
{
if ( templates.size() > 0 )
{
mTemplate->insertSeparator( mTemplate->count() );
QMap<QString, QString>::const_iterator templateIt = templates.constBegin();
for ( ; templateIt != templates.constEnd(); ++templateIt )
{
mTemplate->addItem( templateIt.key(), templateIt.value() );
}
}

}

void QgsComposerManager::activate()
{
raise();
Expand All @@ -144,17 +143,39 @@ void QgsComposerManager::activate()

QMap<QString, QString> QgsComposerManager::defaultTemplates( bool fromUser ) const
{
QMap<QString, QString> templateMap;

//search for default templates in $pkgDataPath/composer_templates
// user templates in $qgisSettingsDirPath/composer_templates
QDir defaultTemplateDir( fromUser ? mUserTemplatesDir : mDefaultTemplatesDir );
if ( !defaultTemplateDir.exists() )
return templatesFromPath( fromUser ? mUserTemplatesDir : mDefaultTemplatesDir );
}

QMap<QString, QString> QgsComposerManager::otherTemplates() const
{
QMap<QString, QString> templateMap;
QStringList paths = QgsApplication::composerTemplatePaths();
Q_FOREACH ( QString path, paths )
{
QMap<QString, QString> templates = templatesFromPath( path );
QMap<QString, QString>::const_iterator templateIt = templates.constBegin();
for ( ; templateIt != templates.constEnd(); ++templateIt )
{
templateMap.insert( templateIt.key(), templateIt.value() );
}
}
return templateMap;
}


QMap<QString, QString> QgsComposerManager::templatesFromPath( QString path ) const
{
QMap<QString, QString> templateMap;

QDir templateDir( path );
if ( !templateDir.exists() )
{
return templateMap;
}

QFileInfoList fileInfoList = defaultTemplateDir.entryInfoList( QDir::Files );
QFileInfoList fileInfoList = templateDir.entryInfoList( QDir::Files );
QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
{
Expand Down
5 changes: 5 additions & 0 deletions src/app/composer/qgscomposermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
QgsComposerManager( QWidget * parent = 0, Qt::WindowFlags f = 0 );
~QgsComposerManager();

void addTemplates( QMap<QString, QString> templates );

public slots:
/** Raise, unminimize and activate this window */
void activate();
Expand All @@ -65,6 +67,9 @@ class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
* @param fromUser whether to return user templates from ~/.qgis/composer_templates
*/
QMap<QString, QString> defaultTemplates( bool fromUser = false ) const;
QMap<QString, QString> otherTemplates() const;

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

/** Open local directory with user's system, creating it if not present
*/
Expand Down
52 changes: 52 additions & 0 deletions src/app/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
}
}

myPaths = settings.value( "composer/searchPathsForTemplates", "" ).toString();
if ( !myPaths.isEmpty() )
{
QStringList myPathList = myPaths.split( '|' );
QStringList::const_iterator pathIt = myPathList.constBegin();
for ( ; pathIt != myPathList.constEnd(); ++pathIt )
{
QListWidgetItem* newItem = new QListWidgetItem( mListComposerTemplatePaths );
newItem->setText( *pathIt );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListComposerTemplatePaths->addItem( newItem );
}
}

//Network timeout
mNetworkTimeoutSpinBox->setValue( settings.value( "/qgis/networkAndProxy/networkTimeout", "60000" ).toInt() );
leUserAgent->setText( settings.value( "/qgis/networkAndProxy/userAgent", "Mozilla/5.0" ).toString() );
Expand Down Expand Up @@ -1012,6 +1026,17 @@ void QgsOptions::saveOptions()
}
settings.setValue( "svg/searchPathsForSVG", myPaths );

myPaths.clear();
for ( int i = 0; i < mListComposerTemplatePaths->count(); ++i )
{
if ( i != 0 )
{
myPaths += '|';
}
myPaths += mListComposerTemplatePaths->item( i )->text();
}
settings.setValue( "composer/searchPathsForTemplates", myPaths );

//Network timeout
settings.setValue( "/qgis/networkAndProxy/networkTimeout", mNetworkTimeoutSpinBox->value() );
settings.setValue( "/qgis/networkAndProxy/userAgent", leUserAgent->text() );
Expand Down Expand Up @@ -1615,6 +1640,33 @@ void QgsOptions::on_mBtnRemovePluginPath_clicked()
delete itemToRemove;
}

void QgsOptions::on_mBtnAddTemplatePath_clicked()
{
QString myDir = QFileDialog::getExistingDirectory(
this,
tr( "Choose a directory" ),
QDir::toNativeSeparators( QDir::homePath() ),
QFileDialog::ShowDirsOnly
);

if ( ! myDir.isEmpty() )
{
QListWidgetItem* newItem = new QListWidgetItem( mListComposerTemplatePaths );
newItem->setText( myDir );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListComposerTemplatePaths->addItem( newItem );
mListComposerTemplatePaths->setCurrentItem( newItem );
}
}

void QgsOptions::on_mBtnRemoveTemplatePath_clicked()
{
int currentRow = mListComposerTemplatePaths->currentRow();
QListWidgetItem* itemToRemove = mListComposerTemplatePaths->takeItem( currentRow );
delete itemToRemove;
}


void QgsOptions::on_mBtnAddSVGPath_clicked()
{
QString myDir = QFileDialog::getExistingDirectory(
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
* used for finding Plugin libs. */
void on_mBtnRemovePluginPath_clicked();

/* Let the user add a path to the list of search paths
* used for finding composer template files. */
void on_mBtnAddTemplatePath_clicked();

/* Let the user remove a path from the list of search paths
* used for finding composer template files. */
void on_mBtnRemoveTemplatePath_clicked();

/* Let the user add a path to the list of search paths
* used for finding SVG files. */
void on_mBtnAddSVGPath_clicked();
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,24 @@ QStringList QgsApplication::svgPaths()
return myPathList;
}

/*!
Returns the paths to the composer template directories.
*/
QStringList QgsApplication::composerTemplatePaths()
{
//local directories to search when looking for an SVG with a given basename
//defined by user in options dialog
QSettings settings;
QStringList myPathList;
QString myPaths = settings.value( "composer/searchPathsForTemplates", "" ).toString();
if ( !myPaths.isEmpty() )
{
myPathList = myPaths.split( '|' );
}

return myPathList;
}

QString QgsApplication::userStyleV2Path()
{
return qgisSettingsDirPath() + QLatin1String( "symbology-ng-style.db" );
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class CORE_EXPORT QgsApplication : public QApplication
//! Returns the pathes to svg directories.
static QStringList svgPaths();

//! Returns the pathes to composer template directories
static QStringList composerTemplatePaths();

//! Returns the system environment variables passed to application.
static QMap<QString, QString> systemEnvVars() { return ABISYM( mSystemEnvVars ); }

Expand Down
Loading

0 comments on commit 49a16fc

Please sign in to comment.