Skip to content

Commit

Permalink
[FEATURE] Add option to auto-open 'New, Most recent or Specific' proj…
Browse files Browse the repository at this point in the history
…ect on app launch

- Add error trapping to catch bad projects that crash app, then notify user and avoid reopening
  • Loading branch information
dakcarto committed Jan 30, 2013
1 parent b983f12 commit ecb261f
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 19 deletions.
90 changes: 90 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1816,6 +1816,10 @@ void QgisApp::setupConnections()
SIGNAL( layersWillBeRemoved( QStringList ) ),
this, SLOT( removingLayers( QStringList ) ) );

// connect initialization signal
connect( this, SIGNAL( initializationCompleted() ),
this, SLOT( fileOpenAfterLaunch() ) );

// Connect warning dialog from project reading
connect( QgsProject::instance(), SIGNAL( oldProjectVersionWarning( QString ) ),
this, SLOT( oldProjectVersionWarning( QString ) ) );
Expand All @@ -1831,6 +1835,9 @@ void QgisApp::setupConnections()
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), this, SLOT( loadComposersFromProject( const QDomDocument& ) ) );
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), this, SLOT( loadAnnotationItemsFromProject( const QDomDocument& ) ) );

connect( this, SIGNAL( projectRead() ),
this, SLOT( fileOpenedOKAfterLaunch() ) );

//
// Do we really need this ??? - its already connected to the esc key...TS
//
Expand Down Expand Up @@ -3199,6 +3206,33 @@ 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();

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();
}

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

messageBar()->pushMessage( tr( "Auto-open Project Failed" ),
projPath,
QgsMessageBar::CRITICAL );
}

// set the initial map tool
#ifndef HAVE_TOUCH
mMapCanvas->setMapTool( mMapTools.mPan );
Expand All @@ -3222,6 +3256,62 @@ bool QgisApp::fileNewFromTemplate( QString fileName )
return false;
}

void QgisApp::fileOpenAfterLaunch()
{
// TODO: move at-launch options to enums and switch statement
QSettings settings;
int projOpen = settings.value( "/qgis/projOpenAtLaunch", 0 ).toInt();

if ( projOpen == 0 ) // new project (default)
{
return; // fileNew() has already been called in constructor
}

QString projPath = QString();
if ( projOpen == 1 && mRecentProjectPaths.size() > 0 ) // open most recent project
{
projPath = mRecentProjectPaths.at( 0 );
}

if ( projOpen == 2 ) // open specific project
{
projPath = settings.value( "/qgis/projOpenAtLaunchPath" ).toString();
}

if ( projPath.isEmpty() )
{
return;
}

if ( !projPath.endsWith( QString( "qgs" ), Qt::CaseInsensitive ) )
{
messageBar()->pushMessage( tr( "Auto-open Project" ),
tr( "File not valid project: %1" ).arg( projPath ),
QgsMessageBar::WARNING );
return;
}

if ( QFile::exists( projPath ) )
{
// set flag to check on next app launch if the following project opened OK
settings.setValue( "/qgis/projOpenedOKAtLaunch" , QVariant( false ) );

addProject( projPath );
}
else
{
messageBar()->pushMessage( tr( "Auto-open Project" ),
tr( "File not found: %1" ).arg( projPath ),
QgsMessageBar::WARNING );
}
}

void QgisApp::fileOpenedOKAfterLaunch()
{
QSettings settings;
settings.setValue( "/qgis/projOpenedOKAtLaunch" , QVariant( true ) );
}

void QgisApp::fileNewFromTemplateAction( QAction * qAction )
{
if ( ! qAction )
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -730,6 +730,12 @@ 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 );
/** What type of project to open after launch
* @note Added in QGIS 1.9 */
void fileOpenAfterLaunch();
/** After project read, set any auto-opened project as successful
* @note Added in QGIS 1.9 */
void fileOpenedOKAfterLaunch();
//! Create a new file from a template project
bool fileNewFromTemplate( QString fileName );
void fileNewFromTemplateAction( QAction * qAction );
Expand Down
31 changes: 31 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -567,6 +567,12 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :

capitaliseCheckBox->setChecked( settings.value( "/qgis/capitaliseLayerName", QVariant( false ) ).toBool() );

int projOpen = settings.value( "/qgis/projOpenAtLaunch", 0 ).toInt();
mProjectOnLaunchCmbBx->setCurrentIndex( projOpen );
mProjectOnLaunchLineEdit->setText( settings.value( "/qgis/projOpenAtLaunchPath" ).toString() );
mProjectOnLaunchLineEdit->setEnabled( projOpen == 2 );
mProjectOnLaunchPushBtn->setEnabled( projOpen == 2 );

