Skip to content
Permalink
Browse files

use project template folder and "New project from template" action

  • Loading branch information
etiennesky committed Jul 18, 2012
1 parent c668c5d commit 8e66f2c45cf7d2c74ae9891e2570a3e7748719ee
Showing with 161 additions and 46 deletions.
  1. +62 −7 src/app/qgisapp.cpp
  2. +6 −0 src/app/qgisapp.h
  3. +46 −31 src/app/qgsoptions.cpp
  4. +5 −3 src/app/qgsoptions.h
  5. +6 −1 src/ui/qgisapp.ui
  6. +35 −3 src/ui/qgsoptionsbase.ui
  7. +1 −1 src/ui/qgsprojectpropertiesbase.ui
@@ -485,6 +485,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
createDecorations();
readSettings();
updateRecentProjectPaths();
updateProjectFromTemplates();
activateDeactivateLayerRelatedActions( NULL );

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

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

} // QgisApp::saveRecentProjectPath

// Update file menu with the project templates
void QgisApp::updateProjectFromTemplates()
{
// get list of project files in template dir
QSettings settings;
QString templateDirName = settings.value( "/qgis/projectTemplateDir",
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
QDir templateDir( templateDirName );
QStringList filters( "*.qgs" );
templateDir.setNameFilters( filters );
QStringList templateFiles = templateDir.entryList( filters );

// Remove existing entries
mProjectFromTemplateMenu->clear();

// Add entries
foreach( QString templateFile, templateFiles )
{
mProjectFromTemplateMenu->addAction( templateFile );
}

// add <blank> entry, which loads a blank template (regardless of "default template")
if ( settings.value( "/qgis/newProjectDefault", QVariant( false ) ).toBool() )
mProjectFromTemplateMenu->addAction( tr( "< Blank >" ) );

} // QgisApp::updateProjectFromTemplates

void QgisApp::saveWindowState()
{
// store window and toolbar positions
@@ -2920,17 +2950,12 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
QSettings settings;
if ( ! forceBlank )
{
QString projectTemplate = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
QString projectTemplate = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
if ( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() &&
! projectTemplate.isEmpty() )
{
QgsDebugMsg( QString( "loading template: %1 - %2" ).arg( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() ).arg( projectTemplate ) );
if ( addProject( projectTemplate ) )
{
// set null filename so we don't override the template
QgsProject::instance()->setFileName( QString() );
if ( fileNewFromTemplate( projectTemplate ) )
return;
}
}
}

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

} // QgisApp::fileNew(bool thePromptToSaveFlag)

bool QgisApp::fileNewFromTemplate( QString fileName )
{
QgsDebugMsg( QString( "loading project template: %1" ).arg( fileName ) );
if ( addProject( fileName ) )
{
// set null filename so we don't override the template
QgsProject::instance()->setFileName( QString() );
return true;
}
return false;
}

void QgisApp::fileNewFromTemplateAction( QAction * qAction )
{
if ( ! qAction )
return;

if ( qAction->text() == tr( "< Blank >" ) )
{
fileNewBlank();
}
else
{
QSettings settings;
QString templateDirName = settings.value( "/qgis/projectTemplateDir",
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
fileNewFromTemplate( templateDirName + QDir::separator() + qAction->text() );
}
}


void QgisApp::newVectorLayer()
{
@@ -472,6 +472,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow

QgsMessageLogViewer *logViewer() { return mLogViewer; }

//! Update file menu with the project templates
void updateProjectFromTemplates();

protected:

//! Handle state changes (WindowTitleChange)
@@ -623,6 +626,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void fileNewBlank();
//! As above but allows forcing without prompt and forcing blank project
void fileNew( bool thePromptToSaveFlag, bool forceBlank = false );
//! Create a new file from a template project
bool fileNewFromTemplate( QString fileName );
void fileNewFromTemplateAction( QAction * qAction );
//! Calculate new rasters from existing ones
void showRasterCalculator();
void embedLayers();
@@ -413,7 +413,18 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :

chbAskToSaveProjectChanges->setChecked( settings.value( "qgis/askToSaveProjectChanges", QVariant( true ) ).toBool() );
chbWarnOldProjectVersion->setChecked( settings.value( "/qgis/warnOldProjectVersion", QVariant( true ) ).toBool() );
cbxNewProjectTemplate->setChecked( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() );

// templates
cbxProjectDefaultNew->setChecked( settings.value( "/qgis/newProjectDefault", QVariant( false ) ).toBool() );
QString templateDirName = settings.value( "/qgis/projectTemplateDir",
QgsApplication::qgisSettingsDirPath() + "project_templates" ).toString();
// make dir if it doesn't exists - should just be called once
QDir templateDir;
if ( ! templateDir.exists( templateDirName ) )
{
templateDir.mkdir( templateDirName );
}
leTemplateFolder->setText( templateDirName );

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

#if 0
void QgsOptions::on_pbtnNewProjectTemplate_pressed( )
{
QString lastUsedDir = QFileInfo( leNewProjectTemplate->text() ).canonicalFilePath();
if ( lastUsedDir == "" )
{
QSettings settings;
lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
}

QString fullPath = QFileDialog::getOpenFileName( this,
tr( "Choose a QGIS project file to open" ),
lastUsedDir,
tr( "QGis files" ) + " (*.qgs *.QGS)" );
if ( ! fullPath.isNull() )
{
leNewProjectTemplate->setText( fullPath );
}
}
#endif

void QgsOptions::on_cbxNewProjectTemplate_toggled( bool checked )
void QgsOptions::on_cbxProjectDefaultNew_toggled( bool checked )
{
if ( checked )
{
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
if ( ! QFile::exists( fileName ) )
{
QMessageBox::information( 0, tr( "Save default project" ), tr( "You must set a default project" ) );
cbxNewProjectTemplate->setChecked( false );
cbxProjectDefaultNew->setChecked( false );
}
}
}

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

void QgsOptions::on_pbnResetCurrentProjectDefault_clicked( )
void QgsOptions::on_pbnProjectDefaultReset_clicked( )
{
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "default.qgs" );
QString fileName = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
if ( QFile::exists( fileName ) )
{
QFile::remove( fileName );
}
cbxNewProjectTemplate->setChecked( false );
cbxProjectDefaultNew->setChecked( false );
}

void QgsOptions::on_pbnTemplateFolderBrowse_pressed( )
{
QString newDir = QFileDialog::getExistingDirectory( 0, tr( "Choose a directory to store project template files" ),
leTemplateFolder->text() );
if ( ! newDir.isNull() )
{
leTemplateFolder->setText( newDir );
}
}

void QgsOptions::on_pbnTemplateFolderReset_pressed( )
{
leTemplateFolder->setText( QgsApplication::qgisSettingsDirPath() + QString( "project_templates" ) );
}


void QgsOptions::on_pbnSelectionColor_clicked()
{
#if QT_VERSION >= 0x040500
@@ -797,9 +803,18 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/use_symbology_ng", chkUseSymbologyNG->isChecked() );
settings.setValue( "/qgis/legendDoubleClickAction", cmbLegendDoubleClickAction->currentIndex() );
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );

// project
settings.setValue( "/qgis/askToSaveProjectChanges", chbAskToSaveProjectChanges->isChecked() );
settings.setValue( "/qgis/warnOldProjectVersion", chbWarnOldProjectVersion->isChecked() );
settings.setValue( "/qgis/newProjectTemplate", cbxNewProjectTemplate->isChecked() );
if (( settings.value( "/qgis/projectTemplateDir" ).toString() != leTemplateFolder->text() ) ||
( settings.value( "/qgis/newProjectDefault" ).toBool() != cbxProjectDefaultNew->isChecked() ) )
{
settings.setValue( "/qgis/newProjectDefault", cbxProjectDefaultNew->isChecked() );
settings.setValue( "/qgis/projectTemplateDir", leTemplateFolder->text() );
QgisApp::instance()->updateProjectFromTemplates();
}

settings.setValue( "/qgis/nullValue", leNullValue->text() );
settings.setValue( "/qgis/style", cmbStyle->currentText() );

@@ -50,9 +50,11 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase
QString theme();

public slots:
void on_cbxNewProjectTemplate_toggled( bool checked );
void on_pbnSetCurrentProjectDefault_clicked();
void on_pbnResetCurrentProjectDefault_clicked();
void on_cbxProjectDefaultNew_toggled( bool checked );
void on_pbnProjectDefaultSetCurrent_clicked();
void on_pbnProjectDefaultReset_clicked();
void on_pbnTemplateFolderBrowse_pressed();
void on_pbnTemplateFolderReset_pressed();
//! Slot called when user chooses to change the project wide projection.
void on_pbnSelectProjection_clicked();
//! Slot called when user chooses to change the default 'on the fly' projection.
@@ -62,9 +62,14 @@
<string>Print Composers</string>
</property>
</widget>
<widget class="QMenu" name="mProjectFromTemplateMenu">
<property name="title">
<string>New Project From Template</string>
</property>
</widget>
<addaction name="mActionNewProject"/>
<addaction name="mActionOpenProject"/>
<addaction name="mActionNewBlankProject"/>
<addaction name="mProjectFromTemplateMenu"/>
<addaction name="separator"/>
<addaction name="mRecentProjectsMenu"/>
<addaction name="mActionSaveProject"/>
@@ -94,7 +94,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_16">
<item>
<widget class="QCheckBox" name="cbxNewProjectTemplate">
<widget class="QCheckBox" name="cbxProjectDefaultNew">
<property name="text">
<string>Create new project from default project</string>
</property>
@@ -114,7 +114,7 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="pbnSetCurrentProjectDefault">
<widget class="QPushButton" name="pbnProjectDefaultSetCurrent">
<property name="text">
<string>Set current project as default</string>
</property>
@@ -134,14 +134,46 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="pbnResetCurrentProjectDefault">
<widget class="QPushButton" name="pbnProjectDefaultReset">
<property name="text">
<string>Reset default</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_15">
<item>
<widget class="QLabel" name="label_31">
<property name="text">
<string>Template folder</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="leTemplateFolder">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pbnTemplateFolderBrowse">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pbnTemplateFolderReset">
<property name="text">
<string>Reset</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
@@ -702,7 +702,7 @@
<property name="title">
<string>Options</string>
</property>
<layout class="QGridLayout" name="gridLayout_7">
<layout class="QGridLayout" name="gridLayout_71">
<item row="3" column="0" colspan="4">
<widget class="QCheckBox" name="cbxStyleRandomColors">
<property name="text">

0 comments on commit 8e66f2c

Please sign in to comment.
You can’t perform that action at this time.