Skip to content

Commit 8e66f2c

Browse files
committed
use project template folder and "New project from template" action
1 parent c668c5d commit 8e66f2c

7 files changed

+161
-46
lines changed

src/app/qgisapp.cpp

+62-7
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
485485
createDecorations();
486486
readSettings();
487487
updateRecentProjectPaths();
488+
updateProjectFromTemplates();
488489
activateDeactivateLayerRelatedActions( NULL );
489490

490491
addDockWidget( Qt::LeftDockWidgetArea, mUndoWidget );
@@ -1140,6 +1141,8 @@ void QgisApp::createMenus()
11401141
// Connect once for the entire submenu.
11411142
connect( mRecentProjectsMenu, SIGNAL( triggered( QAction * ) ),
11421143
this, SLOT( openProject( QAction * ) ) );
1144+
connect( mProjectFromTemplateMenu, SIGNAL( triggered( QAction * ) ),
1145+
this, SLOT( fileNewFromTemplateAction( QAction * ) ) );
11431146

11441147
if ( layout == QDialogButtonBox::GnomeLayout || layout == QDialogButtonBox::MacLayout )
11451148
{
@@ -2111,6 +2114,33 @@ void QgisApp::saveRecentProjectPath( QString projectPath, QSettings & settings )
21112114

21122115
} // QgisApp::saveRecentProjectPath
21132116

