Skip to content

Commit ecb261f

Browse files
committedJan 30, 2013
[FEATURE] Add option to auto-open 'New, Most recent or Specific' project on app launch
- Add error trapping to catch bad projects that crash app, then notify user and avoid reopening
1 parent b983f12 commit ecb261f

File tree

5 files changed

+250
-19
lines changed

5 files changed

+250
-19
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,10 @@ void QgisApp::setupConnections()
18161816
SIGNAL( layersWillBeRemoved( QStringList ) ),
18171817
this, SLOT( removingLayers( QStringList ) ) );
18181818

1819+
// connect initialization signal
1820+
connect( this, SIGNAL( initializationCompleted() ),
1821+
this, SLOT( fileOpenAfterLaunch() ) );
1822+
18191823
// Connect warning dialog from project reading
18201824
connect( QgsProject::instance(), SIGNAL( oldProjectVersionWarning( QString ) ),
18211825
this, SLOT( oldProjectVersionWarning( QString ) ) );
@@ -1831,6 +1835,9 @@ void QgisApp::setupConnections()
18311835
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), this, SLOT( loadComposersFromProject( const QDomDocument& ) ) );
18321836
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), this, SLOT( loadAnnotationItemsFromProject( const QDomDocument& ) ) );
18331837

1838+
connect( this, SIGNAL( projectRead() ),
1839+
this, SLOT( fileOpenedOKAfterLaunch() ) );
1840+
18341841
//
18351842
// Do we really need this ??? - its already connected to the esc key...TS
18361843
//
@@ -3199,6 +3206,33 @@ void QgisApp::fileNew( bool thePromptToSaveFlag, bool forceBlank )
31993206

32003207
updateCRSStatusBar();
32013208

3209+
// notify user if last attempt at auto-opening a project failed
3210+
bool projOpenedOK = settings.value( "/qgis/projOpenedOKAtLaunch", QVariant( true ) ).toBool();
3211+
if ( !projOpenedOK )
3212+
{
3213+
// only show the following 'auto-open project failed' message once
3214+
settings.setValue( "/qgis/projOpenedOKAtLaunch", QVariant( true ) );
3215+
3216+
int projOpen = settings.value( "/qgis/projOpenAtLaunch", QVariant( 0 ) ).toInt();
3217+
3218+
QString projPath = QString();
3219+
if ( projOpen == 1 && mRecentProjectPaths.size() > 0 ) // most recent project
3220+
{
3221+
projPath = mRecentProjectPaths.at( 0 );
3222+
}
3223+
if ( projOpen == 2 ) // specific project
3224+
{
3225+
projPath = settings.value( "/qgis/projOpenAtLaunchPath" ).toString();
3226+
}
3227+
3228+
// set auto-open project back to 'New' to avoid re-opening bad project
3229+
settings.setValue( "/qgis/projOpenAtLaunch" , QVariant( 0 ) );
3230+
3231+
messageBar()->pushMessage( tr( "Auto-open Project Failed" ),
3232+
projPath,
3233+
QgsMessageBar::CRITICAL );
3234+
}
3235+
32023236
// set the initial map tool
32033237
#ifndef HAVE_TOUCH
32043238
mMapCanvas->setMapTool( mMapTools.mPan );
@@ -3222,6 +3256,62 @@ bool QgisApp::fileNewFromTemplate( QString fileName )
32223256
return false;
32233257
}
32243258

