Skip to content

Commit 49a16fc

Browse files
committed
Add other composer templates loading paths
1 parent 65fb72a commit 49a16fc

File tree

7 files changed

+214
-54
lines changed

7 files changed

+214
-54
lines changed

src/app/composer/qgscomposermanager.cpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,12 @@ QgsComposerManager::QgsComposerManager( QWidget * parent, Qt::WindowFlags f ): Q
7474

7575
mUserTemplatesDir = QgsApplication::qgisSettingsDirPath() + "/composer_templates";
7676
QMap<QString, QString> userTemplateMap = defaultTemplates( true );
77-
if ( userTemplateMap.size() > 0 )
78-
{
79-
mTemplate->insertSeparator( mTemplate->count() );
80-
QMap<QString, QString>::const_iterator templateIt = userTemplateMap.constBegin();
81-
for ( ; templateIt != userTemplateMap.constEnd(); ++templateIt )
82-
{
83-
mTemplate->addItem( templateIt.key(), templateIt.value() );
84-
}
85-
}
77+
this->addTemplates( userTemplateMap );
8678

8779
mDefaultTemplatesDir = QgsApplication::pkgDataPath() + "/composer_templates";
8880
QMap<QString, QString> defaultTemplateMap = defaultTemplates( false );
89-
if ( defaultTemplateMap.size() > 0 )
90-
{
91-
mTemplate->insertSeparator( mTemplate->count() );
92-
QMap<QString, QString>::const_iterator templateIt = defaultTemplateMap.constBegin();
93-
for ( ; templateIt != defaultTemplateMap.constEnd(); ++templateIt )
94-
{
95-
mTemplate->addItem( templateIt.key(), templateIt.value() );
96-
}
97-
}
81+
this->addTemplates( defaultTemplateMap );
82+
this->addTemplates( this->otherTemplates() );
9883

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

@@ -135,6 +120,20 @@ void QgsComposerManager::refreshComposers()
135120
}
136121
}
137122

123+
void QgsComposerManager::addTemplates( QMap<QString, QString> templates )
124+
{
125+
if ( templates.size() > 0 )
126+
{
127+
mTemplate->insertSeparator( mTemplate->count() );
128+
QMap<QString, QString>::const_iterator templateIt = templates.constBegin();
129+
for ( ; templateIt != templates.constEnd(); ++templateIt )
130+
{
131+
mTemplate->addItem( templateIt.key(), templateIt.value() );
132+
}
133+
}
134+
135+
}
136+
138137
void QgsComposerManager::activate()
139138
{
140139
raise();
@@ -144,17 +143,39 @@ void QgsComposerManager::activate()
144143

145144
QMap<QString, QString> QgsComposerManager::defaultTemplates( bool fromUser ) const
146145
{
147-
QMap<QString, QString> templateMap;
148-
149146
//search for default templates in $pkgDataPath/composer_templates
150147
// user templates in $qgisSettingsDirPath/composer_templates
151-
QDir defaultTemplateDir( fromUser ? mUserTemplatesDir : mDefaultTemplatesDir );
152-
if ( !defaultTemplateDir.exists() )
148+
return templatesFromPath( fromUser ? mUserTemplatesDir : mDefaultTemplatesDir );
149+
}
150+
151+
QMap<QString, QString> QgsComposerManager::otherTemplates() const
152+
{
153+
QMap<QString, QString> templateMap;
154+
QStringList paths = QgsApplication::composerTemplatePaths();
155+
Q_FOREACH ( QString path, paths )
156+
{
157+
QMap<QString, QString> templates = templatesFromPath( path );
158+
QMap<QString, QString>::const_iterator templateIt = templates.constBegin();
159+
for ( ; templateIt != templates.constEnd(); ++templateIt )
160+
{
161+
templateMap.insert( templateIt.key(), templateIt.value() );
162+
}
163+
}
164+
return templateMap;
165+
}
166+
167+
168+
QMap<QString, QString> QgsComposerManager::templatesFromPath( QString path ) const
169+
{
170+
QMap<QString, QString> templateMap;
171+
172+
QDir templateDir( path );
173+
if ( !templateDir.exists() )
153174
{
154175
return templateMap;
155176
}
156177

157-
QFileInfoList fileInfoList = defaultTemplateDir.entryInfoList( QDir::Files );
178+
QFileInfoList fileInfoList = templateDir.entryInfoList( QDir::Files );
158179
QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
159180
for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
160181
{

src/app/composer/qgscomposermanager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
5252
QgsComposerManager( QWidget * parent = 0, Qt::WindowFlags f = 0 );
5353
~QgsComposerManager();
5454

55+
void addTemplates( QMap<QString, QString> templates );
56+
5557
public slots:
5658
/** Raise, unminimize and activate this window */
5759
void activate();
@@ -65,6 +67,9 @@ class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
6567
* @param fromUser whether to return user templates from ~/.qgis/composer_templates
6668
*/
6769
QMap<QString, QString> defaultTemplates( bool fromUser = false ) const;
70+
QMap<QString, QString> otherTemplates() const;
71+
72+
QMap<QString, QString> templatesFromPath( QString path ) const;
6873

6974
/** Open local directory with user's system, creating it if not present
7075
*/

src/app/qgsoptions.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
244244
}
245245
}
246246

247+
myPaths = settings.value( "composer/searchPathsForTemplates", "" ).toString();
248+
if ( !myPaths.isEmpty() )
249+
{
250+
QStringList myPathList = myPaths.split( '|' );
251+
QStringList::const_iterator pathIt = myPathList.constBegin();
252+
for ( ; pathIt != myPathList.constEnd(); ++pathIt )
253+
{
254+
QListWidgetItem* newItem = new QListWidgetItem( mListComposerTemplatePaths );
255+
newItem->setText( *pathIt );
256+
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
257+
mListComposerTemplatePaths->addItem( newItem );
258+
}
259+
}
260+
247261
//Network timeout
248262
mNetworkTimeoutSpinBox->setValue( settings.value( "/qgis/networkAndProxy/networkTimeout", "60000" ).toInt() );
249263
leUserAgent->setText( settings.value( "/qgis/networkAndProxy/userAgent", "Mozilla/5.0" ).toString() );
@@ -1012,6 +1026,17 @@ void QgsOptions::saveOptions()
10121026
}
10131027
settings.setValue( "svg/searchPathsForSVG", myPaths );
10141028