2117+
// Update file menu with the project templates
2118+
void QgisApp::updateProjectFromTemplates()
2119+
{
2120+
// get list of project files in template dir
2121+
QSettings settings;
2122+
QString templateDirName = settings.value( "/qgis/projectTemplateDir",
2123+
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
2124+
QDir templateDir( templateDirName );
2125+
QStringList filters( "*.qgs" );
2126+
templateDir.setNameFilters( filters );
2127+
QStringList templateFiles = templateDir.entryList( filters );
2128+
2129+
// Remove existing entries
2130+
mProjectFromTemplateMenu->clear();
2131+
2132+
// Add entries
2133+
foreach( QString templateFile, templateFiles )
2134+
{
2135+
mProjectFromTemplateMenu->addAction( templateFile );
2136+
}
2137+
2138+
// add <blank> entry, which loads a blank template (regardless of "default template")
2139+
if ( settings.value( "/qgis/newProjectDefault", QVariant( false ) ).toBool() )
2140+
mProjectFromTemplateMenu->addAction( tr( "< Blank >" ) );
2141+
2142+
} // QgisApp::updateProjectFromTemplates
2143+
21142144
void QgisApp::saveWindowState()
21152145
{
21162146
// store window and toolbar positions
@@ -2920,17 +2950,12 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
29202950
QSettings settings;
29212951
if ( ! forceBlank )
29222952
{
2923-
QString projectTemplate = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
2953+
QString projectTemplate = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
29242954
if ( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() &&
29252955
! projectTemplate.isEmpty() )
29262956
{
2927-
QgsDebugMsg( QString( "loading template: %1 - %2" ).arg( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() ).arg( projectTemplate ) );
2928-
if ( addProject( projectTemplate ) )
2929-
{
2930-
// set null filename so we don't override the template
2931-
QgsProject::instance()->setFileName( QString() );
2957+
if ( fileNewFromTemplate( projectTemplate ) )
29322958
return;
2933-
}
29342959
}
29352960
}
29362961

@@ -3014,6 +3039,36 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
30143039

30153040
} // QgisApp::fileNew(bool thePromptToSaveFlag)
30163041

3042+
bool QgisApp::fileNewFromTemplate( QString fileName )
3043+
{
3044+
QgsDebugMsg( QString( "loading project template: %1" ).arg( fileName ) );
3045+
if ( addProject( fileName ) )
3046+
{
3047+
// set null filename so we don't override the template
3048+
QgsProject::instance()->setFileName( QString() );
3049+
return true;
3050+
}
3051+
return false;
3052+
}
3053+
3054+
void QgisApp::fileNewFromTemplateAction( QAction * qAction )
3055+
{
3056+
if ( ! qAction )
3057+
return;
3058+
3059+
if ( qAction->text() == tr( "< Blank >" ) )
3060+
{
3061+
fileNewBlank();
3062+
}
3063+
else
3064+
{
3065+
QSettings settings;
3066+
QString templateDirName = settings.value( "/qgis/projectTemplateDir",
3067+
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
3068+
fileNewFromTemplate( templateDirName + QDir::separator() + qAction->text() );
3069+
}
3070+
}
3071+
30173072

30183073
void QgisApp::newVectorLayer()
30193074
{

src/app/qgisapp.h

+6
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
472472

473473
QgsMessageLogViewer *logViewer() { return mLogViewer; }
474474

475+
//! Update file menu with the project templates
476+
void updateProjectFromTemplates();
477+
475478
protected:
476479

477480
//! Handle state changes (WindowTitleChange)
@@ -623,6 +626,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
623626
void fileNewBlank();
624627
//! As above but allows forcing without prompt and forcing blank project
625628
void fileNew( bool thePromptToSaveFlag, bool forceBlank = false );
629+
//! Create a new file from a template project
630+
bool fileNewFromTemplate( QString fileName );
631+
void fileNewFromTemplateAction( QAction * qAction );
626632
//! Calculate new rasters from existing ones
627633
void showRasterCalculator();
628634
void embedLayers();

src/app/qgsoptions.cpp

+46-31
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,18 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
413413

414414
chbAskToSaveProjectChanges->setChecked( settings.value( "qgis/askToSaveProjectChanges", QVariant( true ) ).toBool() );
415415
chbWarnOldProjectVersion->setChecked( settings.value( "/qgis/warnOldProjectVersion", QVariant( true ) ).toBool() );
416-
cbxNewProjectTemplate->setChecked( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() );
416+
417+
// templates
418+
cbxProjectDefaultNew->setChecked( settings.value( "/qgis/newProjectDefault", QVariant( false ) ).toBool() );
419+
QString templateDirName = settings.value( "/qgis/projectTemplateDir",
420+
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
421+
// make dir if it doesn't exists - should just be called once
422+
QDir templateDir;
423+
if ( ! templateDir.exists( templateDirName ) )
424+
{
425+
templateDir.mkdir( templateDirName );
426+
}
427+
leTemplateFolder->setText( templateDirName );
417428

418429
cmbWheelAction->setCurrentIndex( settings.value( "/qgis/wheel_action", 2 ).toInt() );
419430
spinZoomFactor->setValue( settings.value( "/qgis/zoom_factor", 2 ).toDouble() );
@@ -578,43 +589,22 @@ QgsOptions::~QgsOptions()
578589
settings.setValue( "/Windows/Options/row", tabWidget->currentIndex() );
579590
}
580591

581-
#if 0
582-
void QgsOptions::on_pbtnNewProjectTemplate_pressed( )
583-
{
584-
QString lastUsedDir = QFileInfo( leNewProjectTemplate->text() ).canonicalFilePath();
585-
if ( lastUsedDir == "" )
586-
{
587-
QSettings settings;
588-
lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
589-
}
590-
591-
QString fullPath = QFileDialog::getOpenFileName( this,
592-
tr( "Choose a QGIS project file to open" ),
593-
lastUsedDir,
594-
tr( "QGis files" ) + " (*.qgs *.QGS)" );
595-
if ( ! fullPath.isNull() )
596-
{
597-
leNewProjectTemplate->setText( fullPath );
598-
}
599-
}
600-
#endif
601-
602-
void QgsOptions::on_cbxNewProjectTemplate_toggled( bool checked )
592+
void QgsOptions::on_cbxProjectDefaultNew_toggled( bool checked )
603593
{
604594
if ( checked )
605595
{
606-
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
596+
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
607597
if ( ! QFile::exists( fileName ) )
608598
{
609599
QMessageBox::information( 0, tr( "Save default project" ), tr( "You must set a default project" ) );
610-
cbxNewProjectTemplate->setChecked( false );
600+
cbxProjectDefaultNew->setChecked( false );
611601
}
612602
}
613603
}
614604

615-
void QgsOptions::on_pbnSetCurrentProjectDefault_clicked( )
605+
void QgsOptions::on_pbnProjectDefaultSetCurrent_clicked( )
616606
{
617-
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
607+
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
618608
if ( QgsProject::instance()->write( QFileInfo( fileName ) ) )
619609
{
620610
QMessageBox::information( 0, tr( "Save default project" ), tr( "Current project saved as default" ) );
@@ -625,16 +615,32 @@ void QgsOptions::on_pbnSetCurrentProjectDefault_clicked( )
625615
}
626616
}
627617

628-
void QgsOptions::on_pbnResetCurrentProjectDefault_clicked( )
618+
void QgsOptions::on_pbnProjectDefaultReset_clicked( )
629619
{
630-
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
620+
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
631621
if ( QFile::exists( fileName ) )
632622
{
633623
QFile::remove( fileName );
634624
}
635-
cbxNewProjectTemplate->setChecked( false );
625+
cbxProjectDefaultNew->setChecked( false );
636626
}
637627

628+
void QgsOptions::on_pbnTemplateFolderBrowse_pressed( )
629+
{
630+
QString newDir = QFileDialog::getExistingDirectory( 0, tr( "Choose a directory to store project template files" ),
631+
leTemplateFolder->text() );
632+
if ( ! newDir.isNull() )
633+
{
634+
leTemplateFolder->setText( newDir );
635+
}
636+
}
637+
638+
void QgsOptions::on_pbnTemplateFolderReset_pressed( )
639+
{
640+
leTemplateFolder->setText( QgsApplication::qgisSettingsDirPath() + QString( "project_templates" ) );
641+
}
642+
643+
638644
void QgsOptions::on_pbnSelectionColor_clicked()
639645
{
640646
#if QT_VERSION >= 0x040500
@@ -797,9 +803,18 @@ void QgsOptions::saveOptions()
797803
settings.setValue( "/qgis/use_symbology_ng", chkUseSymbologyNG->isChecked() );
798804
settings.setValue( "/qgis/legendDoubleClickAction", cmbLegendDoubleClickAction->currentIndex() );
799805
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );
806+
807+
// project
800808
settings.setValue( "/qgis/askToSaveProjectChanges", chbAskToSaveProjectChanges->isChecked() );
801809
settings.setValue( "/qgis/warnOldProjectVersion", chbWarnOldProjectVersion->isChecked() );
802-
settings.setValue( "/qgis/newProjectTemplate", cbxNewProjectTemplate->isChecked() );
810+
if (( settings.value( "/qgis/projectTemplateDir" ).toString() != leTemplateFolder->text() ) ||
811+
( settings.value( "/qgis/newProjectDefault" ).toBool() != cbxProjectDefaultNew->isChecked() ) )
812+
{
813+
settings.setValue( "/qgis/newProjectDefault", cbxProjectDefaultNew->isChecked() );
814+
settings.setValue( "/qgis/projectTemplateDir", leTemplateFolder->text() );
815+
QgisApp::instance()->updateProjectFromTemplates();
816+
}
817+
803818
settings.setValue( "/qgis/nullValue", leNullValue->text() );
804819
settings.setValue( "/qgis/style", cmbStyle->currentText() );
805820

src/app/qgsoptions.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase
5050
QString theme();
5151

5252
public slots:
53-
void on_cbxNewProjectTemplate_toggled( bool checked );
54-
void on_pbnSetCurrentProjectDefault_clicked();
55-
void on_pbnResetCurrentProjectDefault_clicked();
53+
void on_cbxProjectDefaultNew_toggled( bool checked );
54+
void on_pbnProjectDefaultSetCurrent_clicked();
55+
void on_pbnProjectDefaultReset_clicked();
56+
void on_pbnTemplateFolderBrowse_pressed();
57+
void on_pbnTemplateFolderReset_pressed();
5658
//! Slot called when user chooses to change the project wide projection.
5759
void on_pbnSelectProjection_clicked();
5860
//! Slot called when user chooses to change the default 'on the fly' projection.

src/ui/qgisapp.ui

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@
6262
<string>Print Composers</string>
6363
</property>
6464
</widget>
65+
<widget class="QMenu" name="mProjectFromTemplateMenu">
66+
<property name="title">
67+
<string>New Project From Template</string>
68+
</property>
69+
</widget>
6570
<addaction name="mActionNewProject"/>
6671
<addaction name="mActionOpenProject"/>
67-
<addaction name="mActionNewBlankProject"/>
72+
<addaction name="mProjectFromTemplateMenu"/>
6873
<addaction name="separator"/>
6974
<addaction name="mRecentProjectsMenu"/>
7075
<addaction name="mActionSaveProject"/>

src/ui/qgsoptionsbase.ui

+35-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<item>
9595
<layout class="QHBoxLayout" name="horizontalLayout_16">
9696
<item>
97-
<widget class="QCheckBox" name="cbxNewProjectTemplate">
97+
<widget class="QCheckBox" name="cbxProjectDefaultNew">
9898
<property name="text">
9999
<string>Create new project from default project</string>
100100
</property>
@@ -114,7 +114,7 @@
114114
</spacer>
115115
</item>
116116
<item>
117-
<widget class="QPushButton" name="pbnSetCurrentProjectDefault">
117+
<widget class="QPushButton" name="pbnProjectDefaultSetCurrent">
118118
<property name="text">
119119
<string>Set current project as default</string>
120120
</property>
@@ -134,14 +134,46 @@
134134
</spacer>
135135
</item>
136136
<item>
137-
<widget class="QPushButton" name="pbnResetCurrentProjectDefault">
137+
<widget class="QPushButton" name="pbnProjectDefaultReset">
138138
<property name="text">
139139
<string>Reset default</string>
140140
</property>
141141
</widget>
142142
</item>
143143
</layout>
144144
</item>
145+
<item>
146+
<layout class="QHBoxLayout" name="horizontalLayout_15">
147+
<item>
148+
<widget class="QLabel" name="label_31">
149+
<property name="text">
150+
<string>Template folder</string>
151+
</property>
152+
</widget>
153+
</item>
154+
<item>
155+
<widget class="QLineEdit" name="leTemplateFolder">
156+
<property name="enabled">
157+
<bool>false</bool>
158+
</property>
159+
</widget>
160+
</item>
161+
<item>
162+
<widget class="QPushButton" name="pbnTemplateFolderBrowse">
163+
<property name="text">
164+
<string>Browse</string>
165+
</property>
166+
</widget>
167+
</item>
168+
<item>
169+
<widget class="QPushButton" name="pbnTemplateFolderReset">
170+
<property name="text">
171+
<string>Reset</string>
172+
</property>
173+
</widget>
174+
</item>
175+
</layout>
176+
</item>
145177
</layout>
146178
</widget>
147179
</item>

src/ui/qgsprojectpropertiesbase.ui

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@
702702
<property name="title">
703703
<string>Options</string>
704704
</property>
705-
<layout class="QGridLayout" name="gridLayout_7">
705+
<layout class="QGridLayout" name="gridLayout_71">
706706
<item row="3" column="0" colspan="4">
707707
<widget class="QCheckBox" name="cbxStyleRandomColors">
708708
<property name="text">

0 commit comments

Comments
 (0)