3259+
void QgisApp::fileOpenAfterLaunch()
3260+
{
3261+
// TODO: move at-launch options to enums and switch statement
3262+
QSettings settings;
3263+
int projOpen = settings.value( "/qgis/projOpenAtLaunch", 0 ).toInt();
3264+
3265+
if ( projOpen == 0 ) // new project (default)
3266+
{
3267+
return; // fileNew() has already been called in constructor
3268+
}
3269+
3270+
QString projPath = QString();
3271+
if ( projOpen == 1 && mRecentProjectPaths.size() > 0 ) // open most recent project
3272+
{
3273+
projPath = mRecentProjectPaths.at( 0 );
3274+
}
3275+
3276+
if ( projOpen == 2 ) // open specific project
3277+
{
3278+
projPath = settings.value( "/qgis/projOpenAtLaunchPath" ).toString();
3279+
}
3280+
3281+
if ( projPath.isEmpty() )
3282+
{
3283+
return;
3284+
}
3285+
3286+
if ( !projPath.endsWith( QString( "qgs" ), Qt::CaseInsensitive ) )
3287+
{
3288+
messageBar()->pushMessage( tr( "Auto-open Project" ),
3289+
tr( "File not valid project: %1" ).arg( projPath ),
3290+
QgsMessageBar::WARNING );
3291+
return;
3292+
}
3293+
3294+
if ( QFile::exists( projPath ) )
3295+
{
3296+
// set flag to check on next app launch if the following project opened OK
3297+
settings.setValue( "/qgis/projOpenedOKAtLaunch" , QVariant( false ) );
3298+
3299+
addProject( projPath );
3300+
}
3301+
else
3302+
{
3303+
messageBar()->pushMessage( tr( "Auto-open Project" ),
3304+
tr( "File not found: %1" ).arg( projPath ),
3305+
QgsMessageBar::WARNING );
3306+
}
3307+
}
3308+
3309+
void QgisApp::fileOpenedOKAfterLaunch()
3310+
{
3311+
QSettings settings;
3312+
settings.setValue( "/qgis/projOpenedOKAtLaunch" , QVariant( true ) );
3313+
}
3314+
32253315
void QgisApp::fileNewFromTemplateAction( QAction * qAction )
32263316
{
32273317
if ( ! qAction )

‎src/app/qgisapp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,12 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
730730
void fileNewBlank();
731731
//! As above but allows forcing without prompt and forcing blank project
732732
void fileNew( bool thePromptToSaveFlag, bool forceBlank = false );
733+
/** What type of project to open after launch
734+
* @note Added in QGIS 1.9 */
735+
void fileOpenAfterLaunch();
736+
/** After project read, set any auto-opened project as successful
737+
* @note Added in QGIS 1.9 */
738+
void fileOpenedOKAfterLaunch();
733739
//! Create a new file from a template project
734740
bool fileNewFromTemplate( QString fileName );
735741
void fileNewFromTemplateAction( QAction * qAction );

‎src/app/qgsoptions.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
567567

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

570+
int projOpen = settings.value( "/qgis/projOpenAtLaunch", 0 ).toInt();
571+
mProjectOnLaunchCmbBx->setCurrentIndex( projOpen );
572+
mProjectOnLaunchLineEdit->setText( settings.value( "/qgis/projOpenAtLaunchPath" ).toString() );
573+
mProjectOnLaunchLineEdit->setEnabled( projOpen == 2 );
574+
mProjectOnLaunchPushBtn->setEnabled( projOpen == 2 );
575+
570576
chbAskToSaveProjectChanges->setChecked( settings.value( "qgis/askToSaveProjectChanges", QVariant( true ) ).toBool() );
571577
chbWarnOldProjectVersion->setChecked( settings.value( "/qgis/warnOldProjectVersion", QVariant( true ) ).toBool() );
572578
cmbEnableMacros->setCurrentIndex( settings.value( "/qgis/enableMacros", 1 ).toInt() );
@@ -899,6 +905,28 @@ void QgsOptions::iconSizeChanged( const QString &iconSize )
899905
QgisApp::instance()->setIconSizes( iconSize.toInt() );
900906
}
901907