1029+
myPaths.clear();
1030+
for ( int i = 0; i < mListComposerTemplatePaths->count(); ++i )
1031+
{
1032+
if ( i != 0 )
1033+
{
1034+
myPaths += '|';
1035+
}
1036+
myPaths += mListComposerTemplatePaths->item( i )->text();
1037+
}
1038+
settings.setValue( "composer/searchPathsForTemplates", myPaths );
1039+
10151040
//Network timeout
10161041
settings.setValue( "/qgis/networkAndProxy/networkTimeout", mNetworkTimeoutSpinBox->value() );
10171042
settings.setValue( "/qgis/networkAndProxy/userAgent", leUserAgent->text() );
@@ -1615,6 +1640,33 @@ void QgsOptions::on_mBtnRemovePluginPath_clicked()
16151640
delete itemToRemove;
16161641
}
16171642

1643+
void QgsOptions::on_mBtnAddTemplatePath_clicked()
1644+
{
1645+
QString myDir = QFileDialog::getExistingDirectory(
1646+
this,
1647+
tr( "Choose a directory" ),
1648+
QDir::toNativeSeparators( QDir::homePath() ),
1649+
QFileDialog::ShowDirsOnly
1650+
);
1651+
1652+
if ( ! myDir.isEmpty() )
1653+
{
1654+
QListWidgetItem* newItem = new QListWidgetItem( mListComposerTemplatePaths );
1655+
newItem->setText( myDir );
1656+
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
1657+
mListComposerTemplatePaths->addItem( newItem );
1658+
mListComposerTemplatePaths->setCurrentItem( newItem );
1659+
}
1660+
}
1661+
1662+
void QgsOptions::on_mBtnRemoveTemplatePath_clicked()
1663+
{
1664+
int currentRow = mListComposerTemplatePaths->currentRow();
1665+
QListWidgetItem* itemToRemove = mListComposerTemplatePaths->takeItem( currentRow );
1666+
delete itemToRemove;
1667+
}
1668+
1669+
16181670
void QgsOptions::on_mBtnAddSVGPath_clicked()
16191671
{
16201672
QString myDir = QFileDialog::getExistingDirectory(

src/app/qgsoptions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
139139
* used for finding Plugin libs. */
140140
void on_mBtnRemovePluginPath_clicked();
141141

142+
/* Let the user add a path to the list of search paths
143+
* used for finding composer template files. */
144+
void on_mBtnAddTemplatePath_clicked();
145+
146+
/* Let the user remove a path from the list of search paths
147+
* used for finding composer template files. */
148+
void on_mBtnRemoveTemplatePath_clicked();
149+
142150
/* Let the user add a path to the list of search paths
143151
* used for finding SVG files. */
144152
void on_mBtnAddSVGPath_clicked();

src/core/qgsapplication.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,24 @@ QStringList QgsApplication::svgPaths()
690690
return myPathList;
691691
}
692692

693+
/*!
694+
Returns the paths to the composer template directories.
695+
*/
696+
QStringList QgsApplication::composerTemplatePaths()
697+
{
698+
//local directories to search when looking for an SVG with a given basename
699+
//defined by user in options dialog
700+
QSettings settings;
701+
QStringList myPathList;
702+
QString myPaths = settings.value( "composer/searchPathsForTemplates", "" ).toString();
703+
if ( !myPaths.isEmpty() )
704+
{
705+
myPathList = myPaths.split( '|' );
706+
}
707+
708+
return myPathList;
709+
}
710+
693711
QString QgsApplication::userStyleV2Path()
694712
{
695713
return qgisSettingsDirPath() + QLatin1String( "symbology-ng-style.db" );

src/core/qgsapplication.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ class CORE_EXPORT QgsApplication : public QApplication
153153
//! Returns the pathes to svg directories.
154154
static QStringList svgPaths();
155155

156+
//! Returns the pathes to composer template directories
157+
static QStringList composerTemplatePaths();
158+
156159
//! Returns the system environment variables passed to application.
157160
static QMap<QString, QString> systemEnvVars() { return ABISYM( mSystemEnvVars ); }
158161

0 commit comments

Comments
 (0)