chbAskToSaveProjectChanges->setChecked( settings.value( "qgis/askToSaveProjectChanges", QVariant( true ) ).toBool() );
chbWarnOldProjectVersion->setChecked( settings.value( "/qgis/warnOldProjectVersion", QVariant( true ) ).toBool() );
cmbEnableMacros->setCurrentIndex( settings.value( "/qgis/enableMacros", 1 ).toInt() );
Expand Down Expand Up @@ -899,6 +905,28 @@ void QgsOptions::iconSizeChanged( const QString &iconSize )
QgisApp::instance()->setIconSizes( iconSize.toInt() );
}

void QgsOptions::on_mProjectOnLaunchCmbBx_currentIndexChanged( int indx )
{
bool specific = ( indx == 2 );
mProjectOnLaunchLineEdit->setEnabled( specific );
mProjectOnLaunchPushBtn->setEnabled( specific );
}

void QgsOptions::on_mProjectOnLaunchPushBtn_pressed()
{
// Retrieve last used project dir from persistent settings
QSettings settings;
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
QString projPath = QFileDialog::getOpenFileName( this,
tr( "Choose project file to open at launch" ),
lastUsedDir,
tr( "QGis files" ) + " (*.qgs *.QGS)" );
if ( !projPath.isNull() )
{
mProjectOnLaunchLineEdit->setText( projPath );
}
}

void QgsOptions::toggleEnableBackbuffer( int state )
{
#ifdef Q_WS_X11
Expand Down Expand Up @@ -1040,6 +1068,9 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );

// project
settings.setValue( "/qgis/projOpenAtLaunch", mProjectOnLaunchCmbBx->currentIndex() );
settings.setValue( "/qgis/projOpenAtLaunchPath", mProjectOnLaunchLineEdit->text() );

settings.setValue( "/qgis/askToSaveProjectChanges", chbAskToSaveProjectChanges->isChecked() );
settings.setValue( "/qgis/warnOldProjectVersion", chbWarnOldProjectVersion->isChecked() );
if (( settings.value( "/qgis/projectTemplateDir" ).toString() != leTemplateFolder->text() ) ||
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgsoptions.h
Expand Up @@ -77,6 +77,16 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase

void iconSizeChanged( const QString &iconSize );

/** Slot to handle when type of project to open after launch is changed
* @note added in QGIS 1.9
*/
void on_mProjectOnLaunchCmbBx_currentIndexChanged( int indx );

/** Slot to choose path to project to open after launch
* @note added in QGIS 1.9
*/
void on_mProjectOnLaunchPushBtn_pressed();

//! Slot to change backbuffering. This is handled when the user changes
// the value of the checkbox
void toggleEnableBackbuffer( int );
Expand Down
132 changes: 113 additions & 19 deletions src/ui/qgsoptionsbase.ui
Expand Up @@ -267,7 +267,7 @@
<x>0</x>
<y>0</y>
<width>654</width>
<height>538</height>
<height>612</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_28">
Expand Down Expand Up @@ -604,18 +604,98 @@
</property>
<layout class="QVBoxLayout">
<item>
<widget class="QCheckBox" name="chbAskToSaveProjectChanges">
<property name="text">
<string>Prompt to save project and data source changes when required</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chbWarnOldProjectVersion">
<property name="text">
<string>Warn when opening a project file saved with an older version of QGIS</string>
</property>
</widget>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="2" colspan="2">
<widget class="QComboBox" name="mProjectOnLaunchCmbBx">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string>New</string>
</property>
</item>
<item>
<property name="text">
<string>Most recent</string>
</property>
</item>
<item>
<property name="text">
<string>Specific</string>
</property>
</item>
</widget>
</item>
<item row="1" column="5">
<widget class="QPushButton" name="mProjectOnLaunchPushBtn">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer_35">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>4</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="4">
<spacer name="horizontalSpacer_36">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1" colspan="4">
<widget class="QLineEdit" name="mProjectOnLaunchLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_54">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Open project on launch</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="cbxProjectDefaultNew">
Expand Down Expand Up @@ -703,6 +783,20 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="chbAskToSaveProjectChanges">
<property name="text">
<string>Prompt to save project and data source changes when required</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chbWarnOldProjectVersion">
<property name="text">
<string>Warn when opening a project file saved with an older version of QGIS</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_21">
<item>
Expand Down Expand Up @@ -807,7 +901,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>654</width>
<width>611</width>
<height>808</height>
</rect>
</property>
Expand Down Expand Up @@ -1137,8 +1231,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>669</width>
<height>490</height>
<width>559</width>
<height>417</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_27">
Expand Down Expand Up @@ -1456,7 +1550,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>654</width>
<width>615</width>
<height>681</height>
</rect>
</property>
Expand Down Expand Up @@ -1944,8 +2038,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>669</width>
<height>490</height>
<width>498</width>
<height>396</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_25">
Expand Down

0 comments on commit ecb261f

Please sign in to comment.