908+
void QgsOptions::on_mProjectOnLaunchCmbBx_currentIndexChanged( int indx )
909+
{
910+
bool specific = ( indx == 2 );
911+
mProjectOnLaunchLineEdit->setEnabled( specific );
912+
mProjectOnLaunchPushBtn->setEnabled( specific );
913+
}
914+
915+
void QgsOptions::on_mProjectOnLaunchPushBtn_pressed()
916+
{
917+
// Retrieve last used project dir from persistent settings
918+
QSettings settings;
919+
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
920+
QString projPath = QFileDialog::getOpenFileName( this,
921+
tr( "Choose project file to open at launch" ),
922+
lastUsedDir,
923+
tr( "QGis files" ) + " (*.qgs *.QGS)" );
924+
if ( !projPath.isNull() )
925+
{
926+
mProjectOnLaunchLineEdit->setText( projPath );
927+
}
928+
}
929+
902930
void QgsOptions::toggleEnableBackbuffer( int state )
903931
{
904932
#ifdef Q_WS_X11
@@ -1040,6 +1068,9 @@ void QgsOptions::saveOptions()
10401068
settings.setValue( "/qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );
10411069

10421070
// project
1071+
settings.setValue( "/qgis/projOpenAtLaunch", mProjectOnLaunchCmbBx->currentIndex() );
1072+
settings.setValue( "/qgis/projOpenAtLaunchPath", mProjectOnLaunchLineEdit->text() );
1073+
10431074
settings.setValue( "/qgis/askToSaveProjectChanges", chbAskToSaveProjectChanges->isChecked() );
10441075
settings.setValue( "/qgis/warnOldProjectVersion", chbWarnOldProjectVersion->isChecked() );
10451076
if (( settings.value( "/qgis/projectTemplateDir" ).toString() != leTemplateFolder->text() ) ||

‎src/app/qgsoptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase
7777

7878
void iconSizeChanged( const QString &iconSize );
7979

80+
/** Slot to handle when type of project to open after launch is changed
81+
* @note added in QGIS 1.9
82+
*/
83+
void on_mProjectOnLaunchCmbBx_currentIndexChanged( int indx );
84+
85+
/** Slot to choose path to project to open after launch
86+
* @note added in QGIS 1.9
87+
*/
88+
void on_mProjectOnLaunchPushBtn_pressed();
89+
8090
//! Slot to change backbuffering. This is handled when the user changes
8191
// the value of the checkbox
8292
void toggleEnableBackbuffer( int );

‎src/ui/qgsoptionsbase.ui

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
<x>0</x>
268268
<y>0</y>
269269
<width>654</width>
270-
<height>538</height>
270+
<height>612</height>
271271
</rect>
272272
</property>
273273
<layout class="QVBoxLayout" name="verticalLayout_28">
@@ -604,18 +604,98 @@
604604
</property>
605605
<layout class="QVBoxLayout">
606606
<item>
607-
<widget class="QCheckBox" name="chbAskToSaveProjectChanges">
608-
<property name="text">
609-
<string>Prompt to save project and data source changes when required</string>
610-
</property>
611-
</widget>
612-
</item>
613-
<item>
614-
<widget class="QCheckBox" name="chbWarnOldProjectVersion">
615-
<property name="text">
616-
<string>Warn when opening a project file saved with an older version of QGIS</string>
617-
</property>
618-
</widget>
607+
<layout class="QGridLayout" name="gridLayout_4">
608+
<item row="0" column="2" colspan="2">
609+
<widget class="QComboBox" name="mProjectOnLaunchCmbBx">
610+
<property name="sizePolicy">
611+
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
612+
<horstretch>0</horstretch>
613+
<verstretch>0</verstretch>
614+
</sizepolicy>
615+
</property>
616+
<item>
617+
<property name="text">
618+
<string>New</string>
619+
</property>
620+
</item>
621+
<item>
622+
<property name="text">
623+
<string>Most recent</string>
624+
</property>
625+
</item>
626+
<item>
627+
<property name="text">
628+
<string>Specific</string>
629+
</property>
630+
</item>
631+
</widget>
632+
</item>
633+
<item row="1" column="5">
634+
<widget class="QPushButton" name="mProjectOnLaunchPushBtn">
635+
<property name="sizePolicy">
636+
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
637+
<horstretch>0</horstretch>
638+
<verstretch>0</verstretch>
639+
</sizepolicy>
640+
</property>
641+
<property name="text">
642+
<string>...</string>
643+
</property>
644+
</widget>
645+
</item>
646+
<item row="1" column="0">
647+
<spacer name="horizontalSpacer_35">
648+
<property name="orientation">
649+
<enum>Qt::Horizontal</enum>
650+
</property>
651+
<property name="sizeType">
652+
<enum>QSizePolicy::Fixed</enum>
653+
</property>
654+
<property name="sizeHint" stdset="0">
655+
<size>
656+
<width>4</width>
657+
<height>20</height>
658+
</size>
659+
</property>
660+
</spacer>
661+
</item>
662+
<item row="0" column="4">
663+
<spacer name="horizontalSpacer_36">
664+
<property name="orientation">
665+
<enum>Qt::Horizontal</enum>
666+
</property>
667+
<property name="sizeHint" stdset="0">
668+
<size>
669+
<width>40</width>
670+
<height>20</height>
671+
</size>
672+
</property>
673+
</spacer>
674+
</item>
675+
<item row="1" column="1" colspan="4">
676+
<widget class="QLineEdit" name="mProjectOnLaunchLineEdit">
677+
<property name="sizePolicy">
678+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
679+
<horstretch>0</horstretch>
680+
<verstretch>0</verstretch>
681+
</sizepolicy>
682+
</property>
683+
</widget>
684+
</item>
685+
<item row="0" column="0" colspan="2">
686+
<widget class="QLabel" name="label_54">
687+
<property name="sizePolicy">
688+
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
689+
<horstretch>0</horstretch>
690+
<verstretch>0</verstretch>
691+
</sizepolicy>
692+
</property>
693+
<property name="text">
694+
<string>Open project on launch</string>
695+
</property>
696+
</widget>
697+
</item>
698+
</layout>
619699
</item>
620700
<item>
621701
<widget class="QCheckBox" name="cbxProjectDefaultNew">
@@ -703,6 +783,20 @@
703783
</item>
704784
</layout>
705785
</item>
786+
<item>
787+
<widget class="QCheckBox" name="chbAskToSaveProjectChanges">
788+
<property name="text">
789+
<string>Prompt to save project and data source changes when required</string>
790+
</property>
791+
</widget>
792+
</item>
793+
<item>
794+
<widget class="QCheckBox" name="chbWarnOldProjectVersion">
795+
<property name="text">
796+
<string>Warn when opening a project file saved with an older version of QGIS</string>
797+
</property>
798+
</widget>
799+
</item>
706800
<item>
707801
<layout class="QHBoxLayout" name="horizontalLayout_21">
708802
<item>
@@ -807,7 +901,7 @@
807901
<rect>
808902
<x>0</x>
809903
<y>0</y>
810-
<width>654</width>
904+
<width>611</width>
811905
<height>808</height>
812906
</rect>
813907
</property>
@@ -1137,8 +1231,8 @@
11371231
<rect>
11381232
<x>0</x>
11391233
<y>0</y>
1140-
<width>669</width>
1141-
<height>490</height>
1234+
<width>559</width>
1235+
<height>417</height>
11421236
</rect>
11431237
</property>
11441238
<layout class="QVBoxLayout" name="verticalLayout_27">
@@ -1456,7 +1550,7 @@
14561550
<rect>
14571551
<x>0</x>
14581552
<y>0</y>
1459-
<width>654</width>
1553+
<width>615</width>
14601554
<height>681</height>
14611555
</rect>
14621556
</property>
@@ -1944,8 +2038,8 @@
19442038
<rect>
19452039
<x>0</x>
19462040
<y>0</y>
1947-
<width>669</width>
1948-
<height>490</height>
2041+
<width>498</width>
2042+
<height>396</height>
19492043
</rect>
19502044
</property>
19512045
<layout class="QVBoxLayout" name="verticalLayout_25">

0 commit comments

Comments
 (0)
Please sign in to comment.