Skip to content

Commit

Permalink
Update project file opening precedence order at app launch
Browse files Browse the repository at this point in the history
- Fix typo so default template on new project works
- Project open precedence order is (highest first):
-  1) specified on command line
-  2) specified or most recent via Options gui
-  3) new project from template
-  4) new blank project
- Keep new project template from loading in app constructor (in case already project via command line)
- Keep new project template from loading if previous auto-open project crashed
- Add status bar messages about loading project during addProject()
  • Loading branch information
dakcarto committed Jan 31, 2013
1 parent 9bcddc5 commit 859854f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 47 deletions.
153 changes: 106 additions & 47 deletions src/app/qgisapp.cpp
Expand Up @@ -713,7 +713,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
// update windows
qApp->processEvents();

fileNew(); // prepare empty project
fileNewBlank(); // prepare empty project

} // QgisApp ctor

Expand Down Expand Up @@ -3128,18 +3128,7 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
}
}

// load template instead of loading defaults - or should this be done *after* loading defaults?
QSettings settings;
if ( ! forceBlank )
{
QString projectTemplate = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
if ( settings.value( "/qgis/newProjectTemplate", QVariant( false ) ).toBool() &&
! projectTemplate.isEmpty() )
{
if ( fileNewFromTemplate( projectTemplate ) )
return;
}
}

closeProject();
mMapCanvas->clear();
Expand Down Expand Up @@ -3206,31 +3195,22 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )

updateCRSStatusBar();

// notify user if last attempt at auto-opening a project failed
bool projOpenedOK = settings.value( "/qgis/projOpenedOKAtLaunch", QVariant( true ) ).toBool();
if ( !projOpenedOK )
{
// only show the following 'auto-open project failed' message once
settings.setValue( "/qgis/projOpenedOKAtLaunch", QVariant( true ) );

int projOpen = settings.value( "/qgis/projOpenAtLaunch", QVariant( 0 ) ).toInt();
/** New Empty Project Created
(before attempting to load custom project templates/filepaths) */

QString projPath = QString();
if ( projOpen == 1 && mRecentProjectPaths.size() > 0 ) // most recent project
{
projPath = mRecentProjectPaths.at( 0 );
}
if ( projOpen == 2 ) // specific project
{
projPath = settings.value( "/qgis/projOpenAtLaunchPath" ).toString();
}
// load default template
/* NOTE: don't open default template on launch until after initialization,
in case a project was defined via command line */

// set auto-open project back to 'New' to avoid re-opening bad project
settings.setValue( "/qgis/projOpenAtLaunch" , QVariant( 0 ) );
// don't open template if last auto-opening of a project failed
if ( ! forceBlank )
{
forceBlank = ! settings.value( "/qgis/projOpenedOKAtLaunch", QVariant( true ) ).toBool();
}

messageBar()->pushMessage( tr( "Auto-open Project Failed" ),
projPath,
QgsMessageBar::CRITICAL );
if ( ! forceBlank && settings.value( "/qgis/newProjectDefault", QVariant( false ) ).toBool() )
{
fileNewFromDefaultTemplate();
}

// set the initial map tool
Expand All @@ -3256,37 +3236,97 @@ bool QgisApp::fileNewFromTemplate( QString fileName )
return false;
}

void QgisApp::fileNewFromDefaultTemplate()
{
QString projectTemplate = QgsApplication::qgisSettingsDirPath() + QString( "project_default.qgs" );
QString msgTxt;
if ( !projectTemplate.isEmpty() && QFile::exists( projectTemplate ) )
{
if ( fileNewFromTemplate( projectTemplate ) )
{
return;
}
msgTxt = tr( "Default failed to open: %1" );
}
else
{
msgTxt = tr( "Default not found: %1" );
}
messageBar()->pushMessage( tr( "Open Template Project" ),
msgTxt.arg( projectTemplate ),
QgsMessageBar::WARNING );
}

void QgisApp::fileOpenAfterLaunch()
{
// TODO: move at-launch options to enums and switch statement
// TODO: move auto-open project options to enums

// fileNewBlank() has already been called in QgisApp constructor
// loaded project is either a new blank one, or one from command line
QSettings settings;
QString autoOpenMsgTitle = tr( "Auto-open Project" );

// what type of project to auto-open
int projOpen = settings.value( "/qgis/projOpenAtLaunch", 0 ).toInt();

if ( projOpen == 0 ) // new project (default)
// get path of project file to open, or was attempted
QString projPath = QString();
if ( projOpen == 1 && mRecentProjectPaths.size() > 0 ) // most recent project
{
return; // fileNew() has already been called in constructor
projPath = mRecentProjectPaths.at( 0 );
}
if ( projOpen == 2 ) // specific project
{
projPath = settings.value( "/qgis/projOpenAtLaunchPath" ).toString();
}

QString projPath = QString();
if ( projOpen == 1 && mRecentProjectPaths.size() > 0 ) // open most recent project
// whether last auto-opening of a project failed
bool projOpenedOK = settings.value( "/qgis/projOpenedOKAtLaunch", QVariant( true ) ).toBool();

// notify user if last attempt at auto-opening a project failed
/** NOTE: Notification will not show if last auto-opened project failed but
next project opened is from command line (minor issue) */
/** TODO: Keep projOpenedOKAtLaunch from being reset to true after
reading command line project (which happens before initialization signal) */
if ( !projOpenedOK )
{
projPath = mRecentProjectPaths.at( 0 );
// only show the following 'auto-open project failed' message once, at launch
settings.setValue( "/qgis/projOpenedOKAtLaunch", QVariant( true ) );

// set auto-open project back to 'New' to avoid re-opening bad project
settings.setValue( "/qgis/projOpenAtLaunch" , QVariant( 0 ) );

messageBar()->pushMessage( autoOpenMsgTitle,
tr( "Failed to open: %1" ).arg( projPath ),
QgsMessageBar::CRITICAL );
return;
}

if ( projOpen == 2 ) // open specific project
// check if a project is already loaded via command line
if ( !QgsProject::instance()->fileName().isNull() )
{
projPath = settings.value( "/qgis/projOpenAtLaunchPath" ).toString();
return;
}

if ( projOpen == 0 ) // new project (default)
{
// open default template, if defined
if ( settings.value( "/qgis/newProjectDefault", QVariant( false ) ).toBool() )
{
fileNewFromDefaultTemplate();
}
return;
}

if ( projPath.isEmpty() )
if ( projPath.isEmpty() ) // projPath required from here
{
return;
}

if ( !projPath.endsWith( QString( "qgs" ), Qt::CaseInsensitive ) )
{
messageBar()->pushMessage( tr( "Auto-open Project" ),
tr( "File not valid project: %1" ).arg( projPath ),
messageBar()->pushMessage( autoOpenMsgTitle,
tr( "Not valid project file: %1" ).arg( projPath ),
QgsMessageBar::WARNING );
return;
}
Expand All @@ -3296,11 +3336,23 @@ void QgisApp::fileOpenAfterLaunch()
// set flag to check on next app launch if the following project opened OK
settings.setValue( "/qgis/projOpenedOKAtLaunch" , QVariant( false ) );

addProject( projPath );
if ( !addProject( projPath ) )
{
messageBar()->pushMessage( autoOpenMsgTitle,
tr( "Project failed to open: %1" ).arg( projPath ),
QgsMessageBar::WARNING );
}

if ( projPath.endsWith( QString( "project_default.qgs" ) ) )
{
messageBar()->pushMessage( autoOpenMsgTitle,
tr( "Default template has been reopened: %1" ).arg( projPath ),
QgsMessageBar::INFO );
}
}
else
{
messageBar()->pushMessage( tr( "Auto-open Project" ),
messageBar()->pushMessage( autoOpenMsgTitle,
tr( "File not found: %1" ).arg( projPath ),
QgsMessageBar::WARNING );
}
Expand Down Expand Up @@ -3434,6 +3486,10 @@ void QgisApp::enableProjectMacros()
*/
bool QgisApp::addProject( QString projectFile )
{
QFileInfo pfi( projectFile );
statusBar()->showMessage( tr( "Loading project: %1" ).arg( pfi.fileName() ) );
qApp->processEvents();

QApplication::setOverrideCursor( Qt::WaitCursor );

// close the previous opened project if any
Expand All @@ -3442,6 +3498,7 @@ bool QgisApp::addProject( QString projectFile )
if ( ! QgsProject::instance()->read( projectFile ) )
{
QApplication::restoreOverrideCursor();
statusBar()->clearMessage();

QMessageBox::critical( this,
tr( "Unable to open project" ),
Expand Down Expand Up @@ -3533,6 +3590,8 @@ bool QgisApp::addProject( QString projectFile )

mMapCanvas->freeze( false );
mMapCanvas->refresh();

statusBar()->showMessage( tr( "Project loaded" ), 3000 );
return true;
} // QgisApp::addProject(QString projectFile)

Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.h
Expand Up @@ -739,6 +739,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
//! Create a new file from a template project
bool fileNewFromTemplate( QString fileName );
void fileNewFromTemplateAction( QAction * qAction );
void fileNewFromDefaultTemplate();
//! Calculate new rasters from existing ones
void showRasterCalculator();
void embedLayers();
Expand Down

0 comments on commit 859854f

Please sign in to comment.